kadirnar commited on
Commit
d14e05c
·
verified ·
1 Parent(s): 6bda6c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -3
app.py CHANGED
@@ -1,7 +1,30 @@
 
1
  import gradio as gr
2
  from ultralytics import YOLO
3
  import spaces
4
- import cv2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  def attempt_download_from_hub(repo_id, hf_token=None):
7
  # https://github.com/fcakyon/yolov5-pip/blob/main/yolov5/utils/downloads.py
@@ -21,15 +44,23 @@ def attempt_download_from_hub(repo_id, hf_token=None):
21
  except (RepositoryNotFoundError, HFValidationError):
22
  return None
23
 
 
24
  @spaces.GPU(duration=200)
25
  def LeYOLO_inference(image, model_id, image_size, conf_threshold, iou_threshold):
26
  MODEL_PATH = attempt_download_from_hub(model_id)
27
  model = model = YOLO(MODEL_PATH)
28
  results = model(source=image, imgsz=image_size, iou=iou_threshold, conf=conf_threshold, verbose=False)[0]
29
- annotated_image = results[0].plot()
30
- annotated_image = cv2.cvtColor(annotated_image, cv2.COLOR_RGB2BGR)
 
 
 
 
 
 
31
  return annotated_image
32
 
 
33
  def app():
34
  with gr.Blocks():
35
  with gr.Row():
 
1
+
2
  import gradio as gr
3
  from ultralytics import YOLO
4
  import spaces
5
+ import supervision as sv
6
+
7
+ box_annotator = sv.BoxAnnotator()
8
+
9
+ category_dict = {
10
+ 0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus',
11
+ 6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant',
12
+ 11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat',
13
+ 16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear',
14
+ 22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag',
15
+ 27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard',
16
+ 32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove',
17
+ 36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle',
18
+ 40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl',
19
+ 46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli',
20
+ 51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake',
21
+ 56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table',
22
+ 61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard',
23
+ 67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink',
24
+ 72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors',
25
+ 77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'
26
+ }
27
+
28
 
29
  def attempt_download_from_hub(repo_id, hf_token=None):
30
  # https://github.com/fcakyon/yolov5-pip/blob/main/yolov5/utils/downloads.py
 
44
  except (RepositoryNotFoundError, HFValidationError):
45
  return None
46
 
47
+
48
  @spaces.GPU(duration=200)
49
  def LeYOLO_inference(image, model_id, image_size, conf_threshold, iou_threshold):
50
  MODEL_PATH = attempt_download_from_hub(model_id)
51
  model = model = YOLO(MODEL_PATH)
52
  results = model(source=image, imgsz=image_size, iou=iou_threshold, conf=conf_threshold, verbose=False)[0]
53
+ detections = sv.Detections.from_ultralytics(results)
54
+
55
+ labels = [
56
+ f"{category_dict[class_id]} {confidence:.2f}"
57
+ for class_id, confidence in zip(detections.class_id, detections.confidence)
58
+ ]
59
+ annotated_image = box_annotator.annotate(image, detections=detections, labels=labels)
60
+
61
  return annotated_image
62
 
63
+
64
  def app():
65
  with gr.Blocks():
66
  with gr.Row():