I created a small library and an example application in Python for learning about facial recognition and experimenting with it. Right now though, I am loading a list of file names into memory and then allowing the code to iterate over those names in order to do comparisons or to find the faces.
I think my example and the library is pretty inefficient but I am unsure of how best to handle large amounts of photos using my library. Also, it only works on one photo at a time but my understanding is that the facial recognition library uses all cores and I should only look at one photo at a time.
How can I make my code more efficient?
The original facial recognition library I am using is found at https://github.com/ageitgey/face_recognition
import face_recognition import os, os.path import ntpath from PIL import Image def get_image_directory(path): imgs =  valid_images = [".jpg",".gif",".png",".tga"] for f in os.listdir(path): ext = os.path.splitext(f) if ext.lower() not in valid_images: continue imgs.append(os.path.join(path,f)) return imgs def recognize_faces(path): file_name = path_leaf(path) image = face_recognition.load_image_file(path) face_locations = face_recognition.face_locations(image) i = 0 for face_location in face_locations: top, right, bottom, left = face_location face_image = image[top:bottom, left:right] pil_image = Image.fromarray(face_image) pil_image.save( 'output/' + str(file_name) + '_' +str(i)+".png") i+=1 def compare_faces(unknown_images, known_images): for image in unknown_images: unknown_comparison_image = create_face_comparison_encoding(image) for k_image in known_images: results = face_recognition.compare_faces([k_image],unknown_comparison_image) if results == True: print("This person appears familiar!") print("Writing the faces to output folder!") recognize_faces(image) else: print("This doesn't appear to be familiar!") def path_leaf(path): head, tail = ntpath.split(path) return tail or ntpath.basename(head) def create_face_comparison_encoding(image): known_image = face_recognition.load_image_file(image) known_image_encoding = face_recognition.face_encodings(known_image) return known_image_encoding
from ml_face import * ## # The Program ## unknown_persons = get_image_directory('images/') # Unknown Persons Directory known_persons = get_image_directory('known_person/') # Known Persons Directory known_list =  # Empty List for k_per in known_persons: known_comparison_image = create_face_comparison_encoding(k_per) known_list.append(known_comparison_image) compare_faces(unknown_persons,known_list)