From 5455a4f9625873b0b8968158d84fbb1c6acf868d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Sun, 15 Jan 2023 16:49:24 +0100 Subject: [PATCH] chore: renaming variables --- src/bodypart.py | 52 ++++++++++++++++++++++------------------------ src/environment.py | 10 +++++---- 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/src/bodypart.py b/src/bodypart.py index a5bfb12..f4dfc03 100644 --- a/src/bodypart.py +++ b/src/bodypart.py @@ -15,9 +15,25 @@ class BodyPart: def __init__(self, env: Environment, image_path: str, position, height) -> None: """Initialize the part.""" self.env = env + self.image = cv2.imread(image_path, cv2.IMREAD_UNCHANGED) self.ratio = self.image.shape[1] / self.image.shape[0] - self.bounding_box = np.array( + + self.position = position + + self.old_bounding_box = np.zeros((4, 1, 2)) + self.nominal_box = ( + np.array( + [ + self.ratio * self.env.x + self.env.y, + self.ratio * -self.env.x + self.env.y, + self.ratio * -self.env.x + -self.env.y, + self.ratio * self.env.x + -self.env.y, + ] + ) + * height + ) + self.image_box = np.array( [ [0, 0], [self.image.shape[1], 0], @@ -26,37 +42,19 @@ class BodyPart: ], dtype=np.float32, ) - self.old_box = np.zeros((4, 1, 2)) - - self.position = position - self.height = height def draw(self) -> None: """Draw the part on the screen.""" # compute position if self.env.results.multi_face_landmarks: - x = self.env.x - y = self.env.y - for face_landmarks in self.env.results.multi_face_landmarks: # compute bounding box from position and height - head_box = ( - np.array( - [ - self.ratio * x * self.height + y * self.height, - self.ratio * -x * self.height + y * self.height, - self.ratio * -x * self.height + -y * self.height, - self.ratio * x * self.height + -y * self.height, - ] - ) - + self.position - + self.env.center - ) + bounding_box = self.nominal_box + self.position + self.env.center # project bounding box to camera - (box, _) = cv2.projectPoints( - head_box, + (bounding_box, _) = cv2.projectPoints( + bounding_box, self.env.mp_rotation_vector, self.env.mp_translation_vector, self.env.camera_matrix, @@ -64,13 +62,13 @@ class BodyPart: ) # interpolation with self.old_box - box = (box + self.old_box) / 2 - self.old_box = box + bounding_box = (bounding_box + self.old_bounding_box) / 2 + self.old_bounding_box = bounding_box # get perspective transform transform_mat = cv2.getPerspectiveTransform( - self.bounding_box, - box.astype(np.float32), + self.image_box, + bounding_box.astype(np.float32), ) # apply perspective transform to image @@ -87,7 +85,7 @@ class BodyPart: """Draw debug information on the screen.""" cv2.polylines( self.env.frame, - [self.old_box.squeeze().astype(int)], + [self.old_bounding_box.squeeze().astype(int)], True, (255, 255, 255), 2, diff --git a/src/environment.py b/src/environment.py index 9eb4bf2..0dcac65 100644 --- a/src/environment.py +++ b/src/environment.py @@ -35,6 +35,11 @@ class Environment: self.camera_width = camera.get(3) self.camera_height = camera.get(4) + # setup face axis + self.x = np.array([7, 0, 0]) # TODO: replace 7s by 1s + self.y = np.array([0, 7, 0]) + self.z = np.array([0, 0, 7]) + # create body parts self.body_parts = [ BodyPart( @@ -195,11 +200,8 @@ class Environment: self.mp_rotation_vector, _ = cv2.Rodrigues(pose_transform_mat[:3, :3]) self.mp_translation_vector = pose_transform_mat[:3, 3, None] - # define axis + # retrieve center of face self.center = metric_landmarks[:, NOSE_LANDMARK].T - self.x = np.array([7, 0, 0]) - self.y = np.array([0, 7, 0]) - self.z = np.array([0, 0, 7]) def draw_axis(self) -> None: """Draw the face axis on the frame."""