Skip to content
Merged
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
61 changes: 57 additions & 4 deletions lib/Service/SettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@
use OCA\Recognize\Classifiers\Images\ClusteringFaceClassifier;
use OCA\Recognize\Classifiers\Images\ImagenetClassifier;
use OCA\Recognize\Classifiers\Images\LandmarksClassifier;
use OCA\Recognize\Classifiers\TaskProcessing\ImageFaceRecognitionClassifier;
use OCA\Recognize\Classifiers\Video\MovinetClassifier;
use OCA\Recognize\Exception\Exception;
use OCP\AppFramework\Services\IAppConfig;
use OCP\BackgroundJob\IJobList;
use OCP\DB\Exception as DBException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IDBConnection;
use OCP\Server;

final class SettingsService {
/**
* App id of the ExApp that provides TaskProcessing classifiers; when installed
* and enabled, taskprocessing mode is on by default.
* and enabled, taskprocessing mode is on by default, unless that would mix
* face embeddings of different models (see getTaskProcessingDefault()).
*/
public const RECOGNIZE_BACKEND_APP_ID = 'recognize_backend';

Expand Down Expand Up @@ -123,11 +127,13 @@ final class SettingsService {
private IAppConfig $config;
private IJobList $jobList;
private IEventDispatcher $eventDispatcher;
private IDBConnection $db;

public function __construct(IAppConfig $config, IJobList $jobList, IEventDispatcher $eventDispatcher) {
public function __construct(IAppConfig $config, IJobList $jobList, IEventDispatcher $eventDispatcher, IDBConnection $db) {
$this->config = $config;
$this->jobList = $jobList;
$this->eventDispatcher = $eventDispatcher;
$this->db = $db;
}

/**
Expand All @@ -139,8 +145,11 @@ public function getSetting(string $key): string {
return $this->config->getAppValueString($key, $this->getSetting('tensorflow.purejs') === 'false' ? self::DEFAULTS[$key] : self::PUREJS_DEFAULTS[$key]);
}
if ($key === 'taskprocessing.enabled') {
$default = $this->isRecognizeBackendInstalled() ? 'true' : 'false';
return $this->config->getAppValueString($key, $default);
$value = $this->config->getAppValueString($key, '');
if ($value !== '') {
return $value;
}
return $this->getTaskProcessingDefault() ? 'true' : 'false';
}
$lazy = false;
if (in_array($key, self::LAZY_SETTINGS, true)) {
Expand All @@ -149,6 +158,50 @@ public function getSetting(string $key): string {
return $this->config->getAppValueString($key, self::DEFAULTS[$key], lazy: $lazy);
}

/**
* TaskProcessing mode defaults to on when the recognize_backend ExApp is installed.
* The local face model and the TaskProcessing face model produce embeddings of
* different dimensions that cannot be clustered together, so we don't switch
* the mode implicitly when the existing face detections stem from the local model.
*/
private function getTaskProcessingDefault(): bool {
if (!$this->isRecognizeBackendInstalled()) {
return false;
}
$dimensions = $this->getFaceEmbeddingDimensions();
return $dimensions === null || $dimensions === ImageFaceRecognitionClassifier::DIMENSIONS;
}

/**
* Returns the dimensionality of the most recently stored face embedding,
* or null if there are no face detections.
*/
public function getFaceEmbeddingDimensions(): ?int {
try {
$qb = $this->db->getQueryBuilder();
$qb->select('face_vector')
->from('recognize_face_detections')
->orderBy('id', 'DESC')
->setMaxResults(1);
$result = $qb->executeQuery();
/** @var string|false|null $vector */
$vector = $result->fetchOne();
$result->closeCursor();
} catch (DBException $e) {
return null;
}
if (!is_string($vector) || $vector === '') {
return null;
}
try {
/** @var array|string|int|float|bool|null $decoded */
$decoded = json_decode($vector, true, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $e) {
return null;
}
return is_array($decoded) ? count($decoded) : null;
}

/**
* Whether the recognize_backend ExApp is installed and enabled. The lookup
* goes through app_api's PublicFunctions service so we don't impose a hard
Expand Down
1 change: 1 addition & 0 deletions lib/Settings/AdminSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function getForm(): TemplateResponse {
$this->initialState->provideInitialState('tagsEnabled', $tagsEnabled);

$this->initialState->provideInitialState('recognizeBackendInstalled', $this->settingsService->isRecognizeBackendInstalled());
$this->initialState->provideInitialState('faceEmbeddingDimensions', $this->settingsService->getFaceEmbeddingDimensions() ?? 0);

return new TemplateResponse('recognize', 'admin');
}
Expand Down
22 changes: 22 additions & 0 deletions src/components/ViewAdmin.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@
{{ t('recognize', 'Use Nextcloud TaskProcessing for classification') }}
</NcCheckboxRadioSwitch>
</p>
<NcNoteCard v-if="faceEmbeddingMismatch" type="warning">
{{ settings['taskprocessing.enabled']
? t('recognize', 'The existing face detections were created with the local face recognition model, which is incompatible with the face recognition model used in TaskProcessing mode. Faces detected by the two models cannot be grouped into the same people.')
: t('recognize', 'The existing face detections were created in TaskProcessing mode, which uses a face recognition model that is incompatible with the local one. Faces detected by the two models cannot be grouped into the same people.') }}
{{ t('recognize', 'Use "Reset faces" below and then "Rescan all files" to detect all faces again with the selected backend. Note that this deletes all people and the names users have given them.') }}
</NcNoteCard>
<p>
{{ t('recognize', 'When enabled, Recognize hands files off to a Nextcloud TaskProcessing provider (typically the recognize_backend ExApp) instead of running TensorFlow locally. Hardware checks and Node.js / FFmpeg requirements no longer apply in this mode.') }}
</p>
Expand Down Expand Up @@ -495,6 +501,10 @@ const SETTINGS = ['tensorflow.cores', 'tensorflow.gpu', 'tensorflow.purejs', 'im

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']

// Dimensions of the face embeddings produced by the local model and by the TaskProcessing backend
const LOCAL_FACE_EMBEDDING_DIMENSIONS = 128
const TASK_PROCESSING_FACE_EMBEDDING_DIMENSIONS = 512

const MAX_RELATIVE_DATE = 1000 * 60 * 60 * 24 * 7 // one week

export default {
Expand Down Expand Up @@ -533,10 +543,20 @@ export default {
musicnnTpStats: null,
tagsEnabled: null,
recognizeBackendInstalled: false,
faceEmbeddingDimensions: null,
}
},

computed: {
faceEmbeddingMismatch() {
if (!this.faceEmbeddingDimensions) {
return false
}
const expected = this.settings['taskprocessing.enabled']
? TASK_PROCESSING_FACE_EMBEDDING_DIMENSIONS
: LOCAL_FACE_EMBEDDING_DIMENSIONS
return this.faceEmbeddingDimensions !== expected
},
readyToClassify() {
if (this.cron !== 'cron') {
return false
Expand Down Expand Up @@ -571,6 +591,7 @@ export default {
this.modelsDownloaded = loadState('recognize', 'modelsDownloaded')
this.tagsEnabled = loadState('recognize', 'tagsEnabled')
this.recognizeBackendInstalled = loadState('recognize', 'recognizeBackendInstalled', false)
this.faceEmbeddingDimensions = loadState('recognize', 'faceEmbeddingDimensions', null)
this.getCount()
this.getAVX()
this.getPlatform()
Expand Down Expand Up @@ -657,6 +678,7 @@ export default {
async onResetFaces() {
this.loading = true
await axios.get(generateUrl('/apps/recognize/admin/resetFaces'))
this.faceEmbeddingDimensions = null
this.loading = false
this.success = true
setTimeout(() => {
Expand Down
3 changes: 2 additions & 1 deletion tests/Service/SettingsServiceAuditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCP\AppFramework\Services\IAppConfig;
use OCP\BackgroundJob\IJobList;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IDBConnection;
use OCP\Log\Audit\CriticalActionPerformedEvent;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand All @@ -39,7 +40,7 @@ protected function setUp(): void {
});
$this->jobList = $this->createMock(IJobList::class);
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->service = new SettingsService($this->config, $this->jobList, $this->eventDispatcher);
$this->service = new SettingsService($this->config, $this->jobList, $this->eventDispatcher, $this->createMock(IDBConnection::class));
}

public function testChangedSettingIsLogged(): void {
Expand Down
Loading