Spaces:
Build error
Build error
Hkoradiya123
feat: Initialize frontend with Zustand for state management, Tailwind CSS for styling, and TypeScript for type safety
4854e2b Download frontend/src/components/LiveRecognitionComponent.tsx from jinksQspider/fmproService: direct link, hf CLI and curl.
- Browser
- Download file 12.6 kB
-
https://huggingface.co/spaces/jinksQspider/fmproService/resolve/main/frontend/src/components/LiveRecognitionComponent.tsx
- Command line
-
hf download hf://spaces/jinksQspider/fmproService/frontend/src/components/LiveRecognitionComponent.tsx
-
curl -L -o LiveRecognitionComponent.tsx https://huggingface.co/spaces/jinksQspider/fmproService/resolve/main/frontend/src/components/LiveRecognitionComponent.tsx
12.6 kB
| import React, { useCallback, useEffect, useRef, useState } from 'react'; | |
| import api from '../services/api'; | |
| import { FaceRecognitionResult } from '../types'; | |
| export const LiveRecognitionComponent: React.FC = () => { | |
| const videoRef = useRef<HTMLVideoElement>(null); | |
| const canvasRef = useRef<HTMLCanvasElement>(null); | |
| const recognitionIntervalRef = useRef<number | null>(null); | |
| const isProcessingRef = useRef(false); | |
| const thresholdRef = useRef(0.5); | |
| const [isRunning, setIsRunning] = useState(false); | |
| const [isCameraReady, setIsCameraReady] = useState(false); | |
| const [error, setError] = useState(''); | |
| const [results, setResults] = useState<FaceRecognitionResult | null>(null); | |
| const [isProcessing, setIsProcessing] = useState(false); | |
| const [threshold, setThreshold] = useState(0.5); | |
| const [devices, setDevices] = useState<MediaDeviceInfo[]>([]); | |
| const [selectedDeviceId, setSelectedDeviceId] = useState<string>(''); | |
| useEffect(() => { | |
| thresholdRef.current = threshold; | |
| }, [threshold]); | |
| const setProcessingState = (value: boolean) => { | |
| isProcessingRef.current = value; | |
| setIsProcessing(value); | |
| }; | |
| const loadDevices = useCallback(async () => { | |
| if (!navigator.mediaDevices?.enumerateDevices) { | |
| return; | |
| } | |
| const list = await navigator.mediaDevices.enumerateDevices(); | |
| const videoDevices = list.filter((device) => device.kind === 'videoinput'); | |
| setDevices(videoDevices); | |
| if (!selectedDeviceId && videoDevices.length > 0) { | |
| setSelectedDeviceId(videoDevices[0].deviceId); | |
| } | |
| }, [selectedDeviceId]); | |
| const initializeCamera = useCallback(async () => { | |
| if (!navigator.mediaDevices?.getUserMedia) { | |
| setError('This browser does not support webcam access.'); | |
| return; | |
| } | |
| try { | |
| setError(''); | |
| const stream = await navigator.mediaDevices.getUserMedia({ | |
| video: selectedDeviceId | |
| ? { deviceId: { exact: selectedDeviceId }, width: { ideal: 960 }, height: { ideal: 540 } } | |
| : { facingMode: 'user', width: { ideal: 960 }, height: { ideal: 540 } }, | |
| audio: false, | |
| }); | |
| if (videoRef.current) { | |
| videoRef.current.srcObject = stream; | |
| await videoRef.current.play(); | |
| setIsCameraReady(true); | |
| } | |
| await loadDevices(); | |
| } catch (err) { | |
| setError('Failed to access webcam. Check browser permissions and HTTPS/localhost access.'); | |
| console.error(err); | |
| } | |
| }, [loadDevices, selectedDeviceId]); | |
| const processFrame = useCallback(async () => { | |
| const video = videoRef.current; | |
| const canvas = canvasRef.current; | |
| if (isProcessingRef.current || !video || !canvas || video.readyState < HTMLMediaElement.HAVE_CURRENT_DATA) { | |
| return; | |
| } | |
| setProcessingState(true); | |
| try { | |
| const context = canvas.getContext('2d'); | |
| if (!context || video.videoWidth === 0 || video.videoHeight === 0) { | |
| return; | |
| } | |
| canvas.width = video.videoWidth; | |
| canvas.height = video.videoHeight; | |
| context.drawImage(video, 0, 0, canvas.width, canvas.height); | |
| const blob = await new Promise<Blob | null>((resolve) => | |
| canvas.toBlob((value) => resolve(value), 'image/jpeg', 0.82) | |
| ); | |
| if (!blob) { | |
| setError('Could not capture a camera frame.'); | |
| return; | |
| } | |
| const formData = new FormData(); | |
| formData.append('image', blob, 'frame.jpg'); | |
| formData.append('threshold', thresholdRef.current.toString()); | |
| formData.append('return_bboxes', 'true'); | |
| const result = await api.recognizeFace(formData); | |
| setResults(result); | |
| } catch (err: any) { | |
| setError(err.response?.data?.detail || 'Recognition request failed.'); | |
| console.error('Recognition error:', err); | |
| } finally { | |
| setProcessingState(false); | |
| } | |
| }, []); | |
| const startRecognition = useCallback(() => { | |
| if (!videoRef.current || !canvasRef.current || !isCameraReady || recognitionIntervalRef.current) return; | |
| setError(''); | |
| setIsRunning(true); | |
| processFrame(); | |
| recognitionIntervalRef.current = window.setInterval(processFrame, 1200); | |
| }, [isCameraReady, processFrame]); | |
| const stopRecognition = useCallback(() => { | |
| if (recognitionIntervalRef.current) { | |
| window.clearInterval(recognitionIntervalRef.current); | |
| recognitionIntervalRef.current = null; | |
| } | |
| setIsRunning(false); | |
| }, []); | |
| const stopCamera = useCallback(() => { | |
| stopRecognition(); | |
| const stream = videoRef.current?.srcObject as MediaStream | null; | |
| stream?.getTracks().forEach((track) => track.stop()); | |
| if (videoRef.current) { | |
| videoRef.current.srcObject = null; | |
| } | |
| setIsCameraReady(false); | |
| setResults(null); | |
| setProcessingState(false); | |
| }, [stopRecognition]); | |
| useEffect(() => stopCamera, [stopCamera]); | |
| useEffect(() => { | |
| loadDevices(); | |
| }, [loadDevices]); | |
| const handleDeviceChange = async (event: React.ChangeEvent<HTMLSelectElement>) => { | |
| const nextId = event.target.value; | |
| setSelectedDeviceId(nextId); | |
| if (isCameraReady) { | |
| stopCamera(); | |
| await initializeCamera(); | |
| } | |
| }; | |
| const handleUpload = async (event: React.ChangeEvent<HTMLInputElement>) => { | |
| const file = event.target.files?.[0]; | |
| if (!file) { | |
| return; | |
| } | |
| setProcessingState(true); | |
| setError(''); | |
| try { | |
| const formData = new FormData(); | |
| formData.append('image', file, file.name || 'capture.jpg'); | |
| formData.append('threshold', thresholdRef.current.toString()); | |
| formData.append('return_bboxes', 'true'); | |
| const result = await api.recognizeFace(formData); | |
| setResults(result); | |
| } catch (err: any) { | |
| setError(err.response?.data?.detail || 'Recognition request failed.'); | |
| console.error('Recognition error:', err); | |
| } finally { | |
| setProcessingState(false); | |
| event.target.value = ''; | |
| } | |
| }; | |
| return ( | |
| <div className="mx-auto max-w-6xl space-y-6"> | |
| <div> | |
| <p className="eyebrow">Live recognition</p> | |
| <h2 className="mt-2 text-2xl font-black text-[color:var(--ink)]">Camera matching station</h2> | |
| <p className="mt-1 text-sm text-[color:var(--muted)]"> | |
| Process one frame at a time to avoid overlapping recognition requests. | |
| </p> | |
| </div> | |
| {error && ( | |
| <div role="alert" className="rounded-md border border-red-200 bg-red-50 px-4 py-3 text-sm text-red-800"> | |
| {error} | |
| </div> | |
| )} | |
| <div className="grid grid-cols-1 gap-6 lg:grid-cols-[minmax(0,1fr)_320px]"> | |
| <div> | |
| <div className="relative aspect-video overflow-hidden rounded-md border border-[color:var(--line)] bg-black"> | |
| <video ref={videoRef} autoPlay muted playsInline className="h-full w-full object-cover" /> | |
| <canvas ref={canvasRef} className="hidden" /> | |
| {!isCameraReady && ( | |
| <div className="absolute inset-0 flex items-center justify-center bg-black/75 text-center text-white"> | |
| <div> | |
| <p className="font-bold">Camera idle</p> | |
| <p className="mt-1 text-sm text-white/70">Start camera to begin recognition.</p> | |
| </div> | |
| </div> | |
| )} | |
| {isProcessing && ( | |
| <div className="absolute left-4 top-4 rounded-full bg-[color:var(--brand)] px-3 py-1 text-sm font-bold text-white"> | |
| Processing... | |
| </div> | |
| )} | |
| </div> | |
| <div className="mt-4 grid grid-cols-1 gap-3 sm:grid-cols-2"> | |
| {!isCameraReady ? ( | |
| <button onClick={initializeCamera} className="btn-primary px-4 py-2 sm:col-span-2"> | |
| Start Camera | |
| </button> | |
| ) : ( | |
| <> | |
| <button onClick={isRunning ? stopRecognition : startRecognition} className="btn-primary px-4 py-2"> | |
| {isRunning ? 'Stop Recognition' : 'Start Recognition'} | |
| </button> | |
| <button onClick={stopCamera} className="btn-quiet px-4 py-2"> | |
| Close Camera | |
| </button> | |
| </> | |
| )} | |
| </div> | |
| <div className="mt-4 grid grid-cols-1 gap-3 sm:grid-cols-2"> | |
| <label className="text-sm font-bold text-[color:var(--ink)]">Camera Source</label> | |
| <select | |
| className="field" | |
| value={selectedDeviceId} | |
| onChange={handleDeviceChange} | |
| disabled={devices.length === 0} | |
| > | |
| {devices.length === 0 && <option value="">No cameras found</option>} | |
| {devices.map((device, index) => ( | |
| <option key={device.deviceId} value={device.deviceId}> | |
| {device.label || `Camera ${index + 1}`} | |
| </option> | |
| ))} | |
| </select> | |
| </div> | |
| <div className="mt-4 rounded-md border border-[color:var(--line)] bg-white/70 p-4"> | |
| <label className="block text-sm font-bold text-[color:var(--ink)]">Upload or capture photo</label> | |
| <p className="mt-1 text-xs text-[color:var(--muted)]"> | |
| Use the camera on mobile or upload an image from your device. | |
| </p> | |
| <input | |
| type="file" | |
| accept="image/*" | |
| capture="environment" | |
| onChange={handleUpload} | |
| className="mt-3 block w-full text-sm" | |
| /> | |
| </div> | |
| <div className="mt-4 rounded-md border border-[color:var(--line)] bg-white/70 p-4"> | |
| <label htmlFor="confidence-threshold" className="block text-sm font-bold text-[color:var(--ink)]"> | |
| Confidence Threshold: {threshold.toFixed(2)} | |
| </label> | |
| <input | |
| id="confidence-threshold" | |
| type="range" | |
| min="0" | |
| max="1" | |
| step="0.05" | |
| value={threshold} | |
| onChange={(e) => setThreshold(parseFloat(e.target.value))} | |
| className="mt-3 w-full accent-[color:var(--brand)]" | |
| disabled={!isCameraReady} | |
| /> | |
| <div className="mt-1 flex justify-between text-xs text-[color:var(--muted)]"> | |
| <span>Loose 0.0</span> | |
| <span>Strict 1.0</span> | |
| </div> | |
| </div> | |
| </div> | |
| <aside className="panel p-4"> | |
| <h3 className="text-lg font-black text-[color:var(--ink)]">Recognition Results</h3> | |
| {results ? ( | |
| <div className="mt-4 space-y-3"> | |
| {[ | |
| ['Faces Detected', results.faces_detected], | |
| ['Recognized', results.recognized.length], | |
| ['Unrecognized', results.unrecognized_count], | |
| ['Processing Time', `${results.processing_time_ms.toFixed(0)}ms`], | |
| ].map(([label, value]) => ( | |
| <div key={label} className="rounded-md border border-[color:var(--line)] bg-[color:var(--field)] p-3"> | |
| <p className="text-xs font-bold uppercase text-[color:var(--muted)]">{label}</p> | |
| <p className="mt-1 text-2xl font-black text-[color:var(--ink)]">{value}</p> | |
| </div> | |
| ))} | |
| {results.recognized.length > 0 && ( | |
| <div className="border-t border-[color:var(--line)] pt-4"> | |
| <p className="mb-3 text-sm font-black text-[color:var(--ink)]">Recognized Faces</p> | |
| <div className="space-y-2"> | |
| {results.recognized.map((face) => ( | |
| <div key={`${face.person_id}-${face.embedding_id || face.confidence}`} className="rounded-md bg-white p-3 text-sm"> | |
| <div className="font-black text-[color:var(--ink)]">{face.name}</div> | |
| <div className="text-[color:var(--muted)]">ID: {face.person_id}</div> | |
| <div className="font-bold text-[color:var(--success)]"> | |
| Confidence: {(face.confidence * 100).toFixed(1)}% | |
| </div> | |
| </div> | |
| ))} | |
| </div> | |
| </div> | |
| )} | |
| </div> | |
| ) : ( | |
| <div className="mt-4 rounded-md border border-dashed border-[color:var(--line)] py-10 text-center text-[color:var(--muted)]"> | |
| <p className="font-bold text-[color:var(--ink)]">No frame processed</p> | |
| <p className="mt-1 text-xs">Results will appear here.</p> | |
| </div> | |
| )} | |
| </aside> | |
| </div> | |
| </div> | |
| ); | |
| }; | |