def detect_emotion_in_frame(): global ct, er detected_emotions_on_faces = OrderedDict() request_data = request.get_json() if request_data["clearGlobals"]: print("reset signal sent") ct = CentroidTracker(max_dissapeared=10) er = EmotionRecognition() gc.collect() try: frame = cv2.imdecode(np.frombuffer(base64.b64decode( request_data["capturedFrame"][23:]), np.uint8), cv2.IMREAD_COLOR) except: return jsonify({"error": "error"}) rects = er.detect_faces(frame) objects, dissapeared = ct.track(rects) frame, detected_emotions_on_faces = er.detect_emotions(objects) flag, encoded_img = cv2.imencode(".jpg", frame) return jsonify({"frame": base64.b64encode(encoded_img).decode("utf-8"), "detectedEmotionsOnFaces": list(detected_emotions_on_faces.values()), "dissapearedFaces": list({"id": d[0], "counter": d[1]} for d in dissapeared.items())})
rects = list() for i in range(0, detections.shape[2]): # extract the confidence (i.e., probability) associated with the prediction confidence = detections[0, 0, i, 2] # filter out weak detections by ensuring the `confidence` is greater than the minimum confidence if confidence < 0.6: continue # compute the (x, y)-coordinates of the bounding box for the object box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) rects.append(box.astype("int")) objects = ct.track(rects) for (object_id, centroid) in objects.items(): for rect in rects: (start_x, start_y, end_x, end_y) = rect c_x = int((start_x + end_x) / 2.0) c_y = int((start_y + end_y) / 2.0) if np.array_equal([c_x, c_y], centroid): face = frame[start_y:end_y, start_x:end_x] try: gray_face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) except: break