diff --git a/lib/Service/SettingsService.php b/lib/Service/SettingsService.php index abb0bfea..9f6f34f0 100644 --- a/lib/Service/SettingsService.php +++ b/lib/Service/SettingsService.php @@ -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'; @@ -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; } /** @@ -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)) { @@ -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 diff --git a/lib/Settings/AdminSettings.php b/lib/Settings/AdminSettings.php index 31f13370..58adc159 100644 --- a/lib/Settings/AdminSettings.php +++ b/lib/Settings/AdminSettings.php @@ -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'); } diff --git a/src/components/ViewAdmin.vue b/src/components/ViewAdmin.vue index 35cea400..e2b27e61 100644 --- a/src/components/ViewAdmin.vue +++ b/src/components/ViewAdmin.vue @@ -77,6 +77,12 @@ {{ t('recognize', 'Use Nextcloud TaskProcessing for classification') }}
+{{ 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.') }}
@@ -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 { @@ -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 @@ -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() @@ -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(() => { diff --git a/tests/Service/SettingsServiceAuditTest.php b/tests/Service/SettingsServiceAuditTest.php index e210e0c7..58b1cdd2 100644 --- a/tests/Service/SettingsServiceAuditTest.php +++ b/tests/Service/SettingsServiceAuditTest.php @@ -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; @@ -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 {