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;
|
2023-03-23 23:58:49 +00:00
|
|
|
use OCP\IRequest;
|
2023-03-23 18:08:36 +00:00
|
|
|
|
|
|
|
class FaceRecognitionBackend extends Backend
|
2023-03-23 00:42:30 +00:00
|
|
|
{
|
2023-03-23 18:08:36 +00:00
|
|
|
use PeopleBackendUtils;
|
|
|
|
|
2023-03-23 23:58:49 +00:00
|
|
|
protected IRequest $request;
|
|
|
|
protected TimelineQuery $tq;
|
2023-03-23 18:08:36 +00:00
|
|
|
protected IConfig $config;
|
|
|
|
|
|
|
|
public function __construct(
|
2023-03-23 23:58:49 +00:00
|
|
|
IRequest $request,
|
|
|
|
TimelineQuery $tq,
|
2023-03-23 18:08:36 +00:00
|
|
|
IConfig $config
|
|
|
|
) {
|
2023-03-23 23:58:49 +00:00
|
|
|
$this->request = $request;
|
|
|
|
$this->tq = $tq;
|
2023-03-23 18:08:36 +00:00
|
|
|
$this->config = $config;
|
|
|
|
}
|
2023-03-23 00:42:30 +00:00
|
|
|
|
2023-03-24 11:17:54 +00:00
|
|
|
public static function appName(): string
|
2023-03-23 00:42:30 +00:00
|
|
|
{
|
|
|
|
return 'Face Recognition';
|
|
|
|
}
|
|
|
|
|
2023-03-24 11:17:54 +00:00
|
|
|
public static function clusterType(): string
|
|
|
|
{
|
|
|
|
return 'facerecognition';
|
|
|
|
}
|
|
|
|
|
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-24 00:19:05 +00:00
|
|
|
public function transformDayQuery(&$query, bool $aggregate): void
|
2023-03-23 23:58:49 +00:00
|
|
|
{
|
|
|
|
$personStr = (string) $this->request->getParam('facerecognition');
|
|
|
|
|
|
|
|
// Get title and uid of face user
|
|
|
|
$personNames = explode('/', $personStr);
|
|
|
|
if (2 !== \count($personNames)) {
|
|
|
|
throw new \Exception('Invalid person query');
|
|
|
|
}
|
|
|
|
|
|
|
|
$personUid = $personNames[0];
|
|
|
|
$personName = $personNames[1];
|
|
|
|
|
|
|
|
// Join with images
|
|
|
|
$query->innerJoin('m', 'facerecog_images', 'fri', $query->expr()->andX(
|
|
|
|
$query->expr()->eq('fri.file', 'm.fileid'),
|
|
|
|
$query->expr()->eq('fri.model', $query->createNamedParameter($this->model())),
|
|
|
|
));
|
|
|
|
|
|
|
|
// Join with faces
|
2023-03-24 00:19:05 +00:00
|
|
|
$query->innerJoin('fri', 'facerecog_faces', 'frf', $query->expr()->eq('frf.image', 'fri.id'));
|
2023-03-23 23:58:49 +00:00
|
|
|
|
|
|
|
// Join with persons
|
|
|
|
$nameField = is_numeric($personName) ? 'frp.id' : 'frp.name';
|
|
|
|
$query->innerJoin('frf', 'facerecog_persons', 'frp', $query->expr()->andX(
|
|
|
|
$query->expr()->eq('frf.person', 'frp.id'),
|
|
|
|
$query->expr()->eq('frp.user', $query->createNamedParameter($personUid)),
|
|
|
|
$query->expr()->eq($nameField, $query->createNamedParameter($personName)),
|
|
|
|
));
|
|
|
|
|
|
|
|
// Add face rect
|
|
|
|
if (!$aggregate && $this->request->getParam('facerect')) {
|
|
|
|
$query->addSelect(
|
|
|
|
'frf.x AS face_x',
|
|
|
|
'frf.y AS face_y',
|
|
|
|
'frf.width AS face_width',
|
|
|
|
'frf.height AS face_height',
|
|
|
|
'm.w AS image_width',
|
|
|
|
'm.h AS image_height',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 00:19:05 +00:00
|
|
|
public function transformDayPost(array &$row): void
|
2023-03-23 23:58:49 +00:00
|
|
|
{
|
|
|
|
// Differentiate Recognize queries from Face Recognition
|
|
|
|
if (!isset($row) || !isset($row['face_width']) || !isset($row['image_width'])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get percentage position and size
|
|
|
|
$row['facerect'] = [
|
|
|
|
'w' => (float) $row['face_width'] / $row['image_width'],
|
|
|
|
'h' => (float) $row['face_height'] / $row['image_height'],
|
|
|
|
'x' => (float) $row['face_x'] / $row['image_width'],
|
|
|
|
'y' => (float) $row['face_y'] / $row['image_height'],
|
|
|
|
];
|
|
|
|
|
|
|
|
unset($row['face_x'], $row['face_y'], $row['face_w'], $row['face_h'], $row['image_height'], $row['image_width']);
|
|
|
|
}
|
|
|
|
|
2023-08-04 01:56:51 +00:00
|
|
|
public function getClusters(int $fileid = 0): array
|
2023-03-23 00:42:30 +00:00
|
|
|
{
|
2023-08-04 01:56:51 +00:00
|
|
|
if ($fileid) {
|
|
|
|
throw new \Exception('FaceRecognitionBackend: fileid filter not implemented');
|
|
|
|
}
|
|
|
|
|
2023-03-23 23:58:49 +00:00
|
|
|
$faces = array_merge(
|
|
|
|
$this->getFaceRecognitionPersons(),
|
|
|
|
$this->getFaceRecognitionClusters()
|
2023-03-23 00:42:30 +00:00
|
|
|
);
|
2023-03-23 23:58:49 +00:00
|
|
|
|
|
|
|
// Post process
|
|
|
|
foreach ($faces as &$row) {
|
2023-03-25 14:46:41 +00:00
|
|
|
$row['id'] = \array_key_exists('name', $row) ? $row['name'] : (int) $row['id'];
|
2023-03-23 23:58:49 +00:00
|
|
|
$row['count'] = (int) $row['count'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $faces;
|
2023-03-23 00:42:30 +00:00
|
|
|
}
|
|
|
|
|
2023-03-24 11:17:54 +00:00
|
|
|
public static function getClusterId(array $cluster)
|
|
|
|
{
|
|
|
|
return $cluster['id'];
|
|
|
|
}
|
|
|
|
|
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 23:58:49 +00:00
|
|
|
$query = $this->tq->getBuilder();
|
|
|
|
|
|
|
|
// SELECT face detections
|
|
|
|
$query->select(
|
|
|
|
'fri.file as file_id', // Get actual file
|
|
|
|
'frf.x', // Image cropping
|
|
|
|
'frf.y',
|
|
|
|
'frf.width',
|
|
|
|
'frf.height',
|
|
|
|
'm.w as image_width', // Scoring
|
|
|
|
'm.h as image_height',
|
|
|
|
'frf.confidence',
|
|
|
|
'm.fileid',
|
|
|
|
'm.datetaken', // Just in case, for postgres
|
|
|
|
)->from('facerecog_faces', 'frf');
|
|
|
|
|
|
|
|
// WHERE faces are from images and current model.
|
|
|
|
$query->innerJoin('frf', 'facerecog_images', 'fri', $query->expr()->andX(
|
|
|
|
$query->expr()->eq('fri.id', 'frf.image'),
|
|
|
|
$query->expr()->eq('fri.model', $query->createNamedParameter($this->model())),
|
|
|
|
));
|
|
|
|
|
|
|
|
// WHERE these photos are memories indexed
|
|
|
|
$query->innerJoin('fri', 'memories', 'm', $query->expr()->eq('m.fileid', 'fri.file'));
|
|
|
|
|
|
|
|
$query->innerJoin('frf', 'facerecog_persons', 'frp', $query->expr()->eq('frp.id', 'frf.person'));
|
2023-03-24 00:19:05 +00:00
|
|
|
|
|
|
|
// WHERE faces are from id persons (or a cluster).
|
|
|
|
$nameField = is_numeric($name) ? 'frp.id' : 'frp.name';
|
|
|
|
$query->where($query->expr()->eq($nameField, $query->createNamedParameter($name)));
|
2023-03-23 23:58:49 +00:00
|
|
|
|
|
|
|
// WHERE these photos are in the user's requested folder recursively
|
|
|
|
$query = $this->tq->joinFilecache($query);
|
|
|
|
|
|
|
|
// LIMIT results
|
|
|
|
if (null !== $limit) {
|
|
|
|
$query->setMaxResults($limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort by date taken so we get recent photos
|
|
|
|
$query->orderBy('m.datetaken', 'DESC');
|
|
|
|
$query->addOrderBy('m.fileid', 'DESC'); // tie-breaker
|
|
|
|
|
|
|
|
// FETCH face detections
|
|
|
|
return $this->tq->executeQueryWithCTEs($query)->fetchAll() ?: [];
|
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);
|
|
|
|
}
|
2023-03-23 23:58:49 +00:00
|
|
|
|
|
|
|
private function getFaceRecognitionClusters(bool $show_singles = false, bool $show_hidden = false)
|
|
|
|
{
|
|
|
|
$query = $this->tq->getBuilder();
|
|
|
|
|
|
|
|
// SELECT all face clusters
|
|
|
|
$count = $query->func()->count($query->createFunction('DISTINCT m.fileid'));
|
|
|
|
$query->select('frp.id')->from('facerecog_persons', 'frp');
|
|
|
|
$query->selectAlias($count, 'count');
|
|
|
|
$query->selectAlias('frp.user', 'user_id');
|
|
|
|
|
|
|
|
// WHERE there are faces with this cluster
|
|
|
|
$query->innerJoin('frp', 'facerecog_faces', 'frf', $query->expr()->eq('frp.id', 'frf.person'));
|
|
|
|
|
|
|
|
// WHERE faces are from images.
|
|
|
|
$query->innerJoin('frf', 'facerecog_images', 'fri', $query->expr()->eq('fri.id', 'frf.image'));
|
|
|
|
|
|
|
|
// WHERE these items are memories indexed photos
|
|
|
|
$query->innerJoin('fri', 'memories', 'm', $query->expr()->andX(
|
|
|
|
$query->expr()->eq('fri.file', 'm.fileid'),
|
|
|
|
$query->expr()->eq('fri.model', $query->createNamedParameter($this->model())),
|
|
|
|
));
|
|
|
|
|
|
|
|
// WHERE these photos are in the user's requested folder recursively
|
|
|
|
$query = $this->tq->joinFilecache($query);
|
|
|
|
|
|
|
|
// GROUP by ID of face cluster
|
|
|
|
$query->groupBy('frp.id');
|
|
|
|
$query->addGroupBy('frp.user');
|
|
|
|
$query->where($query->expr()->isNull('frp.name'));
|
|
|
|
|
|
|
|
// By default hides individual faces when they have no name.
|
|
|
|
if (!$show_singles) {
|
2023-04-25 05:07:58 +00:00
|
|
|
$query->having($query->expr()->gt($count, $query->expr()->literal(1, \PDO::PARAM_INT)));
|
2023-03-23 23:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// By default it shows the people who were not hidden
|
|
|
|
if (!$show_hidden) {
|
2023-04-25 06:03:33 +00:00
|
|
|
$query->andWhere($query->expr()->eq('frp.is_visible', $query->expr()->literal(1)));
|
2023-03-23 23:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ORDER by number of faces in cluster and id for response stability.
|
|
|
|
$query->orderBy('count', 'DESC');
|
|
|
|
$query->addOrderBy('frp.id', 'DESC');
|
|
|
|
|
|
|
|
// It is not worth displaying all unnamed clusters. We show 15 to name them progressively,
|
|
|
|
$query->setMaxResults(15);
|
|
|
|
|
|
|
|
// FETCH all faces
|
|
|
|
return $this->tq->executeQueryWithCTEs($query)->fetchAll() ?: [];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getFaceRecognitionPersons()
|
|
|
|
{
|
|
|
|
$query = $this->tq->getBuilder();
|
|
|
|
|
|
|
|
// SELECT all face clusters
|
|
|
|
$count = $query->func()->count($query->createFunction('DISTINCT m.fileid'));
|
|
|
|
$query->select('frp.name')->from('facerecog_persons', 'frp');
|
|
|
|
$query->selectAlias($count, 'count');
|
|
|
|
$query->selectAlias('frp.user', 'user_id');
|
|
|
|
|
|
|
|
// WHERE there are faces with this cluster
|
|
|
|
$query->innerJoin('frp', 'facerecog_faces', 'frf', $query->expr()->eq('frp.id', 'frf.person'));
|
|
|
|
|
|
|
|
// WHERE faces are from images.
|
|
|
|
$query->innerJoin('frf', 'facerecog_images', 'fri', $query->expr()->eq('fri.id', 'frf.image'));
|
|
|
|
|
|
|
|
// WHERE these items are memories indexed photos
|
|
|
|
$query->innerJoin('fri', 'memories', 'm', $query->expr()->andX(
|
|
|
|
$query->expr()->eq('fri.file', 'm.fileid'),
|
|
|
|
$query->expr()->eq('fri.model', $query->createNamedParameter($this->model())),
|
|
|
|
));
|
|
|
|
|
|
|
|
// WHERE these photos are in the user's requested folder recursively
|
|
|
|
$query = $this->tq->joinFilecache($query);
|
|
|
|
|
|
|
|
// GROUP by name of face clusters
|
|
|
|
$query->where($query->expr()->isNotNull('frp.name'));
|
|
|
|
$query->groupBy('frp.user');
|
|
|
|
$query->addGroupBy('frp.name');
|
|
|
|
|
|
|
|
// ORDER by number of faces in cluster
|
|
|
|
$query->orderBy('count', 'DESC');
|
|
|
|
$query->addOrderBy('frp.name', 'ASC');
|
|
|
|
|
|
|
|
// FETCH all faces
|
|
|
|
return $this->tq->executeQueryWithCTEs($query)->fetchAll() ?: [];
|
|
|
|
}
|
2023-03-23 00:42:30 +00:00
|
|
|
}
|