Kesheratmex commited on
Commit
e4bf22b
·
1 Parent(s): 46e1cb9

Improve detection parsing with safe conversions and error handling

Browse files
Files changed (1) hide show
  1. gptoss_wrapper.py +26 -9
gptoss_wrapper.py CHANGED
@@ -521,16 +521,33 @@ class GPTOSSWrapper:
521
 
522
  print(f"Grounding DINO found {len(boxes)} detections")
523
 
524
- for box, score, label_idx in zip(boxes, scores, labels):
525
- if score >= threshold:
526
- x1, y1, x2, y2 = box.tolist()
527
- label = text_queries[label_idx] if label_idx < len(text_queries) else "unknown"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
528
 
529
- detections.append({
530
- "label": label,
531
- "confidence": float(score),
532
- "bbox": [int(x1), int(y1), int(x2), int(y2)]
533
- })
 
 
 
534
 
535
  return {"detections": detections}
536
 
 
521
 
522
  print(f"Grounding DINO found {len(boxes)} detections")
523
 
524
+ for i, (box, score, label_info) in enumerate(zip(boxes, scores, labels)):
525
+ try:
526
+ # Convert score to float safely
527
+ score_val = float(score.item() if hasattr(score, 'item') else score)
528
+
529
+ if score_val >= threshold:
530
+ # Convert box coordinates safely
531
+ if hasattr(box, 'tolist'):
532
+ x1, y1, x2, y2 = box.tolist()
533
+ else:
534
+ x1, y1, x2, y2 = box
535
+
536
+ # Handle label safely
537
+ if isinstance(label_info, (int, float)):
538
+ label_idx = int(label_info)
539
+ label = text_queries[label_idx] if label_idx < len(text_queries) else "unknown"
540
+ else:
541
+ label = str(label_info)
542
 
543
+ detections.append({
544
+ "label": label,
545
+ "confidence": score_val,
546
+ "bbox": [int(x1), int(y1), int(x2), int(y2)]
547
+ })
548
+ except Exception as e:
549
+ print(f"Error processing detection {i}: {e}")
550
+ continue
551
 
552
  return {"detections": detections}
553