|
|
@@ -21,6 +21,9 @@ import random
|
|
|
import math
|
|
|
from ossClient import OssClient
|
|
|
import string
|
|
|
+import requests
|
|
|
+from settings import face_callback, number_callback
|
|
|
+from datetime import datetime
|
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
@@ -227,31 +230,163 @@ def generate_random_str(randomlength=16):
|
|
|
random_str = ''.join(str_list)
|
|
|
return random_str
|
|
|
|
|
|
-# API
|
|
|
-@app.route("/", methods=['POST'])
|
|
|
+
|
|
|
+def checkNum(remotePath, localPath):
|
|
|
+ image = (remotePath, localPath)
|
|
|
+ gpu_memory_fraction=1.0
|
|
|
+ minsize = 20 # minimum size of face
|
|
|
+ threshold = [0.6, 0.7, 0.7] # three steps's threshold
|
|
|
+ factor = 0.709 # scale factor
|
|
|
+
|
|
|
+ print('Creating networks and loading parameters')
|
|
|
+ with tf.Graph().as_default():
|
|
|
+ gpu_options = tf.GPUOptions(
|
|
|
+ per_process_gpu_memory_fraction=gpu_memory_fraction)
|
|
|
+ sess = tf.Session(config=tf.ConfigProto(
|
|
|
+ gpu_options=gpu_options, log_device_placement=False))
|
|
|
+ with sess.as_default():
|
|
|
+ pnet, rnet, onet = align.detect_face.create_mtcnn(sess, None)
|
|
|
+
|
|
|
+ img = misc.imread(os.path.expanduser(image[1]), mode='RGB')
|
|
|
+ bounding_boxes, _ = align.detect_face.detect_face(
|
|
|
+ img, minsize, pnet, rnet, onet, threshold, factor)
|
|
|
+ if len(bounding_boxes) < 1:
|
|
|
+ return 0;
|
|
|
+ bounding_boxes = filter_box(sort_box(bounding_boxes))
|
|
|
+ return len(bounding_boxes)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+# API /face
|
|
|
+
|
|
|
+# request params:
|
|
|
+# {
|
|
|
+# "examId": 1,
|
|
|
+# "userId": 1,
|
|
|
+# "info": {
|
|
|
+# "baseface": "url1",
|
|
|
+# "face": "url2",
|
|
|
+# “ok": true / false/ None
|
|
|
+# }
|
|
|
+# }
|
|
|
+
|
|
|
+# callback params:
|
|
|
+# {
|
|
|
+# "userId": 1,
|
|
|
+# "examId": 1,
|
|
|
+# "info": {
|
|
|
+# "current_face_address": "/development/uploadImages/xxx.png",
|
|
|
+# "current_shot_time": "UTC time"
|
|
|
+# },
|
|
|
+# "ok": False # True true代表考前人脸识别成功,false代表考前人脸识别失败,None表示周期性人脸识别
|
|
|
+# }
|
|
|
+
|
|
|
+@app.route("/face", methods=['POST'])
|
|
|
def compare():
|
|
|
content_type = request.headers.get('Content-Type')
|
|
|
if (content_type == 'application/json'):
|
|
|
- json = request.json
|
|
|
- res = None
|
|
|
+ try:
|
|
|
+ reqjson = request.get_json()
|
|
|
+ except Exception as ignore:
|
|
|
+ return R("","参数出错",status.HTTP_400_BAD_REQUEST)
|
|
|
+ same = None
|
|
|
ossClient = OssClient()
|
|
|
try:
|
|
|
file1 = generate_random_str()
|
|
|
file2 = generate_random_str()
|
|
|
- baseImage = ossClient.downloadFileToLocal(json["baseImageUrl"], file1)
|
|
|
- image = ossClient.downloadFileToLocal(json["imageUrl"], file2)
|
|
|
- res = is_same(compare_images((json["baseImageUrl"],file1),(json["imageUrl"],file2)))
|
|
|
+ ossClient.downloadFileToLocal(reqjson["info"]["baseface"], file1)
|
|
|
+ ossClient.downloadFileToLocal(reqjson["info"]["face"], file2)
|
|
|
+ same = is_same(compare_images((reqjson["info"]["baseface"],file1),(reqjson["info"]["face"],file2)))
|
|
|
# TODO
|
|
|
os.remove(file1)
|
|
|
os.remove(file2)
|
|
|
- except Exception as e:
|
|
|
- raise(e)
|
|
|
+ except Exception as ignore:
|
|
|
return R("","服务出错",status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
|
- return R(res, "")
|
|
|
+ # callback
|
|
|
+ callback_json={
|
|
|
+ "userId": reqjson["userId"],
|
|
|
+ "examId": reqjson["examId"],
|
|
|
+ "info": {
|
|
|
+ "current_face_address": reqjson["info"]["face"],
|
|
|
+ "current_shot_time": "T".join(str(datetime.utcnow()).split(" "))
|
|
|
+ },
|
|
|
+ "ok": None
|
|
|
+ }
|
|
|
+ if reqjson["info"]["ok"]:
|
|
|
+ callback_json["ok"] = same
|
|
|
+ requests.post(
|
|
|
+ url=face_callback,
|
|
|
+ data=None,
|
|
|
+ json=callback_json
|
|
|
+ )
|
|
|
+ elif not same:
|
|
|
+ requests.post(
|
|
|
+ url=face_callback,
|
|
|
+ data=None,
|
|
|
+ json=callback_json
|
|
|
+ )
|
|
|
+ return R(same, "")
|
|
|
else:
|
|
|
return R("","无效的请求类型",status.HTTP_400_BAD_REQUEST)
|
|
|
|
|
|
-
|
|
|
+# API /number
|
|
|
+
|
|
|
+# request params:
|
|
|
+# {
|
|
|
+# "examId": 1,
|
|
|
+# "userId": 1,
|
|
|
+# "info": {
|
|
|
+# "number": "url1",
|
|
|
+# }
|
|
|
+# }
|
|
|
+
|
|
|
+# callback params:
|
|
|
+# {
|
|
|
+# "userId": 1,
|
|
|
+# "examId": 1,
|
|
|
+# "info": {
|
|
|
+# "current_pic_address": "/development/uploadImages/xxx.png",
|
|
|
+# "current_shot_time": "UTC time",
|
|
|
+# "number": 2
|
|
|
+# }
|
|
|
+# }
|
|
|
+@app.route("/number", methods=['POST'])
|
|
|
+def number():
|
|
|
+ content_type = request.headers.get('Content-Type')
|
|
|
+ if (content_type == 'application/json'):
|
|
|
+ try:
|
|
|
+ reqjson = request.get_json()
|
|
|
+ except Exception as ignore:
|
|
|
+ return R("","参数出错",status.HTTP_400_BAD_REQUEST)
|
|
|
+ number = None
|
|
|
+ ossClient = OssClient()
|
|
|
+ try:
|
|
|
+ file1 = generate_random_str()
|
|
|
+ ossClient.downloadFileToLocal(reqjson["info"]["number"], file1)
|
|
|
+ number = checkNum(reqjson["info"]["number"],file1)
|
|
|
+ os.remove(file1)
|
|
|
+ except Exception as ignore:
|
|
|
+ return R("","服务出错",status.HTTP_500_INTERNAL_SERVER_ERROR)
|
|
|
+ # callback
|
|
|
+ callback_json={
|
|
|
+ "userId": reqjson["userId"],
|
|
|
+ "examId": reqjson["examId"],
|
|
|
+ "info": {
|
|
|
+ "current_pic_address": reqjson["info"]["number"],
|
|
|
+ "current_shot_time": "T".join(str(datetime.utcnow()).split(" ")),
|
|
|
+ "number": number
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if number!=1:
|
|
|
+ requests.post(
|
|
|
+ url=number_callback,
|
|
|
+ data=None,
|
|
|
+ json=callback_json
|
|
|
+ )
|
|
|
+ return R(number, "")
|
|
|
+ else:
|
|
|
+ return R("","无效的请求类型",status.HTTP_400_BAD_REQUEST)
|
|
|
+
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
- app.run(host='0.0.0.0', port=5000)
|
|
|
+ app.run(host='0.0.0.0', port=9100)
|