Created
November 13, 2020 23:18
-
-
Save thevickypedia/b0a194627fb06f026a2a88b2fcb42daa to your computer and use it in GitHub Desktop.
Face recognition script that can run in the background
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from threading import Thread | |
import cv2 | |
import face_recognition | |
class Face(Thread): | |
def __init__(self): | |
super(Face, self).__init__() | |
self.training_dataset = "train" | |
self.learning_rate = 0.6 | |
self.model = "hog" # model using which the images are matched | |
self.train_faces, self.train_names = [], [] | |
for character_dir in os.listdir(self.training_dataset): # loads the training dataset | |
try: | |
for file_name in os.listdir(f'{self.training_dataset}/{character_dir}'): | |
# loads all the files within the named repo | |
img = face_recognition.load_image_file(f'{self.training_dataset}/{character_dir}/{file_name}') | |
encoded = face_recognition.face_encodings(img)[0] # generates face encoding matrix | |
self.train_faces.append(encoded) | |
self.train_names.append(character_dir) | |
except (IndexError, NotADirectoryError): | |
pass | |
def run(self): | |
while True: | |
validation_video = cv2.VideoCapture(1) | |
ret, img = validation_video.read() | |
identifier = face_recognition.face_locations(img, model=self.model) | |
encoded_ = face_recognition.face_encodings(img, identifier) | |
for face_encoding, face_location in zip(encoded_, identifier): | |
results = face_recognition.compare_faces(self.train_faces, face_encoding, self.learning_rate) | |
if True in results: | |
match = self.train_names[results.index(True)] | |
print(match) | |
return match | |
if __name__ == '__main__': | |
Face().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment