Support HEIC images for Face Recognition

This commit is contained in:
Nicolas Mowen
2026-11-19 08:04:18 -07:00
parent dfd1fa91b5
commit 130815ac70
5 changed files with 161 additions and 27 deletions
+7
View File
@@ -46,6 +46,7 @@
"date-fns": "^4.4.0",
"date-fns-tz": "^3.2.0",
"framer-motion": "^14.12.0",
"heic-to": "^1.6.7",
"hls.js": "^1.7.4",
"i18next": "^27.1.1",
"i18next-http-backend": "^4.0.10",
@@ -6018,6 +6019,12 @@
"url": "https://opencollective.com/unified"
}
},
"node_modules/heic-to": {
"version": "1.6.7",
"resolved": "https://registry.npmjs.org/heic-to/-/heic-to-1.6.7.tgz",
"integrity": "sha512-GCPYNl86u6eZ7mr2aa+uQB+PrQOVKSBYfH8esovr8kh2c6EYQSDYIJ6uhXSmM2hPC8PdBVJ/Trn0jnfM9H9gvg==",
"license": "LGPL-3.0"
},
"node_modules/hermes-estree": {
"version": "0.25.1",
"resolved": "https://registry.npmjs.org/hermes-estree/-/hermes-estree-0.25.1.tgz",
+1
View File
@@ -58,6 +58,7 @@
"date-fns": "^4.4.0",
"date-fns-tz": "^3.2.0",
"framer-motion": "^14.12.0",
"heic-to": "^1.6.7",
"hls.js": "^1.7.4",
"i18next": "^27.1.1",
"i18next-http-backend": "^4.0.10",
+3 -1
View File
@@ -61,8 +61,10 @@
},
"imageEntry": {
"validation": {
"selectImage": "Please select an image file."
"selectImage": "Please select an image file.",
"heicConversionFailed": "Unable to convert this HEIC image. Try exporting it as a JPEG instead."
},
"converting": "Converting image…",
"dropActive": "Drop the image here…",
"dropInstructions": "Drag and drop or paste an image here, or click to select",
"maxSize": "Max size: {{size}}MB"
+106 -26
View File
@@ -1,3 +1,4 @@
import ActivityIndicator from "@/components/indicators/activity-indicator";
import { Button } from "@/components/ui/button";
import {
Form,
@@ -7,6 +8,11 @@ import {
FormMessage,
} from "@/components/ui/form";
import { cn } from "@/lib/utils";
import {
convertHeicToJpeg,
HEIC_EXTENSIONS,
isHeicFile,
} from "@/utils/heicUtil";
import { zodResolver } from "@hookform/resolvers/zod";
import { useCallback, useEffect, useRef, useState } from "react";
import { useDropzone } from "react-dropzone";
@@ -26,11 +32,25 @@ export default function ImageEntry({
onSave,
children,
maxSize = 20 * 1024 * 1024, // 20MB default
accept = { "image/*": [".jpeg", ".jpg", ".png", ".gif", ".webp"] },
accept = {
"image/*": [".jpeg", ".jpg", ".png", ".gif", ".webp", ...HEIC_EXTENSIONS],
},
}: ImageEntryProps) {
const { t } = useTranslation(["views/faceLibrary"]);
const [preview, setPreview] = useState<string | null>(null);
const [converting, setConverting] = useState(false);
const dropzoneRef = useRef<HTMLDivElement>(null);
// Bumped on every selection and clear, so a HEIC conversion that finishes
// after the selection changed is discarded instead of applied
const selectionId = useRef(0);
// Discard conversions that finish after the component unmounts
useEffect(() => {
const id = selectionId;
return () => {
id.current += 1;
};
}, []);
// Auto focus the dropzone
useEffect(() => {
@@ -60,18 +80,48 @@ export default function ImageEntry({
resolver: zodResolver(formSchema),
});
const selectFile = useCallback(
async (file: File) => {
const id = ++selectionId.current;
let selected = file;
if (isHeicFile(file)) {
setConverting(true);
const converted = await convertHeicToJpeg(file).catch(() => null);
// the selection was cleared or replaced while converting
if (id !== selectionId.current) {
return;
}
setConverting(false);
if (!converted) {
form.setError("file", {
message: t("imageEntry.validation.heicConversionFailed"),
});
return;
}
selected = converted;
}
form.setValue("file", selected, { shouldValidate: true });
// Create preview
const objectUrl = URL.createObjectURL(selected);
setPreview(objectUrl);
},
[form, t],
);
const onDrop = useCallback(
(acceptedFiles: File[]) => {
if (acceptedFiles.length > 0) {
const file = acceptedFiles[0];
form.setValue("file", file, { shouldValidate: true });
// Create preview
const objectUrl = URL.createObjectURL(file);
setPreview(objectUrl);
selectFile(acceptedFiles[0]);
}
},
[form],
[selectFile],
);
const { getRootProps, getInputProps, isDragActive, isDragReject } =
@@ -80,11 +130,17 @@ export default function ImageEntry({
maxSize,
accept,
multiple: false,
disabled: converting,
});
const handlePaste = useCallback(
(event: React.ClipboardEvent<HTMLDivElement>) => {
event.preventDefault();
if (converting) {
return;
}
const clipboardItems = Array.from(event.clipboardData.items);
for (const item of clipboardItems) {
if (item.type.startsWith("image/")) {
@@ -95,27 +151,30 @@ export default function ImageEntry({
if (accept["image/*"].includes(extension)) {
const fileName = blob.name || `pasted-image.${mimeType}`;
const file = new File([blob], fileName, { type: blob.type });
form.setValue("file", file, { shouldValidate: true });
const objectUrl = URL.createObjectURL(file);
setPreview(objectUrl);
selectFile(file);
return; // Take the first valid image
}
}
}
}
},
[form, maxSize, accept],
[converting, maxSize, accept, selectFile],
);
const onSubmit = useCallback(
(data: z.infer<typeof formSchema>) => {
if (!data.file) return;
// the form still holds the previous image until a replacement
// finishes converting, so saving now would upload the wrong one
if (!data.file || converting) return;
onSave(data.file);
},
[onSave],
[converting, onSave],
);
const clearSelection = () => {
// drop any running conversion so it can't restore the selection
selectionId.current += 1;
setConverting(false);
form.reset();
setPreview(null);
};
@@ -142,21 +201,34 @@ export default function ImageEntry({
"flex h-40 flex-col items-center justify-center rounded-lg border-2 border-dashed p-6 transition-colors",
isDragActive && "border-primary bg-primary/5",
isDragReject && "border-destructive bg-destructive/5",
"cursor-pointer hover:border-primary hover:bg-primary/5",
converting
? "cursor-wait"
: "cursor-pointer hover:border-primary hover:bg-primary/5",
)}
>
<input {...getInputProps()} />
<LuUpload className="mb-2 h-10 w-10 text-muted-foreground" />
<p className="text-center text-sm text-muted-foreground">
{isDragActive
? t("imageEntry.dropActive")
: t("imageEntry.dropInstructions")}
</p>
<p className="mt-1 text-xs text-muted-foreground">
{t("imageEntry.maxSize", {
size: Math.round(maxSize / (1024 * 1024)),
})}
</p>
{converting ? (
<>
<ActivityIndicator className="mb-2" />
<p className="text-center text-sm text-muted-foreground">
{t("imageEntry.converting")}
</p>
</>
) : (
<>
<LuUpload className="mb-2 h-10 w-10 text-muted-foreground" />
<p className="text-center text-sm text-muted-foreground">
{isDragActive
? t("imageEntry.dropActive")
: t("imageEntry.dropInstructions")}
</p>
<p className="mt-1 text-xs text-muted-foreground">
{t("imageEntry.maxSize", {
size: Math.round(maxSize / (1024 * 1024)),
})}
</p>
</>
)}
</div>
) : (
<div className="relative h-40 w-full">
@@ -165,6 +237,14 @@ export default function ImageEntry({
alt="Preview"
className="h-full w-full rounded-lg border object-contain"
/>
{converting && (
<div className="absolute inset-0 flex cursor-wait flex-col items-center justify-center rounded-lg bg-background/80">
<ActivityIndicator className="mb-2" />
<p className="text-center text-sm text-muted-foreground">
{t("imageEntry.converting")}
</p>
</div>
)}
<Button
type="button"
variant="destructive"
+44
View File
@@ -0,0 +1,44 @@
// HEIC/HEIF is the default photo format on iPhones. The backend decodes
// uploads with OpenCV, which has no HEIC support, and only Safari can preview
// it, so HEIC images are converted to JPEG in the browser before upload.
export const HEIC_EXTENSIONS = [".heic", ".heif"];
const HEIC_MIME_TYPES = [
"image/heic",
"image/heif",
"image/heic-sequence",
"image/heif-sequence",
];
/**
* Check whether a file is HEIC/HEIF. The extension is checked as well as the
* MIME type because browsers on systems without a HEIF codec installed (such
* as Windows) report an empty type for these files.
*/
export function isHeicFile(file: File): boolean {
if (HEIC_MIME_TYPES.includes(file.type.toLowerCase())) {
return true;
}
const name = file.name.toLowerCase();
return HEIC_EXTENSIONS.some((extension) => name.endsWith(extension));
}
/**
* Convert a HEIC/HEIF file to a JPEG file. Orientation is applied during
* decoding, so the output pixels are already upright.
*/
export async function convertHeicToJpeg(file: File): Promise<File> {
// the decoder is a large WASM bundle, so it is only fetched when needed
const { heicTo } = await import("heic-to");
const jpeg = await heicTo({
blob: file,
type: "image/jpeg",
// faces are cropped and re-encoded by the backend, keep detail here
quality: 0.95,
});
const baseName = file.name.replace(/\.hei[cf]$/i, "") || "image";
return new File([jpeg], `${baseName}.jpg`, { type: "image/jpeg" });
}