def upload_faceimg_to_recognition(): if 'file' not in request.files: return XaResult.error(0, u'请上传照片') if 'name' not in request.form: return XaResult.error(0, u'请提供姓名') faceimg = request.files['file'] name = request.form['name'] if faceimg.filename == '': return XaResult.error(0, u'无效的文件名') if not allowed_file(faceimg.filename): return XaResult.error(0, u'不支持的文件格式') locate_image = face_recognition.load_image_file(faceimg) face_locations = face_recognition.face_locations(locate_image) face_count = len(face_locations) if face_count == 0: return XaResult.error(0, u'照片中没能找到人脸') for face_location in face_locations: top, right, bottom, left = face_location print "A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right) if face_count > 1: return XaResult.error(0, u'照片中存在多张人脸') unknown_face_encodings = face_recognition.face_encodings(face_recognition.load_image_file(faceimg)) if not len(unknown_face_encodings) > 0: return XaResult.error(0, u'照片中没能找到人脸') extendFaceDict = FaceDict.objects(Q(name=name)).first() if extendFaceDict is None: return XaResult.error(0, u'无初始化照片,请先初始化') know_name = extendFaceDict.name know_face_encoding = extendFaceDict.faceEncoding if know_name is None: return XaResult.error(0, u'姓名不存在') if know_face_encoding is None: return XaResult.error(0, u'照片不存在') # 获取已知照片和上传照片,人脸特征点的距离 face_distances = face_recognition.face_distance(know_face_encoding, unknown_face_encodings[0]) print face_distances if not face_distances <= app.config['TOLERANCE']: return XaResult.success(u'人脸匹配失败') return XaResult.success(u'确认过眼神,是同一个人')
def instance_getone(): instanceId = request.values[ 'instanceId'] if 'instanceId' in request.values else '' jobInstance = JobInstance.objects((Q(instanceId=instanceId))).first() if jobInstance is None: return XaResult.error(msg=u'任务实例不存在') return XaResult.success(jobInstance)
def delete(): name = request.form['name'] if 'name' in request.form else '' extendJobDefine = JobDefine.objects((Q(name=name))).first() if not extendJobDefine: return XaResult.error(msg=u'请提供正确且存在的任务名称') if extendJobDefine.isOpen: return XaResult.error(msg=u'该任务正在运行中,请停止后再删除') JobDefine.objects(name=name).update_one(isDelete=True) return XaResult.success()
def create(): name = request.form['name'] if 'name' in request.form else '' displayName = request.form[ 'displayName'] if 'displayName' in request.form else '' defJson = request.form['defJson'] if 'defJson' in request.form else '' isIncrement = request.form[ 'isIncrement'] if 'isIncrement' in request.form else 0 incrementParam = request.form[ 'incrementParam'] if 'incrementParam' in request.form else '' incrementParamStart = request.form[ 'incrementParamStart'] if 'incrementParamStart' in request.form else '' jobCron = request.form['jobCron'] if 'jobCron' in request.form else '' if StringUtil.isBlank(name): return XaResult.error(msg=u'请输入任务名称') if StringUtil.isBlank(displayName): return XaResult.error(msg=u'请输入任务用于显示的中文名称') if StringUtil.isBlank(defJson): return XaResult.error(msg=u'请输入任务Json模板') isIncrement = True if isIncrement and int(isIncrement) == 1 else False if isIncrement and StringUtil.isBlank(incrementParam): return XaResult.error(msg=u'增量同步任务,请指明Json模板中的参数名称') if isIncrement and StringUtil.isBlank(incrementParamStart): return XaResult.error(msg=u'增量同步任务,请指明Json模板中的参数起始值') if not JsonUtil.is_json(defJson): return XaResult.error(msg=u'请确保Json格式正确(先委屈一下,界面化配置后面马上开发)') if StringUtil.isBlank(jobCron): return XaResult.error(msg=u'请输入任务cron表达式') extendJobDefine = JobDefine.objects( Q(name=name) | Q(displayName=displayName)).first() if extendJobDefine is None: jobDefine = JobDefine(name=name, displayName=displayName, defJson=defJson, isIncrement=isIncrement, incrementParam=incrementParam, incrementParamStart=incrementParamStart, jobCron=jobCron) jobDefine.save() else: return XaResult.error(msg=u'任务名称和用于显示的中文名称不可重复') return XaResult.success()
def modify(): # 不能修改名称和用于显示的中文名称,你传了我也不会改 name = request.form['name'] if 'name' in request.form else '' defJson = request.form['defJson'] if 'defJson' in request.form else '' isIncrement = int( request.form['isIncrement']) if 'isIncrement' in request.form else 0 incrementParam = request.form[ 'incrementParam'] if 'incrementParam' in request.form else '' incrementParamStart = request.form[ 'incrementParamStart'] if 'incrementParamStart' in request.form else '' jobCron = request.form['jobCron'] if 'jobCron' in request.form else '' if StringUtil.isBlank(name): return XaResult.error(msg=u'请提供需要修改的任务名称') if StringUtil.isBlank(defJson): return XaResult.error(msg=u'请输入任务Json模板') if isIncrement and StringUtil.isBlank(incrementParam): return XaResult.error(msg=u'增量同步任务,请指明Json模板中的参数名称') if isIncrement and StringUtil.isBlank(incrementParamStart): return XaResult.error(msg=u'增量同步任务,请指明Json模板中的参数起始值') if not JsonUtil.is_json(defJson): return XaResult.error(msg=u'请确保Json格式正确(先委屈一下,界面化配置后面马上开发)') if StringUtil.isBlank(jobCron): return XaResult.error(msg=u'请输入任务cron表达式') extendJobDefine = JobDefine.objects((Q(name=name))).first() if extendJobDefine is None: return XaResult.error(msg=u'请提供正确且存在的任务名称') # 这里有并发更新覆盖的问题(这也是我对mongodb一直不太明白的地方) JobDefine.objects(name=name).update_one( defJson=defJson, isIncrement=True if isIncrement == 1 else False, incrementParam=incrementParam, incrementParamStart=incrementParamStart, jobCron=jobCron) return XaResult.success()
def upload_faceimg_to_init(): if 'file' not in request.files: return XaResult.error(0, u'请上传照片') if 'name' not in request.form: return XaResult.error(0, u'请提供姓名') faceimg = request.files['file'] name = request.form['name'] if faceimg.filename == '': return XaResult.error(0, u'无效的文件名') if not allowed_file(faceimg.filename): return XaResult.error(0, u'不支持的文件格式') face_file_path = app.config['KNOW_FACE_DIR'] + '/' + name if not os.path.exists(face_file_path): os.makedirs(face_file_path) to_save_file_name = str(uuid.uuid1()) + '.' + faceimg.filename.rsplit('.', 1)[1].lower() file_full_pash = os.path.join(face_file_path, to_save_file_name) faceimg.save(file_full_pash) faceimg.seek(0, os.SEEK_SET) face_encoding = face_recognition.face_encodings(face_recognition.load_image_file(faceimg)) extendFaceDict = FaceDict.objects(Q(name=name)).first() if extendFaceDict is None: faceDict = FaceDict(name=name, faceEncoding=face_encoding, faceFilePath=file_full_pash) faceDict.save() else: FaceDict.objects(name=name).update_one(faceEncoding=face_encoding, faceFilePath=file_full_pash) return XaResult.success(u'初始化成功')
def getone(): name = request.values['name'] if 'name' in request.values else '' extendJobDefine = JobDefine.objects((Q(name=name))).first() if extendJobDefine is None: return XaResult.error(msg=u'请提供正确且存在的任务名称') return XaResult.success(extendJobDefine)
if extendJobDefine is None: return XaResult.error(msg=u'请提供正确且存在的任务名称') try: cron = CronTrigger.from_crontab(extendJobDefine.jobCron) scheduler.add_job(id=name, func=job_task_load.to_job_instance, trigger=cron, args=(name, )) JobDefine.objects(name=name).update_one(isRun=True) app.logger.info(u'%s 任务启动成功!', name) except Exception, e: app.logger.error(u'%s 任务启动失败!错误信息:%s', name, e.message) return XaResult.error(msg=e.message) return XaResult.success() @app.route('/job_instance/stop', methods=['POST']) def stop(): name = request.values['name'] if 'name' in request.values else '' extendJobDefine = JobDefine.objects((Q(name=name))).first() if extendJobDefine is None: return XaResult.error(msg=u'请提供正确且存在的任务名称') try: scheduler.remove_job(id=name) app.logger.info(u'%s 任务停止成功!', name) except KeyError, e: app.logger.error(u'%s 任务停止遇到错误,错误信息:%s', name, e.message)