2022-10-29 18:05:05 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Memories\Controller;
|
|
|
|
|
2023-03-18 17:52:04 +00:00
|
|
|
use OCA\Memories\Errors;
|
2022-10-29 18:05:05 +00:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse;
|
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\Files\FileInfo;
|
|
|
|
|
2023-03-22 18:40:56 +00:00
|
|
|
class PeopleController extends GenericApiController
|
2022-10-29 18:05:05 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* Get list of faces with counts of images
|
|
|
|
*/
|
2023-03-19 03:38:37 +00:00
|
|
|
public function recognizePeople(): Http\Response
|
2022-10-29 18:05:05 +00:00
|
|
|
{
|
2023-03-19 02:03:17 +00:00
|
|
|
try {
|
|
|
|
$uid = $this->getUID();
|
|
|
|
} catch (\Exception $e) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotLoggedIn();
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check faces enabled for this user
|
|
|
|
if (!$this->recognizeIsEnabled()) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotEnabled('Recognize');
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this isn't the timeline folder then things aren't going to work
|
2022-11-16 07:45:01 +00:00
|
|
|
$root = $this->getRequestRoot();
|
|
|
|
if ($root->isEmpty()) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NoRequestRoot();
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run actual query
|
2022-12-08 21:00:53 +00:00
|
|
|
$list = $this->timelineQuery->getPeopleRecognize(
|
2023-03-19 03:38:37 +00:00
|
|
|
$root,
|
|
|
|
$uid
|
2022-10-29 18:05:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return new JSONResponse($list, Http::STATUS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* Get face preview image cropped with imagick
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
2022-12-08 21:00:53 +00:00
|
|
|
public function recognizePeoplePreview(int $id): Http\Response
|
2022-10-29 18:05:05 +00:00
|
|
|
{
|
2023-03-19 02:03:17 +00:00
|
|
|
try {
|
|
|
|
$uid = $this->getUID();
|
|
|
|
} catch (\Exception $e) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotLoggedIn();
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check faces enabled for this user
|
|
|
|
if (!$this->recognizeIsEnabled()) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotEnabled('Recognize');
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get folder to search for
|
2022-11-16 07:45:01 +00:00
|
|
|
$root = $this->getRequestRoot();
|
|
|
|
if ($root->isEmpty()) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NoRequestRoot();
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run actual query
|
2023-03-19 02:03:17 +00:00
|
|
|
$detections = $this->timelineQuery->getPeopleRecognizePreview($root, $id, $uid);
|
2022-12-08 21:00:53 +00:00
|
|
|
|
2023-03-19 02:03:17 +00:00
|
|
|
if (0 === \count($detections)) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotFound('detections');
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
2023-03-19 02:03:17 +00:00
|
|
|
return $this->getPreviewResponse($detections, 1.5);
|
2022-12-08 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* Get list of faces with counts of images
|
|
|
|
*/
|
2023-03-19 03:38:37 +00:00
|
|
|
public function facerecognitionPeople(): Http\Response
|
2022-12-08 21:00:53 +00:00
|
|
|
{
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if (null === $user) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotLoggedIn();
|
2022-12-08 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if face recognition is installed and enabled for this user
|
|
|
|
if (!$this->facerecognitionIsInstalled()) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotEnabled('Face Recognition');
|
2022-12-08 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If this isn't the timeline folder then things aren't going to work
|
|
|
|
$root = $this->getRequestRoot();
|
|
|
|
if ($root->isEmpty()) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NoRequestRoot();
|
2022-12-08 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the user has recognition disabled, just returns an empty response.
|
|
|
|
if (!$this->facerecognitionIsEnabled()) {
|
|
|
|
return new JSONResponse([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run actual query
|
|
|
|
$currentModel = (int) $this->config->getAppValue('facerecognition', 'model', -1);
|
2023-02-14 23:59:30 +00:00
|
|
|
$list = $this->timelineQuery->getFaceRecognitionPersons(
|
2022-12-08 21:00:53 +00:00
|
|
|
$root,
|
2023-02-14 23:59:30 +00:00
|
|
|
$currentModel
|
2022-12-08 21:00:53 +00:00
|
|
|
);
|
|
|
|
// Just append unnamed clusters to the end.
|
2023-02-14 23:59:30 +00:00
|
|
|
$list = array_merge($list, $this->timelineQuery->getFaceRecognitionClusters(
|
2022-12-08 21:00:53 +00:00
|
|
|
$root,
|
2023-02-14 23:59:30 +00:00
|
|
|
$currentModel
|
2022-12-08 21:00:53 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
return new JSONResponse($list, Http::STATUS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
|
|
|
*
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* Get face preview image cropped with imagick
|
|
|
|
*
|
|
|
|
* @return DataResponse
|
|
|
|
*/
|
|
|
|
public function facerecognitionPeoplePreview(string $id): Http\Response
|
|
|
|
{
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if (null === $user) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotLoggedIn();
|
2022-12-08 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if face recognition is installed and enabled for this user
|
|
|
|
if (!$this->facerecognitionIsInstalled()) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotEnabled('Face Recognition');
|
2022-12-08 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get folder to search for
|
|
|
|
$root = $this->getRequestRoot();
|
|
|
|
if ($root->isEmpty()) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NoRequestRoot();
|
2022-12-08 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the user has facerecognition disabled, just returns an empty response.
|
|
|
|
if (!$this->facerecognitionIsEnabled()) {
|
|
|
|
return new JSONResponse([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run actual query
|
|
|
|
$currentModel = (int) $this->config->getAppValue('facerecognition', 'model', -1);
|
|
|
|
$detections = $this->timelineQuery->getFaceRecognitionPreview($root, $currentModel, $id);
|
|
|
|
|
|
|
|
if (null === $detections || 0 === \count($detections)) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotFound('detections');
|
2022-12-08 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
2023-03-19 02:03:17 +00:00
|
|
|
return $this->getPreviewResponse($detections, 1.8);
|
2022-12-08 21:00:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get face preview image cropped with imagick.
|
|
|
|
*
|
2022-12-08 21:08:38 +00:00
|
|
|
* @param array $detections Array of detections to search
|
|
|
|
* @param \OCP\IUser $user User to search for
|
|
|
|
* @param int $padding Padding to add to the face in preview
|
2022-12-08 21:00:53 +00:00
|
|
|
*/
|
|
|
|
private function getPreviewResponse(
|
|
|
|
array $detections,
|
|
|
|
float $padding
|
2022-12-08 21:08:38 +00:00
|
|
|
): Http\Response {
|
2022-12-04 17:40:58 +00:00
|
|
|
// Get preview manager
|
2022-12-04 17:57:31 +00:00
|
|
|
$previewManager = \OC::$server->get(\OCP\IPreview::class);
|
2022-12-04 17:40:58 +00:00
|
|
|
|
2022-11-28 13:58:51 +00:00
|
|
|
/** @var \Imagick */
|
|
|
|
$image = null;
|
2023-03-19 02:03:17 +00:00
|
|
|
|
|
|
|
// Find the first detection that has a preview
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder($this->getUID());
|
|
|
|
|
2022-10-29 18:05:05 +00:00
|
|
|
foreach ($detections as &$detection) {
|
|
|
|
// Get the file (also checks permissions)
|
2022-11-16 07:45:01 +00:00
|
|
|
$files = $userFolder->getById($detection['file_id']);
|
2022-10-29 18:05:05 +00:00
|
|
|
if (0 === \count($files) || FileInfo::TYPE_FILE !== $files[0]->getType()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-11-15 13:40:46 +00:00
|
|
|
// Check read permission
|
2023-03-10 00:44:56 +00:00
|
|
|
if (!$files[0]->isReadable()) {
|
2022-11-15 13:40:46 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-10-29 18:05:05 +00:00
|
|
|
// Get (hopefully cached) preview image
|
|
|
|
try {
|
2022-12-04 17:40:58 +00:00
|
|
|
$preview = $previewManager->getPreview($files[0], 2048, 2048, false);
|
2022-11-28 13:58:51 +00:00
|
|
|
|
|
|
|
$image = new \Imagick();
|
|
|
|
if (!$image->readImageBlob($preview->getContent())) {
|
|
|
|
throw new \Exception('Failed to read image blob');
|
|
|
|
}
|
|
|
|
$iw = $image->getImageWidth();
|
|
|
|
$ih = $image->getImageHeight();
|
|
|
|
|
|
|
|
if ($iw <= 0 || $ih <= 0) {
|
|
|
|
$image = null;
|
2022-11-28 14:11:19 +00:00
|
|
|
|
2022-11-28 13:58:51 +00:00
|
|
|
throw new \Exception('Invalid image size');
|
|
|
|
}
|
2022-10-29 18:05:05 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Got the preview
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the preview is valid
|
2022-11-28 13:58:51 +00:00
|
|
|
if (null === $image) {
|
2023-03-18 17:52:04 +00:00
|
|
|
return Errors::NotFound('preview');
|
2022-10-29 18:05:05 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 03:08:53 +00:00
|
|
|
// Set quality and make progressive
|
|
|
|
$image->setImageCompressionQuality(80);
|
|
|
|
$image->setInterlaceScheme(\Imagick::INTERLACE_PLANE);
|
|
|
|
|
2022-10-29 18:05:05 +00:00
|
|
|
// Crop image
|
|
|
|
$dw = (float) $detection['width'];
|
|
|
|
$dh = (float) $detection['height'];
|
|
|
|
$dcx = (float) $detection['x'] + (float) $detection['width'] / 2;
|
|
|
|
$dcy = (float) $detection['y'] + (float) $detection['height'] / 2;
|
2022-12-08 21:00:53 +00:00
|
|
|
$faceDim = max($dw * $iw, $dh * $ih) * $padding;
|
2022-10-29 18:05:05 +00:00
|
|
|
$image->cropImage(
|
|
|
|
(int) $faceDim,
|
|
|
|
(int) $faceDim,
|
|
|
|
(int) ($dcx * $iw - $faceDim / 2),
|
|
|
|
(int) ($dcy * $ih - $faceDim / 2),
|
|
|
|
);
|
2022-12-05 02:37:14 +00:00
|
|
|
$image->scaleImage(512, 512, true);
|
2022-10-29 18:05:05 +00:00
|
|
|
$blob = $image->getImageBlob();
|
|
|
|
|
|
|
|
// Create and send response
|
|
|
|
$response = new DataDisplayResponse($blob, Http::STATUS_OK, [
|
|
|
|
'Content-Type' => $image->getImageMimeType(),
|
|
|
|
]);
|
|
|
|
$response->cacheFor(3600 * 24, false, false);
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|