Spaces:
Running
Running
import gradio as gr | |
from PIL import Image, ImageDraw | |
# Use a pipeline as a high-level helper | |
from transformers import pipeline | |
model_path = ("../Model/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, detection_results): | |
""" | |
Draws bounding boxes on the provided image based on the detection results. | |
Parameters: | |
image (PIL.Image): The input image to be annotated. | |
detection_results (list): A list of dictionaries, each containing the detected object details. | |
Returns: | |
PIL.Image: The image with bounding boxes drawn around the detected objects. | |
""" | |
# Convert the input image to ImageDraw object to draw on it | |
draw = ImageDraw.Draw(image) | |
# Iterate through each detection result | |
for result in detection_results: | |
# Extract the bounding box coordinates and label | |
box = result['box'] | |
label = result['label'] | |
score = result['score'] | |
# Define coordinates for the bounding box | |
xmin, ymin, xmax, ymax = box['xmin'], box['ymin'], box['xmax'], box['ymax'] | |
# Draw the bounding box (with a red outline) | |
draw.rectangle([xmin, ymin, xmax, ymax], outline="red", width=3) | |
# Optionally, add label with score near the bounding box | |
text = f"{label} ({score * 100:.1f}%)" | |
draw.text((xmin, ymin - 10), text, fill="red") | |
return image | |
def detect_objects(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_objects, | |
inputs=[gr.Image(label="Select Image",type="pil")], | |
outputs=[gr.Image(label="Summarized Text ",type="pil")], | |
title="@SherryAhuja Project : Object Detection", | |
description="This AI application will be used to Detect objects in an image.",) | |
demo.launch() |