face_recognition_svm.py 4.8 KB

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