All files / web/src/app/vision-training/train/components/data-panel MiniBoundaryTester.tsx

0% Statements 0/379
0% Branches 0/1
0% Functions 0/1
0% Lines 0/379

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
'use client'

import { useCallback, useEffect, useRef, useState } from 'react'
import Link from 'next/link'
import { css } from '../../../../../../styled-system/css'
import { useBoundaryDetector } from '@/hooks/useBoundaryDetector'
import type { QuadCorners } from '@/types/vision'

export interface MiniBoundaryTesterProps {
  /** URL of the image to test */
  imagePath: string
  /** Ground truth corners from the annotation */
  groundTruthCorners: QuadCorners
}

/**
 * Mini boundary detector tester for the detail panel.
 * Runs inference on the selected training image and shows results with visual overlay.
 */
export function MiniBoundaryTester({ imagePath, groundTruthCorners }: MiniBoundaryTesterProps) {
  const [result, setResult] = useState<{
    corners: QuadCorners
    confidence: number
  } | null>(null)
  const [error, setError] = useState<string | null>(null)
  const [isRunning, setIsRunning] = useState(false)
  const [hasRun, setHasRun] = useState(false)
  const imageRef = useRef<HTMLImageElement | null>(null)

  const detector = useBoundaryDetector({ enabled: true })

  // Reset state when image changes
  useEffect(() => {
    setResult(null)
    setError(null)
    setHasRun(false)
  }, [imagePath])

  const runInference = useCallback(async () => {
    if (!imageRef.current || isRunning) return

    setIsRunning(true)
    setError(null)

    try {
      // Ensure model is loaded
      if (!detector.isReady) {
        await detector.preload()
      }

      const detectionResult = await detector.detectFromImage(imageRef.current)

      if (!detectionResult) {
        throw new Error('Detection failed - model may not be available')
      }

      setResult({
        corners: detectionResult.corners,
        confidence: detectionResult.confidence,
      })
      setHasRun(true)
    } catch (err) {
      setError(err instanceof Error ? err.message : 'Inference failed')
    } finally {
      setIsRunning(false)
    }
  }, [detector, isRunning])

  // Calculate corner error (average distance between predicted and ground truth)
  const calculateError = useCallback((): number | null => {
    if (!result) return null

    const corners = ['topLeft', 'topRight', 'bottomLeft', 'bottomRight'] as const
    let totalError = 0

    for (const corner of corners) {
      const dx = result.corners[corner].x - groundTruthCorners[corner].x
      const dy = result.corners[corner].y - groundTruthCorners[corner].y
      totalError += Math.sqrt(dx * dx + dy * dy)
    }

    return totalError / 4
  }, [result, groundTruthCorners])

  const avgError = calculateError()

  // Get confidence color
  const getConfidenceColor = (conf: number) => {
    if (conf >= 0.8) return 'green.400'
    if (conf >= 0.5) return 'yellow.400'
    return 'red.400'
  }

  // Get error color (in normalized units, ~0.02 is very good, ~0.1 is bad)
  const getErrorColor = (err: number) => {
    if (err <= 0.02) return 'green.400'
    if (err <= 0.05) return 'yellow.400'
    return 'red.400'
  }

  return (
    <div
      data-component="mini-boundary-tester"
      className={css({
        p: 3,
        bg: 'gray.900',
        borderRadius: 'md',
        border: '1px solid',
        borderColor: 'purple.700/50',
      })}
    >
      <div
        className={css({
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'space-between',
          mb: 3,
        })}
      >
        <div className={css({ display: 'flex', alignItems: 'center', gap: 2 })}>
          <span className={css({ fontSize: 'sm' })}>๐Ÿงช</span>
          <span
            className={css({
              fontSize: 'sm',
              fontWeight: 'medium',
              color: 'purple.300',
            })}
          >
            Model Tester
          </span>
        </div>
        <span className={css({ fontSize: 'xs', color: 'gray.500' })}>
          {detector.isReady ? (
            <span className={css({ color: 'green.400' })}>Ready</span>
          ) : detector.isLoading ? (
            <span className={css({ color: 'yellow.400' })}>Loading...</span>
          ) : detector.isUnavailable ? (
            <span className={css({ color: 'red.400' })}>Unavailable</span>
          ) : (
            <span className={css({ color: 'gray.400' })}>Not loaded</span>
          )}
        </span>
      </div>

      {/* Visual preview with corner overlay */}
      <div
        className={css({
          position: 'relative',
          width: '100%',
          aspectRatio: '4/3',
          bg: 'gray.800',
          borderRadius: 'md',
          overflow: 'hidden',
          mb: 3,
        })}
      >
        {/* eslint-disable-next-line @next/next/no-img-element */}
        <img
          ref={imageRef}
          src={imagePath}
          alt="Frame for inference"
          crossOrigin="anonymous"
          className={css({
            width: '100%',
            height: '100%',
            objectFit: 'contain',
          })}
        />

        {/* Corner overlay SVG */}
        {result && (
          <svg
            viewBox="0 0 100 100"
            preserveAspectRatio="none"
            className={css({
              position: 'absolute',
              inset: 0,
              width: '100%',
              height: '100%',
              pointerEvents: 'none',
            })}
          >
            {/* Ground truth quadrilateral (green, dashed) */}
            <polygon
              points={`${groundTruthCorners.topLeft.x * 100},${groundTruthCorners.topLeft.y * 100} ${groundTruthCorners.topRight.x * 100},${groundTruthCorners.topRight.y * 100} ${groundTruthCorners.bottomRight.x * 100},${groundTruthCorners.bottomRight.y * 100} ${groundTruthCorners.bottomLeft.x * 100},${groundTruthCorners.bottomLeft.y * 100}`}
              fill="none"
              stroke="rgba(34, 197, 94, 0.8)"
              strokeWidth="0.5"
              strokeDasharray="2,1"
            />

            {/* Predicted quadrilateral (purple, solid) */}
            <polygon
              points={`${result.corners.topLeft.x * 100},${result.corners.topLeft.y * 100} ${result.corners.topRight.x * 100},${result.corners.topRight.y * 100} ${result.corners.bottomRight.x * 100},${result.corners.bottomRight.y * 100} ${result.corners.bottomLeft.x * 100},${result.corners.bottomLeft.y * 100}`}
              fill="rgba(147, 51, 234, 0.15)"
              stroke="rgba(147, 51, 234, 0.9)"
              strokeWidth="0.5"
            />

            {/* Ground truth corner markers (green squares) */}
            {(['topLeft', 'topRight', 'bottomLeft', 'bottomRight'] as const).map((corner) => (
              <rect
                key={`gt-${corner}`}
                x={groundTruthCorners[corner].x * 100 - 1}
                y={groundTruthCorners[corner].y * 100 - 1}
                width="2"
                height="2"
                fill="#22c55e"
                stroke="white"
                strokeWidth="0.3"
              />
            ))}

            {/* Predicted corner markers (purple circles) */}
            {(['topLeft', 'topRight', 'bottomLeft', 'bottomRight'] as const).map((corner) => (
              <circle
                key={`pred-${corner}`}
                cx={result.corners[corner].x * 100}
                cy={result.corners[corner].y * 100}
                r="1.5"
                fill="#a855f7"
                stroke="white"
                strokeWidth="0.3"
              />
            ))}

            {/* Error lines connecting ground truth to predicted */}
            {(['topLeft', 'topRight', 'bottomLeft', 'bottomRight'] as const).map((corner) => (
              <line
                key={`error-${corner}`}
                x1={groundTruthCorners[corner].x * 100}
                y1={groundTruthCorners[corner].y * 100}
                x2={result.corners[corner].x * 100}
                y2={result.corners[corner].y * 100}
                stroke="rgba(239, 68, 68, 0.8)"
                strokeWidth="0.3"
              />
            ))}
          </svg>
        )}

        {/* Legend overlay */}
        {result && (
          <div
            className={css({
              position: 'absolute',
              bottom: 1,
              left: 1,
              display: 'flex',
              gap: 2,
              px: 1.5,
              py: 0.5,
              bg: 'black/70',
              borderRadius: 'sm',
              fontSize: '10px',
            })}
          >
            <span className={css({ color: 'green.400' })}>โ–  Ground Truth</span>
            <span className={css({ color: 'purple.400' })}>โ— Predicted</span>
          </div>
        )}
      </div>

      {/* Run button */}
      <button
        type="button"
        onClick={runInference}
        disabled={isRunning || detector.isLoading || detector.isUnavailable}
        className={css({
          w: '100%',
          py: 2,
          bg: hasRun ? 'gray.700' : 'purple.600',
          color: 'white',
          borderRadius: 'md',
          border: 'none',
          cursor: 'pointer',
          fontWeight: 'medium',
          fontSize: 'sm',
          _hover: { bg: hasRun ? 'gray.600' : 'purple.500' },
          _disabled: { opacity: 0.5, cursor: 'not-allowed' },
        })}
      >
        {isRunning ? 'Running...' : hasRun ? 'Run Again' : 'Test Model'}
      </button>

      {/* Error display */}
      {error && (
        <div
          className={css({
            mt: 2,
            p: 2,
            bg: 'red.900/30',
            border: '1px solid',
            borderColor: 'red.700',
            borderRadius: 'md',
            color: 'red.300',
            fontSize: 'xs',
          })}
        >
          {error}
        </div>
      )}

      {/* Results summary */}
      {result && (
        <div
          className={css({
            mt: 3,
            display: 'grid',
            gridTemplateColumns: '1fr 1fr',
            gap: 2,
          })}
        >
          <div
            className={css({
              p: 2,
              bg: 'gray.800',
              borderRadius: 'md',
              textAlign: 'center',
            })}
          >
            <div className={css({ fontSize: 'xs', color: 'gray.400', mb: 1 })}>Confidence</div>
            <div
              className={css({
                fontSize: 'lg',
                fontWeight: 'bold',
                fontFamily: 'mono',
                color: getConfidenceColor(result.confidence),
              })}
            >
              {(result.confidence * 100).toFixed(1)}%
            </div>
          </div>
          <div
            className={css({
              p: 2,
              bg: 'gray.800',
              borderRadius: 'md',
              textAlign: 'center',
            })}
          >
            <div className={css({ fontSize: 'xs', color: 'gray.400', mb: 1 })}>Avg Error</div>
            <div
              className={css({
                fontSize: 'lg',
                fontWeight: 'bold',
                fontFamily: 'mono',
                color: avgError !== null ? getErrorColor(avgError) : 'gray.400',
              })}
            >
              {avgError !== null ? `${(avgError * 100).toFixed(1)}%` : '-'}
            </div>
          </div>
        </div>
      )}

      {/* Full screen link */}
      <Link
        href={`/vision-training/boundary-detector/test?image=${encodeURIComponent(imagePath)}`}
        className={css({
          display: 'block',
          mt: 3,
          py: 2,
          textAlign: 'center',
          bg: 'gray.800',
          color: 'purple.300',
          borderRadius: 'md',
          border: '1px solid',
          borderColor: 'gray.700',
          textDecoration: 'none',
          fontSize: 'sm',
          _hover: { bg: 'gray.700', borderColor: 'purple.600' },
        })}
      >
        Open Full Tester โ†’
      </Link>
    </div>
  )
}