someshb07 commited on
Commit
fa1a7d8
·
verified ·
1 Parent(s): d862e55

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -0
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+
4
+ # Use a pipeline as a high-level helper
5
+ from transformers import pipeline
6
+
7
+ pipe = pipeline("object-detection", model="facebook/detr-resnet-101")
8
+
9
+ from PIL import Image, ImageDraw, ImageFont
10
+
11
+ def merge_bboxes(bboxes, threshold=50):
12
+ """
13
+ Merges bounding boxes that are close to each other into one.
14
+
15
+ :param bboxes: List of bounding boxes (xmin, ymin, xmax, ymax)
16
+ :param threshold: Maximum distance between boxes to be considered as part of the same object
17
+ :return: List of merged bounding boxes
18
+ """
19
+ merged_bboxes = []
20
+ for bbox in bboxes:
21
+ added = False
22
+ for i, merged_bbox in enumerate(merged_bboxes):
23
+ if (abs(bbox[0] - merged_bbox[0]) < threshold and abs(bbox[1] - merged_bbox[1]) < threshold) or \
24
+ (abs(bbox[2] - merged_bbox[2]) < threshold and abs(bbox[3] - merged_bbox[3]) < threshold):
25
+ # Merge the boxes by extending the boundaries
26
+ new_bbox = (
27
+ min(bbox[0], merged_bbox[0]),
28
+ min(bbox[1], merged_bbox[1]),
29
+ max(bbox[2], merged_bbox[2]),
30
+ max(bbox[3], merged_bbox[3])
31
+ )
32
+ merged_bboxes[i] = new_bbox
33
+ added = True
34
+ break
35
+ if not added:
36
+ merged_bboxes.append(bbox)
37
+ return merged_bboxes
38
+
39
+ def draw_bounding_boxes(image, detections):
40
+ """
41
+ Draws bounding boxes on the given PIL image based on the object detection results.
42
+ Merges close bounding boxes to avoid duplicate detections.
43
+
44
+ :param image: PIL Image object
45
+ :param detections: List of dictionaries containing object detection results
46
+ :return: PIL Image with bounding boxes
47
+ """
48
+ draw = ImageDraw.Draw(image)
49
+
50
+ try:
51
+ font = ImageFont.truetype("arial.ttf", 40) # Load font (adjust size as needed)
52
+ except:
53
+ font = ImageFont.load_default() # Use default font if Arial is not available
54
+
55
+ bboxes = []
56
+ labels = []
57
+
58
+ # Collect all bounding boxes and labels
59
+ for obj in detections:
60
+ box = obj["box"]
61
+ label = f"{obj['label']} ({obj['score']:.2f})"
62
+ xmin, ymin, xmax, ymax = box["xmin"], box["ymin"], box["xmax"], box["ymax"]
63
+
64
+ bboxes.append((xmin, ymin, xmax, ymax))
65
+ labels.append(label)
66
+
67
+ # Merge close bounding boxes
68
+ merged_bboxes = merge_bboxes(bboxes)
69
+
70
+ # Draw bounding boxes and labels for merged boxes
71
+ for idx, bbox in enumerate(merged_bboxes):
72
+ xmin, ymin, xmax, ymax = bbox
73
+ label = labels[idx]
74
+
75
+ # Draw bounding box
76
+ draw.rectangle([xmin, ymin, xmax, ymax], outline="red", width=5)
77
+
78
+ # Adjust label placement to avoid overlap
79
+ label_bbox = draw.textbbox((xmin, ymin), label, font=font)
80
+ label_width, label_height = label_bbox[2] - label_bbox[0], label_bbox[3] - label_bbox[1]
81
+ label_x = xmin
82
+ label_y = ymin - label_height if ymin - label_height > 0 else ymin + 5 # Adjust above or below box
83
+
84
+ # Draw label background to make text more visible
85
+ draw.rectangle([label_x, label_y, label_x + label_width, label_y + label_height], fill="red")
86
+ draw.text((label_x, label_y), label, fill="white", font=font)
87
+
88
+ return image
89
+
90
+ # Example usage:
91
+ # image = Image.open("your_image.jpg")
92
+ # detections = [{'score': 0.92, 'label': 'dog', 'box': {'xmin': 2929, 'ymin': 1297, 'xmax': 5067, 'ymax': 3693}}, ...]
93
+ # image_with_boxes = draw_bounding_boxes(image, detections)
94
+ # image_with_boxes.show()
95
+
96
+ def detect_object(image):
97
+ raw_imagee = image
98
+ output=pipe(raw_imagee)
99
+ processed_image = draw_bounding_boxes(raw_imagee, output)
100
+ return processed_image
101
+
102
+ demo = gr.Interface(fn = detect_object,
103
+ inputs=[gr.Image(label='Please upload the file to detect the data', type='pil')],
104
+ outputs=[gr.Image(label='Detected object', type='pil')],
105
+ title= 'Object Detector',
106
+ description = 'This application will be used to detect image'
107
+ )
108
+
109
+ demo.launch(share=True)