From ef5a8b8261052c3079f267d1d8d0804a495e3e3d Mon Sep 17 00:00:00 2001
From: bobzel <zzzman@gmail.com>
Date: Mon, 26 Aug 2024 23:29:47 -0400
Subject: fixed naming of face annotations

---
 src/client/views/search/FaceRecognitionHandler.tsx | 56 ++++++++++++++--------
 1 file changed, 37 insertions(+), 19 deletions(-)

(limited to 'src/client/views/search/FaceRecognitionHandler.tsx')

diff --git a/src/client/views/search/FaceRecognitionHandler.tsx b/src/client/views/search/FaceRecognitionHandler.tsx
index 4e500d24e..4906ba4b6 100644
--- a/src/client/views/search/FaceRecognitionHandler.tsx
+++ b/src/client/views/search/FaceRecognitionHandler.tsx
@@ -9,6 +9,7 @@ import { ImageField } from '../../../fields/URLField';
 import { DocumentType } from '../../documents/DocumentTypes';
 import { Docs } from '../../documents/Documents';
 import { DocumentManager } from '../../util/DocumentManager';
+import { ComputedField } from '../../../fields/ScriptField';
 
 /**
  * A singleton class that handles face recognition and manages face Doc collections for each face found.
@@ -176,7 +177,7 @@ export class FaceRecognitionHandler {
         dashboard[DocData].myUniqueFaces_count = faceDocNum; // TODO: improve to a better name
 
         const uniqueFaceDoc = Docs.Create.UniqeFaceDocument({
-            title: `Face ${faceDocNum}`,
+            title: ComputedField.MakeFunction('this.face') as unknown as string,
             _layout_reflowHorizontal: true,
             _layout_reflowVertical: true,
             _layout_nativeDimEditable: true,
@@ -237,27 +238,44 @@ export class FaceRecognitionHandler {
             setTimeout(() => this.classifyFacesInImage(imgDoc), 1000);
         } else {
             const imgUrl = ImageCast(imgDoc[Doc.LayoutFieldKey(imgDoc)]);
-            if (imgUrl && !DocListCast(Doc.MyFaceCollection.examinedFaceDocs).includes(imgDoc[DocData])) { // only examine Docs that have an image and that haven't already been examined.
+            if (imgUrl && !DocListCast(Doc.MyFaceCollection.examinedFaceDocs).includes(imgDoc[DocData])) {
+                // only examine Docs that have an image and that haven't already been examined.
                 Doc.AddDocToList(Doc.MyFaceCollection, 'examinedFaceDocs', imgDoc[DocData]);
                 FaceRecognitionHandler.initImageDocFaceDescriptors(imgDoc);
-                FaceRecognitionHandler.loadImage(imgUrl).then( // load image and analyze faces
-                img => faceapi.detectAllFaces(img).withFaceLandmarks().withFaceDescriptors()
-                    .then(imgDocFaceDescriptions => { // For each face detected, find a match.
-                        const annos = [] as Doc[];
-                        const scale = NumCast(imgDoc.data_nativeWidth) / img.width;
-                        for (const fd of imgDocFaceDescriptions) {
-                            const faceDescriptor = new List<number>(Array.from(fd.descriptor));
-                            annos.push(Docs.Create.FreeformDocument([], {backgroundColor: "#55555555", x: fd.alignedRect.box.left*scale, y: fd.alignedRect.box.top*scale, _width: fd.alignedRect.box.width*scale, _height: fd.alignedRect.box.height*scale}))
-                            FaceRecognitionHandler.ImageDocAddFaceDescriptor(imgDoc, faceDescriptor); // add face descriptor to image's list of descriptors
-                            const matchedUniqueFace = this.findMatchingFaceDoc(fd.descriptor) ?? this.createUniqueFaceDoc(activeDashboard);
-                            FaceRecognitionHandler.UniqueFaceAddFaceImage(imgDoc, faceDescriptor, matchedUniqueFace); // add image/faceDescriptor to matched unique face
-                        } 
+                FaceRecognitionHandler.loadImage(imgUrl).then(
+                    // load image and analyze faces
+                    img => faceapi
+                            .detectAllFaces(img)
+                            .withFaceLandmarks()
+                            .withFaceDescriptors()
+                            .then(imgDocFaceDescriptions => {  // For each face detected, find a match.
+                                const annos = [] as Doc[];
+                                const scale = NumCast(imgDoc.data_nativeWidth) / img.width;
+                                imgDocFaceDescriptions.forEach((fd, i) => {
+                                    const faceDescriptor = new List<number>(Array.from(fd.descriptor));
+                                    FaceRecognitionHandler.ImageDocAddFaceDescriptor(imgDoc, faceDescriptor); // add face descriptor to image's list of descriptors
+                                    const matchedUniqueFace = this.findMatchingFaceDoc(fd.descriptor) ?? this.createUniqueFaceDoc(activeDashboard);
+                                    FaceRecognitionHandler.UniqueFaceAddFaceImage(imgDoc, faceDescriptor, matchedUniqueFace); // add image/faceDescriptor to matched unique face
+                                    annos.push(
+                                        Docs.Create.FreeformDocument([], {
+                                            title: ComputedField.MakeFunction(`this.face.face`) as unknown as string, //
+                                            annotationOn: imgDoc,
+                                            face: matchedUniqueFace,
+                                            backgroundColor: '#55555555',
+                                            x: fd.alignedRect.box.left * scale,
+                                            y: fd.alignedRect.box.top * scale,
+                                            _width: fd.alignedRect.box.width * scale,
+                                            _height: fd.alignedRect.box.height * scale,
+                                        })
+                                    );
+                                    Doc.SetContainer(annos.lastElement(), imgDoc[DocData]);
+                                });
 
-                        imgDoc[DocData].data_annotations = new List<Doc>(annos);
-                        return imgDocFaceDescriptions;
-                    }) 
-                );
-            } // prettier-ignore
+                                imgDoc[DocData].data_annotations = new List<Doc>(annos);
+                                return imgDocFaceDescriptions;
+                            })
+                ); // prettier-ignore
+            }
         }
     };
 }
-- 
cgit v1.2.3-70-g09d2

