Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import face_recognition
- import cv2
- # 1. Process each video frame at 1/4 resolution (though still display it at full resolution)
- # 2. Only detect faces in every other frame of video.
- class Face_Recognition:
- def __init__(self):
- self.video_capture = cv2.VideoCapture(0)
- # Initialize some variables
- self.face_locations = []
- self.face_encodings = []
- self.face_names = []
- self.process_this_frame = True
- self.ret, self.frame = self.video_capture.read()
- self.small_frame = cv2.resize(self.frame, (0, 0), fx=0.25, fy=0.25)
- #self.list = []
- #self.best_match = []
- self.name = "Unknown"
- self.offset = 10
- #################### Load a sample picture and learn how to recognize it #################################
- self.vasilis_image = face_recognition.load_image_file("/home/bill/amy/src/deep/dataset/vasilis.jpg")
- self.vasilis_face_encoding = face_recognition.face_encodings(self.vasilis_image)[0]
- self.pete_image = face_recognition.load_image_file("/home/bill/amy/src/deep/dataset/pete.jpg")
- self.pete_face_encoding = face_recognition.face_encodings(self.pete_image)[0]
- self.shaheer_image = face_recognition.load_image_file("/home/bill/amy/src/deep/dataset/shaheer.jpg")
- self.shaheer_face_encoding = face_recognition.face_encodings(self.shaheer_image)[0]
- self.guang_image = face_recognition.load_image_file("/home/bill/amy/src/deep/dataset/guang.jpg")
- self.guang_face_encoding = face_recognition.face_encodings(self.guang_image)[0]
- self.toby_image = face_recognition.load_image_file("/home/bill/amy/src/deep/dataset/toby.jpg")
- self.toby_face_encoding = face_recognition.face_encodings(self.toby_image)[0]
- self.cyriac_image = face_recognition.load_image_file("/home/bill/amy/src/deep/dataset/cyriac.jpg")
- self.cyriac_face_encoding = face_recognition.face_encodings(self.cyriac_image)[0]
- def check_cam(self):
- # Check if camera opened successfully
- print('hello')
- if (self.video_capture.isOpened() == False):
- print("Unable to read camera feed")
- else:
- return True
- def detect_face(self):
- # Only process every other frame of video to save time
- if self.process_this_frame:
- # Find all the faces and face encodings in the current frame of video
- self.face_locations = face_recognition.face_locations(self.small_frame)
- self.face_encodings = face_recognition.face_encodings(self.small_frame, self.face_locations)
- #face_names = []
- for self.face_encoding in self.face_encodings:
- # See if the face is a match for the known face(s)
- match1 = face_recognition.compare_faces([self.vasilis_face_encoding], self.face_encoding)
- match2 = face_recognition.compare_faces([self.pete_face_encoding], self.face_encoding)
- match3 = face_recognition.compare_faces([self.shaheer_face_encoding], self.face_encoding)
- match4 = face_recognition.compare_faces([self.guang_face_encoding], self.face_encoding)
- match5 = face_recognition.compare_faces([self.toby_face_encoding], self.face_encoding)
- match6 = face_recognition.compare_faces([self.cyriac_face_encoding], self.face_encoding)
- list = [match1, match2, match3, match4, match5, match6]
- best_match = max(list)
- self.name = "Unknown"
- if best_match == match1:
- self.name = "Vasilis"
- elif best_match == match2:
- self.name = "Pete"
- elif best_match == match3:
- self.name = "Shaheer"
- elif best_match == match4:
- self.name = "Guang"
- elif best_match == match5:
- self.name = "Toby"
- elif best_match == match6:
- self.name = "Cyriac"
- else:
- self.name = "Unknown"
- self.face_names.append(self.name)
- else:
- self.name = "No face"
- self.face_names.append(self.name)
- self.process_this_frame = not self.process_this_frame
- # Display the results
- for (top, right, bottom, left), name in zip(self.face_locations, self.face_names):
- # Scale back up face locations since the frame we detected in was scaled to 1/4 size
- top *= 4
- right *= 4
- bottom *= 4
- left *= 4
- # Draw a box around the face
- cv2.rectangle(self.frame, (left, top), (right, bottom), (0, 0, 255), 2)
- # Draw a label with a name below the face
- cv2.rectangle(self.frame, (left, bottom - 35), (right, bottom), (0, 0, 255))
- font = cv2.FONT_HERSHEY_DUPLEX
- cv2.putText(self.frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
- return self.face_names
- def take_signature(self, face_amount):
- if face_amount == 1:
- cv2.imwrite("/home/bill/amy/src/deep/dataset/TEST.jpg", self.frame)
- #for (x,y,w,h) in zip(self.face_locations, self.face_names) :
- #cv2.imwrite("/home/bill/amy/src/deep/dataset/TEST.jpg", self.frame[y-10:y + h+10, x-10:x + w+10])
- #self.video_capture.release()
- A=Face_Recognition()
- #A.check_cam()
- x = len(A.detect_face())
- A.take_signature(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement