def calculate_camera_pose(self, first_frame, second_frame, crop=True, crop_direction='horizontal'): """Returns the head rotation angles. Args: first_frame (list): First scene frame.(numpy array) second_frame (list): Second scene frame.(numpy array) crop (bool): Specifies if the frame should be cropped or not. crop_direction (str): Specifies if the frame should be split horizontally or vertically. Returns: tuple: [`first_frame`, `second_frame`, `pitch`, `yaw`, `roll`] will be returned as a tuple. Examples: >>> cssi.latency.calculate_camera_pose(first_frame,second_frame, True, 'horizontal') """ # prepare the frames for processing first_frame = prep_image(first_frame) second_frame = prep_image(second_frame) # if crop is true, split the image in two and take the # first part and sent it to pose calculator. if crop: first_frame, _ = split_image_in_half(image=first_frame, direction=crop_direction) second_frame, _ = split_image_in_half(image=second_frame, direction=crop_direction) cp = CameraPoseCalculator(debug=self.debug, first_frame=first_frame, second_frame=second_frame) return cp.calculate_camera_pose()
def detect_emotions(self, frame): """Detects the sentiment on a face.""" # prepare the frame for processing frame = prep_image(frame) frame = resize_image(frame, width=400) (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0)) self.face_detector.setInput(blob) detections = self.face_detector.forward() # loop over the detections 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.5: 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]) (startX, startY, endX, endY) = box.astype("int") # extract the face ROI and then preproces it in the exact # same manner as our training data face = frame[startY:endY, startX:endX] face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY) face = cv2.resize(face, (64, 64)) face = face.astype("float") / 255.0 face = img_to_array(face) face = np.expand_dims(face, axis=0) predictions = self.emotion_detector.predict(face)[0] label = self.POSSIBLE_EMOTIONS[predictions.argmax()] logger.debug("Identified emotion is: {0}".format(label)) # draw the bounding box of the face along with the associated # probability text = "{0}: {1:.2f}%".format(label, confidence * 100) y = startY - 10 if startY - 10 > 10 else startY + 10 cv2.rectangle(frame, (startX, startY), (endX, endY), (0, 0, 255), 2) cv2.putText(frame, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2) return label
def calculate_head_pose(self, frame): """Returns the head rotation angles. Args: frame (list): An image frame.(numpy array) Returns: tuple: [`frame`, `pitch`, `yaw`, `roll`] will be returned as a tuple. Examples: >>> cssi.latency.calculate_head_pose(frame) """ # prepare the frames for processing frame = prep_image(frame) hp = HeadPoseCalculator(debug=self.debug, frame=frame, landmark_detector=self.landmark_detector, face_detector=self.face_detector) return hp.calculate_head_pose()