Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image, ImageDraw
|
3 |
+
|
4 |
+
|
5 |
+
# Use a pipeline as a high-level helper
|
6 |
+
from transformers import pipeline
|
7 |
+
|
8 |
+
model_path = ("../Model/models--facebook--detr-resnet-50/snapshots"
|
9 |
+
"/1d5f47bd3bdd2c4bbfa585418ffe6da5028b4c0b")
|
10 |
+
|
11 |
+
|
12 |
+
object_detector = pipeline("object-detection", model="facebook/detr-resnet-50")
|
13 |
+
|
14 |
+
|
15 |
+
# object_detector = pipeline("object-detection", model=model_path)
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
def draw_bounding_boxes(image, detection_results):
|
20 |
+
"""
|
21 |
+
Draws bounding boxes on the provided image based on the detection results.
|
22 |
+
|
23 |
+
Parameters:
|
24 |
+
image (PIL.Image): The input image to be annotated.
|
25 |
+
detection_results (list): A list of dictionaries, each containing the detected object details.
|
26 |
+
|
27 |
+
Returns:
|
28 |
+
PIL.Image: The image with bounding boxes drawn around the detected objects.
|
29 |
+
"""
|
30 |
+
# Convert the input image to ImageDraw object to draw on it
|
31 |
+
draw = ImageDraw.Draw(image)
|
32 |
+
|
33 |
+
# Iterate through each detection result
|
34 |
+
for result in detection_results:
|
35 |
+
# Extract the bounding box coordinates and label
|
36 |
+
box = result['box']
|
37 |
+
label = result['label']
|
38 |
+
score = result['score']
|
39 |
+
|
40 |
+
# Define coordinates for the bounding box
|
41 |
+
xmin, ymin, xmax, ymax = box['xmin'], box['ymin'], box['xmax'], box['ymax']
|
42 |
+
|
43 |
+
# Draw the bounding box (with a red outline)
|
44 |
+
draw.rectangle([xmin, ymin, xmax, ymax], outline="red", width=3)
|
45 |
+
|
46 |
+
# Optionally, add label with score near the bounding box
|
47 |
+
text = f"{label} ({score * 100:.1f}%)"
|
48 |
+
draw.text((xmin, ymin - 10), text, fill="red")
|
49 |
+
|
50 |
+
return image
|
51 |
+
|
52 |
+
def detect_objects(image):
|
53 |
+
raw_image = image
|
54 |
+
output = object_detector(raw_image)
|
55 |
+
processed_image = draw_bounding_boxes(raw_image, output)
|
56 |
+
return processed_image
|
57 |
+
|
58 |
+
|
59 |
+
|
60 |
+
demo = gr.Interface(fn = detect_objects,
|
61 |
+
inputs=[gr.Image(label="Select Image",type="pil")],
|
62 |
+
outputs=[gr.Image(label="Summarized Text ",type="pil")],
|
63 |
+
title="@SherryAhuja Project : Object Detection",
|
64 |
+
description="This AI application will be used to Detect objects in an image.",)
|
65 |
+
demo.launch()
|