Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,13 +34,24 @@ def yolo_inference(image, model_id, conf_threshold, iou_threshold, max_detection
|
|
| 34 |
model_path = download_models(model_id)
|
| 35 |
model = YOLO(model_path)
|
| 36 |
results = model(source=image, imgsz=640, iou=iou_threshold, conf=conf_threshold, verbose=False, max_det=max_detection)[0]
|
|
|
|
|
|
|
| 37 |
detections = sv.Detections.from_ultralytics(results)
|
| 38 |
|
|
|
|
| 39 |
labels = [
|
| 40 |
f"{category_dict[class_id]} {confidence:.2f}"
|
| 41 |
for class_id, confidence in zip(detections.class_id, detections.confidence)
|
| 42 |
]
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
return annotated_image
|
| 46 |
|
|
|
|
| 34 |
model_path = download_models(model_id)
|
| 35 |
model = YOLO(model_path)
|
| 36 |
results = model(source=image, imgsz=640, iou=iou_threshold, conf=conf_threshold, verbose=False, max_det=max_detection)[0]
|
| 37 |
+
|
| 38 |
+
# Get the detections and convert them to the supervision Detections format
|
| 39 |
detections = sv.Detections.from_ultralytics(results)
|
| 40 |
|
| 41 |
+
# Prepare the labels
|
| 42 |
labels = [
|
| 43 |
f"{category_dict[class_id]} {confidence:.2f}"
|
| 44 |
for class_id, confidence in zip(detections.class_id, detections.confidence)
|
| 45 |
]
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
annotated_image = image.copy()
|
| 49 |
+
draw = ImageDraw.Draw(annotated_image)
|
| 50 |
+
|
| 51 |
+
for label, (x1, y1, x2, y2) in zip(labels, detections.xyxy):
|
| 52 |
+
color = random_color()
|
| 53 |
+
draw.rectangle([x1, y1, x2, y2], outline=color, width=3)
|
| 54 |
+
draw.text((x1, y1), label, fill=color)
|
| 55 |
|
| 56 |
return annotated_image
|
| 57 |
|