kevin 2 vuotta sitten
vanhempi
commit
45b781c2ec
3 muutettua tiedostoa jossa 21 lisäystä ja 13 poistoa
  1. 8 4
      face_recognition_svm.py
  2. 12 9
      fr_flask.py
  3. 1 0
      verify_face.py

+ 8 - 4
face_recognition_svm.py

@@ -17,6 +17,9 @@ train_list = []
 lock = None
 model_name = 'saved_model_2.pkl'
 known_people_folder = os.path.join('application_data', 'verification_images')
+face_model = "large"
+if face_model == "large":
+    model_name = 'saved_model_2_large.pkl'
 
 dummy_data = [
     {
@@ -51,6 +54,7 @@ dummy_data = [
     },
 ]
 
+
 def scan_known_people(known_people_folder):
     known_names = []
     known_face_encodings = []
@@ -77,7 +81,7 @@ def image_files_in_folder(folder):
     return img_list
 
 
-face_encodings = scan_known_people(known_people_folder)
+known_faces = scan_known_people(known_people_folder)
 
 is_train = False
 
@@ -96,7 +100,7 @@ def train_image(image, person):
 
     # If training image contains exactly one face
     if len(face_bounding_boxes) == 1:
-        face_enc = face_recognition.face_encodings(face)[0]
+        face_enc = face_recognition.face_encodings(face, model=face_model)[0]
         # Add face encoding for current image with corresponding label (name) to the training data
         tuples.append((face_enc, person))
     else:
@@ -141,10 +145,10 @@ print("Number of faces detected: ", no)
 # Predict all the faces in the test image using the trained classifier
 print("Found:")
 for i in range(no):
-    test_image_enc = face_recognition.face_encodings(test_image)[i]
+    test_image_enc = face_recognition.face_encodings(test_image, model=face_model)[i]
     start_time = time.perf_counter_ns()
     proba_list = clf.predict_proba([test_image_enc])
-    dist = face_recognition.face_distance(face_encodings, test_image_enc)
+    dist = face_recognition.face_distance(known_faces, test_image_enc)
     total = np.subtract(proba_list, dist)
     end_time = time.perf_counter_ns()
     process_time = end_time - start_time

+ 12 - 9
fr_flask.py

@@ -48,18 +48,20 @@ dummy_data = [
     },
 ]
 ssl = None
-known_people_folder = "application_data/verification_images"
-face_encodings = []
-total_threshold = 0.2
+known_people = "application_data/verification_images"
+known_faces = []
+total_threshold = 0.3
+face_model = "large"
+if face_model == "large":
+    model_file_name = "saved_model_2_large.pkl"
 
 
 def scan_known_people(known_people_folder):
-    known_names = []
     known_face_encodings = []
 
     for file in image_files_in_folder(known_people_folder):
         img = face_recognition.load_image_file(file)
-        encodings = face_recognition.face_encodings(img)
+        encodings = face_recognition.face_encodings(img, model=face_model)
 
         if len(encodings) > 1:
             print("WARNING: More than one face found in {}. Only considering the first face.".format(file))
@@ -98,9 +100,9 @@ def predict():
     if no > 0:
         for i in range(no):
             start_time = time.perf_counter()
-            test_image_enc = face_recognition.face_encodings(test_image)[i]
+            test_image_enc = face_recognition.face_encodings(test_image, model=face_model)[i]
             proba_list = clf.predict_proba([test_image_enc])
-            dist = face_recognition.face_distance(face_encodings, test_image_enc)
+            dist = face_recognition.face_distance(known_faces, test_image_enc)
             total = np.subtract(proba_list, dist)
             i = np.argmax(total)
             proba = list(*proba_list)[i]
@@ -112,7 +114,8 @@ def predict():
                 "name": name,
                 "address": address,
                 "nik": nik,
-                "proba": proba
+                "proba": proba,
+                "delta": total[0][i]
             }
             if total[0][i] > total_threshold:
                 result.append(js)
@@ -129,7 +132,7 @@ if __name__ == '__main__':
         clf = joblib.load(model_file_name)
         classes = clf.classes_
         print('model loaded')
-        face_encodings = scan_known_people(known_people_folder)
+        known_faces = scan_known_people(known_people)
         print('known faces scanned')
 
     except FileNotFoundError as e:

+ 1 - 0
verify_face.py

@@ -61,6 +61,7 @@ while True:
 
     # Find all the faces and face enqcodings in the frame of video
     face_locations = face_recognition.face_locations(rgb_frame)
+    print(face_locations)
     no = len(face_locations)
     print("Number of faces detected: ", no)
     face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)