Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. from flask import Flask, Response, json, render_template
  2. from werkzeug.utils import secure_filename
  3. from flask import request
  4. from os import path, getcwd
  5. import time
  6. from face import Face
  7. import cv2
  8. from db import Database
  9. import face_recognition
  10. app = Flask(__name__)
  11.  
  12. app.config['file_allowed'] = ['image/png', 'image/jpeg']
  13. app.config['train_img'] = path.join(getcwd(), 'train_img')
  14. app.db = Database()
  15. aface = Face(app) #You would need an app here
  16. aface.load_all()
  17. known_encoding_faces = aface.known_encoding_faces
  18. user_id = aface.face_user_keys
  19.  
  20. class VideoCamera:
  21. def __init__(self,app):
  22. self.known_encoding_faces = aface.known_encoding_faces
  23. self.user_id = aface.face_user_keys
  24. #print face.known_encoding_faces
  25. # Using OpenCV to capture from device 0. If you have trouble capturing
  26. # from a webcam, comment the line below out and use a video file
  27. # instead.
  28. self.faces = []
  29. self.video_capture = cv2.VideoCapture(0)
  30. self.face_user_keys = {}
  31. #self.recognize()
  32. self.name_face()
  33. # If you decide to use video.mp4, you must have this file in the folder
  34. # as the main.py
  35. def load_user_by_index_key(self, index_key=0):
  36.  
  37. key_str = str(index_key)
  38.  
  39. if key_str in self.face_user_keys:
  40. return self.face_user_keys[key_str]
  41.  
  42. return None
  43.  
  44. def name_face (self):
  45. results = app.db.select('SELECT users.name,faces.id, faces.user_id, faces.filename, faces.created FROM faces INNER JOIN users on users.id = faces.user_id')
  46. for row in results:
  47. user = {
  48. "name": row[0]
  49. }
  50. face = {
  51. "id": row[1],
  52. "user_id": row[2],
  53. "filename": row[3],
  54. "created": row[4]
  55. }
  56. self.faces.append(user)
  57.  
  58. def get_frame(self):
  59. face_locations = []
  60. face_encodings = []
  61. face_names = []
  62. process_this_frame = True
  63. success, frame = self.video_capture.read()
  64. small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
  65. rgb_small_frame = small_frame[:, :, ::-1]
  66.  
  67. # Only process every other frame of video to save time
  68. if process_this_frame:
  69. # Find all the faces and face encodings in the current frame of video
  70. face_locations = face_recognition.face_locations(rgb_small_frame)
  71. #print(face_locations)
  72. face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)[0]
  73. #print(face_encodings)
  74.  
  75.  
  76. face_names = []
  77. for face_encoding in face_encodings:
  78. # See if the face is a match for the known face(s)
  79. matches = face_recognition.compare_faces(self.known_encoding_faces, face_encodings)
  80. name = "Unknown"
  81.  
  82. # If a match was found in known_face_encodings, just use the first one.
  83. if True in matches:
  84. first_match_index = matches.index(True)
  85. name = self.faces[first_match_index]
  86.  
  87.  
  88. face_names.append(name)
  89. #print(face_names)
  90.  
  91. process_this_frame = not process_this_frame
  92.  
  93. # Display the results
  94. for (top, right, bottom, left), name in zip(face_locations, face_names):
  95. #Scale back up face locations since the frame we detected in was scaled to 1/4 size
  96. top *= 4
  97. right *= 4
  98. bottom *= 4
  99. left *= 4
  100.  
  101. # Draw a box around the face
  102. cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
  103.  
  104. # Draw a label with a name below the face
  105. cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
  106. font = cv2.FONT_HERSHEY_DUPLEX
  107. cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
  108.  
  109.  
  110. ret, jpeg = cv2.imencode('.jpg', frame)
  111. return jpeg.tobytes()
  112.  
  113. def __del__(self):
  114. self.video_capture.release()
  115.  
  116. File "C:tutorialface_recognitionvenvsrccamera.py", line 110, in get_frame
  117. cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
  118. TypeError: bad argument type for built-in operation
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement