Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,31 +1,76 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
|
4 |
BACKEND_URL = os.environ.get("BACKEND_URL", "").strip()
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from gradio_client import Client
|
3 |
import os
|
4 |
|
5 |
BACKEND_URL = os.environ.get("BACKEND_URL", "").strip()
|
6 |
|
7 |
+
def process_with_backend(file_obj, webcam_img, model_type, conf_thresh, max_dets, task_type):
|
8 |
+
"""Forward request to backend"""
|
9 |
+
try:
|
10 |
+
client = Client(BACKEND_URL, headers={"ngrok-skip-browser-warning": "true"})
|
11 |
+
result = client.predict(
|
12 |
+
file_obj,
|
13 |
+
webcam_img,
|
14 |
+
model_type,
|
15 |
+
conf_thresh,
|
16 |
+
max_dets,
|
17 |
+
task_type,
|
18 |
+
api_name="/predict"
|
19 |
+
)
|
20 |
+
return result
|
21 |
+
except Exception as e:
|
22 |
+
return [gr.update() for _ in range(5)] + [f"Error: {str(e)}"]
|
23 |
+
|
24 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
25 |
+
gr.Markdown("# 🐵 PrimateFace Detection, Pose Estimation, and Gaze Demo")
|
26 |
+
|
27 |
+
with gr.Row():
|
28 |
+
with gr.Column(scale=1):
|
29 |
+
with gr.Tabs():
|
30 |
+
with gr.TabItem("Upload File"):
|
31 |
+
input_file = gr.File(label="Upload Image or Video", file_types=["image", "video"])
|
32 |
+
display_raw_image_file = gr.Image(visible=False)
|
33 |
+
display_raw_video_file = gr.Video(visible=False)
|
34 |
+
|
35 |
+
with gr.TabItem("Webcam"):
|
36 |
+
gr.Markdown("Click on feed or press Enter to capture")
|
37 |
+
input_webcam = gr.Image(sources=["webcam"], type="pil")
|
38 |
+
display_raw_image_webcam = gr.Image(visible=False)
|
39 |
+
|
40 |
+
clear_button = gr.Button("Clear All Inputs & Outputs")
|
41 |
|
42 |
+
with gr.Column(scale=1):
|
43 |
+
gr.Markdown("### Processed Output")
|
44 |
+
display_processed_image = gr.Image(visible=False)
|
45 |
+
display_processed_video = gr.Video(visible=False)
|
46 |
+
|
47 |
+
submit_button = gr.Button("Detect Faces", variant="primary")
|
48 |
+
|
49 |
+
with gr.Column():
|
50 |
+
gr.Markdown("### Detection Controls")
|
51 |
+
model_choice = gr.Radio(["MMDetection"], value="MMDetection", visible=False)
|
52 |
+
task_type = gr.Dropdown(
|
53 |
+
["Face Detection", "Face Pose Estimation", "Gaze Estimation [experimental]"],
|
54 |
+
value="Face Detection",
|
55 |
+
label="Select Task"
|
56 |
+
)
|
57 |
+
conf_threshold = gr.Slider(0.05, 0.95, 0.25, step=0.05, label="Confidence Threshold")
|
58 |
+
max_detections = gr.Slider(1, 10, 3, step=1, label="Max Detections")
|
59 |
+
|
60 |
+
# Wire events
|
61 |
+
outputs = [display_raw_image_file, display_raw_video_file, display_raw_image_webcam,
|
62 |
+
display_processed_image, display_processed_video]
|
63 |
+
|
64 |
+
submit_button.click(
|
65 |
+
process_with_backend,
|
66 |
+
inputs=[input_file, input_webcam, model_choice, conf_threshold, max_detections, task_type],
|
67 |
+
outputs=outputs
|
68 |
+
)
|
69 |
+
|
70 |
+
# Simplified clear function
|
71 |
+
clear_button.click(
|
72 |
+
lambda: [gr.update(value=None) for _ in range(5)],
|
73 |
+
outputs=outputs
|
74 |
+
)
|
75 |
|
76 |
demo.launch()
|