Spaces:
Runtime error
Runtime error
import gradio as gr | |
import cv2 | |
import torch | |
from PIL import Image | |
from transformers import DetrImageProcessor, DetrForObjectDetection | |
# Load the pre-trained DETR model | |
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", revision="no_timm") | |
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", revision="no_timm") | |
model.eval() | |
# Function for live object detection from the camera | |
def live_object_detection(image_pil): | |
# Convert the frame to PIL Image | |
frame_pil = Image.fromarray(cv2.cvtColor(image_pil, cv2.COLOR_BGR2RGB)) | |
# Process the frame with the DETR model | |
inputs = processor(images=frame_pil, 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([frame_pil.size[::-1]]) | |
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0] | |
# Draw bounding boxes on the frame | |
for score, label, box in zip(results["scores"], results["labels"], results["boxes"]): | |
box = [int(round(i)) for i in box.tolist()] | |
cv2.rectangle(image_pil, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2) | |
cv2.putText(image_pil, f"{model.config.id2label[label.item()]}: {round(score.item(), 3)}", | |
(box[0], box[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) | |
return image_pil | |
# Define the Gradio interface | |
iface = gr.Interface( | |
fn=live_object_detection, | |
inputs="image", | |
outputs="image", | |
live=True, | |
) | |
# Launch the Gradio interface | |
iface.launch() | |