Skip to content

Commit

Permalink
Add command: recognize:reset-face-clusters
Browse files Browse the repository at this point in the history
Signed-off-by: Marcel Klehr <[email protected]>
  • Loading branch information
marcelklehr committed Feb 9, 2023
1 parent 75e1641 commit bae933d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ The app does not send any sensitive data to cloud providers or similar services.

<commands>
<command>OCA\Recognize\Command\ResetFaces</command>
<command>OCA\Recognize\Command\ResetFaceClusters</command>
<command>OCA\Recognize\Command\ResetTags</command>
<command>OCA\Recognize\Command\RemoveLegacyTags</command>
<command>OCA\Recognize\Command\CleanupTags</command>
Expand Down
55 changes: 55 additions & 0 deletions lib/Command/ResetFaceClusters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/*
* Copyright (c) 2022 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
*/

namespace OCA\Recognize\Command;

use OCA\Recognize\Db\FaceClusterMapper;
use OCA\Recognize\Db\FaceDetectionMapper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ResetFaceClusters extends Command {
private FaceDetectionMapper $faceDetectionMapper;
private FaceClusterMapper $clusterMapper;

public function __construct(FaceDetectionMapper $faceDetectionMapper, FaceClusterMapper $clusterMapper) {
parent::__construct();
$this->faceDetectionMapper = $faceDetectionMapper;
$this->clusterMapper = $clusterMapper;
}

/**
* Configure the command
*
* @return void
*/
protected function configure() {
$this->setName('recognize:reset-face-clusters')
->setDescription('Remove all face clusters. Detected face will stay intact.');
}

/**
* Execute the command
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
try {
$this->clusterMapper->deleteAll();
$this->faceDetectionMapper->removeAllClusters();
} catch (\Exception $ex) {
$output->writeln('<error>Failed to reset face clusters</error>');
$output->writeln($ex->getMessage());
return 1;
}

return 0;
}
}
8 changes: 8 additions & 0 deletions lib/Db/FaceDetectionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ public function findClusterSample(int $clusterId, int $n) {
return $this->findEntities($qb);
}

public function removeAllClusters(): void {
$qb = $this->db->getQueryBuilder();
$qb->update('recognize_face_detections')
->set('cluster_id', $qb->createPositionalParameter(null))
->where($qb->expr()->isNotNull('cluster_id'));
$qb->executeStatement();
}

protected function mapRowToEntity(array $row): Entity {
try {
return parent::mapRowToEntity($row);
Expand Down

0 comments on commit bae933d

Please sign in to comment.