chore: renaming variables
This commit is contained in:
parent
3a263937ab
commit
5455a4f962
|
@ -15,9 +15,25 @@ class BodyPart:
|
||||||
def __init__(self, env: Environment, image_path: str, position, height) -> None:
|
def __init__(self, env: Environment, image_path: str, position, height) -> None:
|
||||||
"""Initialize the part."""
|
"""Initialize the part."""
|
||||||
self.env = env
|
self.env = env
|
||||||
|
|
||||||
self.image = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
|
self.image = cv2.imread(image_path, cv2.IMREAD_UNCHANGED)
|
||||||
self.ratio = self.image.shape[1] / self.image.shape[0]
|
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],
|
[0, 0],
|
||||||
[self.image.shape[1], 0],
|
[self.image.shape[1], 0],
|
||||||
|
@ -26,37 +42,19 @@ class BodyPart:
|
||||||
],
|
],
|
||||||
dtype=np.float32,
|
dtype=np.float32,
|
||||||
)
|
)
|
||||||
self.old_box = np.zeros((4, 1, 2))
|
|
||||||
|
|
||||||
self.position = position
|
|
||||||
self.height = height
|
|
||||||
|
|
||||||
def draw(self) -> None:
|
def draw(self) -> None:
|
||||||
"""Draw the part on the screen."""
|
"""Draw the part on the screen."""
|
||||||
# compute position
|
# compute position
|
||||||
if self.env.results.multi_face_landmarks:
|
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:
|
for face_landmarks in self.env.results.multi_face_landmarks:
|
||||||
# compute bounding box from position and height
|
# compute bounding box from position and height
|
||||||
head_box = (
|
bounding_box = self.nominal_box + self.position + self.env.center
|
||||||
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
|
|
||||||
)
|
|
||||||
|
|
||||||
# project bounding box to camera
|
# project bounding box to camera
|
||||||
(box, _) = cv2.projectPoints(
|
(bounding_box, _) = cv2.projectPoints(
|
||||||
head_box,
|
bounding_box,
|
||||||
self.env.mp_rotation_vector,
|
self.env.mp_rotation_vector,
|
||||||
self.env.mp_translation_vector,
|
self.env.mp_translation_vector,
|
||||||
self.env.camera_matrix,
|
self.env.camera_matrix,
|
||||||
|
@ -64,13 +62,13 @@ class BodyPart:
|
||||||
)
|
)
|
||||||
|
|
||||||
# interpolation with self.old_box
|
# interpolation with self.old_box
|
||||||
box = (box + self.old_box) / 2
|
bounding_box = (bounding_box + self.old_bounding_box) / 2
|
||||||
self.old_box = box
|
self.old_bounding_box = bounding_box
|
||||||
|
|
||||||
# get perspective transform
|
# get perspective transform
|
||||||
transform_mat = cv2.getPerspectiveTransform(
|
transform_mat = cv2.getPerspectiveTransform(
|
||||||
self.bounding_box,
|
self.image_box,
|
||||||
box.astype(np.float32),
|
bounding_box.astype(np.float32),
|
||||||
)
|
)
|
||||||
|
|
||||||
# apply perspective transform to image
|
# apply perspective transform to image
|
||||||
|
@ -87,7 +85,7 @@ class BodyPart:
|
||||||
"""Draw debug information on the screen."""
|
"""Draw debug information on the screen."""
|
||||||
cv2.polylines(
|
cv2.polylines(
|
||||||
self.env.frame,
|
self.env.frame,
|
||||||
[self.old_box.squeeze().astype(int)],
|
[self.old_bounding_box.squeeze().astype(int)],
|
||||||
True,
|
True,
|
||||||
(255, 255, 255),
|
(255, 255, 255),
|
||||||
2,
|
2,
|
||||||
|
|
|
@ -35,6 +35,11 @@ class Environment:
|
||||||
self.camera_width = camera.get(3)
|
self.camera_width = camera.get(3)
|
||||||
self.camera_height = camera.get(4)
|
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
|
# create body parts
|
||||||
self.body_parts = [
|
self.body_parts = [
|
||||||
BodyPart(
|
BodyPart(
|
||||||
|
@ -195,11 +200,8 @@ class Environment:
|
||||||
self.mp_rotation_vector, _ = cv2.Rodrigues(pose_transform_mat[:3, :3])
|
self.mp_rotation_vector, _ = cv2.Rodrigues(pose_transform_mat[:3, :3])
|
||||||
self.mp_translation_vector = pose_transform_mat[:3, 3, None]
|
self.mp_translation_vector = pose_transform_mat[:3, 3, None]
|
||||||
|
|
||||||
# define axis
|
# retrieve center of face
|
||||||
self.center = metric_landmarks[:, NOSE_LANDMARK].T
|
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:
|
def draw_axis(self) -> None:
|
||||||
"""Draw the face axis on the frame."""
|
"""Draw the face axis on the frame."""
|
||||||
|
|
Loading…
Reference in a new issue