face_recognition_svm.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import face_recognition
  2. from sklearn import svm
  3. import joblib
  4. import os
  5. import concurrent.futures
  6. import numpy as np
  7. import time
  8. import re
  9. # Training the SVC classifier
  10. # The training data would be all the face encodings from all the known images and the labels are their names
  11. encodings = []
  12. names = []
  13. tuples = []
  14. train_list = []
  15. lock = None
  16. model_name = 'saved_model_2.pkl'
  17. known_people_folder = os.path.join('application_data', 'verification_images')
  18. face_model = "large"
  19. if face_model == "large":
  20. model_name = 'saved_model_2_large.pkl'
  21. dummy_data = [
  22. {
  23. "name": "Bayu",
  24. "address": "299 St Louis Road Oak Forest, IL 60452",
  25. "nik": "1000076456784631"
  26. },
  27. {
  28. "name": "Dio",
  29. "address": "22 Whitemarsh St. Mansfield, MA 02048",
  30. "nik": "1000024792887549"
  31. },
  32. {
  33. "name": "Hadi",
  34. "address": "643 Honey Creek Dr. Milledgeville, GA 31061",
  35. "nik": "1000038502830420"
  36. },
  37. {
  38. "name": "Kevin",
  39. "address": "881 Cooper Ave. Hummelstown, PA 17036",
  40. "nik": "1000045356476664"
  41. },
  42. {
  43. "name": "Matrix",
  44. "address": "580 Glenwood Dr. Garner, NC 27529",
  45. "nik": "1000023452134598"
  46. },
  47. {
  48. "name": "Surya",
  49. "address": "909 South St Paul Street Hopewell, VA 23860",
  50. "nik": "1000075656784734"
  51. },
  52. ]
  53. def scan_known_people(known_people_folder):
  54. known_names = []
  55. known_face_encodings = []
  56. for file in image_files_in_folder(known_people_folder):
  57. img = face_recognition.load_image_file(file)
  58. encodings = face_recognition.face_encodings(img)
  59. if len(encodings) > 1:
  60. print("WARNING: More than one face found in {}. Only considering the first face.".format(file))
  61. if len(encodings) == 0:
  62. print("WARNING: No faces found in {}. Ignoring file.".format(file))
  63. else:
  64. known_face_encodings.append(encodings[0])
  65. return known_face_encodings
  66. def image_files_in_folder(folder):
  67. img_list = [os.path.join(folder, f) for f in os.listdir(folder) if
  68. re.match(r'.*\.(jpg|jpeg|png|webp)', f, flags=re.I)]
  69. img_list.sort()
  70. return img_list
  71. known_faces = scan_known_people(known_people_folder)
  72. is_train = False
  73. if not is_train:
  74. try:
  75. clf = joblib.load(model_name)
  76. except:
  77. clf = None
  78. if clf is None:
  79. is_train = True
  80. def train_image(image, person):
  81. face = face_recognition.load_image_file(image)
  82. face_bounding_boxes = face_recognition.face_locations(face)
  83. # If training image contains exactly one face
  84. if len(face_bounding_boxes) == 1:
  85. face_enc = face_recognition.face_encodings(face, model=face_model)[0]
  86. # Add face encoding for current image with corresponding label (name) to the training data
  87. tuples.append((face_enc, person))
  88. else:
  89. print(image + " was skipped and can't be used for training")
  90. if is_train:
  91. # Training directory
  92. train_str = os.path.join('data', 'train')
  93. train_dir = os.listdir(os.path.join('data', 'train'))
  94. train_dir.sort()
  95. # Loop through each person in the training directory
  96. with concurrent.futures.ThreadPoolExecutor() as executor:
  97. for person in train_dir:
  98. pix_str = os.path.join(train_str, person)
  99. pix = os.listdir(os.path.join(train_str, person))
  100. # Loop through each training image for the current person
  101. for person_img in pix:
  102. # Get the face encodings for the face in each image file
  103. image = os.path.join(pix_str, person_img)
  104. executor.submit(train_image, image, person)
  105. # Create and train the SVC classifier
  106. encodings = [x for x, _ in tuples]
  107. names = [y for _, y in tuples]
  108. clf = svm.SVC(gamma='scale', probability=True)
  109. clf.fit(encodings, names)
  110. joblib.dump(clf, model_name)
  111. # Load the test image with unknown faces into a numpy array
  112. test_image = face_recognition.load_image_file(os.path.join('application_data', 'input_image', 'input_image.jpg'))
  113. # Find all the faces in the test image using the default HOG-based model
  114. face_locations = face_recognition.face_locations(test_image)
  115. no = len(face_locations)
  116. print("Number of faces detected: ", no)
  117. # Predict all the faces in the test image using the trained classifier
  118. print("Found:")
  119. for i in range(no):
  120. test_image_enc = face_recognition.face_encodings(test_image, model=face_model)[i]
  121. start_time = time.perf_counter_ns()
  122. proba_list = clf.predict_proba([test_image_enc])
  123. dist = face_recognition.face_distance(known_faces, test_image_enc)
  124. total = np.subtract(proba_list, dist)
  125. end_time = time.perf_counter_ns()
  126. process_time = end_time - start_time
  127. classes = clf.classes_
  128. print(classes)
  129. print(total)
  130. i = np.argmax(total)
  131. proba = list(*proba_list)
  132. name = dummy_data[i]["name"]
  133. print(name, "{:.2f}".format(proba[i]))
  134. print(proba)
  135. print(list(dist))
  136. print(list(*total))
  137. print(process_time)