I am trying to make a face recognition web app on anvil server linked via jupyter notebook:
**I am trying to Do real time Face recognition in my web app , I have tried making function call via “While” loop and keeps on over writing image source by the media object I am receiving from my function on the jupyter notebook .
Issue I am facing is : It is taking a time to switch from one received media object to other . I am not able to show it like real time face recognition , it is loading and over writing images again and again which is purely not continuous making my real time recognition fail :**
code at anvil -
**def track_button_click(self, **event_args):
track_image = anvil.server.call(“track_encoding”,self.file_loader_1.file)
self.stop=False
while not self.stop:
self.image_2.source= anvil.server.call(“track_criminal”,track_image)
def track_stop_click(self, **event_args):
self.stop=True **
Python code -
def track_criminal(track_face_encoding):
face_locations = []
face_encodings = []
face_names = []
video_capture = cv2.VideoCapture(0)
while True:
if cv2.waitKey(1) & 0xFF == ord('q'):
video_capture.release()
cv2.destroyAllWindows()
break
# Grab a single frame of video
ret, frame = video_capture.read()
if ret:
# Resize frame of video to 1/4 size for faster face recognition processing
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_small_frame = small_frame[:, :, ::-1]
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
for face_encoding in face_encodings:
matches = face_recognition.compare_faces([track_face_encoding], face_encoding)
name = "Unknown"
face_distances = face_recognition.face_distance([track_face_encoding], face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
name = "Detected"
face_names.append(name)
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (255, 153, 153), 2)
# Draw a label with a name below the face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (255, 153, 153), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (53, 255, 153), 1)
cv2.imwrite("buffer.jpeg",frame)
media_objects=anvil.media.from_file("buffer.jpeg",'image/jpeg')
return media_objects
else:
break
Go to Livestream button and chose Track person then upload your image and click on track Person.
Clone Link :