Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,6 @@ import gradio as gr
|
|
2 |
from gradio_client import Client
|
3 |
import os
|
4 |
import tempfile
|
5 |
-
from PIL import Image
|
6 |
|
7 |
BACKEND_URL = os.environ.get("BACKEND_URL", "").strip()
|
8 |
|
@@ -17,153 +16,108 @@ except Exception as e:
|
|
17 |
print(f"Backend not available: {e}")
|
18 |
|
19 |
def process_media(file_obj, webcam_img, model_type, conf_thresh, max_dets, task_type):
|
20 |
-
"""Main processing function"""
|
21 |
if not client:
|
22 |
return [gr.update()] * 5
|
23 |
|
24 |
try:
|
25 |
-
# Handle webcam image -
|
26 |
webcam_file = None
|
27 |
if webcam_img is not None:
|
28 |
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp:
|
29 |
webcam_img.save(tmp, 'PNG')
|
30 |
webcam_file = tmp.name
|
31 |
|
32 |
-
# Call backend
|
33 |
result = client.predict(
|
34 |
file_obj,
|
35 |
-
webcam_file,
|
36 |
model_type,
|
37 |
conf_thresh,
|
38 |
max_dets,
|
39 |
task_type,
|
40 |
-
fn_index=3
|
41 |
)
|
42 |
|
43 |
-
# Clean up
|
44 |
if webcam_file and os.path.exists(webcam_file):
|
45 |
os.unlink(webcam_file)
|
46 |
-
|
47 |
-
return result
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
except Exception as e:
|
50 |
print(f"Error in process_media: {e}")
|
51 |
return [gr.update()] * 5
|
52 |
|
53 |
-
|
54 |
-
"""Clear all inputs and outputs"""
|
55 |
-
try:
|
56 |
-
# Call backend clear function at index 4
|
57 |
-
return client.predict(fn_index=4)
|
58 |
-
except:
|
59 |
-
# Fallback to local clear
|
60 |
-
return [gr.update(value=None)] * 7
|
61 |
-
|
62 |
-
# Build the interface
|
63 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
64 |
-
gr.Markdown("
|
65 |
|
66 |
if not backend_available:
|
67 |
-
gr.Markdown("### 🔴 GPU Server Offline
|
68 |
else:
|
69 |
-
gr.Markdown("Upload an image/video or use your webcam. Click 'Detect Faces' for results.")
|
70 |
-
|
71 |
with gr.Row():
|
72 |
-
with gr.Column(
|
73 |
with gr.Tabs():
|
74 |
-
with gr.TabItem("Upload
|
75 |
-
input_file = gr.File(label="Upload Image
|
76 |
-
display_raw_image_file = gr.Image(
|
77 |
-
display_raw_video_file = gr.Video(
|
78 |
|
79 |
with gr.TabItem("Webcam"):
|
80 |
-
gr.Markdown("Click on feed or press Enter to capture")
|
81 |
input_webcam = gr.Image(sources=["webcam"], type="pil")
|
82 |
-
display_raw_image_webcam = gr.Image(
|
83 |
|
84 |
-
|
85 |
|
86 |
-
with gr.Column(
|
87 |
-
gr.Markdown("###
|
88 |
-
display_processed_image = gr.Image(
|
89 |
-
display_processed_video = gr.Video(
|
90 |
-
|
91 |
-
# Examples with preview
|
92 |
-
example_images = [
|
93 |
-
"images/allocebus_000003.jpeg",
|
94 |
-
"images/tarsius_000120.jpeg",
|
95 |
-
"images/nasalis_proboscis-monkey.png",
|
96 |
-
"images/macaca_000032.jpeg",
|
97 |
-
"images/mandrillus_000011.jpeg",
|
98 |
-
"images/pongo_000006.jpeg"
|
99 |
-
]
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
model_choice = gr.Radio(["MMDetection"], value="MMDetection", visible=False)
|
114 |
-
task_type = gr.Dropdown(
|
115 |
-
["Face Detection", "Face Pose Estimation", "Gaze Estimation [experimental]"],
|
116 |
-
value="Face Detection",
|
117 |
-
label="Select Task"
|
118 |
-
)
|
119 |
-
conf_threshold = gr.Slider(0.05, 0.95, 0.25, step=0.05, label="Confidence Threshold")
|
120 |
-
max_detections = gr.Slider(1, 10, 3, step=1, label="Max Detections")
|
121 |
|
122 |
-
|
123 |
-
def handle_file_change(file_obj):
|
124 |
-
"""Handle file upload preview locally"""
|
125 |
-
if file_obj is None:
|
126 |
-
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
|
127 |
-
|
128 |
-
try:
|
129 |
-
# Forward to backend for preview
|
130 |
-
result = client.predict(file_obj, fn_index=0)
|
131 |
-
return result[:3] # Return first 3 outputs for preview
|
132 |
-
except:
|
133 |
-
# Fallback local preview
|
134 |
-
if file_obj.name.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp')):
|
135 |
-
return gr.update(value=file_obj.name, visible=True), gr.update(visible=False), gr.update(visible=False)
|
136 |
-
else:
|
137 |
-
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
|
138 |
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
result = client.predict(tmp.name, fn_index=1)
|
148 |
-
os.unlink(tmp.name)
|
149 |
-
return result[:2] # Return webcam-related outputs
|
150 |
-
except:
|
151 |
-
# Fallback
|
152 |
-
return gr.update(value=img, visible=True), gr.update(visible=False)
|
153 |
|
154 |
-
#
|
155 |
input_file.change(
|
156 |
-
|
157 |
inputs=[input_file],
|
158 |
-
outputs=[display_raw_image_file, display_raw_video_file
|
159 |
)
|
160 |
|
161 |
input_webcam.change(
|
162 |
-
|
163 |
inputs=[input_webcam],
|
164 |
-
outputs=[display_raw_image_webcam
|
165 |
)
|
166 |
|
|
|
167 |
submit_button.click(
|
168 |
process_media,
|
169 |
inputs=[input_file, input_webcam, model_choice, conf_threshold, max_detections, task_type],
|
@@ -176,8 +130,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
176 |
]
|
177 |
)
|
178 |
|
179 |
-
|
180 |
-
|
|
|
181 |
outputs=[
|
182 |
input_file,
|
183 |
input_webcam,
|
|
|
2 |
from gradio_client import Client
|
3 |
import os
|
4 |
import tempfile
|
|
|
5 |
|
6 |
BACKEND_URL = os.environ.get("BACKEND_URL", "").strip()
|
7 |
|
|
|
16 |
print(f"Backend not available: {e}")
|
17 |
|
18 |
def process_media(file_obj, webcam_img, model_type, conf_thresh, max_dets, task_type):
|
19 |
+
"""Main processing function - expects 5 outputs"""
|
20 |
if not client:
|
21 |
return [gr.update()] * 5
|
22 |
|
23 |
try:
|
24 |
+
# Handle webcam image - need to pass as file path
|
25 |
webcam_file = None
|
26 |
if webcam_img is not None:
|
27 |
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp:
|
28 |
webcam_img.save(tmp, 'PNG')
|
29 |
webcam_file = tmp.name
|
30 |
|
31 |
+
# Call backend
|
32 |
result = client.predict(
|
33 |
file_obj,
|
34 |
+
webcam_file,
|
35 |
model_type,
|
36 |
conf_thresh,
|
37 |
max_dets,
|
38 |
task_type,
|
39 |
+
fn_index=3
|
40 |
)
|
41 |
|
42 |
+
# Clean up
|
43 |
if webcam_file and os.path.exists(webcam_file):
|
44 |
os.unlink(webcam_file)
|
|
|
|
|
45 |
|
46 |
+
# Backend returns 7 values but we only need the last 5
|
47 |
+
# Skip the first 2 (input_file and input_webcam updates)
|
48 |
+
if len(result) == 7:
|
49 |
+
return result[2:] # Return only the display components
|
50 |
+
else:
|
51 |
+
return result[:5] # Safety fallback
|
52 |
+
|
53 |
except Exception as e:
|
54 |
print(f"Error in process_media: {e}")
|
55 |
return [gr.update()] * 5
|
56 |
|
57 |
+
# Build simplified interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
58 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
59 |
+
gr.Markdown("# 🐵 PrimateFace Detection, Pose Estimation, and Gaze Demo")
|
60 |
|
61 |
if not backend_available:
|
62 |
+
gr.Markdown("### 🔴 GPU Server Offline")
|
63 |
else:
|
|
|
|
|
64 |
with gr.Row():
|
65 |
+
with gr.Column():
|
66 |
with gr.Tabs():
|
67 |
+
with gr.TabItem("Upload"):
|
68 |
+
input_file = gr.File(label="Upload Image/Video", file_types=["image", "video"])
|
69 |
+
display_raw_image_file = gr.Image(visible=False)
|
70 |
+
display_raw_video_file = gr.Video(visible=False)
|
71 |
|
72 |
with gr.TabItem("Webcam"):
|
|
|
73 |
input_webcam = gr.Image(sources=["webcam"], type="pil")
|
74 |
+
display_raw_image_webcam = gr.Image(visible=False)
|
75 |
|
76 |
+
clear_button = gr.Button("Clear All")
|
77 |
|
78 |
+
with gr.Column():
|
79 |
+
gr.Markdown("### Output")
|
80 |
+
display_processed_image = gr.Image(visible=False)
|
81 |
+
display_processed_video = gr.Video(visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
+
# Examples
|
84 |
+
gr.Examples(
|
85 |
+
examples=[
|
86 |
+
["images/allocebus_000003.jpeg"],
|
87 |
+
["images/tarsius_000120.jpeg"],
|
88 |
+
["images/nasalis_proboscis-monkey.png"],
|
89 |
+
["images/macaca_000032.jpeg"],
|
90 |
+
["images/mandrillus_000011.jpeg"],
|
91 |
+
["images/pongo_000006.jpeg"]
|
92 |
+
],
|
93 |
+
inputs=input_file
|
94 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
|
96 |
+
submit_button = gr.Button("Detect Faces", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
+
# Controls
|
99 |
+
model_choice = gr.Radio(["MMDetection"], value="MMDetection", visible=False)
|
100 |
+
task_type = gr.Dropdown(
|
101 |
+
["Face Detection", "Face Pose Estimation", "Gaze Estimation [experimental]"],
|
102 |
+
value="Face Detection"
|
103 |
+
)
|
104 |
+
conf_threshold = gr.Slider(0.05, 0.95, 0.25, step=0.05, label="Confidence")
|
105 |
+
max_detections = gr.Slider(1, 10, 3, step=1, label="Max Detections")
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
|
107 |
+
# Simple preview handlers
|
108 |
input_file.change(
|
109 |
+
lambda f: (gr.update(value=f, visible=bool(f)), gr.update(visible=False)) if f and f.name.endswith(('.jpg','.jpeg','.png')) else (gr.update(visible=False), gr.update(value=f, visible=bool(f))),
|
110 |
inputs=[input_file],
|
111 |
+
outputs=[display_raw_image_file, display_raw_video_file]
|
112 |
)
|
113 |
|
114 |
input_webcam.change(
|
115 |
+
lambda img: gr.update(value=img, visible=bool(img)),
|
116 |
inputs=[input_webcam],
|
117 |
+
outputs=[display_raw_image_webcam]
|
118 |
)
|
119 |
|
120 |
+
# Main processing
|
121 |
submit_button.click(
|
122 |
process_media,
|
123 |
inputs=[input_file, input_webcam, model_choice, conf_threshold, max_detections, task_type],
|
|
|
130 |
]
|
131 |
)
|
132 |
|
133 |
+
# Clear all
|
134 |
+
clear_button.click(
|
135 |
+
lambda: [gr.update(value=None)] * 7,
|
136 |
outputs=[
|
137 |
input_file,
|
138 |
input_webcam,
|