Spaces:
Sleeping
Sleeping
Muhammad Anas Akhtar
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,78 +1,74 @@
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image, ImageDraw, ImageFont
|
3 |
-
|
4 |
-
|
5 |
-
# Use a pipeline as a high-level helper
|
6 |
from transformers import pipeline
|
7 |
|
8 |
-
|
9 |
object_detector = pipeline("object-detection",
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
|
14 |
-
def draw_bounding_boxes(image, detections,
|
15 |
"""
|
16 |
Draws bounding boxes on the given image based on the detections.
|
17 |
-
:param image: PIL.Image object
|
18 |
-
:param detections: List of detection results, where each result is a dictionary containing
|
19 |
-
'score', 'label', and 'box' keys. 'box' itself is a dictionary with 'xmin',
|
20 |
-
'ymin', 'xmax', 'ymax'.
|
21 |
-
:param font_path: Path to the TrueType font file to use for text.
|
22 |
-
:param font_size: Size of the font to use for text.
|
23 |
-
:return: PIL.Image object with bounding boxes drawn.
|
24 |
"""
|
25 |
# Make a copy of the image to draw on
|
26 |
draw_image = image.copy()
|
27 |
draw = ImageDraw.Draw(draw_image)
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
font = ImageFont.truetype(font_path, font_size)
|
32 |
-
else:
|
33 |
-
# When font_path is not provided, load default font but it's size is fixed
|
34 |
-
font = ImageFont.load_default()
|
35 |
-
# Increase font size workaround by using a TTF font file, if needed, can download and specify the path
|
36 |
|
37 |
for detection in detections:
|
38 |
box = detection['box']
|
39 |
-
xmin = box['xmin']
|
40 |
-
ymin = box['ymin']
|
41 |
-
xmax = box['xmax']
|
42 |
-
ymax = box['ymax']
|
43 |
|
44 |
# Draw the bounding box
|
45 |
draw.rectangle([(xmin, ymin), (xmax, ymax)], outline="red", width=3)
|
46 |
|
47 |
-
#
|
48 |
label = detection['label']
|
49 |
score = detection['score']
|
50 |
text = f"{label} {score:.2f}"
|
51 |
|
52 |
# Draw text with background rectangle for visibility
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
draw.rectangle([(text_size[0], text_size[1]), (text_size[2], text_size[3])], fill="red")
|
60 |
draw.text((xmin, ymin), text, fill="white", font=font)
|
61 |
|
62 |
return draw_image
|
63 |
|
64 |
-
|
65 |
def detect_object(image):
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
|
|
|
3 |
from transformers import pipeline
|
4 |
|
5 |
+
# Initialize the object detection pipeline
|
6 |
object_detector = pipeline("object-detection",
|
7 |
+
model="facebook/detr-resnet-50")
|
|
|
|
|
8 |
|
9 |
+
def draw_bounding_boxes(image, detections, font_size=20):
|
10 |
"""
|
11 |
Draws bounding boxes on the given image based on the detections.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
"""
|
13 |
# Make a copy of the image to draw on
|
14 |
draw_image = image.copy()
|
15 |
draw = ImageDraw.Draw(draw_image)
|
16 |
|
17 |
+
# Use default font since custom font paths might not be available
|
18 |
+
font = ImageFont.load_default()
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
for detection in detections:
|
21 |
box = detection['box']
|
22 |
+
xmin = int(box['xmin'])
|
23 |
+
ymin = int(box['ymin'])
|
24 |
+
xmax = int(box['xmax'])
|
25 |
+
ymax = int(box['ymax'])
|
26 |
|
27 |
# Draw the bounding box
|
28 |
draw.rectangle([(xmin, ymin), (xmax, ymax)], outline="red", width=3)
|
29 |
|
30 |
+
# Create label with score
|
31 |
label = detection['label']
|
32 |
score = detection['score']
|
33 |
text = f"{label} {score:.2f}"
|
34 |
|
35 |
# Draw text with background rectangle for visibility
|
36 |
+
text_bbox = draw.textbbox((xmin, ymin), text, font=font)
|
37 |
+
draw.rectangle([
|
38 |
+
(text_bbox[0], text_bbox[1]),
|
39 |
+
(text_bbox[2], text_bbox[3])
|
40 |
+
], fill="red")
|
|
|
|
|
41 |
draw.text((xmin, ymin), text, fill="white", font=font)
|
42 |
|
43 |
return draw_image
|
44 |
|
|
|
45 |
def detect_object(image):
|
46 |
+
if image is None:
|
47 |
+
return None
|
48 |
+
|
49 |
+
try:
|
50 |
+
# Detect objects
|
51 |
+
output = object_detector(image)
|
52 |
+
|
53 |
+
# Draw bounding boxes
|
54 |
+
processed_image = draw_bounding_boxes(image, output)
|
55 |
+
return processed_image
|
56 |
+
except Exception as e:
|
57 |
+
print(f"Error during object detection: {str(e)}")
|
58 |
+
return None
|
59 |
+
|
60 |
+
# Create the Gradio interface
|
61 |
+
demo = gr.Interface(
|
62 |
+
fn=detect_object,
|
63 |
+
inputs=[
|
64 |
+
gr.Image(label="Upload Image", type="pil")
|
65 |
+
],
|
66 |
+
outputs=[
|
67 |
+
gr.Image(label="Detected Objects")
|
68 |
+
],
|
69 |
+
title="Object Detection using image",
|
70 |
+
description="Upload an image to detect and identify objects within it. The application will draw bounding boxes around detected objects and show their labels with confidence scores."
|
71 |
+
)
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
demo.launch()
|