Skip to content

Commit

Permalink
Merge pull request #1221 from nextcloud/backport/1172/stable8
Browse files Browse the repository at this point in the history
[stable8] Add option to only retry non-tagged files in classify
  • Loading branch information
marcelklehr authored Dec 8, 2024
2 parents 06604ae + ad3e629 commit 5db4021
Show file tree
Hide file tree
Showing 70 changed files with 162 additions and 65 deletions.
7 changes: 4 additions & 3 deletions lib/BackgroundJobs/ClassifierJob.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down Expand Up @@ -71,15 +72,15 @@ protected function runClassifier(string $model, array $argument): void {
try {
$this->logger->debug('Running ' . $model . ' classifier');
$this->classify($files);
} catch(\RuntimeException $e) {
} catch (\RuntimeException $e) {
$this->logger->warning('Temporary problem with ' . $model . ' classifier, trying again soon', ['exception' => $e]);
} catch(\ErrorException $e) {
} catch (\ErrorException $e) {
$this->settingsService->setSetting($model.'.status', 'false');
$this->logger->warning('Problem with ' . $model . ' classifier', ['exception' => $e]);
$this->logger->debug('Removing '.static::class.' with argument ' . var_export($argument, true) . 'from oc_jobs');
$this->jobList->remove(static::class, $argument);
throw $e;
} catch(\Throwable $e) {
} catch (\Throwable $e) {
$this->settingsService->setSetting($model.'.status', 'false');
throw $e;
}
Expand Down
1 change: 1 addition & 0 deletions lib/BackgroundJobs/ClassifyFacesJob.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/BackgroundJobs/ClassifyImagenetJob.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/BackgroundJobs/ClassifyLandmarksJob.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/BackgroundJobs/ClassifyMovinetJob.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/BackgroundJobs/ClassifyMusicnnJob.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/BackgroundJobs/ClusterFacesJob.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/BackgroundJobs/MaintenanceJob.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2024 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
Expand Down
1 change: 1 addition & 0 deletions lib/BackgroundJobs/SchedulerJob.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2021-2022 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
Expand Down
2 changes: 1 addition & 1 deletion lib/BackgroundJobs/StorageCrawlJob.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2021-2022 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
Expand Down Expand Up @@ -77,7 +78,6 @@ protected function run($argument): void {
}
if (!in_array(ImagenetClassifier::MODEL_NAME, $models) && in_array(LandmarksClassifier::MODEL_NAME, $models)) {
$tags = $this->tagManager->getTagsForFiles([$queueFile->getFileId()]);
/** @var \OCP\SystemTag\ISystemTag[] $fileTags */
$fileTags = $tags[$queueFile->getFileId()];
$landmarkTags = array_filter($fileTags, function ($tag) {
return in_array($tag->getName(), LandmarksClassifier::PRECONDITION_TAGS);
Expand Down
1 change: 1 addition & 0 deletions lib/Classifiers/Audio/MusicnnClassifier.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2021-2022 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
Expand Down
1 change: 1 addition & 0 deletions lib/Classifiers/Classifier.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2021-2022 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
Expand Down
1 change: 1 addition & 0 deletions lib/Classifiers/Images/ClusteringFaceClassifier.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2021-2022 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
Expand Down
1 change: 1 addition & 0 deletions lib/Classifiers/Images/ImagenetClassifier.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2021-2022 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
Expand Down
1 change: 1 addition & 0 deletions lib/Classifiers/Images/LandmarksClassifier.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2021-2022 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
Expand Down
1 change: 1 addition & 0 deletions lib/Classifiers/Video/MovinetClassifier.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2021-2022 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
Expand Down
1 change: 1 addition & 0 deletions lib/Clustering/DualTreeBall.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2023 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
Expand Down
1 change: 1 addition & 0 deletions lib/Clustering/DualTreeClique.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2023 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
Expand Down
1 change: 1 addition & 0 deletions lib/Clustering/HDBSCAN.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2023 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
Expand Down
1 change: 1 addition & 0 deletions lib/Clustering/MrdBallTree.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2023 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
Expand Down
1 change: 1 addition & 0 deletions lib/Clustering/MstClusterer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2023 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
Expand Down
1 change: 1 addition & 0 deletions lib/Clustering/MstSolver.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2023 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
Expand Down
1 change: 1 addition & 0 deletions lib/Clustering/SquaredDistance.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2023 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
Expand Down
30 changes: 25 additions & 5 deletions lib/Command/Classify.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand All @@ -16,11 +17,14 @@
use OCA\Recognize\Service\Logger;
use OCA\Recognize\Service\SettingsService;
use OCA\Recognize\Service\StorageService;
use OCA\Recognize\Service\TagManager;
use OCP\Files\Config\ICachedMountInfo;
use OCP\Files\Config\IUserMountCache;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\ExceptionInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class Classify extends Command {
Expand All @@ -29,6 +33,7 @@ class Classify extends Command {

public function __construct(
private StorageService $storageService,
private TagManager $tagManager,
private Logger $logger,
ImagenetClassifier $imagenet,
ClusteringFaceClassifier $faces,
Expand All @@ -53,7 +58,8 @@ public function __construct(
*/
protected function configure() {
$this->setName('recognize:classify')
->setDescription('Classify all files with the current settings in one go (will likely take a long time)');
->setDescription('Classify all files with the current settings in one go (will likely take a long time)')
->addOption('retry', null, InputOption::VALUE_NONE, "Only classify untagged images");
}

/**
Expand All @@ -68,7 +74,9 @@ protected function configure() {
protected function execute(InputInterface $input, OutputInterface $output): int {
$this->logger->setCliOutput($output);

$this->clearBackgroundJobs->run($input, $output);
// pop "retry" flag from parameters passed to clear background jobs
$clearBackgroundJobs = new ArrayInput([]);
$this->clearBackgroundJobs->run($clearBackgroundJobs, $output);

$models = array_values(array_filter([
ClusteringFaceClassifier::MODEL_NAME,
Expand All @@ -77,6 +85,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
MusicnnClassifier::MODEL_NAME,
], fn ($modelName) => $this->settings->getSetting($modelName . '.enabled') === 'true'));

$processedTag = $this->tagManager->getProcessedTag();

foreach ($this->storageService->getMounts() as $mount) {
$this->logger->info('Processing storage ' . $mount['storage_id'] . ' with root ID ' . $mount['override_root']);

Expand Down Expand Up @@ -107,13 +117,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$queueFile->setUpdate(false);

if ($file['image']) {
if (in_array(ImagenetClassifier::MODEL_NAME, $models)) {
$queues[ImagenetClassifier::MODEL_NAME][] = $queueFile;
}
if (in_array(ClusteringFaceClassifier::MODEL_NAME, $models)) {
$queues[ClusteringFaceClassifier::MODEL_NAME][] = $queueFile;
}
}
// if retry flag is set, skip other classifiers for tagged files
if ($input->getOption('retry')) {
$fileTags = $this->tagManager->getTagsForFiles([$lastFileId]);
// check if processed tag is already in the tags
if (in_array($processedTag, $fileTags[$lastFileId])) {
continue;
}
}
if ($file['image']) {
if (in_array(ImagenetClassifier::MODEL_NAME, $models)) {
$queues[ImagenetClassifier::MODEL_NAME][] = $queueFile;
}
}
if ($file['video']) {
if (in_array(MovinetClassifier::MODEL_NAME, $models)) {
$queues[MovinetClassifier::MODEL_NAME][] = $queueFile;
Expand Down
1 change: 1 addition & 0 deletions lib/Command/CleanupTags.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Command/ClearBackgroundJobs.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* Copyright (c) 2023 The Recognize contributors.
* This file is licensed under the Affero General Public License version 3 or later. See the COPYING file.
Expand Down
1 change: 1 addition & 0 deletions lib/Command/ClusterFaces.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Command/DownloadModels.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Command/Recrawl.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Command/RemoveLegacyTags.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Command/ResetFaceClusters.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Command/ResetFaces.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Command/ResetTags.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Constants.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function hasJobs(string $task): JSONResponse {
}
$iterator = $this->jobList->getJobsIterator($tasks[$task], null, 0);
$lastRun = [];
foreach($iterator as $job) {
foreach ($iterator as $job) {
$lastRun[] = $job->getLastRun();
}
$count = count($lastRun);
Expand Down
1 change: 1 addition & 0 deletions lib/Dav/Faces/FacePhoto.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Dav/Faces/FaceRoot.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Dav/Faces/FacesHome.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Dav/Faces/PropFindPlugin.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Dav/Faces/UnassignedFacePhoto.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Dav/Faces/UnassignedFacesHome.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Dav/RecognizeHome.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Dav/RootCollection.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Db/FaceCluster.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Db/FaceClusterMapper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Db/FaceDetection.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Db/FaceDetectionMapper.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
1 change: 1 addition & 0 deletions lib/Db/FaceDetectionWithTitle.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?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.
Expand Down
Loading

0 comments on commit 5db4021

Please sign in to comment.