Spaces:
Running
on
Zero
Running
on
Zero
Update demo.py
Browse files
demo.py
CHANGED
@@ -93,7 +93,7 @@ def visualize_faces_and_gaze(face_boxes, gaze_points=None, image=None, show_plot
|
|
93 |
return fig
|
94 |
|
95 |
@spaces.GPU(duration=15)
|
96 |
-
def process_image(input_image):
|
97 |
if input_image is None:
|
98 |
return None, ""
|
99 |
|
@@ -106,8 +106,11 @@ def process_image(input_image):
|
|
106 |
|
107 |
# Get image encoding
|
108 |
enc_image = model.encode_image(pil_image)
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
111 |
|
112 |
# Detect faces
|
113 |
faces = model.detect(enc_image, "face")["objects"]
|
@@ -127,13 +130,18 @@ def process_image(input_image):
|
|
127 |
(face["x_max"] - face["x_min"]) * pil_image.width,
|
128 |
(face["y_max"] - face["y_min"]) * pil_image.height,
|
129 |
)
|
|
|
|
|
|
|
|
|
130 |
face_boxes.append(face_box)
|
131 |
|
132 |
# Try to detect gaze
|
133 |
-
|
134 |
-
"prioritize_accuracy":
|
135 |
"flip_enc_img": flip_enc_image
|
136 |
-
}
|
|
|
137 |
|
138 |
if gaze is not None:
|
139 |
gaze_point = (
|
@@ -151,7 +159,7 @@ def process_image(input_image):
|
|
151 |
)
|
152 |
|
153 |
faces_with_gaze = sum(1 for gp in gaze_points if gp is not None)
|
154 |
-
status = f"Detected {len(faces)} faces. {faces_with_gaze
|
155 |
return fig, status
|
156 |
|
157 |
except Exception as e:
|
@@ -164,13 +172,21 @@ with gr.Blocks(title="Moondream Gaze Detection") as app:
|
|
164 |
with gr.Row():
|
165 |
with gr.Column():
|
166 |
input_image = gr.Image(label="Input Image", type="pil")
|
|
|
|
|
|
|
|
|
|
|
167 |
|
168 |
with gr.Column():
|
169 |
output_text = gr.Textbox(label="Status")
|
170 |
output_plot = gr.Plot(label="Visualization")
|
171 |
|
172 |
input_image.change(
|
173 |
-
fn=process_image, inputs=[input_image], outputs=[output_plot, output_text]
|
|
|
|
|
|
|
174 |
)
|
175 |
|
176 |
gr.Examples(
|
|
|
93 |
return fig
|
94 |
|
95 |
@spaces.GPU(duration=15)
|
96 |
+
def process_image(input_image, use_ensemble):
|
97 |
if input_image is None:
|
98 |
return None, ""
|
99 |
|
|
|
106 |
|
107 |
# Get image encoding
|
108 |
enc_image = model.encode_image(pil_image)
|
109 |
+
if use_ensemble:
|
110 |
+
flipped_pil = pil_image.copy().transpose(method=Image.FLIP_LEFT_RIGHT)
|
111 |
+
flip_enc_image = model.encode_image(flipped_pil)
|
112 |
+
else:
|
113 |
+
flip_enc_image = None
|
114 |
|
115 |
# Detect faces
|
116 |
faces = model.detect(enc_image, "face")["objects"]
|
|
|
130 |
(face["x_max"] - face["x_min"]) * pil_image.width,
|
131 |
(face["y_max"] - face["y_min"]) * pil_image.height,
|
132 |
)
|
133 |
+
face_center = (
|
134 |
+
(face["x_min"] + face["x_max"]) / 2,
|
135 |
+
(face["y_min"] + face["y_max"]) / 2
|
136 |
+
)
|
137 |
face_boxes.append(face_box)
|
138 |
|
139 |
# Try to detect gaze
|
140 |
+
gaze_settings = {
|
141 |
+
"prioritize_accuracy": use_ensemble,
|
142 |
"flip_enc_img": flip_enc_image
|
143 |
+
}
|
144 |
+
gaze = model.detect_gaze(enc_image, face=face, eye=face_center, unstable_settings=gaze_settings)["gaze"]
|
145 |
|
146 |
if gaze is not None:
|
147 |
gaze_point = (
|
|
|
159 |
)
|
160 |
|
161 |
faces_with_gaze = sum(1 for gp in gaze_points if gp is not None)
|
162 |
+
status = f"Detected {len(faces)} faces. Gaze detected for {faces_with_gaze} faces."
|
163 |
return fig, status
|
164 |
|
165 |
except Exception as e:
|
|
|
172 |
with gr.Row():
|
173 |
with gr.Column():
|
174 |
input_image = gr.Image(label="Input Image", type="pil")
|
175 |
+
use_ensemble = gr.Checkbox(
|
176 |
+
label="Use Ensemble Mode",
|
177 |
+
value=True,
|
178 |
+
info="Ensemble mode combines predictions from multiple model views for higher accuracy but is slower"
|
179 |
+
)
|
180 |
|
181 |
with gr.Column():
|
182 |
output_text = gr.Textbox(label="Status")
|
183 |
output_plot = gr.Plot(label="Visualization")
|
184 |
|
185 |
input_image.change(
|
186 |
+
fn=process_image, inputs=[input_image, use_ensemble], outputs=[output_plot, output_text]
|
187 |
+
)
|
188 |
+
use_ensemble.change(
|
189 |
+
fn=process_image, inputs=[input_image, use_ensemble], outputs=[output_plot, output_text]
|
190 |
)
|
191 |
|
192 |
gr.Examples(
|