Spaces:
Sleeping
Sleeping
File size: 1,215 Bytes
3048b3b 60e034d 3048b3b 40b13c6 3048b3b 9e28fe1 3048b3b a287077 3048b3b |
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 |
from transformers import DetrImageProcessor, DetrForObjectDetection
import torch
from PIL import Image
import gradio as gr
def detect_objects(image):
# Load the pre-trained DETR model
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-101")
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-101")
inputs = processor(images=image, return_tensors="pt")
outputs = model(**inputs)
# convert outputs (bounding boxes and class logits) to COCO API
# let's only keep detections with score > 0.9
target_sizes = torch.tensor([image.size[::-1]])
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
res = []
for label in results["labels"]:
res.append(model.config.id2label[label.item()])
return ','.join(res)
def upload_image(file):
image = Image.open(file.name)
image_with_boxes = detect_objects(image)
return image_with_boxes
iface = gr.Interface(
fn=upload_image,
inputs="file",
outputs="text",
title="Object Detection",
description="Upload an image and detect objects using DETR model.",
flagging_mode="never"
)
iface.launch() |