All files / web/src/app/vision-training/train/components TrainingDataCapture.tsx

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

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
'use client'

import { useCallback, useRef, useState } from 'react'
import { css } from '../../../../../styled-system/css'
import { CameraCapture, type CameraSource } from '@/components/vision/CameraCapture'
import type { CalibrationGrid } from '@/types/vision'

interface TrainingDataCaptureProps {
  /** Called when samples are saved successfully */
  onSamplesCollected: () => void
  /** Number of physical abacus columns (default 4) */
  columnCount?: number
}

/**
 * Inline training data capture component for the training wizard
 *
 * Uses the reusable CameraCapture component which supports both
 * local device camera and phone camera via QR code.
 *
 * Captures abacus images and saves them as training data for the column classifier.
 */
export function TrainingDataCapture({
  onSamplesCollected,
  columnCount = 4,
}: TrainingDataCaptureProps) {
  // Capture state
  const [inputValue, setInputValue] = useState('')
  const [isCapturing, setIsCapturing] = useState(false)
  const [lastCaptureStatus, setLastCaptureStatus] = useState<{
    success: boolean
    message: string
  } | null>(null)
  const [captureCount, setCaptureCount] = useState(0)
  const [isPhoneConnected, setIsPhoneConnected] = useState(false)
  const [cameraSource, setCameraSource] = useState<CameraSource>('local')
  const [calibration, setCalibration] = useState<CalibrationGrid | null>(null)

  const inputRef = useRef<HTMLInputElement>(null)
  const captureElementRef = useRef<HTMLCanvasElement | HTMLImageElement | HTMLVideoElement | null>(
    null
  )

  // Handle capture from camera
  const handleCapture = useCallback(
    (element: HTMLCanvasElement | HTMLImageElement | HTMLVideoElement) => {
      captureElementRef.current = element
    },
    []
  )

  // Capture training data
  const captureTrainingData = useCallback(async () => {
    const value = parseInt(inputValue, 10)
    if (Number.isNaN(value) || value < 0) {
      setLastCaptureStatus({
        success: false,
        message: 'Enter a valid non-negative number',
      })
      return
    }

    // Get the current capture element
    const element = captureElementRef.current
    if (!element) {
      setLastCaptureStatus({
        success: false,
        message: 'No camera frame available',
      })
      return
    }

    // For video, check if it's playing
    if (element instanceof HTMLVideoElement) {
      if (element.readyState < 2) {
        setLastCaptureStatus({ success: false, message: 'Camera not ready' })
        return
      }
    }

    // For image, check if it's loaded
    if (element instanceof HTMLImageElement) {
      if (!element.complete || element.naturalWidth === 0) {
        setLastCaptureStatus({
          success: false,
          message: 'Camera frame not ready',
        })
        return
      }
    }

    setIsCapturing(true)
    setLastCaptureStatus(null)

    try {
      // Import frame processor dynamically
      const { processImageFrame } = await import('@/lib/vision/frameProcessor')
      const { imageDataToBase64Png } = await import('@/lib/vision/trainingData')

      // For video/canvas elements, we need to draw to a temp image first
      let imageElement: HTMLImageElement
      if (element instanceof HTMLVideoElement || element instanceof HTMLCanvasElement) {
        // Create a canvas to capture the frame
        const canvas = document.createElement('canvas')
        if (element instanceof HTMLVideoElement) {
          canvas.width = element.videoWidth
          canvas.height = element.videoHeight
        } else {
          canvas.width = element.width
          canvas.height = element.height
        }
        const ctx = canvas.getContext('2d')
        if (!ctx) {
          throw new Error('Failed to create canvas context')
        }
        ctx.drawImage(element, 0, 0)

        // Convert canvas to image
        imageElement = new Image()
        imageElement.src = canvas.toDataURL('image/jpeg')
        await new Promise((resolve, reject) => {
          imageElement.onload = resolve
          imageElement.onerror = reject
        })
      } else {
        imageElement = element
      }

      // Slice the image into columns (using calibration if available for perspective correction)
      const columnImages = processImageFrame(imageElement, calibration, columnCount)

      if (columnImages.length === 0) {
        throw new Error('Failed to slice image into columns')
      }

      // Convert to base64 and prepare request
      const columns = columnImages.map((imgData: ImageData, index: number) => ({
        columnIndex: index,
        imageData: imageDataToBase64Png(imgData),
      }))

      // Send to collect API
      const response = await fetch('/api/vision-training/collect', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          columns,
          correctAnswer: value,
          playerId: 'training-wizard',
          sessionId: 'manual-capture',
        }),
      })

      const result = await response.json()

      if (result.success) {
        setCaptureCount((c) => c + 1)
        setLastCaptureStatus({
          success: true,
          message: `Saved ${result.savedCount} columns for "${value}"`,
        })
        setInputValue('')
        // Focus input for next capture
        inputRef.current?.focus()
        onSamplesCollected()
      } else {
        throw new Error(result.error || 'Failed to save')
      }
    } catch (error) {
      console.error('[TrainingDataCapture] Error:', error)
      setLastCaptureStatus({
        success: false,
        message: error instanceof Error ? error.message : 'Failed to capture',
      })
    } finally {
      setIsCapturing(false)
    }
  }, [inputValue, columnCount, calibration, onSamplesCollected])

  // Handle keyboard shortcut (Enter to capture)
  const handleKeyDown = useCallback(
    (e: React.KeyboardEvent) => {
      if (e.key === 'Enter' && !isCapturing && inputValue) {
        captureTrainingData()
      }
    },
    [captureTrainingData, isCapturing, inputValue]
  )

  // Determine if capture is possible
  const canCapture = cameraSource === 'local' || isPhoneConnected

  return (
    <div
      data-component="training-data-capture"
      className={css({
        p: 3,
        bg: 'blue.900/20',
        border: '1px solid',
        borderColor: 'blue.700/50',
        borderRadius: 'lg',
      })}
    >
      {/* Header */}
      <div
        className={css({
          display: 'flex',
          alignItems: 'center',
          gap: 2,
          mb: 3,
        })}
      >
        <span>📸</span>
        <span className={css({ fontWeight: 'medium', color: 'blue.300' })}>
          Capture Training Data
        </span>
        {captureCount > 0 && (
          <span className={css({ fontSize: 'xs', color: 'green.400', ml: 'auto' })}>
            +{captureCount} this session
          </span>
        )}
      </div>

      {/* Camera capture component */}
      <CameraCapture
        initialSource="local"
        onCapture={handleCapture}
        onSourceChange={setCameraSource}
        onPhoneConnected={setIsPhoneConnected}
        compact
        enableMarkerDetection
        columnCount={columnCount}
        onCalibrationChange={setCalibration}
        showRectifiedView
      />

      {/* Capture controls - show when camera is ready */}
      {canCapture && (
        <div className={css({ mt: 3 })}>
          <div className={css({ display: 'flex', gap: 2, mb: 2 })}>
            <input
              ref={inputRef}
              type="number"
              min="0"
              placeholder={`Number (${columnCount} columns)`}
              value={inputValue}
              onChange={(e) => setInputValue(e.target.value)}
              onKeyDown={handleKeyDown}
              disabled={isCapturing}
              className={css({
                flex: 1,
                px: 3,
                py: 2,
                bg: 'gray.700',
                border: '1px solid',
                borderColor: 'gray.600',
                borderRadius: 'md',
                color: 'gray.100',
                fontSize: 'md',
                fontFamily: 'mono',
                _placeholder: { color: 'gray.500' },
                _focus: { outline: 'none', borderColor: 'blue.500' },
                _disabled: { opacity: 0.5 },
              })}
            />
            <button
              type="button"
              onClick={captureTrainingData}
              disabled={isCapturing || !inputValue}
              className={css({
                px: 4,
                py: 2,
                bg: 'green.600',
                color: 'white',
                borderRadius: 'md',
                border: 'none',
                cursor: 'pointer',
                fontWeight: 'medium',
                whiteSpace: 'nowrap',
                _hover: { bg: 'green.500' },
                _disabled: { opacity: 0.5, cursor: 'not-allowed' },
              })}
            >
              {isCapturing ? '...' : 'Capture'}
            </button>
          </div>

          {/* Status message */}
          {lastCaptureStatus && (
            <div
              className={css({
                fontSize: 'sm',
                color: lastCaptureStatus.success ? 'green.400' : 'red.400',
                display: 'flex',
                alignItems: 'center',
                gap: 1,
              })}
            >
              {lastCaptureStatus.success ? '✓' : '✗'} {lastCaptureStatus.message}
            </div>
          )}
        </div>
      )}

      {/* Instructions */}
      <div className={css({ fontSize: 'xs', color: 'gray.500', mt: 3 })}>
        <p>1. Point camera at your abacus with printed markers</p>
        <p>2. Wait for all 4 markers to be detected (green dots)</p>
        <p>3. Set beads to show a number</p>
        <p>4. Type that number and press Capture (or Enter)</p>
      </div>

      {/* Calibration status */}
      {calibration && (
        <div
          className={css({
            fontSize: 'xs',
            color: 'green.400',
            mt: 2,
            display: 'flex',
            alignItems: 'center',
            gap: 1,
          })}
        >
          <span>✓</span>
          <span>Markers detected - using perspective correction</span>
        </div>
      )}
    </div>
  )
}