From 95071fa118ee36d7365250f10756dce335dc76d9 Mon Sep 17 00:00:00 2001
From: bobzel <zzzman@gmail.com>
Date: Thu, 22 Aug 2024 11:00:14 -0400
Subject: more cleanup for face collections

---
 .../collectionFreeForm/FaceCollectionBox.tsx       | 62 ++++++++++++++--------
 1 file changed, 40 insertions(+), 22 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 dd8dea41e..6005da6dd 100644
--- a/src/client/views/collections/collectionFreeForm/FaceCollectionBox.tsx
+++ b/src/client/views/collections/collectionFreeForm/FaceCollectionBox.tsx
@@ -26,18 +26,27 @@ import { FaceRecognitionHandler } from '../../search/FaceRecognitionHandler';
 import './FaceCollectionBox.scss';
 import { MarqueeOptionsMenu } from './MarqueeOptionsMenu';
 
-interface FaceDocumentProps {
+/**
+ * This code is used to render the sidebar collection of unique recognized faces, where each
+ * unique face in turn displays the set of images that correspond to the face.
+ */
+
+interface UniqueFaceProps {
     faceDoc: Doc;
 }
 
 /**
- * A componenent to visually represent a Face Document.
+ * React component for rendering a unique face and its collection of image Docs.
+ *
+ * This both displays a collection of images corresponding tp a unique face, and
+ * allows for editing the face collection by removing an image, or drag-and-dropping
+ * an image that was not recognized.
  */
 @observer
-export class FaceDocumentItem extends ObservableReactComponent<FaceDocumentProps> {
+export class UniqueFaceView extends ObservableReactComponent<UniqueFaceProps> {
     private _dropDisposer?: DragManager.DragDropDisposer;
 
-    constructor(props: FaceDocumentProps) {
+    constructor(props: UniqueFaceProps) {
         super(props);
         makeObservable(this);
     }
@@ -54,12 +63,12 @@ export class FaceDocumentItem extends ObservableReactComponent<FaceDocumentProps
             ?.filter(doc => doc.type === DocumentType.IMG)
             .forEach(imgDoc => {
                 // If the current Face Document has no faces, and the doc has more than one face descriptor, don't let the user add the document first.  Or should we just use the first face ?
-                if (FaceRecognitionHandler.FaceDocDescriptors(this._props.faceDoc).length === 0 && FaceRecognitionHandler.ImageDocFaceDescriptors(imgDoc).length > 1) {
+                if (FaceRecognitionHandler.UniqueFaceDescriptors(this._props.faceDoc).length === 0 && FaceRecognitionHandler.ImageDocFaceDescriptors(imgDoc).length > 1) {
                     alert('Cannot add a document with multiple faces as the first item!');
                 } else {
                     // Loop through the documents' face descriptors and choose the face in the iage with the smallest distance (most similar to the face colleciton)
-                    const faceDescriptorsAsFloat32Array = FaceRecognitionHandler.FaceDocDescriptors(this._props.faceDoc).map(fd => new Float32Array(Array.from(fd)));
-                    const labeledFaceDescriptor = new faceapi.LabeledFaceDescriptors(FaceRecognitionHandler.FaceDocLabel(this._props.faceDoc), faceDescriptorsAsFloat32Array);
+                    const faceDescriptorsAsFloat32Array = FaceRecognitionHandler.UniqueFaceDescriptors(this._props.faceDoc).map(fd => new Float32Array(Array.from(fd)));
+                    const labeledFaceDescriptor = new faceapi.LabeledFaceDescriptors(FaceRecognitionHandler.UniqueFaceLabel(this._props.faceDoc), faceDescriptorsAsFloat32Array);
                     const faceMatcher = new FaceMatcher([labeledFaceDescriptor], 1);
                     const { face_match } = FaceRecognitionHandler.ImageDocFaceDescriptors(imgDoc).reduce(
                         (prev, face) => {
@@ -71,8 +80,8 @@ export class FaceDocumentItem extends ObservableReactComponent<FaceDocumentProps
 
                     // assign the face in the image that's closest to the face collection to be the face that's assigned to the collection
                     if (face_match) {
-                        FaceRecognitionHandler.ImageDocAddFace(imgDoc, face_match, this._props.faceDoc);
-                        FaceRecognitionHandler.FaceDocAddImageDocFace(imgDoc, face_match, this._props.faceDoc);
+                        FaceRecognitionHandler.ImageDocAssociateUniqueFace(imgDoc, face_match, this._props.faceDoc);
+                        FaceRecognitionHandler.UniqueFaceAddFaceImage(imgDoc, face_match, this._props.faceDoc);
                     }
                 }
             });
@@ -88,28 +97,29 @@ export class FaceDocumentItem extends ObservableReactComponent<FaceDocumentProps
     }
 
     /**
-     * Deletes a Face Document.
+     * Removes a unique face Doc from the colelction of unique faces.
      */
-    deleteFaceDocument = undoable(() => {
-        FaceRecognitionHandler.DeleteFaceDoc(this._props.faceDoc);
+    deleteUniqueFace = undoable(() => {
+        FaceRecognitionHandler.DeleteUniqueFace(this._props.faceDoc);
     }, 'delete face');
 
     /**
-     * Deletes a document from a Face Document's associated docs list.
-     * @param doc
+     * Removes a face image Doc from a unique face's list of images.
+     * @param imgDoc - image Doc to remove
      */
-    deleteAssociatedDoc = undoable((imgDoc: Doc) => {
-        FaceRecognitionHandler.FaceDocRemoveImageDocFace(imgDoc, this._props.faceDoc);
+    removeFaceImageFromUniqueFace = undoable((imgDoc: Doc) => {
+        FaceRecognitionHandler.ImageDocDeassociateUniqueFace(imgDoc, this._props.faceDoc);
+        FaceRecognitionHandler.UniqueFaceRemoveFaceImage(imgDoc, this._props.faceDoc);
     }, 'remove doc from face');
 
     render() {
         return (
             <div className="face-document-item" ref={ele => this.createDropTarget(ele!)}>
                 <div className="face-collection-buttons">
-                    <IconButton tooltip="Delete Face From Collection" onPointerDown={this.deleteFaceDocument} icon={'x'} style={{ width: '4px' }} size={Size.XSMALL} />
+                    <IconButton tooltip="Delete Face From Collection" onPointerDown={this.deleteUniqueFace} icon={'x'} style={{ width: '4px' }} size={Size.XSMALL} />
                 </div>
                 <div className="face-document-top">
-                    <h1>{FaceRecognitionHandler.FaceDocLabel(this._props.faceDoc)}</h1>
+                    <h1>{FaceRecognitionHandler.UniqueFaceLabel(this._props.faceDoc)}</h1>
                 </div>
                 <IconButton
                     tooltip="See image information"
@@ -120,7 +130,7 @@ export class FaceDocumentItem extends ObservableReactComponent<FaceDocumentProps
                 />
                 {this._displayImages ? (
                     <div className="face-document-image-container">
-                        {FaceRecognitionHandler.FaceDocFaces(this._props.faceDoc).map(doc => {
+                        {FaceRecognitionHandler.UniqueFaceImages(this._props.faceDoc).map(doc => {
                             const [name, type] = ImageCast(doc[Doc.LayoutFieldKey(doc)]).url.href.split('.');
                             return (
                                 <div
@@ -140,7 +150,7 @@ export class FaceDocumentItem extends ObservableReactComponent<FaceDocumentProps
                                     }>
                                     <img onClick={() => DocumentView.showDocument(doc, { willZoomCentered: true })} style={{ maxWidth: '60px', margin: '10px' }} src={`${name}_o.${type}`} />
                                     <div className="remove-item">
-                                        <IconButton tooltip={'Remove Doc From Face Collection'} onPointerDown={() => this.deleteAssociatedDoc(doc)} icon={'x'} style={{ width: '4px' }} size={Size.XSMALL} />
+                                        <IconButton tooltip={'Remove Doc From Face Collection'} onPointerDown={() => this.removeFaceImageFromUniqueFace(doc)} icon={'x'} style={{ width: '4px' }} size={Size.XSMALL} />
                                     </div>
                                 </div>
                             );
@@ -152,6 +162,14 @@ export class FaceDocumentItem extends ObservableReactComponent<FaceDocumentProps
     }
 }
 
+/**
+ * This renders the sidebar collection of the unique faces that have been recognized.
+ *
+ * Since the collection of recognized faces is stored on the active dashboard, this class
+ * does not itself store any Docs, but accesses the uniqueFaces field of the current
+ * dashboard.  Each Face collection Doc is rendered using a FaceCollectionDocView which
+ * is not a CollectionView or even a DocumentView (but probably should be).
+ */
 @observer
 export class FaceCollectionBox extends ViewBoxBaseComponent<FieldViewProps>() {
     public static LayoutString(fieldKey: string) {
@@ -166,8 +184,8 @@ export class FaceCollectionBox extends ViewBoxBaseComponent<FieldViewProps>() {
     render() {
         return (
             <div className="searchBox-container" style={{ pointerEvents: 'all', color: SnappingManager.userColor, background: SnappingManager.userBackgroundColor }}>
-                {FaceRecognitionHandler.FaceDocuments().map(doc => (
-                    <FaceDocumentItem key={doc[Id]} faceDoc={doc} />
+                {FaceRecognitionHandler.UniqueFaces().map(doc => (
+                    <UniqueFaceView key={doc[Id]} faceDoc={doc} />
                 ))}
             </div>
         );
-- 
cgit v1.2.3-70-g09d2

