feat: draft eyes
This commit is contained in:
parent
58cac43a37
commit
6b751757dd
|
@ -8,7 +8,16 @@ from parts.eye import Eye
|
||||||
|
|
||||||
|
|
||||||
class Environment:
|
class Environment:
|
||||||
|
"""The environment is the main class of the application.
|
||||||
|
|
||||||
|
It is responsible for the following:
|
||||||
|
- detecting the keypoints
|
||||||
|
- drawing the keypoints
|
||||||
|
- drawing the avatar
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self, camera: cv2.VideoCapture) -> None:
|
def __init__(self, camera: cv2.VideoCapture) -> None:
|
||||||
|
"""Initialize the environment."""
|
||||||
# store reference to webcam
|
# store reference to webcam
|
||||||
self.cam = camera
|
self.cam = camera
|
||||||
|
|
||||||
|
@ -33,11 +42,12 @@ class Environment:
|
||||||
|
|
||||||
# create body parts
|
# create body parts
|
||||||
self.body_parts = {
|
self.body_parts = {
|
||||||
"left_eye": Eye(False),
|
"left_eye": Eye(False, self),
|
||||||
"right_eye": Eye(True),
|
"right_eye": Eye(True, self),
|
||||||
}
|
}
|
||||||
|
|
||||||
def start(self) -> None:
|
def start(self) -> None:
|
||||||
|
"""Start the environment."""
|
||||||
while self.cam.isOpened():
|
while self.cam.isOpened():
|
||||||
# read webcam
|
# read webcam
|
||||||
success, self.frame = self.cam.read()
|
success, self.frame = self.cam.read()
|
||||||
|
@ -65,6 +75,7 @@ class Environment:
|
||||||
self.draw_avatar()
|
self.draw_avatar()
|
||||||
|
|
||||||
def detect_keypoints(self) -> None:
|
def detect_keypoints(self) -> None:
|
||||||
|
"""Detect the keypoints on the frame."""
|
||||||
with self.mp_face_mesh.FaceMesh(
|
with self.mp_face_mesh.FaceMesh(
|
||||||
max_num_faces=1,
|
max_num_faces=1,
|
||||||
refine_landmarks=True,
|
refine_landmarks=True,
|
||||||
|
@ -72,7 +83,7 @@ class Environment:
|
||||||
min_tracking_confidence=0.5,
|
min_tracking_confidence=0.5,
|
||||||
) as face_mesh:
|
) as face_mesh:
|
||||||
|
|
||||||
# perf, mark image as not writeable to pass by reference
|
# perf, mark image as not writeable, to pass by reference
|
||||||
self.frame.flags.writeable = False
|
self.frame.flags.writeable = False
|
||||||
|
|
||||||
# convert the BGR image to RGB before processing
|
# convert the BGR image to RGB before processing
|
||||||
|
@ -88,6 +99,7 @@ class Environment:
|
||||||
self.frame = cv2.cvtColor(self.frame, cv2.COLOR_RGB2BGR)
|
self.frame = cv2.cvtColor(self.frame, cv2.COLOR_RGB2BGR)
|
||||||
|
|
||||||
def draw_keypoints(self) -> None:
|
def draw_keypoints(self) -> None:
|
||||||
|
"""Draw the keypoints on the screen."""
|
||||||
# draw the face mesh annotations on the image.
|
# draw the face mesh annotations on the image.
|
||||||
if self.results.multi_face_landmarks:
|
if self.results.multi_face_landmarks:
|
||||||
for face_landmarks in self.results.multi_face_landmarks:
|
for face_landmarks in self.results.multi_face_landmarks:
|
||||||
|
@ -117,6 +129,7 @@ class Environment:
|
||||||
cv2.imshow("MediaPipe Face Mesh", cv2.flip(self.frame, 1))
|
cv2.imshow("MediaPipe Face Mesh", cv2.flip(self.frame, 1))
|
||||||
|
|
||||||
def draw_avatar(self) -> None:
|
def draw_avatar(self) -> None:
|
||||||
|
"""Draw the avatar on the screen."""
|
||||||
self.screen.fill(pg.Color("green"))
|
self.screen.fill(pg.Color("green"))
|
||||||
|
|
||||||
for part in self.body_parts.values():
|
for part in self.body_parts.values():
|
||||||
|
|
Loading…
Reference in a new issue