chore: renaming variables

This commit is contained in:
Laureηt 2023-01-15 16:49:24 +01:00
parent 3a263937ab
commit 5455a4f962
Signed by: Laurent
SSH key fingerprint: SHA256:kZEpW8cMJ54PDeCvOhzreNr4FSh6R13CMGH/POoO8DI
2 changed files with 31 additions and 31 deletions

View file

@ -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,

View file

@ -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."""