Codex commited on
Commit
2d200d5
·
1 Parent(s): 00d92f6

Serialize detector outputs for ZeroGPU

Browse files
Files changed (1) hide show
  1. app.py +53 -6
app.py CHANGED
@@ -138,6 +138,29 @@ def detector_gpu_duration(frames, conf):
138
  return max(5, min(60, int(math.ceil(seconds))))
139
 
140
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  @spaces.GPU(duration=detector_gpu_duration)
142
  def predict_detector_batch(frames, conf):
143
  if not frames:
@@ -160,10 +183,19 @@ def predict_detector_batch(frames, conf):
160
  torch = None
161
 
162
  if torch is None:
163
- return model.predict(frames, **predict_kwargs)
 
164
 
165
  with torch.inference_mode():
166
- return model.predict(frames, **predict_kwargs)
 
 
 
 
 
 
 
 
167
 
168
 
169
  def warmup_detector():
@@ -406,13 +438,28 @@ def extract_grouped_input(
406
  def detections_from_result(frame, result):
407
  frame_height, frame_width = frame.shape[:2]
408
  detections = []
409
- if result is None or result.boxes is None or len(result.boxes) == 0:
410
  return detections
411
 
412
- boxes = result.boxes.xyxy.cpu().numpy()
413
- confs = result.boxes.conf.cpu().numpy() if result.boxes.conf is not None else np.ones(len(boxes))
 
 
 
 
 
 
 
 
 
 
 
 
 
414
 
415
- for detected_box, det_conf in zip(boxes, confs):
 
 
416
  x1, y1, x2, y2 = detected_box[:4]
417
  x1, y1, x2, y2 = clamp_box(x1, y1, x2, y2, frame_width, frame_height)
418
 
 
138
  return max(5, min(60, int(math.ceil(seconds))))
139
 
140
 
141
+ def serialize_detector_results(results):
142
+ serialized = []
143
+ for result in results:
144
+ frame_items = []
145
+ if result is None or result.boxes is None or len(result.boxes) == 0:
146
+ serialized.append(frame_items)
147
+ continue
148
+
149
+ boxes = result.boxes.xyxy.detach().cpu().tolist()
150
+ if result.boxes.conf is None:
151
+ confs = [1.0] * len(boxes)
152
+ else:
153
+ confs = result.boxes.conf.detach().cpu().tolist()
154
+
155
+ for box, box_conf in zip(boxes, confs):
156
+ frame_items.append({
157
+ "box": [float(value) for value in box[:4]],
158
+ "conf": float(box_conf),
159
+ })
160
+ serialized.append(frame_items)
161
+ return serialized
162
+
163
+
164
  @spaces.GPU(duration=detector_gpu_duration)
165
  def predict_detector_batch(frames, conf):
166
  if not frames:
 
183
  torch = None
184
 
185
  if torch is None:
186
+ results = model.predict(frames, **predict_kwargs)
187
+ return serialize_detector_results(results)
188
 
189
  with torch.inference_mode():
190
+ results = model.predict(frames, **predict_kwargs)
191
+ serialized = serialize_detector_results(results)
192
+
193
+ if device == "cuda":
194
+ try:
195
+ torch.cuda.empty_cache()
196
+ except Exception:
197
+ pass
198
+ return serialized
199
 
200
 
201
  def warmup_detector():
 
438
  def detections_from_result(frame, result):
439
  frame_height, frame_width = frame.shape[:2]
440
  detections = []
441
+ if not result:
442
  return detections
443
 
444
+ if isinstance(result, list):
445
+ raw_detections = []
446
+ for item in result:
447
+ if isinstance(item, dict):
448
+ raw_detections.append((item.get("box", []), item.get("conf", 1.0)))
449
+ else:
450
+ raw_detections.append((item[:4], item[4] if len(item) > 4 else 1.0))
451
+ else:
452
+ boxes = result.boxes.xyxy.detach().cpu().tolist()
453
+ confs = (
454
+ result.boxes.conf.detach().cpu().tolist()
455
+ if result.boxes.conf is not None
456
+ else [1.0] * len(boxes)
457
+ )
458
+ raw_detections = zip(boxes, confs)
459
 
460
+ for detected_box, det_conf in raw_detections:
461
+ if len(detected_box) < 4:
462
+ continue
463
  x1, y1, x2, y2 = detected_box[:4]
464
  x1, y1, x2, y2 = clamp_box(x1, y1, x2, y2, frame_width, frame_height)
465