sehaj13 commited on
Commit
be5545f
·
verified ·
1 Parent(s): 99d343e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -6
app.py CHANGED
@@ -1,7 +1,5 @@
1
  # app.py
2
 
3
- # app.py
4
-
5
  from transformers import pipeline
6
  import gradio as gr
7
  from PIL import Image, ImageDraw, ImageFont
@@ -25,7 +23,6 @@ def get_color(label):
25
  # Detection function
26
  def detect_objects(img):
27
  results = detector(img)
28
-
29
  draw = ImageDraw.Draw(img)
30
  font = ImageFont.load_default()
31
 
@@ -44,12 +41,12 @@ def detect_objects(img):
44
 
45
  # Prepare label with confidence
46
  label_text = f"{label} ({score:.2f})"
47
- text_size = draw.textsize(label_text, font=font)
48
- text_background = [box["xmin"], box["ymin"] - text_size[1], box["xmin"] + text_size[0], box["ymin"]]
49
 
50
  # Draw background for text
51
  draw.rectangle(text_background, fill=color)
52
- draw.text((box["xmin"], box["ymin"] - text_size[1]), label_text, fill="black", font=font)
53
 
54
  return img
55
 
 
1
  # app.py
2
 
 
 
3
  from transformers import pipeline
4
  import gradio as gr
5
  from PIL import Image, ImageDraw, ImageFont
 
23
  # Detection function
24
  def detect_objects(img):
25
  results = detector(img)
 
26
  draw = ImageDraw.Draw(img)
27
  font = ImageFont.load_default()
28
 
 
41
 
42
  # Prepare label with confidence
43
  label_text = f"{label} ({score:.2f})"
44
+ text_bbox = draw.textbbox((box["xmin"], box["ymin"]), label_text, font=font)
45
+ text_background = [text_bbox[0], text_bbox[1], text_bbox[2], text_bbox[3]]
46
 
47
  # Draw background for text
48
  draw.rectangle(text_background, fill=color)
49
+ draw.text((text_bbox[0], text_bbox[1]), label_text, fill="black", font=font)
50
 
51
  return img
52