Spaces:
Runtime error
Runtime error
File size: 1,551 Bytes
d10f466 bebfcb3 d10f466 4cbee43 69c84d2 d10f466 bebfcb3 d10f466 bebfcb3 bf51623 bebfcb3 bf51623 bebfcb3 bf51623 bebfcb3 bf51623 bebfcb3 bf51623 d10f466 bebfcb3 d10f466 bebfcb3 d10f466 bebfcb3 d10f466 |
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 |
import gradio as gr
import torch
import torchvision.transforms as transforms
from torchvision.models.detection import detr
from PIL import Image
import cv2
import numpy as np
# Load the pretrained DETR model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = detr.DETR(resnet50=True)
model = model.to(device).eval()
# Define the transformation for the input image
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Resize((800, 800)),
])
# Define the object detection function
def detect_objects(frame):
# Convert the frame to PIL image
image = Image.fromarray(frame)
# Apply the transformation
image = transform(image).unsqueeze(0).to(device)
# Perform object detection
with torch.no_grad():
outputs = model(image)
# Get the bounding boxes and labels
boxes = outputs['pred_boxes'][0].cpu().numpy()
labels = outputs['pred_classes'][0].cpu().numpy()
# Draw bounding boxes on the frame
for box, label in zip(boxes, labels):
box = [int(coord) for coord in box]
frame = cv2.rectangle(frame, (box[0], box[1]), (box[2], box[3]), (0, 255, 0), 2)
frame = cv2.putText(frame, f'Class: {label}', (box[0], box[1] - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2, cv2.LINE_AA)
return frame
# Define the Gradio interface
iface = gr.Interface(
fn=detect_objects,
inputs=gr.Video(),
outputs="video",
live=True,
capture_session=True,
)
# Launch the Gradio app
iface.launch()
|