Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,73 @@
|
|
| 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 |
-
# model_path = ("../Models/models--facebook--detr-resnet-50/snapshots"
|
| 9 |
-
# "/1d5f47bd3bdd2c4bbfa585418ffe6da5028b4c0b")
|
| 10 |
-
|
| 11 |
-
object_detector = pipeline("object-detection",
|
| 12 |
-
model="facebook/detr-resnet-50")
|
| 13 |
-
|
| 14 |
-
# object_detector = pipeline("object-detection",
|
| 15 |
-
# model=model_path)
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
def draw_bounding_boxes(image, detections, font_path=None, font_size=20):
|
| 19 |
-
# Make a copy of the image to draw on
|
| 20 |
-
draw_image = image.copy()
|
| 21 |
-
draw = ImageDraw.Draw(draw_image)
|
| 22 |
-
|
| 23 |
-
# Load custom font or default font if path not provided
|
| 24 |
-
if font_path:
|
| 25 |
-
font = ImageFont.truetype(font_path, font_size)
|
| 26 |
-
else:
|
| 27 |
-
# When font_path is not provided, load default font but it's size is fixed
|
| 28 |
-
font = ImageFont.load_default()
|
| 29 |
-
# Increase font size workaround by using a TTF font file, if needed, can download and specify the path
|
| 30 |
-
|
| 31 |
-
for detection in detections:
|
| 32 |
-
box = detection['box']
|
| 33 |
-
xmin = box['xmin']
|
| 34 |
-
ymin = box['ymin']
|
| 35 |
-
xmax = box['xmax']
|
| 36 |
-
ymax = box['ymax']
|
| 37 |
-
|
| 38 |
-
# Draw the bounding box
|
| 39 |
-
draw.rectangle([(xmin, ymin), (xmax, ymax)], outline="red", width=3)
|
| 40 |
-
|
| 41 |
-
# Optionally, you can also draw the label and score
|
| 42 |
-
label = detection['label']
|
| 43 |
-
score = detection['score']
|
| 44 |
-
text = f"{label} {score:.2f}"
|
| 45 |
-
|
| 46 |
-
# Draw text with background rectangle for visibility
|
| 47 |
-
if font_path: # Use the custom font with increased size
|
| 48 |
-
text_size = draw.textbbox((xmin, ymin), text, font=font)
|
| 49 |
-
else:
|
| 50 |
-
# Calculate text size using the default font
|
| 51 |
-
text_size = draw.textbbox((xmin, ymin), text)
|
| 52 |
-
|
| 53 |
-
draw.rectangle([(text_size[0], text_size[1]), (text_size[2], text_size[3])], fill="red")
|
| 54 |
-
draw.text((xmin, ymin), text, fill="white", font=font)
|
| 55 |
-
|
| 56 |
-
return draw_image
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
def detect_object(image):
|
| 60 |
-
raw_image = image
|
| 61 |
-
lst=[]
|
| 62 |
-
output = object_detector(raw_image)
|
| 63 |
-
for i in output:
|
| 64 |
-
lst.append(i['label'])
|
| 65 |
-
processed_image = draw_bounding_boxes(raw_image, output)
|
| 66 |
-
return processed_image,lst
|
| 67 |
-
|
| 68 |
-
demo = gr.Interface(fn=detect_object,
|
| 69 |
-
inputs=[gr.Image(label="Select Image",type="pil")],
|
| 70 |
-
outputs=[gr.Image(label="Processed Image", type="pil"),gr.Textbox(label="Objcts", lines=3),],
|
| 71 |
-
title="
|
| 72 |
-
description="THIS APPLICATION WILL BE USED TO DETECT OBJECTS INSIDE THE PROVIDED INPUT IMAGE.")
|
| 73 |
demo.launch()
|
|
|
|
| 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 |
+
# model_path = ("../Models/models--facebook--detr-resnet-50/snapshots"
|
| 9 |
+
# "/1d5f47bd3bdd2c4bbfa585418ffe6da5028b4c0b")
|
| 10 |
+
|
| 11 |
+
object_detector = pipeline("object-detection",
|
| 12 |
+
model="facebook/detr-resnet-50")
|
| 13 |
+
|
| 14 |
+
# object_detector = pipeline("object-detection",
|
| 15 |
+
# model=model_path)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def draw_bounding_boxes(image, detections, font_path=None, font_size=20):
|
| 19 |
+
# Make a copy of the image to draw on
|
| 20 |
+
draw_image = image.copy()
|
| 21 |
+
draw = ImageDraw.Draw(draw_image)
|
| 22 |
+
|
| 23 |
+
# Load custom font or default font if path not provided
|
| 24 |
+
if font_path:
|
| 25 |
+
font = ImageFont.truetype(font_path, font_size)
|
| 26 |
+
else:
|
| 27 |
+
# When font_path is not provided, load default font but it's size is fixed
|
| 28 |
+
font = ImageFont.load_default()
|
| 29 |
+
# Increase font size workaround by using a TTF font file, if needed, can download and specify the path
|
| 30 |
+
|
| 31 |
+
for detection in detections:
|
| 32 |
+
box = detection['box']
|
| 33 |
+
xmin = box['xmin']
|
| 34 |
+
ymin = box['ymin']
|
| 35 |
+
xmax = box['xmax']
|
| 36 |
+
ymax = box['ymax']
|
| 37 |
+
|
| 38 |
+
# Draw the bounding box
|
| 39 |
+
draw.rectangle([(xmin, ymin), (xmax, ymax)], outline="red", width=3)
|
| 40 |
+
|
| 41 |
+
# Optionally, you can also draw the label and score
|
| 42 |
+
label = detection['label']
|
| 43 |
+
score = detection['score']
|
| 44 |
+
text = f"{label} {score:.2f}"
|
| 45 |
+
|
| 46 |
+
# Draw text with background rectangle for visibility
|
| 47 |
+
if font_path: # Use the custom font with increased size
|
| 48 |
+
text_size = draw.textbbox((xmin, ymin), text, font=font)
|
| 49 |
+
else:
|
| 50 |
+
# Calculate text size using the default font
|
| 51 |
+
text_size = draw.textbbox((xmin, ymin), text)
|
| 52 |
+
|
| 53 |
+
draw.rectangle([(text_size[0], text_size[1]), (text_size[2], text_size[3])], fill="red")
|
| 54 |
+
draw.text((xmin, ymin), text, fill="white", font=font)
|
| 55 |
+
|
| 56 |
+
return draw_image
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def detect_object(image):
|
| 60 |
+
raw_image = image
|
| 61 |
+
lst=[]
|
| 62 |
+
output = object_detector(raw_image)
|
| 63 |
+
for i in output:
|
| 64 |
+
lst.append(i['label'])
|
| 65 |
+
processed_image = draw_bounding_boxes(raw_image, output)
|
| 66 |
+
return processed_image,lst
|
| 67 |
+
|
| 68 |
+
demo = gr.Interface(fn=detect_object,
|
| 69 |
+
inputs=[gr.Image(label="Select Image",type="pil")],
|
| 70 |
+
outputs=[gr.Image(label="Processed Image", type="pil"),gr.Textbox(label="Objcts", lines=3),],
|
| 71 |
+
title="Object Detector",
|
| 72 |
+
description="THIS APPLICATION WILL BE USED TO DETECT OBJECTS INSIDE THE PROVIDED INPUT IMAGE / Live FEED .")
|
| 73 |
demo.launch()
|