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 | 'use client' import { useCallback, useEffect, useRef, useState } from 'react' import { detectBoundary, isModelLoaded, isModelUnavailable, preloadModel, resetModelState, type BoundaryDetectionResult, } from '@/lib/vision/boundaryDetector' import type { QuadCorners, CalibrationGrid } from '@/types/vision' export interface UseBoundaryDetectorOptions { /** Whether detection should be active */ enabled?: boolean /** Number of abacus columns (for calibration grid) */ columnCount?: number /** Called when corners are detected with good confidence */ onCornersDetected?: (corners: QuadCorners) => void /** Minimum confidence threshold (0-1) to accept detection */ confidenceThreshold?: number /** Number of consecutive stable frames required */ stabilityFrames?: number } export interface UseBoundaryDetectorReturn { /** Whether the model is loaded and ready */ isReady: boolean /** Whether the model is currently loading */ isLoading: boolean /** Whether the model is unavailable (not trained) */ isUnavailable: boolean /** Error message if any */ error: string | null /** Current detected corners (if stable) */ detectedCorners: QuadCorners | null /** Current detection confidence */ confidence: number /** Number of consecutive frames with same detection */ consecutiveFrames: number /** Detect corners from a video element */ detectFromVideo: (video: HTMLVideoElement) => Promise<BoundaryDetectionResult | null> /** Detect corners from an image element */ detectFromImage: (image: HTMLImageElement) => Promise<BoundaryDetectionResult | null> /** Create a CalibrationGrid from the current detection */ createCalibrationGrid: () => CalibrationGrid | null /** Preload the model */ preload: () => Promise<boolean> /** Reset model state (allows retrying after update) */ resetModel: () => void } /** * Hook for using the boundary detector ML model * * Provides lazy-loading of the TensorFlow.js model and stable detection * of abacus corners from video or image frames. */ export function useBoundaryDetector( options: UseBoundaryDetectorOptions = {} ): UseBoundaryDetectorReturn { const { enabled = false, columnCount = 5, onCornersDetected, confidenceThreshold = 0.7, stabilityFrames = 3, } = options const [isReady, setIsReady] = useState(false) const [isLoading, setIsLoading] = useState(false) const [isUnavailable, setIsUnavailable] = useState(false) const [error, setError] = useState<string | null>(null) const [detectedCorners, setDetectedCorners] = useState<QuadCorners | null>(null) const [confidence, setConfidence] = useState(0) const [consecutiveFrames, setConsecutiveFrames] = useState(0) // Stability tracking const lastCornersRef = useRef<QuadCorners | null>(null) const consecutiveFramesRef = useRef(0) const canvasRef = useRef<HTMLCanvasElement | null>(null) // Create canvas for extracting image data useEffect(() => { if (!canvasRef.current) { canvasRef.current = document.createElement('canvas') } }, []) // Preload model when enabled useEffect(() => { if (enabled && !isReady && !isLoading && !isUnavailable) { preload() } }, [enabled, isReady, isLoading, isUnavailable]) /** * Preload the boundary detector model */ const preload = useCallback(async (): Promise<boolean> => { if (isModelLoaded()) { setIsReady(true) return true } if (isModelUnavailable()) { setIsUnavailable(true) return false } setIsLoading(true) setError(null) try { const success = await preloadModel() setIsReady(success) setIsUnavailable(!success) return success } catch (err) { const message = err instanceof Error ? err.message : 'Failed to load model' setError(message) setIsUnavailable(true) return false } finally { setIsLoading(false) } }, []) /** * Reset model state to allow retrying */ const resetModel = useCallback(() => { resetModelState() setIsReady(false) setIsLoading(false) setIsUnavailable(false) setError(null) }, []) /** * Extract ImageData from a video element */ const getImageDataFromVideo = useCallback((video: HTMLVideoElement): ImageData | null => { if (!canvasRef.current || video.readyState < 2) return null const canvas = canvasRef.current canvas.width = video.videoWidth canvas.height = video.videoHeight const ctx = canvas.getContext('2d') if (!ctx) return null ctx.drawImage(video, 0, 0) return ctx.getImageData(0, 0, canvas.width, canvas.height) }, []) /** * Extract ImageData from an image element */ const getImageDataFromImage = useCallback((image: HTMLImageElement): ImageData | null => { if (!canvasRef.current || !image.complete) return null const canvas = canvasRef.current canvas.width = image.naturalWidth canvas.height = image.naturalHeight const ctx = canvas.getContext('2d') if (!ctx) return null ctx.drawImage(image, 0, 0) return ctx.getImageData(0, 0, canvas.width, canvas.height) }, []) /** * Check if two corner sets are "similar" (for stability detection) */ const cornersAreSimilar = useCallback( (a: QuadCorners, b: QuadCorners, threshold = 0.02): boolean => { const corners = ['topLeft', 'topRight', 'bottomLeft', 'bottomRight'] as const for (const corner of corners) { const dx = Math.abs(a[corner].x - b[corner].x) const dy = Math.abs(a[corner].y - b[corner].y) if (dx > threshold || dy > threshold) { return false } } return true }, [] ) /** * Update stability tracking */ const updateStability = useCallback( (corners: QuadCorners | null, conf: number) => { if (!corners || conf < confidenceThreshold) { // Reset stability consecutiveFramesRef.current = 0 lastCornersRef.current = null setConsecutiveFrames(0) return } if (lastCornersRef.current && cornersAreSimilar(corners, lastCornersRef.current)) { // Similar to last detection - increment stability consecutiveFramesRef.current++ setConsecutiveFrames(consecutiveFramesRef.current) if (consecutiveFramesRef.current >= stabilityFrames) { // Stable detection achieved setDetectedCorners(corners) onCornersDetected?.(corners) } } else { // Different from last detection - reset consecutiveFramesRef.current = 1 lastCornersRef.current = corners setConsecutiveFrames(1) } setConfidence(conf) }, [confidenceThreshold, stabilityFrames, cornersAreSimilar, onCornersDetected] ) /** * Detect corners from a video element */ const detectFromVideo = useCallback( async (video: HTMLVideoElement): Promise<BoundaryDetectionResult | null> => { const imageData = getImageDataFromVideo(video) if (!imageData) return null const result = await detectBoundary(imageData) if (result) { updateStability(result.corners, result.confidence) } return result }, [getImageDataFromVideo, updateStability] ) /** * Detect corners from an image element */ const detectFromImage = useCallback( async (image: HTMLImageElement): Promise<BoundaryDetectionResult | null> => { const imageData = getImageDataFromImage(image) if (!imageData) return null const result = await detectBoundary(imageData) if (result) { updateStability(result.corners, result.confidence) } return result }, [getImageDataFromImage, updateStability] ) /** * Create a CalibrationGrid from the current stable detection */ const createCalibrationGrid = useCallback((): CalibrationGrid | null => { if (!detectedCorners) return null // Calculate ROI from corners const left = Math.min(detectedCorners.topLeft.x, detectedCorners.bottomLeft.x) const right = Math.max(detectedCorners.topRight.x, detectedCorners.bottomRight.x) const top = Math.min(detectedCorners.topLeft.y, detectedCorners.topRight.y) const bottom = Math.max(detectedCorners.bottomLeft.y, detectedCorners.bottomRight.y) return { roi: { x: left, y: top, width: right - left, height: bottom - top, }, corners: detectedCorners, columnCount, columnDividers: Array.from({ length: columnCount - 1 }, (_, i) => (i + 1) / columnCount), rotation: 0, } }, [detectedCorners, columnCount]) return { isReady, isLoading, isUnavailable, error, detectedCorners, confidence, consecutiveFrames, detectFromVideo, detectFromImage, createCalibrationGrid, preload, resetModel, } } |