2023-03-23 00:42:30 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2022 Varun Patil <radialapps@gmail.com>
|
|
|
|
* @author Varun Patil <radialapps@gmail.com>
|
|
|
|
* @license AGPL-3.0-or-later
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2023-03-23 18:08:36 +00:00
|
|
|
namespace OCA\Memories\ClustersBackend;
|
2023-03-23 00:42:30 +00:00
|
|
|
|
2023-03-23 18:08:36 +00:00
|
|
|
use OCA\Memories\Db\TimelineQuery;
|
2023-03-23 20:49:26 +00:00
|
|
|
use OCA\Memories\Util;
|
2023-03-23 18:08:36 +00:00
|
|
|
use OCP\IConfig;
|
|
|
|
|
|
|
|
class FaceRecognitionBackend extends Backend
|
2023-03-23 00:42:30 +00:00
|
|
|
{
|
2023-03-23 18:08:36 +00:00
|
|
|
use PeopleBackendUtils;
|
|
|
|
|
|
|
|
protected TimelineQuery $timelineQuery;
|
|
|
|
protected IConfig $config;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
TimelineQuery $timelineQuery,
|
|
|
|
IConfig $config
|
|
|
|
) {
|
|
|
|
$this->timelineQuery = $timelineQuery;
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
2023-03-23 00:42:30 +00:00
|
|
|
|
2023-03-23 18:08:36 +00:00
|
|
|
public function appName(): string
|
2023-03-23 00:42:30 +00:00
|
|
|
{
|
|
|
|
return 'Face Recognition';
|
|
|
|
}
|
|
|
|
|
2023-03-23 18:08:36 +00:00
|
|
|
public function isEnabled(): bool
|
2023-03-23 00:42:30 +00:00
|
|
|
{
|
2023-03-23 20:49:26 +00:00
|
|
|
return Util::facerecognitionIsInstalled()
|
|
|
|
&& Util::facerecognitionIsEnabled();
|
2023-03-23 00:42:30 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 18:08:36 +00:00
|
|
|
public function getClusters(): array
|
2023-03-23 00:42:30 +00:00
|
|
|
{
|
|
|
|
return array_merge(
|
2023-03-23 21:45:56 +00:00
|
|
|
$this->timelineQuery->getFaceRecognitionPersons($this->model()),
|
|
|
|
$this->timelineQuery->getFaceRecognitionClusters($this->model())
|
2023-03-23 00:42:30 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-03-23 18:08:36 +00:00
|
|
|
public function getPhotos(string $name, ?int $limit = null): array
|
2023-03-23 00:42:30 +00:00
|
|
|
{
|
2023-03-23 21:45:56 +00:00
|
|
|
return $this->timelineQuery->getFaceRecognitionPhotos($name, $this->model(), $limit) ?? [];
|
2023-03-23 00:42:30 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 18:08:36 +00:00
|
|
|
public function sortPhotosForPreview(array &$photos)
|
2023-03-23 00:42:30 +00:00
|
|
|
{
|
|
|
|
// Convert to recognize format (percentage position-size)
|
|
|
|
foreach ($photos as &$p) {
|
2023-03-23 00:47:49 +00:00
|
|
|
$p['x'] = (float) $p['x'] / (float) $p['image_width'];
|
|
|
|
$p['y'] = (float) $p['y'] / (float) $p['image_height'];
|
|
|
|
$p['width'] = (float) $p['width'] / (float) $p['image_width'];
|
|
|
|
$p['height'] = (float) $p['height'] / (float) $p['image_height'];
|
2023-03-23 00:42:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->sortByScores($photos);
|
|
|
|
}
|
|
|
|
|
2023-03-23 18:08:36 +00:00
|
|
|
public function getPreviewBlob($file, $photo): array
|
2023-03-23 00:42:30 +00:00
|
|
|
{
|
|
|
|
return $this->cropFace($file, $photo, 1.8);
|
|
|
|
}
|
|
|
|
|
2023-03-23 18:08:36 +00:00
|
|
|
public function getPreviewQuality(): int
|
|
|
|
{
|
|
|
|
return 2048;
|
|
|
|
}
|
|
|
|
|
2023-03-23 00:42:30 +00:00
|
|
|
private function model(): int
|
|
|
|
{
|
|
|
|
return (int) $this->config->getAppValue('facerecognition', 'model', -1);
|
|
|
|
}
|
|
|
|
}
|