|
import gradio as gr
|
|
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
|
|
|
|
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
object_detector = pipeline("object-detection",
|
|
model="facebook/detr-resnet-50")
|
|
|
|
|
|
|
|
|
|
|
|
def draw_bounding_boxes(image, detections, font_path=None, font_size=20):
|
|
|
|
draw_image = image.copy()
|
|
draw = ImageDraw.Draw(draw_image)
|
|
|
|
|
|
if font_path:
|
|
font = ImageFont.truetype(font_path, font_size)
|
|
else:
|
|
|
|
font = ImageFont.load_default()
|
|
|
|
|
|
for detection in detections:
|
|
box = detection['box']
|
|
xmin = box['xmin']
|
|
ymin = box['ymin']
|
|
xmax = box['xmax']
|
|
ymax = box['ymax']
|
|
|
|
|
|
draw.rectangle([(xmin, ymin), (xmax, ymax)], outline="red", width=3)
|
|
|
|
|
|
label = detection['label']
|
|
score = detection['score']
|
|
text = f"{label} {score:.2f}"
|
|
|
|
|
|
if font_path:
|
|
text_size = draw.textbbox((xmin, ymin), text, font=font)
|
|
else:
|
|
|
|
text_size = draw.textbbox((xmin, ymin), text)
|
|
|
|
draw.rectangle([(text_size[0], text_size[1]), (text_size[2], text_size[3])], fill="red")
|
|
draw.text((xmin, ymin), text, fill="white", font=font)
|
|
|
|
return draw_image
|
|
|
|
|
|
def detect_object(image):
|
|
raw_image = image
|
|
lst=[]
|
|
output = object_detector(raw_image)
|
|
for i in output:
|
|
lst.append(i['label'])
|
|
processed_image = draw_bounding_boxes(raw_image, output)
|
|
return processed_image,lst
|
|
|
|
demo = gr.Interface(fn=detect_object,
|
|
inputs=[gr.Image(label="Select Image",type="pil")],
|
|
outputs=[gr.Image(label="Processed Image", type="pil"),gr.Textbox(label="Objcts", lines=3),],
|
|
title="@GenAILearniverse Project 6: Object Detector",
|
|
description="THIS APPLICATION WILL BE USED TO DETECT OBJECTS INSIDE THE PROVIDED INPUT IMAGE.")
|
|
demo.launch() |