borakol commited on
Commit
72fcc90
·
verified ·
1 Parent(s): c8c2c86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -7
app.py CHANGED
@@ -6,18 +6,39 @@ pipe = pipeline("object-detection" , model = "hustvl/yolos-tiny")
6
 
7
  def detect_objects(image):
8
  results = pipe(image)
 
9
  draw = ImageDraw.Draw(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
- for r in results:
12
- box = r['box']
13
  draw.rectangle(
14
- [(box['xmin'], box['ymin']) , (box['xmax'],box['ymax'])], outline = "green" , width = 3
 
15
  )
16
 
17
- draw.text((box['xmin'], box['ymin']), f"{r['label']} {r['score']:.2f}" , fill = "green")
18
-
19
- for r in results:
20
- print(f"{r['label']} ({r['score']:.2f}) - Box: {r['box']}")
 
 
 
 
 
 
 
21
 
22
  return image
23
 
 
6
 
7
  def detect_objects(image):
8
  results = pipe(image)
9
+
10
  draw = ImageDraw.Draw(image)
11
+ try:
12
+ font = ImageFont.truetype("arial.ttf", 18)
13
+ except:
14
+ font = ImageFont.load_default()
15
+
16
+ for r in results:
17
+ box = r["box"]
18
+ label = f"{r['label']} {r['score']:.2f}"
19
+
20
+ # stil ayarları
21
+ color = "red"
22
+ box_width = 4
23
+ text_padding = 3
24
 
25
+ # Bounding Box
 
26
  draw.rectangle(
27
+ [(box["xmin"], box["ymin"]), (box["xmax"], box["ymax"])],
28
+ outline=color, width=box_width
29
  )
30
 
31
+ # Label
32
+ text_w, text_h = draw.textsize(label, font=font)
33
+ text_bg = [
34
+ (box["xmin"], box["ymin"] - text_h - text_padding),
35
+ (box["xmin"] + text_w + text_padding * 2, box["ymin"])
36
+ ]
37
+ draw.rectangle(text_bg, fill=color)
38
+ draw.text(
39
+ (box["xmin"] + text_padding, box["ymin"] - text_h - text_padding),
40
+ label, fill="white", font=font
41
+ )
42
 
43
  return image
44