GF-John commited on
Commit
7500a70
·
verified ·
1 Parent(s): d78aa39

testing returning the bbox payload

Browse files
Files changed (1) hide show
  1. app.py +17 -2
app.py CHANGED
@@ -17,13 +17,28 @@ subprocess.run(
17
 
18
  from ultralytics import YOLO
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  @spaces.GPU
21
  def yolov12_inference(image, video, model_id, image_size, conf_threshold):
22
  model = YOLO(model_id)
23
  if image:
24
  results = model.predict(source=image, imgsz=image_size, conf=conf_threshold)
25
  annotated_image = results[0].plot()
26
- return annotated_image[:, :, ::-1], None
 
27
  else:
28
  video_path = tempfile.mktemp(suffix=".webm")
29
  with open(video_path, "wb") as f:
@@ -50,7 +65,7 @@ def yolov12_inference(image, video, model_id, image_size, conf_threshold):
50
  cap.release()
51
  out.release()
52
 
53
- return None, output_video_path
54
 
55
 
56
  def yolov12_inference_for_examples(image, model_path, image_size, conf_threshold):
 
17
 
18
  from ultralytics import YOLO
19
 
20
+ def results_to_detections(results):
21
+ detections = []
22
+ for result in results:
23
+ boxes = result.boxes.data # [num_det, 6] (x1, y1, x2, y2, conf, cls)
24
+ for box in boxes:
25
+ x1, y1, x2, y2, conf, cls = box.tolist()
26
+ detections.append({
27
+ "class_id": int(cls),
28
+ "class_name": result.names[int(cls)],
29
+ "confidence": float(conf),
30
+ "bbox": [x1, y1, x2, y2] # XYXY format
31
+ })
32
+ return detections
33
+
34
  @spaces.GPU
35
  def yolov12_inference(image, video, model_id, image_size, conf_threshold):
36
  model = YOLO(model_id)
37
  if image:
38
  results = model.predict(source=image, imgsz=image_size, conf=conf_threshold)
39
  annotated_image = results[0].plot()
40
+ dets = results_to_detections(results)
41
+ return annotated_image[:, :, ::-1], None, dets
42
  else:
43
  video_path = tempfile.mktemp(suffix=".webm")
44
  with open(video_path, "wb") as f:
 
65
  cap.release()
66
  out.release()
67
 
68
+ return None, output_video_path, None
69
 
70
 
71
  def yolov12_inference_for_examples(image, model_path, image_size, conf_threshold):