Spaces:
Sleeping
Sleeping
Siyun He
commited on
Commit
·
29cb19e
1
Parent(s):
22360ca
debug
Browse files
app.py
CHANGED
@@ -34,52 +34,57 @@ def change_glasses():
|
|
34 |
# Process frame for overlay
|
35 |
def process_frame(frame):
|
36 |
global overlay
|
|
|
|
|
|
|
37 |
height, width = frame.shape[:2]
|
38 |
face_detector.setInputSize((width, height))
|
39 |
-
|
40 |
-
# Detect faces
|
41 |
_, faces = face_detector.detect(frame)
|
|
|
42 |
if faces is not None:
|
43 |
for face in faces:
|
44 |
-
# Extract bounding box and facial landmarks
|
45 |
x, y, w, h = face[:4].astype(int)
|
46 |
-
face_landmarks = face[4:14].reshape(5, 2).astype(int) #
|
47 |
|
48 |
-
# Get the nose
|
49 |
-
nose_x, nose_y = face_landmarks[2]
|
50 |
-
|
51 |
-
|
|
|
52 |
|
53 |
-
# Calculate
|
54 |
eye_center_x = (left_eye_x + right_eye_x) // 2
|
55 |
eye_center_y = (left_eye_y + right_eye_y) // 2
|
|
|
|
|
56 |
delta_x = right_eye_x - left_eye_x
|
57 |
delta_y = right_eye_y - left_eye_y
|
58 |
angle = np.degrees(np.arctan2(delta_y, delta_x))
|
59 |
|
60 |
-
# Resize
|
61 |
overlay_resize = cv2.resize(overlay, (int(w * 1.15), int(h * 0.8)))
|
|
|
|
|
62 |
overlay_center = (overlay_resize.shape[1] // 2, overlay_resize.shape[0] // 2)
|
63 |
rotation_matrix = cv2.getRotationMatrix2D(overlay_center, angle, 1.0)
|
64 |
overlay_rotated = cv2.warpAffine(
|
65 |
overlay_resize, rotation_matrix,
|
66 |
-
(overlay_resize.shape[1], overlay_resize.shape[0]),
|
67 |
flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0)
|
68 |
)
|
69 |
|
70 |
-
# Calculate position
|
71 |
-
overlay_x =
|
72 |
-
overlay_y =
|
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 |
-
|
80 |
return frame
|
81 |
|
82 |
-
|
83 |
# Gradio webcam input
|
84 |
def webcam_input(frame):
|
85 |
frame = process_frame(frame)
|
|
|
34 |
# Process frame for overlay
|
35 |
def process_frame(frame):
|
36 |
global overlay
|
37 |
+
# Ensure the frame is writable
|
38 |
+
frame = np.array(frame, copy=True)
|
39 |
+
|
40 |
height, width = frame.shape[:2]
|
41 |
face_detector.setInputSize((width, height))
|
|
|
|
|
42 |
_, faces = face_detector.detect(frame)
|
43 |
+
|
44 |
if faces is not None:
|
45 |
for face in faces:
|
|
|
46 |
x, y, w, h = face[:4].astype(int)
|
47 |
+
face_landmarks = face[4:14].reshape(5, 2).astype(int) # Facial landmarks
|
48 |
|
49 |
+
# Get the nose position
|
50 |
+
nose_x, nose_y = face_landmarks[2].astype(int)
|
51 |
+
# Left and right eye positions
|
52 |
+
left_eye_x, left_eye_y = face_landmarks[0].astype(int)
|
53 |
+
right_eye_x, right_eye_y = face_landmarks[1].astype(int)
|
54 |
|
55 |
+
# Calculate the midpoint between the eyes
|
56 |
eye_center_x = (left_eye_x + right_eye_x) // 2
|
57 |
eye_center_y = (left_eye_y + right_eye_y) // 2
|
58 |
+
|
59 |
+
# Calculate the angle of rotation
|
60 |
delta_x = right_eye_x - left_eye_x
|
61 |
delta_y = right_eye_y - left_eye_y
|
62 |
angle = np.degrees(np.arctan2(delta_y, delta_x))
|
63 |
|
64 |
+
# Resize the overlay
|
65 |
overlay_resize = cv2.resize(overlay, (int(w * 1.15), int(h * 0.8)))
|
66 |
+
|
67 |
+
# Rotate the overlay
|
68 |
overlay_center = (overlay_resize.shape[1] // 2, overlay_resize.shape[0] // 2)
|
69 |
rotation_matrix = cv2.getRotationMatrix2D(overlay_center, angle, 1.0)
|
70 |
overlay_rotated = cv2.warpAffine(
|
71 |
overlay_resize, rotation_matrix,
|
72 |
+
(overlay_resize.shape[1], overlay_resize.shape[0]),
|
73 |
flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0)
|
74 |
)
|
75 |
|
76 |
+
# Calculate the position to center the glasses on the eyes
|
77 |
+
overlay_x = eye_center_x - overlay_rotated.shape[1] // 2
|
78 |
+
overlay_y = eye_center_y - overlay_rotated.shape[0] // 2
|
79 |
|
80 |
+
# Overlay the glasses
|
81 |
try:
|
82 |
frame = cvzone.overlayPNG(frame, overlay_rotated, [overlay_x, overlay_y])
|
83 |
except Exception as e:
|
84 |
print(f"Error overlaying glasses: {e}")
|
85 |
+
|
86 |
return frame
|
87 |
|
|
|
88 |
# Gradio webcam input
|
89 |
def webcam_input(frame):
|
90 |
frame = process_frame(frame)
|