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/RegisterFaceComponent.tsx from jinksQspider/fmproService: direct link, hf CLI and curl.
- Browser
- Download file 7.22 kB
-
https://huggingface.co/spaces/jinksQspider/fmproService/resolve/main/frontend/src/components/RegisterFaceComponent.tsx
- Command line
-
hf download hf://spaces/jinksQspider/fmproService/frontend/src/components/RegisterFaceComponent.tsx
-
curl -L -o RegisterFaceComponent.tsx https://huggingface.co/spaces/jinksQspider/fmproService/resolve/main/frontend/src/components/RegisterFaceComponent.tsx
7.22 kB
| import React, { useRef, useState } from 'react'; | |
| import api from '../services/api'; | |
| interface RegisterFaceComponentProps { | |
| onSuccess: () => void; | |
| } | |
| const REQUIRED_IMAGE_COUNT = 3; | |
| export const RegisterFaceComponent: React.FC<RegisterFaceComponentProps> = ({ onSuccess }) => { | |
| const [personId, setPersonId] = useState(''); | |
| const [images, setImages] = useState<File[]>([]); | |
| const [previews, setPreviews] = useState<string[]>([]); | |
| const [isLoading, setIsLoading] = useState(false); | |
| const [error, setError] = useState(''); | |
| const [success, setSuccess] = useState(''); | |
| const fileInputRef = useRef<HTMLInputElement>(null); | |
| const resetImages = () => { | |
| setImages([]); | |
| setPreviews([]); | |
| if (fileInputRef.current) fileInputRef.current.value = ''; | |
| }; | |
| const handleImageChange = (e: React.ChangeEvent<HTMLInputElement>) => { | |
| const selectedFiles = Array.from(e.target.files || []); | |
| if (selectedFiles.length !== REQUIRED_IMAGE_COUNT) { | |
| setError('Select exactly 3 images: front, left angle, right angle'); | |
| resetImages(); | |
| return; | |
| } | |
| const invalidFile = selectedFiles.find((file) => !file.type.startsWith('image/')); | |
| if (invalidFile) { | |
| setError(`${invalidFile.name} is not a valid image file`); | |
| resetImages(); | |
| return; | |
| } | |
| const oversizedFile = selectedFiles.find((file) => file.size > 10 * 1024 * 1024); | |
| if (oversizedFile) { | |
| setError(`${oversizedFile.name} must be 10MB or smaller`); | |
| resetImages(); | |
| return; | |
| } | |
| setError(''); | |
| setImages(selectedFiles); | |
| Promise.all( | |
| selectedFiles.map( | |
| (file) => | |
| new Promise<string>((resolve) => { | |
| const reader = new FileReader(); | |
| reader.onloadend = () => resolve(reader.result as string); | |
| reader.readAsDataURL(file); | |
| }) | |
| ) | |
| ).then(setPreviews); | |
| }; | |
| const handleSubmit = async (e: React.FormEvent) => { | |
| e.preventDefault(); | |
| setError(''); | |
| setSuccess(''); | |
| const cleanPersonId = personId.trim(); | |
| if (!cleanPersonId || images.length !== REQUIRED_IMAGE_COUNT) { | |
| setError('Enter a person ID and select exactly 3 face images'); | |
| return; | |
| } | |
| setIsLoading(true); | |
| try { | |
| const formData = new FormData(); | |
| formData.append('person_id', cleanPersonId); | |
| formData.append('name', cleanPersonId); | |
| images.forEach((image) => formData.append('images', image)); | |
| await api.registerFace(formData); | |
| setSuccess('Person ID registered successfully'); | |
| setPersonId(''); | |
| resetImages(); | |
| onSuccess(); | |
| } catch (err: any) { | |
| setError(err.response?.data?.detail || 'Failed to register face'); | |
| } finally { | |
| setIsLoading(false); | |
| } | |
| }; | |
| return ( | |
| <div className="mx-auto max-w-5xl"> | |
| <div className="mb-6"> | |
| <p className="eyebrow">Enrollment</p> | |
| <h2 className="mt-2 text-2xl font-black text-[color:var(--ink)]">Register 3-angle face profile</h2> | |
| <p className="mt-1 text-sm text-[color:var(--muted)]"> | |
| Enter a person ID and upload exactly 3 images of one person: front, left angle, and right angle. | |
| If the same ID already exists in this tenant, the server will mark it as occupied. | |
| </p> | |
| </div> | |
| <form onSubmit={handleSubmit} className="space-y-6"> | |
| {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> | |
| )} | |
| {success && ( | |
| <div role="status" className="rounded-md border border-green-200 bg-green-50 px-4 py-3 text-sm text-green-800"> | |
| {success} | |
| </div> | |
| )} | |
| <div className="grid grid-cols-1 gap-6 lg:grid-cols-[360px_minmax(0,1fr)]"> | |
| <div className="space-y-4"> | |
| <div> | |
| <label htmlFor="person-id" className="mb-2 block text-sm font-bold text-[color:var(--ink)]"> | |
| Person ID | |
| </label> | |
| <input | |
| id="person-id" | |
| type="text" | |
| value={personId} | |
| onChange={(e) => setPersonId(e.target.value)} | |
| placeholder="e.g., EMP001" | |
| className="field" | |
| disabled={isLoading} | |
| /> | |
| <p className="mt-1 text-xs text-[color:var(--muted)]"> | |
| If this ID already exists, the service will reject it as occupied. | |
| </p> | |
| </div> | |
| <div> | |
| <label className="mb-2 block text-sm font-bold text-[color:var(--ink)]"> | |
| Three Face Images | |
| </label> | |
| <input | |
| ref={fileInputRef} | |
| type="file" | |
| accept="image/*" | |
| multiple | |
| onChange={handleImageChange} | |
| className="hidden" | |
| disabled={isLoading} | |
| /> | |
| <button | |
| type="button" | |
| onClick={() => fileInputRef.current?.click()} | |
| className="min-h-[108px] w-full rounded-md border-2 border-dashed border-[color:var(--line)] bg-[color:var(--field)] px-4 py-3 text-sm font-bold text-[color:var(--muted)] hover:border-[color:var(--brand)] hover:text-[color:var(--brand-strong)]" | |
| > | |
| {images.length === REQUIRED_IMAGE_COUNT | |
| ? `Selected 3 images: ${images.map((image) => image.name).join(', ')}` | |
| : 'Select exactly 3 images'} | |
| </button> | |
| <p className="mt-1 text-xs text-[color:var(--muted)]">One person per frame. Different angles required.</p> | |
| </div> | |
| <button | |
| type="submit" | |
| disabled={isLoading || images.length !== REQUIRED_IMAGE_COUNT} | |
| className="btn-primary w-full px-4 py-2" | |
| > | |
| {isLoading ? 'Building embedding...' : 'Register 3-Angle Face'} | |
| </button> | |
| </div> | |
| <div className="grid grid-cols-1 gap-3 sm:grid-cols-3"> | |
| {['Front', 'Left angle', 'Right angle'].map((label, index) => ( | |
| <div key={label} className="rounded-md border border-[color:var(--line)] bg-[color:var(--field)] p-3"> | |
| <p className="mb-2 text-xs font-black uppercase text-[color:var(--muted)]">{label}</p> | |
| <div className="flex aspect-square items-center justify-center rounded bg-black"> | |
| {previews[index] ? ( | |
| <img | |
| src={previews[index]} | |
| alt={`${label} face preview`} | |
| className="max-h-full max-w-full object-contain" | |
| /> | |
| ) : ( | |
| <span className="px-4 text-center text-xs font-bold text-white/60">No image</span> | |
| )} | |
| </div> | |
| {images[index] && ( | |
| <p className="mt-2 truncate text-xs text-[color:var(--muted)]" title={images[index].name}> | |
| {images[index].name} | |
| </p> | |
| )} | |
| </div> | |
| ))} | |
| </div> | |
| </div> | |
| </form> | |
| </div> | |
| ); | |
| }; | |