bnava commited on
Commit
7a98029
1 Parent(s): 9e82a10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -25
app.py CHANGED
@@ -1,21 +1,7 @@
1
  # !pip install timm
2
  # !pip install transformers
3
  # !pip install pillow
4
- from PIL import Image
5
- import gradio as gr
6
-
7
- # Use a pipeline as a high-level helper
8
-
9
-
10
- # object_detector = pipeline("object-detection", model="facebook/detr-resnet-50")
11
-
12
- # raw_image = Image .open("/content/dogwithman.jpg")
13
-
14
- # output = object_detector(raw_image)
15
- # print(output)
16
-
17
-
18
- from PIL import Image
19
  import gradio as gr
20
  from transformers import pipeline
21
 
@@ -25,26 +11,45 @@ object_detector = pipeline("object-detection", model="facebook/detr-resnet-50")
25
  def detect_objects(image):
26
  # Perform object detection on the input image
27
  results = object_detector(image)
28
-
29
- # Prepare the output string
30
- output = []
 
 
 
 
 
31
  for result in results:
32
  label = result['label']
33
  score = result['score']
34
  box = result['box']
35
- output.append(f"Label: {label}, Score: {score:.2f}, Box: {box}")
36
-
37
- return "\n".join(output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
  # Create the Gradio interface
40
  interface = gr.Interface(
41
- fn=detect_objects,
42
- inputs=gr.Image(type="pil"),
43
- outputs="text",
44
  title="Image Object Detector",
45
  description="Upload an image to detect objects using the DETR model."
46
  )
47
-
48
 
49
  # Launch the app
50
  interface.launch()
 
1
  # !pip install timm
2
  # !pip install transformers
3
  # !pip install pillow
4
+ from PIL import Image, ImageDraw, ImageFont
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
  import gradio as gr
6
  from transformers import pipeline
7
 
 
11
  def detect_objects(image):
12
  # Perform object detection on the input image
13
  results = object_detector(image)
14
+
15
+ # Create a draw object
16
+ draw = ImageDraw.Draw(image)
17
+
18
+ # Load a custom font
19
+ font = ImageFont.truetype("Arial.ttf", size=20)
20
+
21
+ # Draw bounding boxes around detected objects
22
  for result in results:
23
  label = result['label']
24
  score = result['score']
25
  box = result['box']
26
+
27
+ # Draw the bounding box
28
+ draw.rectangle(
29
+ [(box['xmin'], box['ymin']), (box['xmax'], box['ymax'])],
30
+ outline="red", width=2
31
+ )
32
+
33
+ # Draw the label and score with custom font
34
+ text = f"{label} {score:.2f}"
35
+ text_size = draw.textsize(text, font=font)
36
+ text_location = (box['xmin'], box['ymin'] - text_size[1])
37
+ draw.rectangle(
38
+ [(text_location[0], text_location[1]), (text_location[0] + text_size[0], text_location[1] + text_size[1])],
39
+ fill="red"
40
+ )
41
+ draw.text((box['xmin'], box['ymin'] - text_size[1]), text, font=font, fill="white")
42
+
43
+ return image
44
 
45
  # Create the Gradio interface
46
  interface = gr.Interface(
47
+ fn=detect_objects,
48
+ inputs=gr.Image(type="pil"),
49
+ outputs=gr.Image(type="pil"),
50
  title="Image Object Detector",
51
  description="Upload an image to detect objects using the DETR model."
52
  )
 
53
 
54
  # Launch the app
55
  interface.launch()