From e2a1db72c103b7ed878fe876c5fb9eacc7a8c893 Mon Sep 17 00:00:00 2001
From: bobzel <zzzman@gmail.com>
Date: Tue, 27 Aug 2024 19:09:31 -0400
Subject: enabled any image Doc to be added to a face Doc

---
 .../collectionFreeForm/FaceCollectionBox.tsx            | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)

(limited to 'src/client/views/collections/collectionFreeForm/FaceCollectionBox.tsx')

diff --git a/src/client/views/collections/collectionFreeForm/FaceCollectionBox.tsx b/src/client/views/collections/collectionFreeForm/FaceCollectionBox.tsx
index c62303dc0..717081666 100644
--- a/src/client/views/collections/collectionFreeForm/FaceCollectionBox.tsx
+++ b/src/client/views/collections/collectionFreeForm/FaceCollectionBox.tsx
@@ -95,17 +95,18 @@ export class UniqueFaceBox extends ViewBoxBaseComponent<FieldViewProps>() {
                     const faceDescriptorsAsFloat32Array = FaceRecognitionHandler.UniqueFaceDescriptors(this.Document).map(fd => new Float32Array(Array.from(fd)));
                     const labeledFaceDescriptor = new faceapi.LabeledFaceDescriptors(FaceRecognitionHandler.UniqueFaceLabel(this.Document), faceDescriptorsAsFloat32Array);
                     const faceMatcher = new FaceMatcher([labeledFaceDescriptor], 1);
-                    const { faceAnno } = FaceRecognitionHandler.ImageDocFaceAnnos(imgDoc).reduce(
-                        (prev, faceAnno) => {
-                            const match = faceMatcher.matchDescriptor(new Float32Array(Array.from(faceAnno.faceDescriptor as List<number>)));
-                            return match.distance < prev.dist ? { dist: match.distance, faceAnno } : prev;
-                        },
-                        { dist: 1, faceAnno: undefined as Opt<Doc> }
-                    );
+                    const faceAnno =
+                        FaceRecognitionHandler.ImageDocFaceAnnos(imgDoc).reduce(
+                            (prev, faceAnno) => {
+                                const match = faceMatcher.matchDescriptor(new Float32Array(Array.from(faceAnno.faceDescriptor as List<number>)));
+                                return match.distance < prev.dist ? { dist: match.distance, faceAnno } : prev;
+                            },
+                            { dist: 1, faceAnno: undefined as Opt<Doc> }
+                        ).faceAnno ?? imgDoc;
 
                     // assign the face in the image that's closest to the face collection's face
                     if (faceAnno) {
-                        FaceRecognitionHandler.UniqueFaceRemoveFaceImage(faceAnno, DocCast(faceAnno.face));
+                        faceAnno.face && FaceRecognitionHandler.UniqueFaceRemoveFaceImage(faceAnno, DocCast(faceAnno.face));
                         FaceRecognitionHandler.UniqueFaceAddFaceImage(faceAnno, this.Document);
                         faceAnno.face = this.Document;
                     }
-- 
cgit v1.2.3-70-g09d2

