Spaces:
Build error
Build error
File size: 7,222 Bytes
4854e2b | 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 | 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>
);
};
|