Vishaltiwari2019 commited on
Commit
6ac27af
·
verified ·
1 Parent(s): 3ba60ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -51
app.py CHANGED
@@ -1,89 +1,67 @@
 
1
  from transformers import DetrImageProcessor, DetrForObjectDetection
2
  import torch
3
- from PIL import Image, ImageDraw, ImageFont # Import ImageFont
4
- import gradio as gr
5
- import requests
6
  import random
7
 
8
  def detect_objects(image):
9
- # Load the pre-trained DETR model
10
  processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
11
  model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
12
 
13
  inputs = processor(images=image, return_tensors="pt")
14
  outputs = model(**inputs)
15
 
16
- # convert outputs (bounding boxes and class logits) to COCO API
17
- # let's only keep detections with score > 0.9
18
  target_sizes = torch.tensor([image.size[::-1]])
19
  results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
20
 
21
- # Draw bounding boxes and labels on the image
22
  draw = ImageDraw.Draw(image)
 
23
  for i, (score, label, box) in enumerate(zip(results["scores"], results["labels"], results["boxes"])):
24
  box = [round(i, 2) for i in box.tolist()]
25
  color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
26
  draw.rectangle(box, outline=color, width=3)
27
  label_text = f"{model.config.id2label[label.item()]}: {round(score.item(), 2)}"
28
- # Larger and bolder font
29
  draw.text((box[0], box[1]), label_text, fill=color,)
 
30
 
31
- return image
32
-
33
- def detect_labels(image):
34
- # Load the pre-trained DETR model
35
- processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
36
- model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
37
-
38
- inputs = processor(images=image, return_tensors="pt")
39
- outputs = model(**inputs)
40
 
41
- # convert outputs (bounding boxes and class logits) to COCO API
42
- # let's only keep detections with score > 0.9
43
- target_sizes = torch.tensor([image.size[::-1]])
44
- results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
45
-
46
- labels = []
47
- for label_id in results["labels"]:
48
- labels.append(model.config.id2label[label_id.item()])
49
-
50
- return labels
51
-
52
- def upload_image_with_boxes(file):
53
  image = Image.open(file.name)
54
- image_with_boxes = detect_objects(image)
55
- return image_with_boxes
56
 
57
- def upload_image_with_labels(file):
58
- image = Image.open(file.name)
59
- labels = detect_labels(image)
60
- return ", ".join(labels)
61
 
62
- iface_switch = gr.Interface(
63
- fn=upload_image_with_boxes,
 
64
  inputs="file",
65
- outputs="image",
66
- title="Object Detection with Boxes",
67
- description="Upload an image and detect objects using DETR model. Tap the image to switch to labels view.",
68
  allow_flagging=False
69
  )
70
 
 
71
  iface_labels = gr.Interface(
72
- fn=upload_image_with_labels,
73
- inputs="file",
74
  outputs="text",
75
- title="Detected Object Labels",
76
- description="Upload an image and get the detected object labels using DETR model. Tap the text to switch back to object detection with boxes view.",
77
  allow_flagging=False
78
  )
79
 
80
- iface_tapped = gr.Interface(
81
- [iface_switch, iface_labels],
82
- inputs="file",
83
- outputs="image",
84
- title="Object Detection and Labels",
85
- description="Upload an image and switch between object detection with boxes and displaying detected object labels as text.",
 
86
  allow_flagging=False
87
  )
88
 
89
- iface_tapped.launch()
 
1
+ import gradio as gr
2
  from transformers import DetrImageProcessor, DetrForObjectDetection
3
  import torch
4
+ from PIL import Image, ImageDraw
 
 
5
  import random
6
 
7
  def detect_objects(image):
 
8
  processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
9
  model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
10
 
11
  inputs = processor(images=image, return_tensors="pt")
12
  outputs = model(**inputs)
13
 
 
 
14
  target_sizes = torch.tensor([image.size[::-1]])
15
  results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
16
 
 
17
  draw = ImageDraw.Draw(image)
18
+ labels = []
19
  for i, (score, label, box) in enumerate(zip(results["scores"], results["labels"], results["boxes"])):
20
  box = [round(i, 2) for i in box.tolist()]
21
  color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
22
  draw.rectangle(box, outline=color, width=3)
23
  label_text = f"{model.config.id2label[label.item()]}: {round(score.item(), 2)}"
 
24
  draw.text((box[0], box[1]), label_text, fill=color,)
25
+ labels.append(label_text)
26
 
27
+ return image, labels
 
 
 
 
 
 
 
 
28
 
29
+ def upload_image(file):
 
 
 
 
 
 
 
 
 
 
 
30
  image = Image.open(file.name)
31
+ image_with_boxes, labels = detect_objects(image)
32
+ return image_with_boxes, labels
33
 
34
+ def show_labels(labels):
35
+ return "\n".join(labels)
 
 
36
 
37
+ # Interface to display the image with bounding boxes
38
+ iface_objects = gr.Interface(
39
+ fn=upload_image,
40
  inputs="file",
41
+ outputs=["image", "text"],
42
+ title="Object Detection",
43
+ description="Upload an image and detect objects using DETR model.",
44
  allow_flagging=False
45
  )
46
 
47
+ # Interface to display the detected labels
48
  iface_labels = gr.Interface(
49
+ fn=show_labels,
50
+ inputs="text",
51
  outputs="text",
52
+ title="Detected Labels",
53
+ description="Displays the labels detected in the uploaded image.",
54
  allow_flagging=False
55
  )
56
 
57
+ # Combine interfaces with a tapped interface
58
+ interface = gr.Interface(
59
+ [iface_objects, iface_labels],
60
+ inputs="text",
61
+ outputs="text",
62
+ title="Object Detection with Labels",
63
+ description="Upload an image and view detected objects and labels.",
64
  allow_flagging=False
65
  )
66
 
67
+ interface.launch()