kevin 2 年之前
父節點
當前提交
812c93a8d9
共有 4 個文件被更改,包括 119 次插入7 次删除
  1. 9 0
      configure.sh
  2. 4 3
      face_recognition_svm.py
  3. 95 0
      fr_flask.py
  4. 11 4
      requirements.txt

+ 9 - 0
configure.sh

@@ -0,0 +1,9 @@
+#! /bin/bash
+
+ABSPATH=$(cd "$(dirname "$0")"; pwd -P)
+if ! python -m venv "$ABSPATH"/venv;
+then
+  exit 1
+fi
+source "$ABSPATH"/venv/bin/activate
+python -m pip install -r "$ABSPATH"/requirements.txt

+ 4 - 3
face_recognition_svm.py

@@ -39,8 +39,8 @@ def train_image(image, person):
 
 
 if is_train:
 if is_train:
     # Training directory
     # Training directory
-    train_str = os.path.join('data', 'peeps', 'anchor')
-    train_dir = os.listdir(os.path.join('data', 'peeps', 'anchor'))
+    train_str = os.path.join('data', 'train')
+    train_dir = os.listdir(os.path.join('data', 'train'))
 
 
     # Loop through each person in the training directory
     # Loop through each person in the training directory
     with concurrent.futures.ThreadPoolExecutor() as executor:
     with concurrent.futures.ThreadPoolExecutor() as executor:
@@ -64,7 +64,7 @@ if is_train:
     joblib.dump(clf, 'saved_model.pkl')
     joblib.dump(clf, 'saved_model.pkl')
 
 
 # Load the test image with unknown faces into a numpy array
 # Load the test image with unknown faces into a numpy array
-test_image = face_recognition.load_image_file(os.path.join('application_data', 'input_image', 'input_image3.jpg'))
+test_image = face_recognition.load_image_file(os.path.join('application_data', 'input_image', 'input_image2.jpg'))
 
 
 # Find all the faces in the test image using the default HOG-based model
 # Find all the faces in the test image using the default HOG-based model
 face_locations = face_recognition.face_locations(test_image)
 face_locations = face_recognition.face_locations(test_image)
@@ -80,6 +80,7 @@ for i in range(no):
     end_time = time.perf_counter_ns()
     end_time = time.perf_counter_ns()
     process_time = end_time - start_time
     process_time = end_time - start_time
     classes = clf.classes_
     classes = clf.classes_
+    print(classes)
     i = np.argmax(proba)
     i = np.argmax(proba)
     proba = list(*proba)
     proba = list(*proba)
     name = classes[i]
     name = classes[i]

+ 95 - 0
fr_flask.py

@@ -0,0 +1,95 @@
+from PIL import ImageFile
+from flask import Flask, request, jsonify
+import face_recognition
+import base64
+from io import BytesIO
+import joblib
+import numpy as np
+
+ImageFile.SAFEBLOCK = 2048 * 2048
+
+app = Flask(__name__)
+model_file_name = "saved_model.pkl"
+clf = None
+classes = None
+dummy_data = [
+    {
+        "name": "Bayu",
+        "address": "299 St Louis Road Oak Forest, IL 60452",
+        "nik": "1000076456784631"
+    },
+    {
+        "name": "Dio",
+        "address": "22 Whitemarsh St. Mansfield, MA 02048",
+        "nik": "1000024792887549"
+    },
+    {
+        "name": "Hadi",
+        "address": "643 Honey Creek Dr. Milledgeville, GA 31061",
+        "nik": "1000038502830420"
+    },
+    {
+        "name": "Kevin",
+        "address": "881 Cooper Ave. Hummelstown, PA 17036",
+        "nik": "1000045356476664"
+    },
+    {
+        "name": "Matrix",
+        "address": "580 Glenwood Dr. Garner, NC 27529",
+        "nik": "1000023452134598"
+    },
+    {
+        "name": "Surya",
+        "address": "909 South St Paul Street Hopewell, VA 23860",
+        "nik": "1000075656784734"
+    },
+]
+ssl = None
+
+
+@app.route('/predict', methods=['POST'])
+def predict():
+    result = []
+    if "image" in request.json:
+        im_b64 = request.json["image"]
+    elif "image" in request.files:
+        im_b64 = request.files["image"]
+    elif "image" in request.form:
+        im_b64 = request.form["image"]
+    else:
+        return {"error": "Error reading image"}
+    im_bytes = base64.b64decode(im_b64)
+    im_file = BytesIO(im_bytes)
+    test_image = face_recognition.load_image_file(im_file)
+    face_locations = face_recognition.face_locations(test_image)
+    no = len(face_locations)
+    for i in range(no):
+        test_image_enc = face_recognition.face_encodings(test_image)[i]
+        proba_list = clf.predict_proba([test_image_enc])
+        i = np.argmax(proba_list)
+        proba = list(*proba_list)[i]
+        name = dummy_data[i]["name"]
+        address = dummy_data[i]["address"]
+        nik = dummy_data[i]["nik"]
+        js = {
+            "id": str(i),
+            "name": name,
+            "address": address,
+            "nik": nik,
+            "proba": proba
+        }
+        result.append(js)
+    return jsonify(result)
+
+
+if __name__ == '__main__':
+    try:
+        clf = joblib.load(model_file_name)
+        classes = clf.classes_
+        print('model loaded')
+
+    except FileNotFoundError as e:
+        print('No model here')
+        exit(1)
+
+    app.run(host='0.0.0.0', port=8349, debug=True, ssl_context=ssl)

+ 11 - 4
requirements.txt

@@ -2,10 +2,17 @@ click==8.1.3
 dlib==19.24.0
 dlib==19.24.0
 face-recognition==1.3.0
 face-recognition==1.3.0
 face-recognition-models==0.3.0
 face-recognition-models==0.3.0
+Flask==2.2.2
+importlib-metadata==4.12.0
+itsdangerous==2.1.2
+Jinja2==3.1.2
 joblib==1.1.0
 joblib==1.1.0
-numpy==1.23.3
-opencv-python==4.6.0.66
+MarkupSafe==2.1.1
+numpy==1.21.6
 Pillow==9.2.0
 Pillow==9.2.0
-scikit-learn==1.1.2
-scipy==1.9.1
+scikit-learn==1.0.2
+scipy==1.7.3
 threadpoolctl==3.1.0
 threadpoolctl==3.1.0
+typing_extensions==4.3.0
+Werkzeug==2.2.2
+zipp==3.8.1