Clementapa commited on
Commit
82f7f8f
β€’
1 Parent(s): fd3c45b

Adapt bounding box annotation to image ratio

Browse files
Files changed (1) hide show
  1. app.py +18 -3
app.py CHANGED
@@ -4,6 +4,7 @@ import gradio as gr
4
  import supervision as sv
5
  import torch
6
  from PIL import Image
 
7
  from ultralytics import YOLO
8
 
9
  MARKDOWN = """
@@ -12,7 +13,7 @@ MARKDOWN = """
12
  ## About the model πŸ‘οΈ
13
  This is a demo for my YOLOv8 nano trained for orang outan detection.\\
14
  The model was trained using [this dataset](https://images.cv/dataset/orangutan-image-classification-dataset)
15
- for orang outan images and [this dataset](https://www.kaggle.com/datasets/slothkong/10-monkey-species/data) as background images. Annotations were obtained using zero shot detection method GroundingDino.\\
16
 
17
  The code can be found on my github repository: https://github.com/clementapa/orang-outan-image-video-detection.
18
 
@@ -30,7 +31,7 @@ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
30
 
31
  YOLO_MODEL = YOLO("train_7best.pt")
32
 
33
- BOX_ANNOTATOR = sv.BoxAnnotator()
34
 
35
 
36
  def annotate(
@@ -39,6 +40,20 @@ def annotate(
39
  annotator: sv.BoxAnnotator,
40
  labels: str,
41
  ) -> Image.Image:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  annotated_bgr_image = annotator.annotate(
43
  scene=image_bgr_numpy, detections=detections, labels=labels
44
  )
@@ -46,7 +61,7 @@ def annotate(
46
 
47
 
48
  def inference(image_rgb_pil: Image.Image, confidence: float) -> List[Image.Image]:
49
- output = YOLO_MODEL(image_rgb_pil, verbose=False)[0]
50
  detections = sv.Detections.from_ultralytics(output)
51
 
52
  detections = detections[detections.confidence >= confidence]
 
4
  import supervision as sv
5
  import torch
6
  from PIL import Image
7
+ from supervision import Color
8
  from ultralytics import YOLO
9
 
10
  MARKDOWN = """
 
13
  ## About the model πŸ‘οΈ
14
  This is a demo for my YOLOv8 nano trained for orang outan detection.\\
15
  The model was trained using [this dataset](https://images.cv/dataset/orangutan-image-classification-dataset)
16
+ for orang outan images and [this dataset](https://www.kaggle.com/datasets/slothkong/10-monkey-species/data) as background images. Annotations were obtained using zero shot object detection method GroundingDino.\\
17
 
18
  The code can be found on my github repository: https://github.com/clementapa/orang-outan-image-video-detection.
19
 
 
31
 
32
  YOLO_MODEL = YOLO("train_7best.pt")
33
 
34
+ BOX_ANNOTATOR = sv.BoxAnnotator(color=Color.from_hex("#FF00E4"))
35
 
36
 
37
  def annotate(
 
40
  annotator: sv.BoxAnnotator,
41
  labels: str,
42
  ) -> Image.Image:
43
+ thickness = 2
44
+ text_thickness = 1
45
+ text_scale = 1.0
46
+
47
+ height, width, _ = image_bgr_numpy.shape
48
+
49
+ thickness_ratio = ((width + height) / 2) / 400
50
+ text_scale_ratio = ((width + height) / 2) / 600
51
+ text_thickness_ratio = ((width + height) / 2) / 400
52
+
53
+ annotator.thickness = int(thickness * thickness_ratio)
54
+ annotator.text_scale = float(text_scale * text_scale_ratio)
55
+ annotator.text_thickness = int(text_thickness * text_thickness_ratio)
56
+
57
  annotated_bgr_image = annotator.annotate(
58
  scene=image_bgr_numpy, detections=detections, labels=labels
59
  )
 
61
 
62
 
63
  def inference(image_rgb_pil: Image.Image, confidence: float) -> List[Image.Image]:
64
+ output = YOLO_MODEL(image_rgb_pil, imgsz=640, verbose=False)[0]
65
  detections = sv.Detections.from_ultralytics(output)
66
 
67
  detections = detections[detections.confidence >= confidence]