Spaces:
Sleeping
Sleeping
Siyun He
commited on
Commit
·
92727b3
1
Parent(s):
4aebdd5
add glass shape recommendation
Browse files
app.py
CHANGED
@@ -38,6 +38,15 @@ def determine_face_shape(landmarks):
|
|
38 |
else:
|
39 |
return "Square"
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
directory_path = 'glasses'
|
42 |
total_glass_num = count_files_in_directory(directory_path)
|
43 |
|
@@ -62,16 +71,12 @@ def process_frame(frame):
|
|
62 |
results = face_mesh.process(frame_rgb)
|
63 |
|
64 |
face_shape = "Unknown"
|
|
|
65 |
if faces is not None and results.multi_face_landmarks:
|
66 |
for face in faces:
|
67 |
x, y, w, h = face[:4].astype(int)
|
68 |
face_landmarks = face[4:14].reshape(5, 2).astype(int)
|
69 |
|
70 |
-
# Determine face shape
|
71 |
-
# face_shape = determine_face_shape(landmarks)
|
72 |
-
|
73 |
-
# Adjust the overlay size and position
|
74 |
-
# nose_x, nose_y = face_landmarks[2].astype(int)
|
75 |
left_eye_x, left_eye_y = face_landmarks[0].astype(int)
|
76 |
right_eye_x, right_eye_y = face_landmarks[1].astype(int)
|
77 |
|
@@ -102,10 +107,10 @@ def process_frame(frame):
|
|
102 |
|
103 |
for face_landmarks_mp in results.multi_face_landmarks:
|
104 |
landmarks = np.array([(lm.x * frame.shape[1], lm.y * frame.shape[0]) for lm in face_landmarks_mp.landmark])
|
105 |
-
# Determine face shape
|
106 |
face_shape = determine_face_shape(landmarks)
|
|
|
107 |
|
108 |
-
return frame, face_shape
|
109 |
|
110 |
# Transform function
|
111 |
def transform_cv2(frame, transform):
|
@@ -195,9 +200,9 @@ def save_frame(frame):
|
|
195 |
|
196 |
# Gradio webcam input
|
197 |
def webcam_input(frame, transform):
|
198 |
-
frame, face_shape = process_frame(frame)
|
199 |
frame = transform_cv2(frame, transform)
|
200 |
-
return frame, face_shape
|
201 |
|
202 |
# Gradio Interface
|
203 |
with gr.Blocks() as demo:
|
@@ -207,10 +212,11 @@ with gr.Blocks() as demo:
|
|
207 |
value="none", label="Transformation")
|
208 |
input_img = gr.Image(sources=["webcam"], type="numpy", streaming=True)
|
209 |
face_shape_output = gr.Textbox(label="Detected Face Shape")
|
|
|
210 |
next_button = gr.Button("Next Glasses")
|
211 |
save_button = gr.Button("Save as a Picture")
|
212 |
|
213 |
-
input_img.stream(webcam_input, [input_img, transform], [input_img, face_shape_output], time_limit=30, stream_every=0.1)
|
214 |
with gr.Row():
|
215 |
next_button.click(change_glasses, [], [])
|
216 |
with gr.Row():
|
|
|
38 |
else:
|
39 |
return "Square"
|
40 |
|
41 |
+
# Recommend glass shape based on face shape
|
42 |
+
def recommend_glass_shape(face_shape):
|
43 |
+
if face_shape == "Round":
|
44 |
+
return "Square"
|
45 |
+
elif face_shape == "Oval":
|
46 |
+
return "Round"
|
47 |
+
else:
|
48 |
+
return "Square"
|
49 |
+
|
50 |
directory_path = 'glasses'
|
51 |
total_glass_num = count_files_in_directory(directory_path)
|
52 |
|
|
|
71 |
results = face_mesh.process(frame_rgb)
|
72 |
|
73 |
face_shape = "Unknown"
|
74 |
+
glass_shape = "Unknown"
|
75 |
if faces is not None and results.multi_face_landmarks:
|
76 |
for face in faces:
|
77 |
x, y, w, h = face[:4].astype(int)
|
78 |
face_landmarks = face[4:14].reshape(5, 2).astype(int)
|
79 |
|
|
|
|
|
|
|
|
|
|
|
80 |
left_eye_x, left_eye_y = face_landmarks[0].astype(int)
|
81 |
right_eye_x, right_eye_y = face_landmarks[1].astype(int)
|
82 |
|
|
|
107 |
|
108 |
for face_landmarks_mp in results.multi_face_landmarks:
|
109 |
landmarks = np.array([(lm.x * frame.shape[1], lm.y * frame.shape[0]) for lm in face_landmarks_mp.landmark])
|
|
|
110 |
face_shape = determine_face_shape(landmarks)
|
111 |
+
glass_shape = recommend_glass_shape(face_shape)
|
112 |
|
113 |
+
return frame, face_shape, glass_shape
|
114 |
|
115 |
# Transform function
|
116 |
def transform_cv2(frame, transform):
|
|
|
200 |
|
201 |
# Gradio webcam input
|
202 |
def webcam_input(frame, transform):
|
203 |
+
frame, face_shape, glass_shape = process_frame(frame)
|
204 |
frame = transform_cv2(frame, transform)
|
205 |
+
return frame, face_shape, glass_shape
|
206 |
|
207 |
# Gradio Interface
|
208 |
with gr.Blocks() as demo:
|
|
|
212 |
value="none", label="Transformation")
|
213 |
input_img = gr.Image(sources=["webcam"], type="numpy", streaming=True)
|
214 |
face_shape_output = gr.Textbox(label="Detected Face Shape")
|
215 |
+
glass_shape_output = gr.Textbox(label="Recommended Glass Shape")
|
216 |
next_button = gr.Button("Next Glasses")
|
217 |
save_button = gr.Button("Save as a Picture")
|
218 |
|
219 |
+
input_img.stream(webcam_input, [input_img, transform], [input_img, face_shape_output, glass_shape_output], time_limit=30, stream_every=0.1)
|
220 |
with gr.Row():
|
221 |
next_button.click(change_glasses, [], [])
|
222 |
with gr.Row():
|