Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/BackgroundJob/BackgroundService.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,19 @@ public function setLogger(OutputInterface $logger): void {
* @param IUser|null $user ID of user to execute background operations for
* @param int|null $maxImageArea Max image area (in pixels^2) to be fed to neural network when doing face detection
* @param string $runMode The command execution mode
* @param array $forceAnalyzeFiles An array containing the names of files that shall be analyzed
*
* @return void
*/
public function execute(int $timeout, bool $verbose, IUser $user = null, int $maxImageArea = null, string $runMode) {
public function execute(int $timeout, bool $verbose, IUser $user = null, int $maxImageArea = null, string $runMode, $forceAnalyzeFiles = []) {
// Put to context all the stuff we are figuring only now
//
$this->context->user = $user;
$this->context->verbose = $verbose;
$this->context->setRunningThroughCommand();
$this->context->propertyBag['max_image_area'] = $maxImageArea;
$this->context->propertyBag['run_mode'] = $runMode;
$this->context->propertyBag['force_analyze_files'] = $forceAnalyzeFiles;

// Here we are defining all the tasks that will get executed.
//
Expand Down
45 changes: 45 additions & 0 deletions lib/BackgroundJob/Tasks/EnumerateImagesMissingFacesTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

use OCA\FaceRecognition\Db\Image;
use OCA\FaceRecognition\Db\ImageMapper;

use OCA\FaceRecognition\Service\FileService;
use OCA\FaceRecognition\Service\SettingsService;

use OCA\FaceRecognition\BackgroundJob\FaceRecognitionBackgroundTask;
Expand All @@ -36,6 +38,9 @@
*/
class EnumerateImagesMissingFacesTask extends FaceRecognitionBackgroundTask {

/** @var FileService */
private $fileService;

/** @var SettingsService Settings service */
private $settingsService;

Expand All @@ -47,9 +52,11 @@ class EnumerateImagesMissingFacesTask extends FaceRecognitionBackgroundTask {
* @param ImageMapper $imageMapper Image mapper
*/
public function __construct(SettingsService $settingsService,
FileService $fileService,
ImageMapper $imageMapper)
{
parent::__construct();
$this->fileService = $fileService;
$this->settingsService = $settingsService;
$this->imageMapper = $imageMapper;
}
Expand All @@ -73,6 +80,44 @@ public function execute(FaceRecognitionContext $context) {
yield;

shuffle($images);

// add the image that the user explicitly wants to analyze
$forceAnalyzeFiles = $this->context->propertyBag['force_analyze_files'] ;

// get images corresponding to the file names
$forceAnalyzeImages = [];
foreach($forceAnalyzeFiles as $forceAnalyzeFile) {
$file = $this->fileService->getFileByPath($forceAnalyzeFile, $this->context->user->getUID());
if(is_null($file)) {
$this->context->logger->logInfo("ERROR: The file {$this->context->user->getUID()}/files/$forceAnalyzeFile does not exist.");
continue;
}
$this->context->logger->logInfo("Adding {$this->context->user->getUID()}/files/$forceAnalyzeFile to the list of files that will be analyzed.");
$image = $this->imageMapper->findFromFile($this->context->user->getUID(), $this->settingsService->getCurrentFaceModel(), $file->getId());
$image->setUser($this->context->user->getUID());
$image->setModel($this->settingsService->getCurrentFaceModel());
$image->setFile($file->getId());
$this->imageMapper->resetImage($image);
$forceAnalyzeImages[] = $image;
}

// prepend images array with images that we explicitly want to analyze
if(!empty($forceAnalyzeImages)) {
$images = array_merge($forceAnalyzeImages, $images);

// remove dupes
$images2 = [];
foreach($images as $key => $image) {
// echo var_export($key,true) . " => " . var_export($image,true) . "\n";
if(array_key_exists($image->id, $images2)) {
unset($images[$key]);
continue;
}
$images2[$image->id] = true;
}
unset($images2);
}

$this->context->propertyBag['images'] = $images;

return true;
Expand Down
27 changes: 26 additions & 1 deletion lib/Command/BackgroundCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ protected function configure() {
InputOption::VALUE_REQUIRED,
'Sets timeout in seconds for this command. Default is without timeout, e.g. command runs indefinitely.',
0
)
->addOption(
'force_analyze_files',
'f',
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'Forces the given file to be analyzed - even if has been scanned previously.
Can be supplied multiple times to analyze more than one file. Requires that a USER_ID is specified.
If the user does not explicitly specify a mode, the --defer-clustering option will be set automatically.',
[]
);
}

Expand Down Expand Up @@ -171,6 +180,22 @@ protected function execute(InputInterface $input, OutputInterface $output) {
$mode = 'defer-mode';
}

// Check if user wants to (re-) analyze specific images
//
$forceAnalyzeFiles = $input->getOption('force_analyze_files');
if(is_null($userId) and !empty($forceAnalyzeFiles)) {
$output->writeln("FATAL: you must specify a USER_ID when using the --force_analyze_files option.");
return 1;
}
if($mode === 'default-mode' and !empty($forceAnalyzeFiles)) {
// Switch to defer mode to have a more intuitive user experience. Otherwise the user would have to run the background_job again without the -f option to see the result of the analysis.
$output->writeln('INFO: Switching to "defer mode" because the --force_analyze_files option is set.');
$mode = 'defer-mode';
} else if(!($mode === 'analyze-mode' or $mode === 'defer-mode') and !empty($forceAnalyzeFiles)) {
$output->writeln("FATAL: $mode cannot be used together with the --force_analyze_files option.");
return 1;
}

// Extract verbosity (for command, we don't need this, but execute asks for it, if running from cron job).
//
$verbose = $input->getOption('verbose');
Expand All @@ -189,7 +214,7 @@ protected function execute(InputInterface $input, OutputInterface $output) {

// Main thing
//
$this->backgroundService->execute($timeout, $verbose, $user, $maxImageArea, $mode);
$this->backgroundService->execute($timeout, $verbose, $user, $maxImageArea, $mode, $forceAnalyzeFiles);

// Release obtained lock
//
Expand Down
6 changes: 5 additions & 1 deletion lib/Service/FileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,11 @@ public function getFileById(int $fileId, $userId = null): ?Node {
* @return Node | null
*/
public function getFileByPath($fullpath, $userId = null): ?Node {
$file = $this->rootFolder->getUserFolder($this->userId ?? $userId)->get($fullpath);
try {
$file = $this->rootFolder->getUserFolder($this->userId ?? $userId)->get($fullpath);
} catch(NotFoundException $e) {
$file = null;
}
return $file;
}

Expand Down