import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { IconButton, Size } from 'browndash-components'; import * as faceapi from 'face-api.js'; import { FaceMatcher } from 'face-api.js'; import 'ldrs/ring'; import { action, makeObservable, observable } from 'mobx'; import { observer } from 'mobx-react'; import React from 'react'; import { setupMoveUpEvents } from '../../../../ClientUtils'; import { Utils, emptyFunction } from '../../../../Utils'; import { Doc, Opt } from '../../../../fields/Doc'; import { Id } from '../../../../fields/FieldSymbols'; import { List } from '../../../../fields/List'; import { ImageCast } from '../../../../fields/Types'; import { DocumentType } from '../../../documents/DocumentTypes'; import { Docs } from '../../../documents/Documents'; import { DragManager } from '../../../util/DragManager'; import { dropActionType } from '../../../util/DropActionTypes'; import { SnappingManager } from '../../../util/SnappingManager'; import { undoable } from '../../../util/UndoManager'; import { ViewBoxBaseComponent } from '../../DocComponent'; import { ObservableReactComponent } from '../../ObservableReactComponent'; import { DocumentView } from '../../nodes/DocumentView'; import { FieldView, FieldViewProps } from '../../nodes/FieldView'; import { FaceRecognitionHandler } from '../../search/FaceRecognitionHandler'; import './FaceCollectionBox.scss'; import { MarqueeOptionsMenu } from './MarqueeOptionsMenu'; interface FaceDocumentProps { faceDoc: Doc; } /** * A componenent to visually represent a Face Document. */ @observer export class FaceDocumentItem extends ObservableReactComponent { private _dropDisposer?: DragManager.DragDropDisposer; constructor(props: FaceDocumentProps) { super(props); makeObservable(this); } @observable _displayImages: boolean = true; protected createDropTarget = (ele: HTMLDivElement) => { this._dropDisposer?.(); ele && (this._dropDisposer = DragManager.MakeDropTarget(ele, this.onInternalDrop.bind(this), this._props.faceDoc)); }; protected onInternalDrop(e: Event, de: DragManager.DropEvent): boolean { de.complete.docDragData?.droppedDocuments ?.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) { 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 faceMatcher = new FaceMatcher([labeledFaceDescriptor], 1); const { face_match } = FaceRecognitionHandler.ImageDocFaceDescriptors(imgDoc).reduce( (prev, face) => { const match = faceMatcher.matchDescriptor(new Float32Array(Array.from(face))); return match.distance < prev.dist ? { dist: match.distance, face_match: face } : prev; }, { dist: 1, face_match: new List() as Opt> } ); // assign the face in the image that's closest to the face collection to be the face that's assigned to the collection face_match && FaceRecognitionHandler.ImageDocAddFace(imgDoc, face_match, this._props.faceDoc); } }); return false; } /** * Toggles whether a Face Document displays its associated docs. */ @action onDisplayClick() { this._displayImages = !this._displayImages; } /** * Deletes a Face Document. */ deleteFaceDocument = undoable(() => { FaceRecognitionHandler.DeleteFaceDoc(this._props.faceDoc); }, 'delete face'); /** * Deletes a document from a Face Document's associated docs list. * @param doc */ deleteAssociatedDoc = undoable((imgDoc: Doc) => { FaceRecognitionHandler.FaceDocRemoveImageDocFace(imgDoc, this._props.faceDoc); }, 'remove doc from face'); render() { return (
this.createDropTarget(ele!)}>

{FaceRecognitionHandler.FaceDocLabel(this._props.faceDoc)}

this.onDisplayClick()} icon={} color={MarqueeOptionsMenu.Instance.userColor} style={{ width: '19px' }} /> {this._displayImages ? (
{FaceRecognitionHandler.FaceDocFaces(this._props.faceDoc).map(doc => { const [name, type] = ImageCast(doc[Doc.LayoutFieldKey(doc)]).url.href.split('.'); return (
setupMoveUpEvents( this, e, () => { DragManager.StartDocumentDrag([e.target as HTMLElement], new DragManager.DocumentDragData([doc], dropActionType.embed), e.clientX, e.clientY); return true; }, emptyFunction, emptyFunction ) }> DocumentView.showDocument(doc, { willZoomCentered: true })} style={{ maxWidth: '60px', margin: '10px' }} src={`${name}_o.${type}`} />
this.deleteAssociatedDoc(doc)} icon={'x'} style={{ width: '4px' }} size={Size.XSMALL} />
); })}
) : null}
); } } @observer export class FaceCollectionBox extends ViewBoxBaseComponent() { public static LayoutString(fieldKey: string) { return FieldView.LayoutString(FaceCollectionBox, fieldKey); } constructor(props: FieldViewProps) { super(props); makeObservable(this); } render() { return (
{FaceRecognitionHandler.FaceDocuments().map(doc => ( ))}
); } } Docs.Prototypes.TemplateMap.set(DocumentType.FACECOLLECTION, { layout: { view: FaceCollectionBox, dataField: 'data' }, options: { acl: '', _width: 400 }, });