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
3 changes: 3 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ The app does not send any sensitive data to cloud providers or similar services.
<post-migration>
<step>OCA\Recognize\Migration\InstallDeps</step>
</post-migration>
<post-migration>
<step>OCA\Recognize\Migration\MoveDefaultModelFolder</step>
</post-migration>
<live-migration>
<step>OCA\Recognize\Migration\RemoveDuplicateFaceDetections</step>
</live-migration>
Expand Down
68 changes: 68 additions & 0 deletions lib/Migration/MoveDefaultModelFolder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2020, Joas Schilling <coding@schilljs.com>
* @copyright Copyright (c) 2021, Marcel Klehr <mklehr@gmx.net>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Recognize\Migration;

use OCA\Recognize\Service\SettingsService;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
use Psr\Log\LoggerInterface;
use function Safe\rename;
use function Safe\scandir;

final class MoveDefaultModelFolder implements IRepairStep {

public function __construct(
private SettingsService $settingsService,
private LoggerInterface $logger,
) {
}

public function getName(): string {
return 'Try to move the default Model Folder';
}

public function run(IOutput $output): void {
$oldModelTargetPath = __DIR__ . '/../../models';
$oldModelArchivePath = __DIR__ . '/../../models.tar.gz';
$newPath = $this->settingsService->getSetting('models_target_path');
$newModelTargetPath = $newPath . '/models';
$newModelArchivePath = $newPath . '/models.tar.gz';

if (is_dir($oldModelTargetPath)) {
$filesToMove = scandir($oldModelTargetPath);
$filesToMove = array_filter($filesToMove, fn ($value) => $value !== '.' && $value === '..');
$filesToMove = array_map(fn ($value) => $oldModelTargetPath.'/'.$value, $filesToMove);
mkdir($newModelTargetPath);
foreach ($filesToMove as $file) {
rename($file, $newModelTargetPath.'/'.basename($file));
}
}

if (is_file($oldModelArchivePath)) {
rename($oldModelArchivePath, $newModelArchivePath);
}
}
}
15 changes: 9 additions & 6 deletions lib/Service/DownloadModelsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@
final class DownloadModelsService {
private IClientService $clientService;
private bool $isCLI;
private SettingsService $settingsService;

public function __construct(IClientService $clientService, bool $isCLI) {
public function __construct(IClientService $clientService, bool $isCLI, SettingsService $settingsService) {
$this->clientService = $clientService;
$this->isCLI = $isCLI;
$this->settingsService = $settingsService;
}

/**
* @return void
* @throws \Exception
*/
public function download() : void {
$targetPath = __DIR__ . '/../../models';
if (file_exists($targetPath)) {
$targetPath = $this->settingsService->getSetting('models_target_path');
$modelPath = $targetPath . '/models';
if (file_exists($modelPath)) {
// remove models directory
$it = new RecursiveDirectoryIterator($targetPath, FilesystemIterator::SKIP_DOTS);
$it = new RecursiveDirectoryIterator($modelPath, FilesystemIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($it,
RecursiveIteratorIterator::CHILD_FIRST);
foreach ($files as $file) {
Expand All @@ -40,11 +43,11 @@ public function download() : void {
unlink($file->getRealPath());
}
}
rmdir($targetPath);
rmdir($modelPath);
}

$archiveUrl = $this->getArchiveUrl($this->getNeededArchiveRef());
$archivePath = __DIR__ . '/../../models.tar.gz';
$archivePath = $targetPath . '/models.tar.gz';
$timeout = $this->isCLI ? 0 : 480;
$this->clientService->newClient()->get($archiveUrl, ['sink' => $archivePath, 'timeout' => $timeout]);
$tarManager = new TAR($archivePath);
Expand Down
31 changes: 30 additions & 1 deletion lib/Service/SettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ final class SettingsService {
'nice_value' => '0',
'concurrency.enabled' => 'false',
'ffmpeg_binary' => '',
'models_target_path' => '../../models_cache',
];

/** @var array<string,string> */
Expand Down Expand Up @@ -94,7 +95,13 @@ final class SettingsService {
'landmarks.batchSize',
'movinet.batchSize',
'musicnn.batchSize',
'concurrency.enabled'
'concurrency.enabled',
'models_target_path',
'models_archive_file',
];

private const PATH_SETTINGS = [
'models_target_path',
];

private IAppConfig $config;
Expand All @@ -121,6 +128,14 @@ public function getSetting(string $key): string {
if (in_array($key, self::LAZY_SETTINGS, true)) {
$lazy = true;
}

if (in_array($key, self::PATH_SETTINGS, true)) {
$path = $this->config->getAppValueString($key, self::DEFAULTS[$key], lazy: $lazy);
if (!$this->isPathAbsolute($path)) {
$path = __DIR__ .'/'. $path;
}
return $path;
}
return $this->config->getAppValueString($key, self::DEFAULTS[$key], lazy: $lazy);
}

Expand Down Expand Up @@ -182,6 +197,9 @@ public function setSetting(string $key, string $value): void {
if (in_array($key, self::LAZY_SETTINGS, true)) {
$lazy = true;
}
if (in_array($key, self::PATH_SETTINGS) && $value === '') {
$value = self::DEFAULTS[$key];
}
$this->config->setAppValueString($key, $value, lazy: $lazy);
}

Expand All @@ -195,4 +213,15 @@ public function getAll(): array {
}
return $settings;
}

private function isPathAbsolute(string $path): bool {
if ($path === '') {
return false;
}
if ($path[0] === '/') {
return true;
}

return false;
}
}
6 changes: 4 additions & 2 deletions lib/Settings/AdminSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ public function getForm(): TemplateResponse {
$settings = $this->settingsService->getAll();
$this->initialState->provideInitialState('settings', $settings);

$modelsPath = __DIR__ . '/../../models';
$modelsDownloaded = file_exists($modelsPath);
$targetPath = $this->settingsService->getSetting('models_target_path');
$modelsDownloaded = file_exists($targetPath .' /models');
$modelsTargetPathWritable = is_writable($targetPath);
$this->initialState->provideInitialState('modelsDownloaded', $modelsDownloaded);
$this->initialState->provideInitialState('modelsTargetPathWritable', $modelsTargetPathWritable);

$tagsEnabled = $this->appManager->isEnabledForAnyone('systemtags');
$this->initialState->provideInitialState('tagsEnabled', $tagsEnabled);
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 25 additions & 6 deletions src/components/ViewAdmin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@
</p>
</template>
</NcSettingsSection>
<NcSettingsSection :name="t('recognize', 'Model Setting')">
<NcNoteCard v-if="modelsTargetPathWritable" show-alert type="success">
{{ t('recognize', 'The Model Target Path is Writable') }}
</NcNoteCard>
<NcNoteCard v-else-if="!modelsTargetPathWritable" show-alert type="warning">
{{ t('recognize', 'Model Target Path is not Writable') }}
</NcNoteCard>
<p>
<NcTextField v-model="settings['models_target_path']"
:label="t('recognize', 'Path Model Folder')"
:label-visible="true"
@update:model-value="onChange" />
</p>
<p>
{{ t('recognize', 'Changing this Value will require you to redownload the Models. Also the models in the old Directory need to be deleted manually') }}
</p>
</NcSettingsSection>
<NcSettingsSection :name="t('recognize', 'Classifier backend')">
<NcNoteCard v-if="recognizeBackendInstalled" type="success">
{{ t('recognize', 'The recognize_backend ExApp is installed; TaskProcessing mode is recommended.') }}
Expand Down Expand Up @@ -81,8 +98,8 @@
{{ t('recognize', 'Enable face recognition (groups photos by faces that appear in them; UI is in the photos app)') }}
</NcCheckboxRadioSwitch>
<NcTextField v-if="!settings['taskprocessing.enabled']"
:disabled="!settings['faces.enabled']"
v-model="settings['faces.batchSize']"
:disabled="!settings['faces.enabled']"
:label-visible="true"
:label="t('recognize', 'The number of files to process per job run (A job will be scheduled every 5 minutes; For normal operation ~500 or more, in WASM mode ~50 is recommended)')"
:title="t('recognize', 'The number of files to process per job run (A job will be scheduled every 5 minutes; For normal operation ~500 or more, in WASM mode ~50 is recommended)')"
Expand Down Expand Up @@ -130,8 +147,8 @@
{{ t('recognize', 'Enable object recognition (e.g. food, vehicles, landscapes)') }}
</NcCheckboxRadioSwitch>
<NcTextField v-if="!settings['taskprocessing.enabled']"
:disabled="!settings['imagenet.enabled']"
v-model="settings['imagenet.batchSize']"
:disabled="!settings['imagenet.enabled']"
:label-visible="true"
:label="t('recognize', 'The number of files to process per job run (A job will be scheduled every 5 minutes; For normal operation ~100 or more, in WASM mode ~20 is recommended)')"
:title="t('recognize', 'The number of files to process per job run (A job will be scheduled every 5 minutes; For normal operation ~100 or more, in WASM mode ~20 is recommended)')"
Expand All @@ -146,8 +163,8 @@
{{ t('recognize', 'Enable landmark recognition (e.g. Eiffel Tower, Golden Gate Bridge)') }}
</NcCheckboxRadioSwitch>
<NcTextField v-if="!settings['taskprocessing.enabled']"
:disabled="!settings['imagenet.enabled'] || !settings['landmarks.enabled']"
v-model="settings['landmarks.batchSize']"
:disabled="!settings['imagenet.enabled'] || !settings['landmarks.enabled']"
:label-visible="true"
:label="t('recognize', 'The number of files to process per job run (A job will be scheduled every 5 minutes; For normal operation ~100 or more, in WASM mode ~20 is recommended)')"
:title="t('recognize', 'The number of files to process per job run (A job will be scheduled every 5 minutes; For normal operation ~100 or more, in WASM mode ~20 is recommended)')"
Expand Down Expand Up @@ -177,8 +194,8 @@
{{ t('recognize', 'Enable music genre recognition (e.g. pop, rock, folk, metal, new age)') }}
</NcCheckboxRadioSwitch>
<NcTextField v-if="!settings['taskprocessing.enabled']"
:disabled="!settings['musicnn.enabled']"
v-model="settings['musicnn.batchSize']"
:disabled="!settings['musicnn.enabled']"
:label-visible="true"
:label="t('recognize', 'The number of files to process per job run (A job will be scheduled every 5 minutes; For normal operation ~100 or more, in WASM mode ~20 is recommended)')"
:title="t('recognize', 'The number of files to process per job run (A job will be scheduled every 5 minutes; For normal operation ~100 or more, in WASM mode ~20 is recommended)')"
Expand Down Expand Up @@ -211,8 +228,8 @@
{{ t('recognize', 'Enable human action recognition (e.g. arm wrestling, dribbling basketball, hula hooping)') }}
</NcCheckboxRadioSwitch>
<NcTextField v-if="!settings['taskprocessing.enabled']"
:disabled="!settings['movinet.enabled']"
v-model="settings['movinet.batchSize']"
:disabled="!settings['movinet.enabled']"
:label-visible="true"
:label="t('recognize', 'The number of files to process per job run (A job will be scheduled every 5 minutes; For normal operation ~20 or more, in WASM mode ~5 is recommended)')"
:title="t('recognize', 'The number of files to process per job run (A job will be scheduled every 5 minutes; For normal operation ~20 or more, in WASM mode ~5 is recommended)')"
Expand Down Expand Up @@ -445,7 +462,7 @@ const TASK_PROCESSING_TASK_TYPES = {
musicnn: 'recognize:audio:classification',
}

const SETTINGS = ['tensorflow.cores', 'tensorflow.gpu', 'tensorflow.purejs', 'imagenet.enabled', 'landmarks.enabled', 'faces.enabled', 'musicnn.enabled', 'movinet.enabled', 'node_binary', 'ffmpeg_binary', 'faces.status', 'imagenet.status', 'landmarks.status', 'movinet.status', 'musicnn.status', 'faces.lastFile', 'imagenet.lastFile', 'landmarks.lastFile', 'movinet.lastFile', 'musicnn.lastFile', 'faces.batchSize', 'imagenet.batchSize', 'landmarks.batchSize', 'movinet.batchSize', 'musicnn.batchSize', 'clusterFaces.status', 'clusterFaces.lastRun', 'nice_binary', 'nice_value', 'concurrency.enabled', 'taskprocessing.enabled']
const SETTINGS = ['tensorflow.cores', 'tensorflow.gpu', 'tensorflow.purejs', 'imagenet.enabled', 'landmarks.enabled', 'faces.enabled', 'musicnn.enabled', 'movinet.enabled', 'node_binary', 'ffmpeg_binary', 'faces.status', 'imagenet.status', 'landmarks.status', 'movinet.status', 'musicnn.status', 'faces.lastFile', 'imagenet.lastFile', 'landmarks.lastFile', 'movinet.lastFile', 'musicnn.lastFile', 'faces.batchSize', 'imagenet.batchSize', 'landmarks.batchSize', 'movinet.batchSize', 'musicnn.batchSize', 'clusterFaces.status', 'clusterFaces.lastRun', 'nice_binary', 'nice_value', 'concurrency.enabled', 'taskprocessing.enabled', 'models_target_path', 'models_archive_file']

const BOOLEAN_SETTINGS = ['tensorflow.gpu', 'tensorflow.purejs', 'imagenet.enabled', 'landmarks.enabled', 'faces.enabled', 'musicnn.enabled', 'movinet.enabled', 'faces.status', 'imagenet.status', 'landmarks.status', 'movinet.status', 'musicnn.status', 'faces.lastFile', 'imagenet.lastFile', 'landmarks.lastFile', 'movinet.lastFile', 'musicnn.lastFile', 'clusterFaces.status', 'concurrency.enabled', 'taskprocessing.enabled']

Expand Down Expand Up @@ -487,6 +504,7 @@ export default {
musicnnTpStats: null,
tagsEnabled: null,
recognizeBackendInstalled: false,
modelsTargetPathWritable: null,
}
},

Expand Down Expand Up @@ -523,6 +541,7 @@ export default {
},
async created() {
this.modelsDownloaded = loadState('recognize', 'modelsDownloaded')
this.modelsTargetPathWritable = loadState('recognize', 'modelsTargetPathWritable')
this.tagsEnabled = loadState('recognize', 'tagsEnabled')
this.recognizeBackendInstalled = loadState('recognize', 'recognizeBackendInstalled', false)
this.getCount()
Expand Down