Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 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
  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.  
  57. def read_encodings(self, frame):
  58. if(self.dirty == True):
  59. print("dirty")
  60. self.current_face_encodings = face_recognition.face_encodings(frame, self.current_face_locations)
  61. self.dirty = False
  62.  
  63. def label_faces(self, frame):
  64. pil_image = Image.fromarray(frame)
  65.  
  66. draw = ImageDraw.Draw(pil_image)
  67.  
  68. copy = self.current_face_encodings
  69.  
  70. for(top, right, bottom, left), self.current_face_encodings in zip(self.current_face_locations, self.current_face_encodings):
  71.  
  72. drawing.draw_on_frame(draw, top, right, bottom, left, self.current_face_encodings, self.current_face_locations, self.known_face_encodings, self.known_face_names)
  73. del draw
  74.  
  75. opencv_image = np.array(pil_image)
  76. return opencv_image
  77.  
  78. def dirtyflag_check(self):
  79. # If found more/less faces
  80. if((self.current_face_locations != None) & (self.last_face_locations != None)):
  81. if(len(self.current_face_locations) != len(self.last_face_locations)):
  82. dirty = True
  83. return
  84. else:
  85. return 0
  86.  
  87. change_factor = 5
  88. loop_index = 0
  89. for location in self.current_face_locations:
  90.  
  91. if((location[0] < self.last_face_locations[loop_index][0] + change_factor) and (location[0] > self.last_face_locations[loop_index][0] - change_factor)):
  92. #print("not enough changes")
  93. pass
  94. else:
  95. self.dirty = True
  96. #print("changes!")
  97.  
  98. loop_index += 1
  99.  
  100.  
  101. img_scale = 0.25
  102.  
  103. recognition = Recognition()
  104.  
  105. cap = WebcamVideoStream().start()
  106.  
  107. while(True):
  108. ret, frame = cap.read()
  109.  
  110. newX,newY = frame.shape[1]*img_scale, frame.shape[0]*img_scale
  111. low_resolution_frame = cv2.resize(frame,(int(newX),int(newY)))
  112. gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  113.  
  114. recognition.dirtyflag_check()
  115.  
  116. recognition.read_locations(low_resolution_frame)
  117.  
  118. recognition.read_encodings(low_resolution_frame)
  119.  
  120. recognition.rescale_locations(img_scale)
  121.  
  122. labeled_frame = recognition.label_faces(gray_frame)
  123.  
  124. cv2.imshow('feed', labeled_frame)
  125. if cv2.waitKey(1) & 0xFF == 27:
  126. cap.stop()
  127. break
  128.  
  129. recognition.save_locations()
  130.  
  131. cap.release()
  132. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement