fparodi commited on
Commit
d414e6c
·
verified ·
1 Parent(s): 0a2b228

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -104
app.py CHANGED
@@ -5,31 +5,30 @@ import tempfile
5
 
6
  BACKEND_URL = os.environ.get("BACKEND_URL", "").strip()
7
 
8
- # Create persistent client
9
  try:
10
  client = Client(BACKEND_URL, headers={"ngrok-skip-browser-warning": "true"})
11
  backend_available = True
12
- except Exception as e:
13
  client = None
14
  backend_available = False
15
 
16
  def process_media(file_obj, webcam_img, model_type, conf_thresh, max_dets, task_type):
17
- """Process media using backend API"""
18
  if not client:
19
  return [gr.update()] * 5
20
 
21
  try:
22
- # Convert webcam PIL to file path
23
  webcam_path = None
24
  if webcam_img is not None:
25
  with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp:
26
  webcam_img.save(tmp, 'PNG')
27
  webcam_path = tmp.name
28
 
29
- # Call backend with proper api_name
30
  result = client.predict(
31
- uploaded_file_obj=file_obj,
32
- webcam_image_pil=webcam_path,
33
  model_type_choice=model_type,
34
  conf_threshold_ui=conf_thresh,
35
  max_detections_ui=max_dets,
@@ -37,141 +36,97 @@ def process_media(file_obj, webcam_img, model_type, conf_thresh, max_dets, task_
37
  api_name="/process_media"
38
  )
39
 
40
- # Cleanup temp file
41
  if webcam_path and os.path.exists(webcam_path):
42
  os.unlink(webcam_path)
43
 
44
  return result
45
 
46
  except Exception as e:
47
- print(f"Error: {e}")
48
- return [gr.update()] * 5
 
 
 
 
 
 
 
49
 
50
- def handle_file_preview(file_obj):
51
- """Handle file upload preview"""
52
- if not client or not file_obj:
53
- return [gr.update()] * 5
54
-
55
- try:
56
- return client.predict(
57
- file_obj=file_obj,
58
- api_name="/handle_file_upload_preview"
59
- )
60
- except:
61
- return [gr.update()] * 5
62
-
63
- def handle_webcam_capture(webcam_img):
64
- """Handle webcam capture"""
65
- if not client or not webcam_img:
66
- return [gr.update()] * 4
67
-
68
- try:
69
- # Save PIL to temp file
70
- with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp:
71
- webcam_img.save(tmp, 'PNG')
72
- temp_path = tmp.name
73
-
74
- result = client.predict(
75
- snapshot_from_feed=temp_path,
76
- api_name="/handle_webcam_capture"
77
- )
78
-
79
- # Cleanup
80
- if os.path.exists(temp_path):
81
- os.unlink(temp_path)
82
-
83
- return result
84
- except:
85
- return [gr.update()] * 4
86
-
87
- def clear_all():
88
- """Clear all inputs and outputs"""
89
- if not client:
90
- return [gr.update(value=None)] * 7
91
-
92
- try:
93
- return client.predict(api_name="/clear_all_media_and_outputs")
94
- except:
95
- return [gr.update(value=None)] * 7
96
-
97
- # Build interface
98
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
99
- gr.Markdown("# 🐵 PrimateFace Detection, Pose Estimation, and Gaze Demo")
100
 
101
  if not backend_available:
102
  gr.Markdown("### 🔴 GPU Server Offline")
103
  else:
104
  with gr.Row():
105
- with gr.Column(scale=1):
106
  with gr.Tabs():
107
- with gr.TabItem("Upload File"):
108
- input_file = gr.File(label="Upload Image or Video Here", file_types=["image", "video"])
109
- display_raw_image_file = gr.Image(label="Raw Image Preview", visible=False)
110
- display_raw_video_file = gr.Video(label="Raw Video Preview", visible=False)
111
 
112
  with gr.TabItem("Webcam"):
113
- gr.Markdown("Click on feed or press Enter to capture")
114
  input_webcam = gr.Image(sources=["webcam"], type="pil")
115
- display_raw_image_webcam = gr.Image(label="Captured Snapshot Preview", visible=False)
116
 
117
- clear_button = gr.Button("Clear All Inputs & Outputs")
118
 
119
- with gr.Column(scale=1):
120
- gr.Markdown("### Processed Output")
121
- display_processed_image = gr.Image(label="Processed Image", visible=False)
122
- display_processed_video = gr.Video(label="Processed Video", visible=False)
123
 
124
  # Examples
125
  gr.Examples(
126
- examples=[
127
- ["images/allocebus_000003.jpeg"],
128
- ["images/tarsius_000120.jpeg"],
129
- ["images/nasalis_proboscis-monkey.png"],
130
- ["images/macaca_000032.jpeg"],
131
- ["images/mandrillus_000011.jpeg"],
132
- ["images/pongo_000006.jpeg"]
133
- ],
134
  inputs=input_file
135
  )
136
 
137
- submit_button = gr.Button("Detect Faces", variant="primary")
138
 
139
  # Controls
140
  model_choice = gr.Radio(["MMDetection"], value="MMDetection", visible=False)
141
  task_type = gr.Dropdown(
142
  ["Face Detection", "Face Pose Estimation", "Gaze Estimation [experimental]"],
143
- value="Face Detection",
144
- label="Select Task"
145
  )
146
- conf_threshold = gr.Slider(0.05, 0.95, 0.25, step=0.05, label="Confidence Threshold")
147
  max_detections = gr.Slider(1, 10, 3, step=1, label="Max Detections")
148
 
149
- # Wire events
150
- input_file.change(
151
- handle_file_preview,
152
- inputs=[input_file],
153
- outputs=[display_raw_image_file, display_raw_video_file, input_file,
154
- display_processed_image, display_processed_video]
155
- )
156
 
157
- input_webcam.change(
158
- handle_webcam_capture,
159
- inputs=[input_webcam],
160
- outputs=[display_raw_image_webcam, input_webcam,
161
- display_processed_image, display_processed_video]
162
- )
 
163
 
164
- submit_button.click(
165
- process_media,
166
  inputs=[input_file, input_webcam, model_choice, conf_threshold, max_detections, task_type],
167
- outputs=[display_raw_image_file, display_raw_video_file, display_raw_image_webcam,
168
- display_processed_image, display_processed_video]
169
  )
170
 
171
- clear_button.click(
172
- clear_all,
173
- outputs=[input_file, input_webcam, display_raw_image_file, display_raw_video_file,
174
- display_raw_image_webcam, display_processed_image, display_processed_video]
 
175
  )
176
 
177
  demo.launch()
 
5
 
6
  BACKEND_URL = os.environ.get("BACKEND_URL", "").strip()
7
 
 
8
  try:
9
  client = Client(BACKEND_URL, headers={"ngrok-skip-browser-warning": "true"})
10
  backend_available = True
11
+ except:
12
  client = None
13
  backend_available = False
14
 
15
  def process_media(file_obj, webcam_img, model_type, conf_thresh, max_dets, task_type):
16
+ """Process media - backend expects both file and webcam paths"""
17
  if not client:
18
  return [gr.update()] * 5
19
 
20
  try:
21
+ # Convert webcam PIL to file path if present
22
  webcam_path = None
23
  if webcam_img is not None:
24
  with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as tmp:
25
  webcam_img.save(tmp, 'PNG')
26
  webcam_path = tmp.name
27
 
28
+ # Backend expects both parameters - use None for missing one
29
  result = client.predict(
30
+ uploaded_file_obj=file_obj if file_obj else None,
31
+ webcam_image_pil=webcam_path if webcam_path else None,
32
  model_type_choice=model_type,
33
  conf_threshold_ui=conf_thresh,
34
  max_detections_ui=max_dets,
 
36
  api_name="/process_media"
37
  )
38
 
39
+ # Cleanup
40
  if webcam_path and os.path.exists(webcam_path):
41
  os.unlink(webcam_path)
42
 
43
  return result
44
 
45
  except Exception as e:
46
+ print(f"Process error: {e}")
47
+ # Return error message in processed image slot
48
+ return [
49
+ gr.update(), # raw image file
50
+ gr.update(), # raw video file
51
+ gr.update(), # raw image webcam
52
+ gr.update(value=None, visible=True), # processed image - show error
53
+ gr.update() # processed video
54
+ ]
55
 
56
+ # Simplified interface without complex preview forwarding
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
58
+ gr.Markdown("# 🐵 PrimateFace Detection, Pose & Gaze Demo")
59
 
60
  if not backend_available:
61
  gr.Markdown("### 🔴 GPU Server Offline")
62
  else:
63
  with gr.Row():
64
+ with gr.Column():
65
  with gr.Tabs():
66
+ with gr.TabItem("Upload"):
67
+ input_file = gr.File(label="Upload Image/Video")
68
+ # Simple local preview
69
+ preview_img = gr.Image(label="Preview", visible=False)
70
 
71
  with gr.TabItem("Webcam"):
 
72
  input_webcam = gr.Image(sources=["webcam"], type="pil")
 
73
 
74
+ clear_btn = gr.Button("Clear All")
75
 
76
+ with gr.Column():
77
+ gr.Markdown("### Results")
78
+ output_image = gr.Image(label="Processed", visible=False)
79
+ output_video = gr.Video(label="Processed", visible=False)
80
 
81
  # Examples
82
  gr.Examples(
83
+ examples=[["images/" + f] for f in [
84
+ "allocebus_000003.jpeg",
85
+ "tarsius_000120.jpeg",
86
+ "nasalis_proboscis-monkey.png",
87
+ "macaca_000032.jpeg",
88
+ "mandrillus_000011.jpeg",
89
+ "pongo_000006.jpeg"
90
+ ]],
91
  inputs=input_file
92
  )
93
 
94
+ submit_btn = gr.Button("Detect Faces", variant="primary")
95
 
96
  # Controls
97
  model_choice = gr.Radio(["MMDetection"], value="MMDetection", visible=False)
98
  task_type = gr.Dropdown(
99
  ["Face Detection", "Face Pose Estimation", "Gaze Estimation [experimental]"],
100
+ value="Face Detection"
 
101
  )
102
+ conf_threshold = gr.Slider(0.05, 0.95, 0.25, step=0.05, label="Confidence")
103
  max_detections = gr.Slider(1, 10, 3, step=1, label="Max Detections")
104
 
105
+ # Simple local preview for uploaded files
106
+ def show_preview(file):
107
+ if file and file.name.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp')):
108
+ return gr.update(value=file, visible=True)
109
+ return gr.update(visible=False)
 
 
110
 
111
+ input_file.change(show_preview, inputs=[input_file], outputs=[preview_img])
112
+
113
+ # Main processing - only use last 3 outputs (skip raw previews)
114
+ def process_and_extract_outputs(*args):
115
+ result = process_media(*args)
116
+ # Return only processed outputs
117
+ return result[-2:] # Just processed image and video
118
 
119
+ submit_btn.click(
120
+ process_and_extract_outputs,
121
  inputs=[input_file, input_webcam, model_choice, conf_threshold, max_detections, task_type],
122
+ outputs=[output_image, output_video]
 
123
  )
124
 
125
+ # Simple clear
126
+ clear_btn.click(
127
+ lambda: [gr.update(value=None), gr.update(value=None), gr.update(visible=False),
128
+ gr.update(visible=False), gr.update(visible=False)],
129
+ outputs=[input_file, input_webcam, preview_img, output_image, output_video]
130
  )
131
 
132
  demo.launch()