def face_add(self, face_info): """ 添加人脸到人脸库 :return: """ user_id = face_info.get('user_id') face_name = face_info.get('face_name') face_class = face_info.get('face_class') base64_code = face_info.get('base64_code') # 录入前先校验该人脸是否已经录入 result = self.search_face_info(base64_code, user_id) if result and result.get('user_list'): for res in result.get('user_list'): if res.get('score') > FACE_ACCESS: raise Exception("该人脸已经存在, 请勿重复录入") face = FaceDao.get_by_user_id_and_face_name(user_id, face_name) if face: raise Exception('face is exist') try: # 上传图片 face_url = FaceAuthUtils.base642imag(base64_code) face = Face.create(user_id, face_name, face_url, face_class) FaceDao.insert(face) params = {'image': base64_code, 'image_type': 'BASE64', 'group_id': user_id, 'user_id': face.id , 'quality_control': 'NORMAL'} access_token = RequestUtil.get_access_token() url = FACE_LIB_USER_ADD + "?access_token=" + str(access_token) resp = RequestUtil.send_post(url=url, params=json.dumps(params), headers={'content-type': 'application/json'}) self.check_response(resp) db.session.commit() except Exception as e: db.session.rollback() FaceAuthUtils.save_exception(traceback.format_exc()) raise Exception(e.message)
def init_face(self, stu_class, user_id): """ :param stu_class: 班级 :param user_id: 老师的id :return: 0. 检查现在是否处于签到时期 1. 统计出未签到的人,生成签到记录 2. 初始化所有该stu_class & user_id的签到状态为未签到 3. 修改当前不可进行签到 """ # 检查现在是否处于签到时期 faces = FaceDao.get_by_class_user_id2(stu_class, user_id, Open_Check.YES) if not faces: raise Exception("暂未开放签到") unchecked_faces = FaceDao.get_by_class_user_id(stu_class, user_id, FaceStatus.UNCHECK) unchecked = [_.face_name + ' ' for _ in unchecked_faces] try: # 查询签到记录, 并修改为可查 record = RecordDao.get_lastest_record_by_class(user_id, stu_class) record.unchecked = json.dumps({'data': unchecked}) record.status = IS_OPEN.YES checked_faces = FaceDao.get_by_class_user_id(stu_class, user_id, FaceStatus.CHECKED) # 未签到的人不需要初始化了 for face in checked_faces: face.status = FaceStatus.UNCHECK face.open_check = Open_Check.NO for face in unchecked_faces: face.open_check = Open_Check.NO db.session.commit() except Exception as e: db.session.rollback() FaceAuthUtils.save_exception(traceback.format_exc()) raise Exception(e.message)
def __init__(self): self.lucky_draw_dao = LuckyDrawDao() self.face_dao = FaceDao() self.date_util = date_util.date_util() self.prize_tua = [(['a0', 4, '谢谢参与'], 85), (['a1', 1, '神秘大奖'], 1), (['a2', 2, '百醇饼干一盒'], 2), (['a3', 3, '卫龙辣条一包'], 6), (['a4', 3, 'QQ糖一包'], 6)]
def __init__(self): self.face_dao = FaceDao() self.image_util = image_util() self.ConfigReader = ConfigReader() self.config = self.ConfigReader.get_config() self.DataNotifier = DataNotifier() self.face_exception_code = int( self.config.get("face", "face_exception_code"))
class UserService: def __init__(self): self.face_dao = FaceDao() def handle_user_session(self, id): try: face = Face(unionId=None, faceCode=None, profile=None, openId=id) if self.face_dao.count_face_by_open_id(id) is 0: return self.face_dao.add_face(face) except Exception as e: print("Happen:" + str(e))
def get_face_by_user_id(self, user_id, filters, page=1): """ 根据用户id查询其下人脸信息 :param :return: """ faces, total = FaceDao.get_face_by_user_id(user_id, page, filters) if not faces: return {'data': [], 'total': 0} user = UserDao.get_user_by_user_id(user_id) res = {} data = [] for face in faces: data.append({ 'id': face.id, 'name': face.face_name, 'belong': user.username, 'url': face.face_url, 'status': face.status, 'stu_class': face.face_class, 'open_check': face.open_check }) res.update({ 'data': data, 'total': total }) return res
def face_search(self, face_info): """ 指定用户组中搜索人像 user_id = 教师id = group_id 百度返回 user_id = face_id :param face_info: :return: """ try: user_id = face_info.get('user_id') base64_code = face_info.get('base64_code') result = self.search_face_info(base64_code, user_id) for res in result.get('user_list'): if res.get('score') > FACE_ACCESS: face_id = res.get('user_id') face = FaceDao.get_face_by_face_id(face_id) if not face: raise Exception('不存在该学生') if face.open_check != Open_Check.YES: raise Exception('不在签到时间范围内') if face.status == FaceStatus.CHECKED: raise Exception('已签到') face.status = FaceStatus.CHECKED # 获取此次签到记录 record = RecordDao.get_lastest_record_by_class(user_id, face.face_class) record_content = json.loads(record.record) msg = '%s %s 已经签到' % (datetime.now().strftime('%Y-%m-%d %H:%M:%S'), face.face_name.encode('utf-8')) record_content.get('data').append(msg) record.record = json.dumps(record_content) db.session.commit() return raise Exception('人脸验证未通过') except Exception as e: FaceAuthUtils.save_exception(traceback.format_exc()) raise Exception(e.message)
class LuckyDrawService: def __init__(self): self.lucky_draw_dao = LuckyDrawDao() self.face_dao = FaceDao() self.date_util = date_util.date_util() self.prize_tua = [(['a0', 4, '谢谢参与'], 85), (['a1', 1, '神秘大奖'], 1), (['a2', 2, '百醇饼干一盒'], 2), (['a3', 3, '卫龙辣条一包'], 6), (['a4', 3, 'QQ糖一包'], 6)] def prize_drawing(self, unionid): last_prize_date_str = self.lucky_draw_dao.get_last_create_date_by_unionid( unionid) date_interval = self.date_util.get_day_interval_with_sysdate_by_time_str( last_prize_date_str) if date_interval is None or date_interval >= 1: userInfo = self.face_dao.get_profile_by_union_id(unionid) if userInfo is not None: lucky_draw = {} lucky_draw['unionId'] = unionid lucky_draw['userInfo'] = userInfo lucky_draw['createDate'] = time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime()) lucky_draw['isRedeem'] = False prize_result = random_util.random_util(self.prize_tua)() prize = {} prize['code'] = prize_result[0] prize['prizeLevel'] = prize_result[1] prize['prizeName'] = prize_result[2] lucky_draw['prize'] = prize self.lucky_draw_dao.add_lucky_draw(lucky_draw) lucky_draw['_id'] = None return lucky_draw else: return None else: return None def get_number_of_remaining_draw_by_unionid(self, unionid): last_prize_date_str = self.lucky_draw_dao.get_last_create_date_by_unionid( unionid) if last_prize_date_str is None: return 1 date_interval = self.date_util.get_day_interval_with_sysdate_by_time_str( last_prize_date_str) if date_interval >= 1: return 1 else: return 0 def get_lucky_draw_result(self): return self.lucky_draw_dao.get_all_results_with_prize() def update_redeem_result(self, id): return self.lucky_draw_dao.update_redeem_result(id)
def update_face_status(self, face_id, face_status): face = FaceDao.get_face_by_face_id(face_id) if not face: raise Exception("当前学生信息不存在") try: face.status = face_status db.session.commit() except Exception as e: db.session.rollback() FaceAuthUtils.save_exception(traceback.format_exc()) raise Exception(e.message)
def get_class_by_user_id(self, user_id): faces = FaceDao.get_class_by_user_id(user_id) face_class2sum = defaultdict(int) res = {'checked': [], 'unchecked': []} for face in faces: checked = res.get('checked') unchecked = res.get('unchecked') if face.open_check == Open_Check.YES and face.face_class not in checked: checked.append(face.face_class) if face.open_check == Open_Check.NO and face.face_class not in unchecked: unchecked.append(face.face_class) face_class2sum[face.face_class] += 1 res.update({'sum': face_class2sum}) return res
def get_real_record_by_class(self, user_id, pro_class): """ 获取某老师下某班级实时签到记录 :param user_id: :param pro_class: :return: """ record = RecordDao.get_lastest_record_by_class(user_id, pro_class) if not record: raise Exception('您还未开启签到') record_content = json.loads(record.record) # 获取班级下未签到的人 unchecked = [] face_infos = FaceDao.get_by_class_user_id(pro_class, user_id, FaceStatus.UNCHECK) for face_info in face_infos: unchecked.append(face_info.face_name) res = {'checked': record_content.get('data'), 'unchecked': unchecked} return res
def start_check(self, user_id, stu_class): # 检查现在是否处于签到时期 faces = FaceDao.get_by_class_user_id2(stu_class, user_id, Open_Check.NO) if not faces: raise Exception("该班已处于签到状态") try: for face in faces: face.open_check = Open_Check.YES # 生成一条签到记录 record = Record() record.user_id = user_id record.pro_class = stu_class record.unchecked = json.dumps({'data': []}) record.record = json.dumps({'data': []}) db.session.add(record) db.session.commit() except Exception as e: db.session.rollback() FaceAuthUtils.save_exception(traceback.format_exc()) raise Exception(e.message)
class FaceService: def __init__(self): self.face_dao = FaceDao() self.image_util = image_util() self.ConfigReader = ConfigReader() self.config = self.ConfigReader.get_config() self.DataNotifier = DataNotifier() self.face_exception_code = int( self.config.get("face", "face_exception_code")) def get_all_faces(self): all_faces = self.face_dao.find_all() return all_faces def handle_face_by_id(self, id, file, profile): try: if file is not None: face_code = self.image_util.handle_image(file) if face_code is self.face_exception_code: return self.face_exception_code else: face_code = None if profile is not None: open_id = profile['openId'] else: open_id = None face = Face(unionId=id, faceCode=face_code, profile=profile, openId=open_id) if self.face_dao.count_face_by_id(id) is 0: if self.face_dao.count_face_by_open_id(open_id) is not 0: res = self.face_dao.update_face_by_open_id(face) else: res = self.face_dao.add_face(face) else: res = self.face_dao.update_face_by_id(face) if res is True: DataNotifier.publish_change_to_redis_chanel(self.config, face) return res except Exception as e: print("Happen:" + str(e)) logging.error(str(e)) def recognize_face_by_image(self, file): all_face_list = self.face_dao.find_all() known_face_list = [] profile_list = [] if len(all_face_list) > 0: for face_info in all_face_list: if face_info.get('faceCode') is not None: known_face_list.append(face_info.get('faceCode')) profile_list.append(face_info.get('profile')) result = self.image_util.recognize_image(known_face_list, file, 0.4, profile_list) return result
def __init__(self): self.face_dao = FaceDao()