Prechádzať zdrojové kódy

feat: face / number detect

sky 3 rokov pred
rodič
commit
456adc5e74
18 zmenil súbory, kde vykonal 167 pridanie a 14 odobranie
  1. 16 1
      curl_test.sh
  2. 1 1
      dockerrun.sh
  3. BIN
      image1878.png
  4. BIN
      img1.jpg
  5. BIN
      img2.jpg
  6. BIN
      img3.png
  7. BIN
      img4.png
  8. BIN
      img5.png
  9. BIN
      img6.png
  10. BIN
      img_2_big_small.png
  11. BIN
      img_2_small_big.png
  12. BIN
      img_2people.png
  13. BIN
      img_buff_dieman.png
  14. BIN
      img_buff_dieman_small.png
  15. BIN
      img_test5.png
  16. BIN
      img_test5_with_phone.png
  17. 147 12
      src/app.py
  18. 3 0
      src/settings.py

+ 16 - 1
curl_test.sh

@@ -1 +1,16 @@
-curl -H "Content-Type: application/json" -X POST -d '{"baseImageUrl": "development/test1.png", "imageUrl":"development/test2.png" }' "http://127.0.0.1:5000/"
+# face
+# test1 为 true
+# test2 为 false
+
+# number
+# test3 为 2
+# test4 为 0
+
+# curl -H "Content-Type: application/json" -X POST -d '{"examId": 1,"userId": 1,"info": {"baseface": "development/baseImages/MF21320091.png","face": "development/uploadImages/test1.jpg","ok": null}}' "http://127.0.0.1:9100/face"
+
+# curl -H "Content-Type: application/json" -X POST -d '{"examId": 1,"userId": 1,"info": {"baseface": "development/baseImages/MF21320091.png","face": "development/uploadImages/test2.jpg","ok": null}}' "http://127.0.0.1:9100/face"
+
+# curl -H "Content-Type: application/json" -X POST -d '{"examId": 1,"userId": 1,"info": {"number": "development/uploadImages/test3.jpg","ok": null}}' "http://127.0.0.1:9100/number"
+
+curl -H "Content-Type: application/json" -X POST -d '{"examId": 1,"userId": 1,"info": {"number": "development/uploadImages/test4.jpg","ok": null}}' "http://127.0.0.1:9100/number"
+

+ 1 - 1
dockerrun.sh

@@ -1 +1 @@
-docker run -p 5000:5000 -v D:\\study\\研究生毕业设计\\监考\\facenet:/facenet -it --name facenet facenet:v1 bash 
+docker run -p 9100:9100 -v D:\\study\\研究生毕业设计\\监考\\facenet:/facenet -it --name facenet facenet:v1 bash 

BIN
image1878.png


BIN
img1.jpg


BIN
img2.jpg


BIN
img3.png


BIN
img4.png


BIN
img5.png


BIN
img6.png


BIN
img_2_big_small.png


BIN
img_2_small_big.png


BIN
img_2people.png


BIN
img_buff_dieman.png


BIN
img_buff_dieman_small.png


BIN
img_test5.png


BIN
img_test5_with_phone.png


+ 147 - 12
src/app.py

@@ -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)

+ 3 - 0
src/settings.py

@@ -0,0 +1,3 @@
+callback_endpoint="http://172.26.16.1:8080"
+face_callback=callback_endpoint+"/faceCallback/invoke"
+number_callback=callback_endpoint+"/numberCallback/invoke"