Spaces:
Sleeping
Sleeping
Siyun He
commited on
Commit
·
74def1e
1
Parent(s):
15023ca
glass move along when head nodding left or right
Browse files
app.py
CHANGED
@@ -54,22 +54,28 @@ def process_frame(frame):
|
|
54 |
eye_center_x = (left_eye_x + right_eye_x) // 2
|
55 |
eye_center_y = (left_eye_y + right_eye_y) // 2
|
56 |
|
|
|
|
|
|
|
|
|
|
|
57 |
# Resize the overlay
|
58 |
overlay_resize = cv2.resize(overlay, (int(w * 1.15), int(h * 0.8)))
|
59 |
|
60 |
-
#
|
61 |
-
|
|
|
|
|
62 |
|
63 |
# Calculate the position to center the glasses on the eyes
|
64 |
-
overlay_x = eye_center_x -
|
65 |
-
overlay_y = eye_center_y -
|
66 |
|
67 |
# Overlay the glasses
|
68 |
try:
|
69 |
-
frame = cvzone.overlayPNG(frame,
|
70 |
except Exception as e:
|
71 |
print(f"Error overlaying glasses: {e}")
|
72 |
-
|
73 |
return frame
|
74 |
|
75 |
|
|
|
54 |
eye_center_x = (left_eye_x + right_eye_x) // 2
|
55 |
eye_center_y = (left_eye_y + right_eye_y) // 2
|
56 |
|
57 |
+
# Calculate the angle of rotation
|
58 |
+
delta_x = right_eye_x - left_eye_x
|
59 |
+
delta_y = right_eye_y - left_eye_y
|
60 |
+
angle = np.degrees(np.arctan2(delta_y, delta_x))
|
61 |
+
|
62 |
# Resize the overlay
|
63 |
overlay_resize = cv2.resize(overlay, (int(w * 1.15), int(h * 0.8)))
|
64 |
|
65 |
+
# Rotate the overlay
|
66 |
+
overlay_center = (overlay_resize.shape[1] // 2, overlay_resize.shape[0] // 2)
|
67 |
+
rotation_matrix = cv2.getRotationMatrix2D(overlay_center, angle, 1.0)
|
68 |
+
overlay_rotated = cv2.warpAffine(overlay_resize, rotation_matrix, (overlay_resize.shape[1], overlay_resize.shape[0]), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0))
|
69 |
|
70 |
# Calculate the position to center the glasses on the eyes
|
71 |
+
overlay_x = eye_center_x - overlay_rotated.shape[1] // 2
|
72 |
+
overlay_y = eye_center_y - overlay_rotated.shape[0] // 2 # Adjust vertical position
|
73 |
|
74 |
# Overlay the glasses
|
75 |
try:
|
76 |
+
frame = cvzone.overlayPNG(frame, overlay_rotated, [overlay_x, overlay_y])
|
77 |
except Exception as e:
|
78 |
print(f"Error overlaying glasses: {e}")
|
|
|
79 |
return frame
|
80 |
|
81 |
|