From a1f90b7c3a99aff9366a353631ea963c778b4740 Mon Sep 17 00:00:00 2001
From: bobzel <zzzman@gmail.com>
Date: Thu, 22 Aug 2024 11:16:39 -0400
Subject: fixing up removing faces from unique faces

---
 src/client/views/search/FaceRecognitionHandler.tsx | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 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 c239c775c..487c35d90 100644
--- a/src/client/views/search/FaceRecognitionHandler.tsx
+++ b/src/client/views/search/FaceRecognitionHandler.tsx
@@ -46,6 +46,15 @@ export class FaceRecognitionHandler {
      */
     public static ImageDocFaceDescriptors = (imgDoc: Doc) => imgDoc[DocData][`${Doc.LayoutFieldKey(imgDoc)}_Faces`] as List<List<number>>;
 
+    /**
+     * Adds a face descriptor for a face found in an image
+     * @param imgDoc image Doc with face
+     * @param faceDescriptor descriptor of a face
+     */
+    public static ImageDocAddFaceDescriptor = (imgDoc: Doc, faceDescriptor: List<number>) => {
+        Cast(imgDoc[DocData][`${Doc.LayoutFieldKey(imgDoc)}_Faces`], listSpec('number'), null).push(faceDescriptor as unknown as number);
+    };
+
     /**
      * Adds metadata to an image Doc associating it to a unique face that corresponds to a face found in the image
      * @param imgDoc image Doc containing faces
@@ -59,7 +68,6 @@ export class FaceRecognitionHandler {
         } else {
             imgDoc[DocData][faceFieldKey] = new List<List<number>>([faceDescriptor]);
         }
-        Cast(imgDoc[DocData][`${Doc.LayoutFieldKey(imgDoc)}_Faces`], listSpec('number'), null).push(faceDescriptor as unknown as number);
     };
 
     /**
@@ -67,10 +75,10 @@ export class FaceRecognitionHandler {
      * @param imgDoc image Doc containing faces
      * @param faceDoc unique face
      */
-    public static ImageDocDeassociateUniqueFace = (imgDoc: Doc, faceDoc: Doc) => {
-        // fill in..
+    public static ImageDocDisassociateUniqueFace = (imgDoc: Doc, faceDoc: Doc) => {
+        const faceFieldKey = FaceRecognitionHandler.imgDocFaceField(imgDoc, faceDoc);
+        imgDoc[DocData][faceFieldKey] = undefined;
     };
-
     /**
      * returns a list of all face collection Docs on the current dashboard
      * @returns face collection Doc list
@@ -122,7 +130,8 @@ export class FaceRecognitionHandler {
      */
     public static UniqueFaceRemoveFaceImage = (imgDoc: Doc, faceDoc: Doc) => {
         Doc.RemoveDocFromList(faceDoc[DocData], 'face_images', imgDoc);
-        faceDoc[DocData].face_descriptors = new List<List<number>>(FaceRecognitionHandler.UniqueFaceDescriptors(faceDoc).filter(fd => !(imgDoc[DocData][FaceRecognitionHandler.imgDocFaceField(imgDoc, faceDoc)] as List<List<number>>).includes(fd)));
+        // TODO: remove face descriptor from images' list of face descriptors (below doesn't work)
+        // faceDoc[DocData].face_descriptors = new List<List<number>>(FaceRecognitionHandler.UniqueFaceDescriptors(faceDoc).filter(fd => !(imgDoc[DocData][FaceRecognitionHandler.imgDocFaceField(imgDoc, faceDoc)] as List<List<number>>).includes(fd)));
     };
 
     constructor() {
@@ -203,6 +212,8 @@ export class FaceRecognitionHandler {
             // Add image to unique face's image collection, and assign image metadata referencing unique face
             FaceRecognitionHandler.UniqueFaceAddFaceImage(imgDoc, faceDescriptor, matchedUniqueFace);
             FaceRecognitionHandler.ImageDocAssociateUniqueFace(imgDoc, faceDescriptor, matchedUniqueFace);
+            // save the descriptor for the image's list of faces
+            FaceRecognitionHandler.ImageDocAddFaceDescriptor(imgDoc, faceDescriptor);
         }
     };
 
-- 
cgit v1.2.3-70-g09d2

