Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.54 KB | None | 0 0
  1. import face_recognition
  2. import cv2
  3.  
  4.  
  5. #   1. Process each video frame at 1/4 resolution (though still display it at full resolution)
  6. #   2. Only detect faces in every other frame of video.
  7.  
  8. class Face_Recognition:
  9.  
  10.     def __init__(self):
  11.         self.video_capture = cv2.VideoCapture(0)
  12.  
  13.         # Initialize some variables
  14.         self.face_locations = []
  15.         self.face_encodings = []
  16.         self.face_names = []
  17.         self.process_this_frame = True
  18.         self.ret, self.frame = self.video_capture.read()
  19.         self.small_frame = cv2.resize(self.frame, (0, 0), fx=0.25, fy=0.25)
  20.         #self.list = []
  21.         #self.best_match = []
  22.         self.name = "Unknown"
  23.         self.offset = 10
  24.  
  25.  
  26.         #################### Load a sample picture and learn how to recognize it #################################
  27.         self.vasilis_image = face_recognition.load_image_file("/home/bill/amy/src/deep/dataset/vasilis.jpg")
  28.         self.vasilis_face_encoding = face_recognition.face_encodings(self.vasilis_image)[0]
  29.  
  30.         self.pete_image = face_recognition.load_image_file("/home/bill/amy/src/deep/dataset/pete.jpg")
  31.         self.pete_face_encoding = face_recognition.face_encodings(self.pete_image)[0]
  32.  
  33.         self.shaheer_image = face_recognition.load_image_file("/home/bill/amy/src/deep/dataset/shaheer.jpg")
  34.         self.shaheer_face_encoding = face_recognition.face_encodings(self.shaheer_image)[0]
  35.  
  36.         self.guang_image = face_recognition.load_image_file("/home/bill/amy/src/deep/dataset/guang.jpg")
  37.         self.guang_face_encoding = face_recognition.face_encodings(self.guang_image)[0]
  38.  
  39.         self.toby_image = face_recognition.load_image_file("/home/bill/amy/src/deep/dataset/toby.jpg")
  40.         self.toby_face_encoding = face_recognition.face_encodings(self.toby_image)[0]
  41.  
  42.         self.cyriac_image = face_recognition.load_image_file("/home/bill/amy/src/deep/dataset/cyriac.jpg")
  43.         self.cyriac_face_encoding = face_recognition.face_encodings(self.cyriac_image)[0]
  44.  
  45.     def check_cam(self):
  46.         # Check if camera opened successfully
  47.         print('hello')
  48.         if (self.video_capture.isOpened() == False):
  49.             print("Unable to read camera feed")
  50.         else:
  51.             return True
  52.  
  53.     def detect_face(self):
  54.         # Only process every other frame of video to save time
  55.         if self.process_this_frame:
  56.             # Find all the faces and face encodings in the current frame of video
  57.             self.face_locations = face_recognition.face_locations(self.small_frame)
  58.             self.face_encodings = face_recognition.face_encodings(self.small_frame, self.face_locations)
  59.  
  60.             #face_names = []
  61.             for self.face_encoding in self.face_encodings:
  62.                 # See if the face is a match for the known face(s)
  63.                 match1 = face_recognition.compare_faces([self.vasilis_face_encoding], self.face_encoding)
  64.                 match2 = face_recognition.compare_faces([self.pete_face_encoding], self.face_encoding)
  65.                 match3 = face_recognition.compare_faces([self.shaheer_face_encoding], self.face_encoding)
  66.                 match4 = face_recognition.compare_faces([self.guang_face_encoding], self.face_encoding)
  67.                 match5 = face_recognition.compare_faces([self.toby_face_encoding], self.face_encoding)
  68.                 match6 = face_recognition.compare_faces([self.cyriac_face_encoding], self.face_encoding)
  69.  
  70.                 list = [match1, match2, match3, match4, match5, match6]
  71.                 best_match = max(list)
  72.  
  73.                 self.name = "Unknown"
  74.                 if best_match == match1:
  75.                     self.name = "Vasilis"
  76.                 elif best_match == match2:
  77.                     self.name = "Pete"
  78.                 elif best_match == match3:
  79.                     self.name = "Shaheer"
  80.                 elif best_match == match4:
  81.                     self.name = "Guang"
  82.                 elif best_match == match5:
  83.                     self.name = "Toby"
  84.                 elif best_match == match6:
  85.                     self.name = "Cyriac"
  86.                 else:
  87.                     self.name = "Unknown"
  88.                 self.face_names.append(self.name)
  89.         else:
  90.             self.name = "No face"
  91.             self.face_names.append(self.name)
  92.         self.process_this_frame = not self.process_this_frame
  93.  
  94.         # Display the results
  95.         for (top, right, bottom, left), name in zip(self.face_locations, self.face_names):
  96.             # Scale back up face locations since the frame we detected in was scaled to 1/4 size
  97.             top *= 4
  98.             right *= 4
  99.             bottom *= 4
  100.             left *= 4
  101.  
  102.             # Draw a box around the face
  103.             cv2.rectangle(self.frame, (left, top), (right, bottom), (0, 0, 255), 2)
  104.  
  105.             # Draw a label with a name below the face
  106.             cv2.rectangle(self.frame, (left, bottom - 35), (right, bottom), (0, 0, 255))
  107.             font = cv2.FONT_HERSHEY_DUPLEX
  108.             cv2.putText(self.frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
  109.         return self.face_names
  110.  
  111.     def take_signature(self, face_amount):
  112.         if face_amount == 1:
  113.             cv2.imwrite("/home/bill/amy/src/deep/dataset/TEST.jpg", self.frame)
  114.             #for (x,y,w,h) in zip(self.face_locations, self.face_names) :
  115.                 #cv2.imwrite("/home/bill/amy/src/deep/dataset/TEST.jpg", self.frame[y-10:y + h+10, x-10:x + w+10])
  116.                 #self.video_capture.release()
  117.  
  118.  
  119.  
  120. A=Face_Recognition()
  121.  
  122. #A.check_cam()
  123. x = len(A.detect_face())
  124. A.take_signature(x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement