File size: 7,000 Bytes
1f97e39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>RF-DETR WebGPU</title>
	<link rel="stylesheet" href="style.css" />
</head>
<body>

    <h1>RF-DETR WebGPU</h1>
    <div class="subtitle">
        Real-Time Detection Transformers<br>
        running 100% locally in your browser.
    </div>

    <div class="container">
        <div id="status">
            <div class="spinner"></div>
            <div id="status-content">
                <div id="status-text">Initializing...</div>
                <div id="status-sub">Please allow camera access</div>
            </div>
        </div>
        <div id="fps">FPS: 0.0</div>
        <video id="webcam" autoplay playsinline muted></video>
        <canvas id="overlay"></canvas>
    </div>

    <div class="controls">
        <label class="control-label">
            <span>Threshold</span>
            <input type="range" id="threshold" min="0" max="1" step="0.01" value="0.5">
            <span id="thresh-val">0.50</span>
        </label>
    </div>

    <footer>
        Powered by <a href="https://github.com/huggingface/transformers.js" target="_blank">Transformers.js v4</a>
    </footer>

    <script type="module">
        import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@next';

        const video = document.getElementById('webcam');
        const overlay = document.getElementById('overlay');
        const status = document.getElementById('status');
        const statusText = document.getElementById('status-text');
        const statusSub = document.getElementById('status-sub');
        const fpsElem = document.getElementById('fps');
        const slider = document.getElementById('threshold');
        const sliderVal = document.getElementById('thresh-val');

        let detector;
        let lastTime = performance.now();
        let threshold = 0.5;
        
        const inputCanvas = document.createElement('canvas');
        const inputCtx = inputCanvas.getContext('2d', { willReadFrequently: true });
        
        const COLORS = ['#3b82f6', '#ef4444', '#10b981', '#f59e0b', '#8b5cf6', '#ec4899'];

        slider.addEventListener('input', (e) => {
            threshold = parseFloat(e.target.value);
            sliderVal.textContent = threshold.toFixed(2);
        });
        
        // Handle high DPI displays
        function resizeOverlay() {
            const width = video.clientWidth;
            const height = video.clientHeight;
            const dpr = window.devicePixelRatio || 1;
            
            overlay.width = width * dpr;
            overlay.height = height * dpr;
            
            const ctx = overlay.getContext('2d');
            ctx.scale(dpr, dpr);
            
            inputCanvas.width = video.videoWidth;
            inputCanvas.height = video.videoHeight;
        }

        window.addEventListener('resize', resizeOverlay);

        // 1. Start Camera
        try {
            const stream = await navigator.mediaDevices.getUserMedia({
                video: {
                    facingMode: 'environment',
                    width: { ideal: 640 },
                    height: { ideal: 480 }
                },
                audio: false
            });
            
            video.srcObject = stream;
            await new Promise(r => video.onloadedmetadata = r);
            video.play();
            resizeOverlay();

        } catch (e) {
            statusText.textContent = "Camera Error";
            statusSub.textContent = e.message;
            document.querySelector('.spinner').style.display = 'none';
            throw e;
        }

        // 2. Load Model
        statusText.textContent = "Loading Model...";
        statusSub.textContent = "Downloading RF-DETR Nano (fp32)";
        
        try {
            detector = await pipeline('object-detection', 'onnx-community/rfdetr_nano-ONNX', {
                device: 'webgpu',
                dtype: 'fp32',
            });
            
            // 3. Warmup
            statusText.textContent = "Compiling Shaders...";
            statusSub.textContent = "This may take a moment";
            
            inputCtx.drawImage(video, 0, 0, inputCanvas.width, inputCanvas.height);
            await detector(inputCanvas, { threshold: 0.5, percentage: true });

            status.style.opacity = '0';
            setTimeout(() => status.style.display = 'none', 300);

        } catch (e) {
            statusText.textContent = "Model Error";
            statusSub.textContent = e.message;
            document.querySelector('.spinner').style.display = 'none';
            throw e;
        }

        // 4. Render Loop
        async function loop() {
            const now = performance.now();
            const dt = now - lastTime;
            lastTime = now;
            
            if (dt > 0) {
                fpsElem.textContent = `FPS: ${(1000 / dt).toFixed(1)}`;
            }

            inputCtx.drawImage(video, 0, 0, inputCanvas.width, inputCanvas.height);

            const results = await detector(inputCanvas, {
                threshold: threshold,
                percentage: true
            });
            drawResults(results);

            requestAnimationFrame(loop);
        }

        function drawResults(results) {
            const ctx = overlay.getContext('2d');
            const w = video.clientWidth;
            const h = video.clientHeight;
            
            // Clear canvas
            ctx.save();
            ctx.setTransform(1, 0, 0, 1, 0, 0);
            ctx.clearRect(0, 0, overlay.width, overlay.height);
            ctx.restore();

            // Set styles common to all results
            ctx.font = '600 13px system-ui';
            ctx.lineWidth = 2.5;

            results.forEach((res, i) => {
                const { box, label, score } = res;
                const color = COLORS[i % COLORS.length];

                const x1 = box.xmin * w;
                const y1 = box.ymin * h;
                const width = (box.xmax - box.xmin) * w;
                const height = (box.ymax - box.ymin) * h;

                // Box
                ctx.strokeStyle = color;
                ctx.beginPath();
                ctx.roundRect(x1, y1, width, height, 6);
                ctx.stroke();

                // Label
                ctx.fillStyle = color;
                const text = `${label} ${(score*100).toFixed(0)}%`;
                const textMetrics = ctx.measureText(text);
                const textWidth = textMetrics.width;
                const textHeight = 22;

                ctx.beginPath();
                ctx.roundRect(x1, y1 - textHeight - 4, textWidth + 12, textHeight, 4);
                ctx.fill();

                ctx.fillStyle = 'white';
                ctx.fillText(text, x1 + 6, y1 - 9);
            });
        }

        requestAnimationFrame(loop);

    </script>
</body>
</html>