Created
October 9, 2020 04:36
-
-
Save Abhayparashar31/44815c8c8ab95bcaf608120dcd9a8986 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 face_recognition | |
import cv2 | |
import os | |
from google.colab.patches import cv2_imshow | |
def read_img(path): | |
img = cv2.imread(path) | |
(h,w) = img.shape[:2] | |
width = 500 | |
ratio = width / float(w) | |
height = int(h * ratio) | |
return cv2.resize(img,(width,height)) | |
known_encodings = [] | |
known_names = [] | |
known_dir = 'known' | |
for file in os.listdir(known_dir): | |
img = read_img(known_dir + '/' + file) | |
img_enc = face_recognition.face_encodings(img)[0] | |
known_encodings.append(img_enc) | |
known_names.append(file.split('.')[0]) | |
print(known_names) | |
print(os.listdir(unknown_dir)) | |
unknown_dir = 'unknown' | |
for file in os.listdir(unknown_dir): | |
print("Processing",file) | |
img = read_img(unknown_dir + '/' + file) | |
img_enc = face_recognition.face_encodings(img)[0] | |
results = face_recognition.compare_faces(known_encodings,img_enc) | |
print(face_recognition.face_distance(known_encodings,img_enc)) | |
res = [i for i, val in enumerate(results) if val] | |
name = known_names[res[0]] | |
(top,right,bottom,left) = face_recognition.face_locations(img)[0] | |
cv2.rectangle(img,(left,top),(right,bottom),(255,255,100),2) | |
cv2.putText(img,name,(left+2,bottom+20),cv2.FONT_HERSHEY_PLAIN,3,(100,0,255),2) | |
cv2_imshow(img) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
may error pa din