Spaces:
Sleeping
Sleeping
File size: 2,946 Bytes
654aed7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
import torch
import gradio as gr
from PIL import Image, ImageDraw, ImageFont
# Use a pipeline as a high-level helper
from transformers import pipeline
# model_path = "../Models/models--facebook--detr-resnet-50/snapshots/1d5f47bd3bdd2c4bbfa585418ffe6da5028b4c0b"
object_detector = pipeline("object-detection", model="facebook/detr-resnet-50")
# object_detector = pipeline("object-detection", model=model_path)
def draw_bounding_boxes(image, detections, font_path=None, font_size=500):
"""
Draws bounding boxes on the given image based on the detections.
:param image: PIL.Image object
:param detections: List of detection results, where each result is a dictionary containing
'score', 'label', and 'box' keys. 'box' itself is a dictionary with 'xmin',
'ymin', 'xmax', 'ymax' keys.
:param font_path: Path to the TrueType font file to use for text.
:param font_size: Size of the font to use for text.
:return: PIL.Image object with bounding boxes drawn.
"""
# Make a copy of the image to draw on
draw_image = image.copy()
draw = ImageDraw.Draw(draw_image)
# Load custom font or default font if path not provided
if font_path:
font = ImageFont.truetype(font_path, font_size)
else:
# Use default font, increase font size with TTF font if necessary
font = ImageFont.load_default()
for detection in detections:
box = detection['box']
xmin = box['xmin']
ymin = box['ymin']
xmax = box['xmax']
ymax = box['ymax']
# Draw the bounding box
draw.rectangle([(xmin, ymin), (xmax, ymax)], outline="red", width=3)
# Optionally, draw the label and score
label = detection['label']
score = detection['score']
text = f"{label} {score:.2f}"
# Draw text with background rectangle for visibility
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
# for text output
# raw_image = Image.open("../Files/cat.jpg")
# output = object_detector(raw_image)
# print(output)
# Function for object detection and bounding box drawing
def detect_object(image):
raw_image = image
output = object_detector(raw_image)
processed_image = draw_bounding_boxes(raw_image, output)
return processed_image
demo = gr.Interface(
fn=detect_object,
inputs=[gr.Image(label="Select Image", type="pil")],
outputs=[gr.Image(label="Processed Image", type="pil")],
title="Project 05: Object Detector",
description="As understood from the title, if not already, this application will detect objects in your image"
)
demo.launch()
|