diff --git a/landmarks.py b/landmarks.py index 8f6280b..893face 100644 --- a/landmarks.py +++ b/landmarks.py @@ -280,6 +280,7 @@ class Landmarker: distance = math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2) return distance + def get_center_top_point(self, side_results): gray_image = cv2.cvtColor( self.side_image_keypoints, @@ -290,42 +291,65 @@ class Landmarker: 0 : int(self.side_image_resized.shape[0] / 2), :, ] - self.edges = cv2.Canny(roi, 50, 150) + edges = cv2.Canny(roi, 50, 150) contours, _ = cv2.findContours( - self.edges.copy(), - cv2.RETR_TREE, + edges, + cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE, ) - max_contour = max(contours, key=cv2.contourArea) - rect = cv2.minAreaRect(max_contour) - box = cv2.boxPoints(rect) - box = sorted( - list(box), - key=lambda p: p[1], - ) - top_point = min( - box[0], - box[1], - key=lambda p: p[0], - ) - - left_hip = side_results.pose_landmarks.landmark[LANDMARK_NAME_TO_INDEX["left_hip"]] - right_hip = side_results.pose_landmarks.landmark[LANDMARK_NAME_TO_INDEX["right_hip"]] - - center_x = (left_hip.x + right_hip.x) / 2 - center_y = (left_hip.y + right_hip.y) / 2 - - center_x, center_y = ( - int(center_x * self.resized_width), - int(center_y * self.resized_height), - ) - - self.pixel_distance = self.euclidean_distance( - top_point[0], - top_point[1], - center_x, - center_y, - ) + xt, yt = None, None + topmost_point = None + + if contours: + largest_contour = max( + contours, + key=cv2.contourArea, + ) + topmost_point = tuple(largest_contour[largest_contour[:, :, 1].argmin()][0]) + xt, yt = topmost_point + + cv2.circle( + self.side_image_keypoints, + (xt, yt), + 2, + (255, 255, 0), + -1, + ) + + xc, yc = None, None + landmarks = side_results.pose_landmarks.landmark + + if side_results.pose_landmarks: + left_hip = landmarks[LANDMARK_NAME_TO_INDEX["left_hip"]] + right_hip = landmarks[LANDMARK_NAME_TO_INDEX["right_hip"]] + center_point = ( + (left_hip.x + right_hip.x) / 2, + (left_hip.y + right_hip.y) / 2, + ) + center_point = ( + int(center_point[0] * self.side_image_resized.shape[1]), + int(center_point[1] * self.side_image_resized.shape[0]), + ) + xc, yc = center_point + self.circle( + self.side_image_keypoints, + xc, + yc, + ) + + self.pixel_distance = self.euclidean_distance(xc, yc, xt, yt) + logging.debug( + "top_center_pixel_distance: %s", + self.pixel_distance, + ) + self.pixel_height = self.pixel_distance * 2 + logging.debug( + "pixel height: %s ", + self.pixel_height, + ) + self.distance = self.euclidean_distance(xc, yc, xt, yt) * self.pixel_to_metric_ratio() + + return self.distance if __name__ == "__main__": landmarker = Landmarker()