File size: 4,745 Bytes
433ce57
f99eac1
 
 
 
7f51efa
f99eac1
 
 
 
7f51efa
 
 
 
 
 
 
 
f99eac1
7f51efa
f99eac1
 
7f51efa
f99eac1
7f51efa
f99eac1
7f51efa
 
 
 
 
 
 
 
 
f99eac1
7f51efa
 
 
f99eac1
7f51efa
f99eac1
 
7f51efa
f99eac1
 
 
7f51efa
f99eac1
 
7f51efa
f99eac1
 
7f51efa
 
f99eac1
7f51efa
 
 
 
f99eac1
 
7f51efa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f99eac1
7f51efa
f99eac1
7f51efa
f99eac1
7f51efa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f99eac1
7f51efa
f99eac1
 
7f51efa
f99eac1
7f51efa
 
 
 
f99eac1
 
7f51efa
 
f99eac1
7f51efa
 
 
 
 
f99eac1
 
 
 
 
 
7f51efa
 
 
 
 
f99eac1
433ce57
f99eac1
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import gradio as gr
import torch
import numpy as np
import cv2
from PIL import Image

from ultralytics import YOLO
from torchvision.models.detection import fasterrcnn_resnet50_fpn
from transformers import AutoImageProcessor, AutoModelForObjectDetection

# -------------------------------------------------
# Device
# -------------------------------------------------
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# -------------------------------------------------
# Load Models
# -------------------------------------------------

# YOLOv8
yolo = YOLO("yolov8n.pt")

# Faster R-CNN
frcnn = fasterrcnn_resnet50_fpn(pretrained=True)
frcnn.to(device).eval()

# Deformable DETR
processor = AutoImageProcessor.from_pretrained(
    "SenseTime/deformable-detr",
    use_fast=False
)
detr = AutoModelForObjectDetection.from_pretrained(
    "SenseTime/deformable-detr"
)
detr.to(device).eval()

# -------------------------------------------------
# Utility Functions
# -------------------------------------------------

def compute_iou(box1, box2):
    x1, y1 = max(box1[0], box2[0]), max(box1[1], box2[1])
    x2, y2 = min(box1[2], box2[2]), min(box1[3], box2[3])

    inter = max(0, x2 - x1) * max(0, y2 - y1)
    area1 = (box1[2]-box1[0])*(box1[3]-box1[1])
    area2 = (box2[2]-box2[0])*(box2[3]-box2[1])

    return inter / (area1 + area2 - inter + 1e-6)

def draw_boxes(image, detections):
    img = np.array(image)
    for d in detections:
        x1, y1, x2, y2 = map(int, d["box"])
        label = d["label"]
        cv2.rectangle(img, (x1,y1), (x2,y2), (0,255,0), 2)
        cv2.putText(
            img, label, (x1, y1-6),
            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 1
        )
    return Image.fromarray(img)

# -------------------------------------------------
# Model Inference
# -------------------------------------------------

def yolo_detect(image):
    results = yolo(image)[0]
    dets = []
    for b in results.boxes:
        dets.append({
            "box": b.xyxy[0].cpu().numpy(),
            "model": "YOLO"
        })
    return dets

def frcnn_detect(image):
    img = torch.tensor(np.array(image)/255.).permute(2,0,1).float()
    img = img.unsqueeze(0).to(device)

    with torch.no_grad():
        out = frcnn(img)[0]

    dets = []
    for box, score in zip(out["boxes"], out["scores"]):
        if score > 0.6:
            dets.append({
                "box": box.cpu().numpy(),
                "model": "FRCNN"
            })
    return dets

def detr_detect(image):
    inputs = processor(images=image, return_tensors="pt").to(device)

    with torch.no_grad():
        outputs = detr(**inputs)

    size = torch.tensor([image.size[::-1]]).to(device)
    results = processor.post_process_object_detection(
        outputs, target_sizes=size, threshold=0.7
    )[0]

    dets = []
    for box in results["boxes"]:
        dets.append({
            "box": box.cpu().numpy(),
            "model": "DETR"
        })
    return dets

# -------------------------------------------------
# HARD VOTING
# -------------------------------------------------

def hard_vote(detections, vote_thresh=2, iou_thresh=0.5):
    final = []

    for d in detections:
        votes = [d]
        for o in detections:
            if d["model"] != o["model"]:
                if compute_iou(d["box"], o["box"]) >= iou_thresh:
                    votes.append(o)

        models = set(v["model"] for v in votes)
        if len(models) >= vote_thresh:
            avg_box = np.mean([v["box"] for v in votes], axis=0)
            final.append({
                "box": avg_box,
                "label": f"Ensemble ({len(models)})"
            })

    # remove duplicates
    unique = []
    for d in final:
        if not any(compute_iou(d["box"], u["box"]) > 0.8 for u in unique):
            unique.append(d)

    return unique

# -------------------------------------------------
# LIVE FRAME FUNCTION
# -------------------------------------------------

def live_detect(frame):
    image = Image.fromarray(frame)

    detections = (
        yolo_detect(image) +
        frcnn_detect(image) +
        detr_detect(image)
    )

    voted = hard_vote(detections)
    output = draw_boxes(image, voted)

    return np.array(output)

# -------------------------------------------------
# Gradio Interface (Webcam)
# -------------------------------------------------

demo = gr.Interface(
    fn=live_detect,
    inputs=gr.Image(source="webcam", streaming=True),
    outputs=gr.Image(),
    live=True,
    title="Live Object Detection – Hard Voting Ensemble",
    description=(
        "YOLOv8 + Faster R-CNN + Deformable DETR\n"
        "Browser-based webcam with IoU-based hard voting."
    )
)

demo.launch()