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-10-15 19:46:35 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
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;
|
|
|
|
|
|
|
|
public function __construct(
|
2023-10-15 01:51:17 +00:00
|
|
|
protected IRequest $request,
|
|
|
|
protected TimelineQuery $tq,
|
|
|
|
protected IConfig $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-10-15 19:46:35 +00:00
|
|
|
public function transformDayQuery(IQueryBuilder &$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')) {
|
2023-08-17 17:48:43 +00:00
|
|
|
$query->selectAlias('frf.x', 'face_x')
|
|
|
|
->selectAlias('frf.y', 'face_y')
|
|
|
|
->selectAlias('frf.width', 'face_width')
|
|
|
|
->selectAlias('frf.height', 'face_height')
|
|
|
|
->selectAlias('m.w', 'image_width')
|
|
|
|
->selectAlias('m.h', 'image_height')
|
|
|
|
;
|
2023-03-23 23:58:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2023-10-14 23:06:25 +00:00
|
|
|
if (!isset($row['face_width']) || !isset($row['image_width'])) {
|
2023-03-23 23:58:49 +00:00
|
|
|
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'],
|
|
|
|
];
|
|
|
|
|
2023-08-08 22:21:18 +00:00
|
|
|
unset($row['face_x'], $row['face_y'], $row['face_width'], $row['face_height'], $row['image_height'], $row['image_width']);
|
2023-03-23 23:58:49 +00:00
|
|
|
}
|
|
|
|
|
2023-08-04 02:56:32 +00:00
|
|
|
public function getClustersInternal(int $fileid = 0): array
|
2023-03-23 00:42:30 +00:00
|
|
|
{
|
2023-03-23 23:58:49 +00:00
|
|
|
$faces = array_merge(
|
2023-08-08 00:11:36 +00:00
|
|
|
$this->getFaceRecognitionPersons($fileid),
|
2023-10-15 01:59:00 +00:00
|
|
|
$this->getFaceRecognitionClusters($fileid),
|
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',
|
|
|
|
'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-10-14 08:25:50 +00:00
|
|
|
public function sortPhotosForPreview(array &$photos): void
|
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-10-15 19:46:35 +00:00
|
|
|
public function getPreviewBlob(ISimpleFile $file, array $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
|
|
|
|
{
|
2023-10-14 08:25:50 +00:00
|
|
|
return (int) $this->config->getAppValue('facerecognition', 'model', (string) -1);
|
2023-03-23 00:42:30 +00:00
|
|
|
}
|
2023-03-23 23:58:49 +00:00
|
|
|
|
2023-08-08 23:38:15 +00:00
|
|
|
private function minFaceInClusters(): int
|
|
|
|
{
|
2023-10-14 08:25:50 +00:00
|
|
|
return (int) $this->config->getAppValue('facerecognition', 'min_faces_in_cluster', (string) 5);
|
2023-08-08 23:38:15 +00:00
|
|
|
}
|
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
private function getFaceRecognitionClusters(int $fileid = 0): array
|
2023-03-23 23:58:49 +00:00
|
|
|
{
|
|
|
|
$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'));
|
|
|
|
|
2023-08-08 23:38:15 +00:00
|
|
|
// The query change if we want the people in an fileid, or the unnamed clusters
|
2023-08-08 00:11:36 +00:00
|
|
|
if ($fileid > 0) {
|
2023-08-08 23:38:15 +00:00
|
|
|
// WHERE these clusters contain fileid if specified
|
2023-08-08 00:11:36 +00:00
|
|
|
$query->andWhere($query->expr()->eq('fri.file', $query->createNamedParameter($fileid)));
|
2023-08-08 23:38:15 +00:00
|
|
|
} else {
|
|
|
|
// WHERE these clusters has a minimum number of faces
|
|
|
|
$query->having($query->expr()->gte($count, $query->expr()->literal($this->minFaceInClusters(), \PDO::PARAM_INT)));
|
|
|
|
// WHERE these clusters were not hidden due inconsistencies
|
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() ?: [];
|
|
|
|
}
|
|
|
|
|
2023-10-14 08:25:50 +00:00
|
|
|
private function getFaceRecognitionPersons(int $fileid = 0): array
|
2023-03-23 23:58:49 +00:00
|
|
|
{
|
|
|
|
$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'));
|
2023-08-08 00:11:36 +00:00
|
|
|
|
|
|
|
// WHERE these clusters contain fileid if specified
|
|
|
|
if ($fileid > 0) {
|
|
|
|
$query->andWhere($query->expr()->eq('fri.file', $query->createNamedParameter($fileid)));
|
|
|
|
}
|
|
|
|
|
2023-03-23 23:58:49 +00:00
|
|
|
$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
|
|
|
}
|