Skip to content

Commit

Permalink
Merge pull request nextcloud#665 from nextcloud/enh/cluster-command
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelklehr authored Feb 2, 2023
2 parents 607b800 + 4523135 commit 8821e15
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 4 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/full-run-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,7 @@ jobs:
- name: Upload photos
run: |
for filename in apps/${{ env.APP_NAME }}/tests/res/*; do
curl -u 'admin:password' -T "$filename" 'http://localhost:8080/remote.php/webdav/'
done
find apps/${{ env.APP_NAME }}/tests/res/ -type f -exec curl -u 'admin:password' -T "{}" 'http://localhost:8080/remote.php/webdav/' \;
- name: Set pure-js mode
run: |
Expand Down Expand Up @@ -191,3 +189,4 @@ jobs:
GITHUB_REF: ${{ github.ref }}
run: |
./occ recognize:classify
./occ recognize:cluster-faces
1 change: 1 addition & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ The app does not send any sensitive data to cloud providers or similar services.
<command>OCA\Recognize\Command\Recrawl</command>
<command>OCA\Recognize\Command\DownloadModels</command>
<command>OCA\Recognize\Command\Classify</command>
<command>OCA\Recognize\Command\ClusterFaces</command>
</commands>

<settings>
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"symfony/process": "^5.2",
"ext-json": "*",
"php": ">=7.4",
"rubix/ml": "^2.0"
"rubix/ml": "^2.0",
"ext-pdo": "*"
},
"autoload": {
"psr-4": {
Expand Down
69 changes: 69 additions & 0 deletions lib/Command/ClusterFaces.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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\FaceDetectionMapper;
use OCA\Recognize\Service\FaceClusterAnalyzer;
use OCA\Recognize\Service\Logger;
use OCP\DB\Exception;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ClusterFaces extends Command {
private Logger $logger;

private FaceDetectionMapper $detectionMapper;

private FaceClusterAnalyzer $clusterAnalyzer;

public function __construct(Logger $logger, FaceDetectionMapper $detectionMapper, FaceClusterAnalyzer $clusterAnalyzer) {
parent::__construct();
$this->logger = $logger;
$this->detectionMapper = $detectionMapper;
$this->clusterAnalyzer = $clusterAnalyzer;
}

/**
* Configure the command
*
* @return void
*/
protected function configure() {
$this->setName('recognize:cluster-faces')
->setDescription('Cluster detected faces per user');
}

/**
* Execute the command
*
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$this->logger->setCliOutput($output);

try {
$userIds = $this->detectionMapper->findUserIds();
} catch (Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
return 1;
}

foreach ($userIds as $userId) {
$this->logger->info('Clustering face detections for user ' . $userId);
try {
$this->clusterAnalyzer->calculateClusters($userId);
} catch (\JsonException|Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
}
}
return 0;
}
}
11 changes: 11 additions & 0 deletions lib/Db/FaceDetectionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ public function findByFileIdAndClusterId(int $fileId, int $clusterId) : FaceDete
return $this->findEntity($qb);
}

/**
* @return list<string>
* @throws \OCP\DB\Exception
*/
public function findUserIds() :array {
$qb = $this->db->getQueryBuilder();
$qb->selectDistinct('d.user_id')
->from('recognize_face_detections', 'd');
return $qb->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);
}

protected function mapRowToEntity(array $row): Entity {
try {
return parent::mapRowToEntity($row);
Expand Down
4 changes: 4 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@
<code>$this-&gt;findEntity($qb)</code>
<code>$this-&gt;findEntity($qb)</code>
</LessSpecificReturnStatement>
<MixedReturnTypeCoercion occurrences="2">
<code>$qb-&gt;executeQuery()-&gt;fetchAll(\PDO::FETCH_COLUMN)</code>
<code>list&lt;string&gt;</code>
</MixedReturnTypeCoercion>
<MoreSpecificReturnType occurrences="6">
<code>FaceDetection</code>
<code>FaceDetection</code>
Expand Down

0 comments on commit 8821e15

Please sign in to comment.