File size: 3,988 Bytes
1fc4624
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be69165
 
 
1fc4624
be69165
 
 
 
 
 
 
1fc4624
 
be69165
 
1fc4624
386d682
 
 
 
1fc4624
386d682
 
 
 
 
 
 
 
 
 
 
1fc4624
386d682
 
 
 
1fc4624
386d682
1fc4624
386d682
1fc4624
90504b4
 
386d682
 
90504b4
1fc4624
 
386d682
 
1fc4624
386d682
 
 
 
1fc4624
386d682
 
1fc4624
386d682
1fc4624
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useRef, useEffect } from 'react';

const SEVERITY_COLORS = {
    minor:    '#EAB308',  // yellow
    moderate: '#F97316',  // orange
    severe:   '#EF4444',  // red
};

const getColor = (severity) =>
    SEVERITY_COLORS[severity?.toLowerCase()] ?? '#3B82F6';

const ImageOverlay = ({ imageUrl, detections = [] }) => {
    const imgRef  = useRef(null);
    const canvasRef = useRef(null);

    const drawBoxes = () => {
        const img    = imgRef.current;
        const canvas = canvasRef.current;
        if (!img || !canvas || !detections.length) return;

        const dpr    = window.devicePixelRatio || 1;
        const cssW   = img.clientWidth;
        const cssH   = img.clientHeight;

        canvas.width  = cssW * dpr;
        canvas.height = cssH * dpr;
        canvas.style.width  = `${cssW}px`;
        canvas.style.height = `${cssH}px`;

        const scaleX = cssW / img.naturalWidth;
        const scaleY = cssH / img.naturalHeight;

        const ctx = canvas.getContext('2d');
        ctx.scale(dpr, dpr);
        ctx.clearRect(0, 0, cssW, cssH);

        // Sort ascending by confidence so highest-confidence box is drawn last (on top)
        const sorted = [...detections]
            .filter(d => d.bbox)
            .sort((a, b) => (a.confidence ?? 0) - (b.confidence ?? 0));

        const scaled = sorted.map((det) => {
            const [x1, y1, x2, y2] = det.bbox;
            return {
                det,
                sx: x1 * scaleX,
                sy: y1 * scaleY,
                sw: (x2 - x1) * scaleX,
                sh: (y2 - y1) * scaleY,
                color: getColor(det.severity),
            };
        });

        // Pass 1: draw all box fills + strokes
        scaled.forEach(({ sx, sy, sw, sh, color }) => {
            ctx.fillStyle = color + '22';  // ~13% opacity fill
            ctx.fillRect(sx, sy, sw, sh);
            ctx.strokeStyle = color;
            ctx.lineWidth = 2;
            ctx.strokeRect(sx, sy, sw, sh);
        });

        // Pass 2: draw labels for top 3 by confidence only — avoids clutter with many detections
        const topThree = [...scaled].sort((a, b) => (b.det.confidence ?? 0) - (a.det.confidence ?? 0)).slice(0, 3);
        ctx.font = 'bold 11px sans-serif';
        const padX = 4, padY = 3, textH = 11;
        topThree.forEach(({ det, sx, sy, color }) => {
            const label = `${det.part} — ${det.damage_type}`;
            const textW = ctx.measureText(label).width;
            const bgW = textW + padX * 2;
            const bgH = textH + padY * 2;

            // Place label above box if space, otherwise inside top of box
            const labelY = sy > bgH + 2 ? sy - bgH - 2 : sy + 2;
            // Clamp horizontally so label doesn't run off right edge
            const labelX = Math.min(sx, cssW - bgW - 2);

            ctx.fillStyle = color;
            ctx.fillRect(labelX, labelY, bgW, bgH);
            ctx.fillStyle = '#000000';
            ctx.fillText(label, labelX + padX, labelY + textH + padY - 2);
        });
    };

    useEffect(() => {
        const img = imgRef.current;
        if (!img) return;
        if (img.complete) {
            drawBoxes();
        } else {
            img.addEventListener('load', drawBoxes);
            return () => img.removeEventListener('load', drawBoxes);
        }
    // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [imageUrl, detections]);

    if (!imageUrl) return null;

    return (
        <div className="relative w-full rounded-xl overflow-hidden border border-gray-700">
            <img
                ref={imgRef}
                src={imageUrl}
                alt="Uploaded vehicle"
                className="w-full block"
                onLoad={drawBoxes}
            />
            <canvas
                ref={canvasRef}
                className="absolute inset-0 w-full h-full pointer-events-none"
            />
        </div>
    );
};

export default ImageOverlay;