def post(self): path = get_argument(constants.cons_request_path, required=True, help='文件路径为空') if os.path.isfile(path): img = cv2.imread(path) # cv2.imwrite("src.jpg", img) # 将图象由OpenCv的BGR转成RGB格式,wuyunzhen.zh, 20190309 img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # cv2.imwrite("rgb.jpg", img) tmp_embs = humanRecognizeSrv.get_embedding(img) if len(tmp_embs) > 0: logger.info("计算人脸特征:") emb = tmp_embs[0].reshape(-1) # 将ndArray转byte,再将byte转字符串 emb_list = emb.tolist() data = ",".join(str(s) for s in emb_list) ai_print(data) return make_response(status=HttpStatus.SUCCESS, data={"rslt_cmnt": data}) raise AiException(HttpStatus.SERVER_ERROR, AiErrorCode.YCEA4021009, path) else: raise AiException(HttpStatus.SERVER_ERROR, AiErrorCode.YCEA4021002, path)
def __check_args(self): get_argument(constants.cons_request_appid, required=True, help='应用组件编号不能为空') get_argument(constants.cons_request_trace_id, required=True, help='全局追踪号不能为空') get_argument(constants.cons_request_content_type, required=True, help='文件类型不能为空') get_argument(constants.cons_request_function, required=True, help='功能不能为空') get_argument(constants.cons_request_ccbins_id, required=True, help='机构编号不能为空') model_type = get_argument(constants.cons_request_model_type, type=list, required=True, help='模型服务类型不能为空') if not model_type: raise AiException(HttpStatus.SERVER_ERROR, AiErrorCode.YCEA4021008) path_list = get_argument(constants.cons_request_paths, required=False) img_list = get_argument(constants.cons_request_imgs, required=False) cv_image_list = [] # 从路径数组中读图 path_flag = False if path_list: path_flag = True logger.debug('path字段非空,从路径信息' + ':'.join(path_list)) for path in path_list: if os.path.isfile(path): img = cv2.imread(path) cv_image_list.append(img) else: raise AiException(HttpStatus.SERVER_ERROR, AiErrorCode.YCEA4021002, path) # 如果路径数组为空,从base64数组中读图 elif img_list: logger.debug('path字段为空,取base64图片') for base64_img in img_list: if base64_img: img_data = base64.b64decode(base64_img) # 转换为np数组 img_array = np.fromstring(img_data, np.uint8) # 转换成opencv可用格式 img = cv2.imdecode(img_array, cv2.IMREAD_COLOR) cv_image_list.append(img) else: raise AiException(HttpStatus.SERVER_ERROR, AiErrorCode.YCEA4021006) else: raise AiException(HttpStatus.SERVER_ERROR, AiErrorCode.YCEA4021007) return path_flag, cv_image_list
def __check_args(self): get_argument(constants.cons_request_appid, required=True, help='应用组件编号不能为空') get_argument(constants.cons_request_trace_id, required=True, help='全局追踪号不能为空') ccbins_id = get_argument(constants.cons_request_ccbins_id, required=True, help='机构编号不能为空') empe_Grp = get_argument(constants.cons_request_empe_Grp, type=list, required=True, help='人员信息不能为空') empe_infs = [] for empe in empe_Grp: empe_inf = {} empe_inf[constants.cons_request_ccbins_id] = ccbins_id empe_inf[constants.cons_request_empe_id] = empe[ constants.cons_request_empe_id] empe_inf[constants.cons_request_usr_nm] = empe[ constants.cons_request_usr_nm] empe_inf[constants.cons_request_mnt_tp_cd] = empe[ constants.cons_request_mnt_tp_cd] if os.path.isfile(empe[constants.cons_request_path]): empe_inf[constants.cons_request_img] = cv2.imread( empe[constants.cons_request_path]) else: raise AiException(HttpStatus.SERVER_ERROR, AiErrorCode.YCEA4021002, empe[constants.cons_request_path]) empe_infs.append(empe_inf) return empe_infs
def get_argument(key, *, default=None, type=str, location=None, help=None, required=False, error_code=AiErrorCode.YCEA4021005): # kwargs = dict(default=default, type=type) # if location: # kwargs['location'] = location # if type == 'file': # kwargs['type'] = datastructures.FileStorage # kwargs['location'] = location if location else 'files' # # parser = reqparse.RequestParser() # parser.add_argument(key, **kwargs) # args = parser.parse_args() # # if required and (args[key] is None or type == str and args[key].strip() == '' and key != '_id'): # raise AiException(HttpStatus.BAD_REQUEST, error_code, help if help else key) # # return args[key] arg = None if request.method == 'GET': arg = requests.args.get(key) else: request_param = request.json arg = request_param.get(key) if required and (arg is None or (type == str and arg.strip() == '' and key != '_id') or (type == list and not arg)): raise AiException(HttpStatus.BAD_REQUEST, error_code, help if help else key) return arg
def re_compute_featrue_from_db(self): try: faces_of_inst = face_feture_dao.face_Fetures() for i in range(0, len(faces_of_inst)): try: id = faces_of_inst[i].id uploadfiletrgtrfullnm = faces_of_inst[ i].uploadfiletrgtrfullnm img = cv.imread(uploadfiletrgtrfullnm) # 将图象由OpenCv的BGR转成RGB格式,wuyunzhen.zh, 20190309 img = cv.cvtColor(img, cv.COLOR_BGR2RGB) tmp_embs = self.get_embedding(img) if len(tmp_embs) > 0: logger.info("计算人脸特征:") emb = tmp_embs[0].reshape(-1) # 将ndArray转byte,再将byte转字符串 emb_list = emb.tolist() data = ",".join(str(s) for s in emb_list) face_feture_dao.update_fetures(id, data) else: logger.error("未找到人脸:" + uploadfiletrgtrfullnm) except Exception: logger.error("重新计算人脸特征失败,id:" + faces_of_inst[i].id + ", path:" + faces_of_inst[i].uploadfiletrgtrfullnm) except Exception: raise AiException(error_code=AiErrorCode.YCEA4021009)
def mod_ins_stuff(self, empe_inf): mnt_tp_cd = empe_inf[constants.cons_request_mnt_tp_cd] tmp_emb = None if mnt_tp_cd == constants.cons_mnt_tp_cd_delete: self.__mod_ins_stuff(mnt_tp_cd, empe_inf[constants.cons_request_ccbins_id], empe_inf[constants.cons_request_empe_id], empe_inf[constants.cons_request_usr_nm], None) else: # 将图象由OpenCv的BGR转成RGB格式,wuyunzhen.zh, 20190309 img = cv.cvtColor(empe_inf[constants.cons_request_img], cv.COLOR_BGR2RGB) tmp_embs = self.get_embedding(img) if len(tmp_embs) > 0: tmp_emb = tmp_embs[0] self.__mod_ins_stuff( mnt_tp_cd, empe_inf[constants.cons_request_ccbins_id], empe_inf[constants.cons_request_empe_id], empe_inf[constants.cons_request_usr_nm], tmp_emb) # 将图片保存到磁盘文件 img_path = yaml_cfg.get('ccbins_stuff_pcs_imgs') + "/" + empe_inf[constants.cons_request_ccbins_id] + '_' + \ empe_inf[constants.cons_request_empe_id] + '_' + empe_inf[constants.cons_request_usr_nm] + ".jpg" cv.imwrite(img_path, empe_inf[constants.cons_request_img]) else: raise AiException(HttpStatus.SERVER_ERROR, AiErrorCode.YCEA4021011) return tmp_emb
def reg_ins_stuff(self): try: # 先从数据库装载人脸信息' self.__reg_from_db(read_from_db.db_face_infos) # 再从配置文件夹装载人脸信息 self.__reg_from_file() except Exception: raise AiException(error_code=AiErrorCode.YCEA4021009)
def schedule_task_reg_ins_stuff(self): try: with scheduler.app.app_context(): faces_of_inst = face_feture_dao.face_Fetures() # 先从数据库装载人脸信息' self.__reg_from_db(faces_of_inst) # 再从配置文件夹装载人脸信息 self.__reg_from_file() except Exception: raise AiException(error_code=AiErrorCode.YCEA4021009)
def put(self): try: name = get_argument('username', required=True, help='请输入用户名') email = get_argument('email', required=False, help='请输入邮箱') user1 = users.User.query.filter_by(name).first() user1.name = email db.session.commit() return make_response(status=HttpStatus.SUCCESS) except: db.session.rollback() raise AiException()
def post(self): orgName = get_argument("orgName", required=True, help='所属机构不能为空') deviceName = get_argument("deviceName", required=True, help='所属设备不能为空') alarmType = get_argument("alarmType", required=True, help='报警类型不能为空') alarmLevel = get_argument("alarmLevel", required=True, help='报警等级不能为空') alarmTime = get_argument("alarmTime", required=True, help='报警时间不能为空') remark = get_argument("remark", required=False) # 转回img img = pickle.loads(eval(remark)) # json_data = request.form['json_data'] # dict_data = json.loads(json_data) # orgName =dict_data.get('orgName',-1) # deviceName =dict_data.get('deviceName',-1) # alarmType =dict_data.get('alarmType') # alarmLevel =dict_data.get('alarmLevel') # alarmTime =dict_data.get('alarmTime') # f = request.files['myfile'] # x=f.read() # print('>>>',x) # with open('D:/image/b123.jpg', 'ab') as f: # f.write(x) # for i in f: # print('@@',type(i)) # with open('D:/image/a.jpg', 'ab') as f: # f.write(i) # 告警类别 00 对应海康 人数超限:'131643', '单人异常', '131644', '多人异常' # 告警类别 01 对应海康 非法入侵:'131585', '跨域警戒线','131588', '区域入侵', '131586', '人员进入' if alarmType == '131643': alarmType = '00' vlt_err_alrm_inf = '单人异常' elif alarmType == '131644': alarmType = '00' vlt_err_alrm_inf = '多人异常' elif alarmType == '131585': alarmType = '01' vlt_err_alrm_inf = '跨域警戒线' elif alarmType == '131588': alarmType = '01' vlt_err_alrm_inf = '区域入侵' elif alarmType == '131586': alarmType = '01' vlt_err_alrm_inf = '人员进入' elif alarmType == '150002': alarmType = '01' vlt_err_alrm_inf = '非法人员入侵' else: raise AiException(HttpStatus.SERVER_ERROR, AiErrorCode.YCEA4021012) alarm_info_hk_po = AlarmInfoHk() alarm_info_hk_po.id = gen_uuid() alarm_info_hk_po.ccbins_id = orgName alarm_info_hk_po.eqmt_id = deviceName alarm_info_hk_po.bsn_cgycd = alarmType alarm_info_hk_po.vlt_err_alrm_inf = vlt_err_alrm_inf alarm_info_hk_po.alarm_level = alarmLevel alarm_info_hk_po.stdt_tm = datetime.strptime(alarmTime, '%Y%m%d %H:%M:%S') if remark and len(remark) > 0: try: cur_date = time.strftime('%Y%m%d', time.localtime(time.time())) folder_path = yaml_cfg.get( 'illegal_image_path_hk') + "/" + cur_date + '/' + orgName if not os.path.exists(folder_path): os.makedirs(folder_path) img_path = (folder_path + "/" + alarm_info_hk_po.id + ".jpg") cv.imwrite('D:/image/ab.jpg', img) # f.save(img_path) alarm_info_hk_po.uploadfiletrgtrfullnm = img_path except Exception: logger.error("存储hk告警图片失败, " + alarmType + "," + vlt_err_alrm_inf + "," + alarmTime) alarm_hk_dao.add_record(alarm_info_hk_po) return make_response(status=HttpStatus.SUCCESS, data={"code": "success"})
def post(self): # 错误码包含参数,需要两个参数,且传参正确 raise AiException(500, AiErrorCode.YCEA4021002, '/home/ap', 'a.txt')
def delete(self): # 错误码包含参数,需要两个参数,但少传了参数 raise AiException(500, AiErrorCode.YCEA4021002, 'd:')
def patch(self): # 只传基本参数 raise AiException(500, AiErrorCode.YCEA4021999)
def get(self): # 什么参数都不传 raise AiException()
def get(self): logger.error("my test") raise AiException()