3 files
Browse files- ImageEncoder.py +17 -0
- face_checker.py +48 -0
- face_extract.py +31 -0
ImageEncoder.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import face_recognition as fr
|
| 2 |
+
import pickle
|
| 3 |
+
import os
|
| 4 |
+
def img_enc(face):
|
| 5 |
+
encoded={}
|
| 6 |
+
|
| 7 |
+
faces = fr.load_image_file(face)
|
| 8 |
+
face_enc = fr.face_encodings(faces)[0]
|
| 9 |
+
encoded[face.split(".")[0]] = face_enc
|
| 10 |
+
return list(encoded.keys()),list(encoded.values())
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# face_known,face_enco_done= img_enc()
|
| 14 |
+
# with open("data.pickle","wb")as f:
|
| 15 |
+
# pickle.dump((face_known,face_enco_done),f)
|
| 16 |
+
|
| 17 |
+
|
face_checker.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import face_recognition as fr
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
import ImageEncoder as ie
|
| 5 |
+
|
| 6 |
+
#load encoded images
|
| 7 |
+
# with open("Resource\\data.pickle",'rb')as f:
|
| 8 |
+
# face_known,face_enco_done = pickle.load(f)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def check(face,test_face):
|
| 12 |
+
face_known,face_enco_done=ie.img_enc(face)
|
| 13 |
+
face_loca = []
|
| 14 |
+
face_enco = []
|
| 15 |
+
face_name =[]
|
| 16 |
+
|
| 17 |
+
#to read video and capture attendance
|
| 18 |
+
|
| 19 |
+
img = cv2.imread(test_face)
|
| 20 |
+
#small_frame = cv2.resize(frame,(0,0),fx = 0.25,fy = 0.25)
|
| 21 |
+
rgb_frame = img#small_frame#[:,:,::-1]
|
| 22 |
+
if True:
|
| 23 |
+
face_loca = fr.face_locations(rgb_frame)
|
| 24 |
+
face_enco = fr.face_encodings(rgb_frame,face_loca)
|
| 25 |
+
face_name = []
|
| 26 |
+
for face_enc in face_enco:
|
| 27 |
+
matches = fr.compare_faces(face_enco_done,face_enc)
|
| 28 |
+
name = "Unknown_Unknown"
|
| 29 |
+
face_distance = fr.face_distance(face_enco_done,face_enc)
|
| 30 |
+
best_match = np.argmin(face_distance)
|
| 31 |
+
|
| 32 |
+
if matches[best_match]:
|
| 33 |
+
return True
|
| 34 |
+
else:
|
| 35 |
+
return False
|
| 36 |
+
|
| 37 |
+
# to add a box on the detected face
|
| 38 |
+
## for (top, right, bottom, left), name in zip(face_loca, face_name):
|
| 39 |
+
## # Draw a box around the face
|
| 40 |
+
## cv2.rectangle(frame, (left-20, top-20), (right+20, bottom+20), (255, 0, 0), 2)
|
| 41 |
+
##
|
| 42 |
+
## # Draw a label with a name below the face
|
| 43 |
+
## cv2.rectangle(frame, (left-20, bottom -15), (right+20, bottom+20), (255, 0, 0), cv2.FILLED)
|
| 44 |
+
## font = cv2.FONT_HERSHEY_DUPLEX
|
| 45 |
+
## cv2.putText(frame, name, (left -20, bottom + 15), font, 1.0, (255, 255, 255), 2)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
|
face_extract.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def extract(face):
|
| 8 |
+
imagePath = face
|
| 9 |
+
|
| 10 |
+
image = cv2.imread(imagePath)
|
| 11 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 12 |
+
|
| 13 |
+
faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
|
| 14 |
+
faces = faceCascade.detectMultiScale(
|
| 15 |
+
gray,
|
| 16 |
+
scaleFactor=1.3,
|
| 17 |
+
minNeighbors=3,
|
| 18 |
+
minSize=(30, 30)
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# print("[INFO] Found {0} Faces.".format(len(faces)))
|
| 22 |
+
|
| 23 |
+
for (x, y, w, h) in faces:
|
| 24 |
+
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
|
| 25 |
+
roi_color = image[y:y + h, x:x + w]
|
| 26 |
+
|
| 27 |
+
name = str(w)+str(h)+'_faces.jpg'
|
| 28 |
+
cv2.imwrite(str(w) + str(h) + '_faces.jpg', roi_color)
|
| 29 |
+
return name
|
| 30 |
+
# status = cv2.imwrite('faces_detected.jpg', image)
|
| 31 |
+
# print("[INFO] Image faces_detected.jpg written to filesystem: ", status)
|