Example #1
0
def main():
    """
    更新每日汇总结果到数据库
    :return:
    """
    db = db_helper.DBHelper()
    db_flag = db.connect_database()
    if db_flag:
        name_list = get_name_list(db)
        date = datetime.date.today() - datetime.timedelta(days=1)  # 前一天
        for name in name_list:
            res = db.fetchall(
                """select checkInTime
                                 from check_t
                                 where checkInDate = %s
                                 and userName = %s""", (date, name[0]))
            if len(res) == 2:
                total_time = res[1][0] - res[0][0]
                db.execute(
                    """ insert into sum_t (userName, checkInDate, totalTime, startTime, endTime)
                               values (%s, %s, %s, %s, %s) 
                           """,
                    (name[0], date, total_time, res[0][0], res[1][0]))
            else:
                print(name[0] + " no record")
Example #2
0
 def __init__(self):
     self.ci_users = {}
     self.cfg = config.Config()
     self.db = db_helper.DBHelper(self.cfg).get()
     logger.init(self.cfg)
     self.log = logger.get('scoreboard-gerrit-listener')
     self.g = None
Example #3
0
def frame_register():
    """
    人脸注册逻辑
    Returns:

    """

    db = db_helper.DBHelper()
    db_flag = db.connect_database()

    while True:
        time.sleep(0.001)
        username = raw_input('username: '******'姓名:')  # 写入数据库的中文姓名realname
        # angle 表示人脸的角度 0到4依次表示 mid, up, down, left, right
        global angle
        angle = [0, 0, 0, 0, 0]
        print(realname + "人脸采集开始,请面对摄像头,然后头部上下左右微转保持几秒")
        while not (angle[0] > 0 and angle[1] > 0 and angle[2] > 0
                   and angle[3] > 0 and angle[4] > 0):
            if (angle[0] + angle[1] + angle[2] + angle[3] +
                    angle[4]) >= 3:  # 采集到3个角度就OK
                break
            n_frame = None
            if mutex.acquire():
                global frame_list
                if frame_list:
                    n_frame = frame_list[-1]
                frame_list = []
                mutex.release()
            if n_frame is not None:
                face = face_check.FaceHandler()
                try:
                    face.set_image(image=face_check.get_base64(n_frame),
                                   image_type='BASE64')
                    if face.face_detect():
                        face_list = face.get_faces()
                        face_result = face_list[0]
                        f_quality, f_angle = quality_check(face_result)

                        if f_quality:

                            print("正在采集...")
                            if angle[f_angle] < 1:
                                print(str(angle_name[f_angle]) + "角度完成采集...")
                                if face.face_register(group_id='lab',
                                                      user_id=username):
                                    angle[f_angle] += 1
                        else:
                            print("正在采集...")
                except:
                    traceback.print_exc()
                time.sleep(0.5)
        print('Success ' + str(username) + ' register...')
        if db_flag:
            db.execute(
                "insert into user_t (userName, realName, createTime) "
                "values (%s, %s, now())", (username, realname))
Example #4
0
def frame_check():
    """
    获取帧进行人脸签到
    Returns:

    """
    db = db_helper.DBHelper()
    db_flag = db.connect_database()  # 连接数据库
    global realname
    keep_sql = 10000
    while True:
        time.sleep(0.001)
        n_frame = None
        if mutex.acquire():
            # 拿到帧后清空帧序列
            global frame_list
            if frame_list:
                n_frame = frame_list[-1]
            frame_list = []
            mutex.release()

        if n_frame is not None:
            keep_sql -= 1
            # 保持数据库长连接,用的隔段时间访问一次的方法。。。
            if keep_sql == 0:
                get_realname(db, 'xuhuanmin')
                keep_sql = 5000
                print('keep alive...')
            # 检测人脸
            face = face_check.FaceHandler()
            try:
                face.set_image(image=face_check.get_base64(n_frame),
                               image_type='BASE64')
                try:
                    face_dt = face.face_detect()
                except:
                    face_dt = False

                if face_dt:
                    # show faces
                    print(time.time(), ' detected')
                    face_list = face.get_faces()

                    face_time, username = face.face_search()
                    print(face_time)
                    print(username)
                    if not db_flag:
                        db = db_helper.DBHelper()
                        db_flag = db.connect_database()
                    if username != 'unknown':
                        # 检测到有效人脸,写入数据库
                        realname = get_realname(db, username)
                        if username == face_session['username'] and \
                                                (face_time - face_session['timestamp']) < 10:
                            # 10秒内重复出现记录为同一个人
                            logging.warning("Same person")
                        else:
                            t_s = time.time()
                            insert_check(db, face_time, username)
                            t_e = time.time()
                            print("time: " + str(t_e - t_s))
                        face_session['username'] = username
                        face_session['timestamp'] = face_time
                    else:
                        realname = 'unknown'
                else:
                    realname = ''
                    print(time.time(), ' undetected')
                time.sleep(0.1)  # 挂起减小cpu压力
            except:
                traceback.print_exc()
from bson import json_util
from flask import Flask, request, render_template, send_from_directory
import pymongo

import config
import db_helper
import logger

cfg = config.Config()

app = Flask(__name__)
app.debug = True

logger.init(cfg)

db = db_helper.DBHelper(cfg).get()


@app.route('/')
def index():
    return render_template('index.html', host=request.host)


@app.route('/static/<path:path>')
def send_js(path):
    # TODO: We should probably use a real webserver for this..
    return send_from_directory('static', path)


@app.route('/ci-accounts', methods=['GET'])
def ci_accounts():