Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. import face_recognition
  2. import glob
  3. import drawing
  4. import numpy as np
  5. from PIL import Image, ImageDraw
  6. from webcam_video_stream import WebcamVideoStream
  7. import cv2
  8.  
  9. class Recognition:
  10. def __init__(self):
  11.  
  12. # Variables setup
  13. self.known_face_encodings = []
  14. self.known_face_names = []
  15. self.current_face_encodings = None
  16. self.current_face_locations = None
  17. self.last_face_locations = None
  18.  
  19. self.dirty = True
  20.  
  21. # Loading known faces
  22. path = './img/known/'
  23. path_slice = slice(len(path),100)
  24.  
  25. for filename in glob.glob(f"{path}*.jpg"):
  26. # Get the filename (without path/extension)
  27. name = filename[path_slice]
  28. extension_slice = slice(0, len(name)-4)
  29. name = name[extension_slice]
  30.  
  31. # Load image in face_recognition
  32. current_image = face_recognition.load_image_file(filename)
  33. current_encoding = face_recognition.face_encodings(current_image)[0]
  34.  
  35. # Add encodings and name to the arraylists
  36. self.known_face_encodings.append(current_encoding)
  37. self.known_face_names.append(name)
  38.  
  39. def save_locations(self):
  40. if(self.current_face_locations != None):
  41. self.last_face_locations = self.current_face_locations.copy()
  42.  
  43. def rescale_locations(self, img_scale):
  44. # Rescale the 1/4 img locations for the original image
  45. output_locations = []
  46. for location in self.current_face_locations:
  47. location = [x/img_scale for x in location]
  48. output_locations.append(location)
  49. self.current_face_locations = output_locations
  50.  
  51. def read_locations(self, frame):
  52. self.current_face_locations = face_recognition.face_locations(frame)
  53.  
  54. if(self.current_face_locations != None):
  55. #print(len(self.current_face_locations), " face(s) found")
  56. pass
  57.  
  58. def read_encodings(self, frame):
  59. if(self.dirty == True):
  60. print("dirty")
  61. self.current_face_encodings = face_recognition.face_encodings(frame, self.current_face_locations)
  62. self.dirty = False
  63.  
  64. def label_faces(self, frame):
  65. pil_image = Image.fromarray(frame)
  66.  
  67. draw = ImageDraw.Draw(pil_image)
  68.  
  69. for(top, right, bottom, left), encoding in zip(self.current_face_locations, self.current_face_encodings):
  70.  
  71. drawing.draw_on_frame(draw, top, right, bottom, left, encoding, self.current_face_locations, self.known_face_encodings, self.known_face_names)
  72. del draw
  73.  
  74. opencv_image = np.array(pil_image)
  75. return opencv_image
  76.  
  77. def dirtyflag_check(self):
  78.  
  79. # If found more/less faces
  80. if((self.current_face_locations != None) and (self.last_face_locations != None)):
  81. if(len(self.current_face_locations) != len(self.last_face_locations)):
  82. self.dirty = True
  83. print("number of faces changed")
  84. return
  85.  
  86. change_factor = 10
  87.  
  88. for location, last_location in zip(self.current_face_locations, self.last_face_locations):
  89. if(abs(location[0] - last_location[0]) < change_factor):
  90. #print("not enough changes")
  91. pass
  92. elif(abs(location[1] - last_location[1]) < change_factor):
  93. pass
  94. else:
  95. self.dirty = True
  96. return
  97.  
  98.  
  99.  
  100. img_scale = 0.25
  101.  
  102. recognition = Recognition()
  103.  
  104. cap = WebcamVideoStream().start()
  105.  
  106. while(True):
  107. ret, frame = cap.read()
  108.  
  109. newX,newY = frame.shape[1]*img_scale, frame.shape[0]*img_scale
  110. low_resolution_frame = cv2.resize(frame,(int(newX),int(newY)))
  111. gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  112.  
  113.  
  114.  
  115. recognition.read_locations(low_resolution_frame)
  116.  
  117. recognition.dirtyflag_check()
  118.  
  119. recognition.read_encodings(low_resolution_frame)
  120.  
  121. recognition.save_locations()
  122.  
  123. recognition.rescale_locations(img_scale)
  124.  
  125. labeled_frame = recognition.label_faces(gray_frame)
  126.  
  127. cv2.imshow('feed', labeled_frame)
  128. if cv2.waitKey(1) & 0xFF == 27:
  129. cap.stop()
  130. break
  131.  
  132. cap.release()
  133. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement