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 option to only retry non-tagged files in classify #1172

Merged
merged 13 commits into from
Sep 13, 2024
23 changes: 21 additions & 2 deletions lib/Command/Classify.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,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\InputInterface;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class Classify extends Command {
Expand All @@ -29,6 +32,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 +57,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 +73,11 @@ 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([
'command' => 'recognize:clear-background-jobs',
]);
$this->clearBackgroundJobs->run($clearBackgroundJobs, $output);

$models = array_values(array_filter([
ClusteringFaceClassifier::MODEL_NAME,
Expand All @@ -77,6 +86,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 All @@ -99,7 +110,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
];
foreach ($this->storageService->getFilesInMount($mount['storage_id'], $mount['override_root'], $models, $lastFileId) as $file) {
$i++;
// if retry flag is set, skip tagged files
$lastFileId = $file['fileid'];
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;
}
}
$queueFile = new QueueFile();
$queueFile->setStorageId($mount['storage_id']);
$queueFile->setRootId($mount['root_id']);
Expand Down