File size: 1,362 Bytes
d10f466
 
 
b496423
 
d10f466
bebfcb3
b496423
 
bf51623
bebfcb3
 
 
 
bf51623
b496423
 
bf51623
bebfcb3
b496423
bf51623
b496423
 
 
bebfcb3
 
b496423
 
 
bebfcb3
 
bf51623
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
import gradio as gr
import torch
from PIL import Image
from torchvision.transforms import functional as F
from transformers import DetrImageProcessor, DetrForObjectDetection

# Load the pretrained DETR model
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", revision="no_timm")

# Define the object detection function
def detect_objects(frame):
    # Convert the frame to PIL image
    image = Image.fromarray(frame)

    # Preprocess the image
    inputs = processor(images=image, return_tensors="pt")

    # Perform object detection
    outputs = model(**inputs)

    # Convert outputs to COCO API format
    target_sizes = torch.tensor([image.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 = [round(i, 2) for i in box.tolist()]
        frame = gr.draw_box(frame, box, label=model.config.id2label[label.item()], color=(0, 255, 0))

    return frame

# Define the Gradio interface
iface = gr.Interface(
    fn=detect_objects,
    inputs=gr.Video(),
    outputs="video",
    live=True,
)

# Launch the Gradio app
iface.launch()