Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multiple face group merge support #912

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions api/graphql/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 31 additions & 15 deletions api/graphql/resolvers/faces.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,11 @@
return faceGroup, nil
}

func (r *mutationResolver) CombineFaceGroups(ctx context.Context, destinationFaceGroupID int, sourceFaceGroupID int) (*models.FaceGroup, error) {
func (r *mutationResolver) CombineFaceGroups(ctx context.Context, destinationFaceGroupID int, sourceFaceGroupIDs []int) (*models.FaceGroup, error) {
if len(sourceFaceGroupIDs) == 0 {
return nil, errors.New("at least one source face group ID is required")

Check warning on line 223 in api/graphql/resolvers/faces.go

View check run for this annotation

Codecov / codecov/patch

api/graphql/resolvers/faces.go#L222-L223

Added lines #L222 - L223 were not covered by tests
}

db := r.DB(ctx)
user := auth.UserFromContext(ctx)
if user == nil {
Expand All @@ -234,28 +238,40 @@
return nil, err
}

sourceFaceGroup, err := userOwnedFaceGroup(db, user, sourceFaceGroupID)
if err != nil {
return nil, err
}
var sourceFaceGroups []*models.FaceGroup

Check warning on line 241 in api/graphql/resolvers/faces.go

View check run for this annotation

Codecov / codecov/patch

api/graphql/resolvers/faces.go#L241

Added line #L241 was not covered by tests

updateError := db.Transaction(func(tx *gorm.DB) error {
if err := tx.Model(&models.ImageFace{}).Where("face_group_id = ?", sourceFaceGroup.ID).Update("face_group_id", destinationFaceGroup.ID).Error; err != nil {
return err
for _, sourceID := range sourceFaceGroupIDs {
if sourceID == destinationFaceGroup.ID {
return nil, errors.New("source face groups cannot include the destination face group")

Check warning on line 245 in api/graphql/resolvers/faces.go

View check run for this annotation

Codecov / codecov/patch

api/graphql/resolvers/faces.go#L244-L245

Added lines #L244 - L245 were not covered by tests
}

if err := tx.Delete(&sourceFaceGroup).Error; err != nil {
return err
sourceFaceGroup, err := userOwnedFaceGroup(db, user, sourceID)
if err != nil {
return nil, err

Check warning on line 250 in api/graphql/resolvers/faces.go

View check run for this annotation

Codecov / codecov/patch

api/graphql/resolvers/faces.go#L248-L250

Added lines #L248 - L250 were not covered by tests
}

return nil
})
sourceFaceGroups = append(sourceFaceGroups, sourceFaceGroup)

Check warning on line 253 in api/graphql/resolvers/faces.go

View check run for this annotation

Codecov / codecov/patch

api/graphql/resolvers/faces.go#L253

Added line #L253 was not covered by tests
}
for _, sourceFaceGroup := range sourceFaceGroups {
updateError := db.Transaction(func(tx *gorm.DB) error {

if err := tx.Model(&models.ImageFace{}).Where("face_group_id = ?", sourceFaceGroup.ID).Update("face_group_id", destinationFaceGroup.ID).Error; err != nil {
return err

Check warning on line 259 in api/graphql/resolvers/faces.go

View check run for this annotation

Codecov / codecov/patch

api/graphql/resolvers/faces.go#L258-L259

Added lines #L258 - L259 were not covered by tests
}

if updateError != nil {
return nil, updateError
if err := tx.Delete(&sourceFaceGroup).Error; err != nil {
return err

Check warning on line 263 in api/graphql/resolvers/faces.go

View check run for this annotation

Codecov / codecov/patch

api/graphql/resolvers/faces.go#L262-L263

Added lines #L262 - L263 were not covered by tests
}

return nil

Check warning on line 266 in api/graphql/resolvers/faces.go

View check run for this annotation

Codecov / codecov/patch

api/graphql/resolvers/faces.go#L266

Added line #L266 was not covered by tests
})

if updateError != nil {
return nil, updateError

Check warning on line 270 in api/graphql/resolvers/faces.go

View check run for this annotation

Codecov / codecov/patch

api/graphql/resolvers/faces.go#L269-L270

Added lines #L269 - L270 were not covered by tests
}
}

face_detection.GlobalFaceDetector.MergeCategories(int32(sourceFaceGroupID), int32(destinationFaceGroupID))
face_detection.GlobalFaceDetector.MergeImageFaces(sourceFaceGroupIDs, int32(destinationFaceGroupID))

Check warning on line 274 in api/graphql/resolvers/faces.go

View check run for this annotation

Codecov / codecov/patch

api/graphql/resolvers/faces.go#L274

Added line #L274 was not covered by tests

return destinationFaceGroup, nil
}
Expand Down
2 changes: 1 addition & 1 deletion api/graphql/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ type Mutation {
"Assign a label to a face group, set label to null to remove the current one"
setFaceGroupLabel(faceGroupID: ID!, label: String): FaceGroup! @isAuthorized
"Merge two face groups into a single one, all ImageFaces from source will be moved to destination"
combineFaceGroups(destinationFaceGroupID: ID!, sourceFaceGroupID: ID!): FaceGroup! @isAuthorized
combineFaceGroups(destinationFaceGroupID: ID!, sourceFaceGroupIDs: [ID!]!): FaceGroup! @isAuthorized
"Move a list of ImageFaces to another face group"
moveImageFaces(imageFaceIDs: [ID!]!, destinationFaceGroupID: ID!): FaceGroup! @isAuthorized
"Check all unlabeled faces to see if they match a labeled FaceGroup, and move them if they match"
Expand Down
61 changes: 49 additions & 12 deletions ui/src/Pages/PeoplePage/PeoplePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import {
import { recognizeUnlabeledFaces } from './__generated__/recognizeUnlabeledFaces'
import { isNil, tailwindClassNames } from '../../helpers/utils'
import classNames from 'classnames'
import MergeFaceGroupsModal, {
MergeFaceGroupsModalState,
} from './SingleFaceGroup/MergeFaceGroupsModal'

export const MY_FACES_QUERY = gql`
query myFaces($limit: Int, $offset: Int) {
Expand Down Expand Up @@ -254,6 +257,10 @@ export const PeoplePage = () => {
},
})

const [mergeModalState, setMergeModalState] = useState(
MergeFaceGroupsModalState.Closed
)

const [recognizeUnlabeled, { loading: recognizeUnlabeledLoading }] =
useMutation<recognizeUnlabeledFaces>(RECOGNIZE_UNLABELED_FACES_MUTATION)

Expand All @@ -269,31 +276,61 @@ export const PeoplePage = () => {
return <div>{error.message}</div>
}

let faces = null
let faces: JSX.Element[] | null = null
if (data) {
faces = data.myFaceGroups.map(faceGroup => (
<FaceGroup key={faceGroup.id} group={faceGroup} />
))
}

let modals = null
modals = (
<>
<MergeFaceGroupsModal
state={mergeModalState}
setState={setMergeModalState}
refetchQueries={[
{
query: MY_FACES_QUERY,
},
]}
/>
</>
)

return (
<Layout title={t('title.people', 'People')}>
<Button
disabled={recognizeUnlabeledLoading}
onClick={() => {
recognizeUnlabeled()
}}
>
{t(
'people_page.recognize_unlabeled_faces_button',
'Recognize unlabeled faces'
)}
</Button>
<ul className="flex gap-2 flex-wrap mb-6">
<li>
<Button
disabled={recognizeUnlabeledLoading}
onClick={() => {
recognizeUnlabeled()
}}
>
{t(
'people_page.recognize_unlabeled_faces_button',
'Recognize unlabeled faces'
)}
</Button>
</li>
<li>
<Button
onClick={() => {
setMergeModalState(MergeFaceGroupsModalState.SelectDestination)
}}
>
{t('people_page.action_label.merge_people', 'Merge people')}
</Button>
</li>
</ul>

<FaceGroupsWrapper ref={containerElem}>{faces}</FaceGroupsWrapper>
<PaginateLoader
active={!finishedLoadingMore && !loading}
text={t('general.loading.paginate.faces', 'Loading more people')}
/>
{modals}
</Layout>
)
}
Expand Down
22 changes: 15 additions & 7 deletions ui/src/Pages/PeoplePage/SingleFaceGroup/FaceGroupTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import {
setGroupLabelVariables,
} from '../__generated__/setGroupLabel'
import DetachImageFacesModal from './DetachImageFacesModal'
import MergeFaceGroupsModal from './MergeFaceGroupsModal'
import MergeFaceGroupsModal, {
MergeFaceGroupsModalState,
} from './MergeFaceGroupsModal'
import MoveImageFacesModal from './MoveImageFacesModal'
import { singleFaceGroup_faceGroup } from './__generated__/singleFaceGroup'

Expand All @@ -28,7 +30,9 @@ const FaceGroupTitle = ({ faceGroup }: FaceGroupTitleProps) => {
const [editLabel, setEditLabel] = useState(false)
const [inputValue, setInputValue] = useState(faceGroup?.label ?? '')
const inputRef = createRef<HTMLInputElement>()
const [mergeModalOpen, setMergeModalOpen] = useState(false)
const [mergeModalState, setMergeModalState] = useState(
MergeFaceGroupsModalState.Closed
)
const [moveModalOpen, setMoveModalOpen] = useState(false)
const [detachModalOpen, setDetachModalOpen] = useState(false)

Expand Down Expand Up @@ -109,9 +113,9 @@ const FaceGroupTitle = ({ faceGroup }: FaceGroupTitleProps) => {
modals = (
<>
<MergeFaceGroupsModal
open={mergeModalOpen}
setOpen={setMergeModalOpen}
sourceFaceGroup={faceGroup}
state={mergeModalState}
setState={setMergeModalState}
preselectedDestinationFaceGroup={faceGroup}
refetchQueries={[
{
query: MY_FACES_QUERY,
Expand Down Expand Up @@ -143,8 +147,12 @@ const FaceGroupTitle = ({ faceGroup }: FaceGroupTitleProps) => {
</Button>
</li>
<li>
<Button onClick={() => setMergeModalOpen(true)}>
{t('people_page.action_label.merge_people', 'Merge people')}
<Button
onClick={() =>
setMergeModalState(MergeFaceGroupsModalState.SelectDestination)
}
>
{t('people_page.action_label.merge_face', 'Merge face')}
</Button>
</li>
<li>
Expand Down