|
1
|
|
|
<?php |
|
2
|
|
|
namespace OCA\FaceRecognition\Controller; |
|
3
|
|
|
|
|
4
|
|
|
use OCP\IRequest; |
|
5
|
|
|
use OCP\IConfig; |
|
6
|
|
|
use OCP\Files\IRootFolder; |
|
7
|
|
|
use OCP\IUserSession; |
|
8
|
|
|
use OCP\IURLGenerator; |
|
9
|
|
|
use OCP\AppFramework\Http; |
|
10
|
|
|
use OCP\AppFramework\Http\DataResponse; |
|
11
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
12
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse; |
|
13
|
|
|
use OCP\AppFramework\Controller; |
|
14
|
|
|
|
|
15
|
|
|
use OCA\FaceRecognition\Db\Face; |
|
16
|
|
|
use OCA\FaceRecognition\Db\FaceMapper; |
|
17
|
|
|
|
|
18
|
|
|
use OCA\FaceRecognition\Db\Image; |
|
19
|
|
|
use OCA\FaceRecognition\Db\ImageMapper; |
|
20
|
|
|
|
|
21
|
|
|
use OCA\FaceRecognition\Db\Person; |
|
22
|
|
|
use OCA\FaceRecognition\Db\PersonMapper; |
|
23
|
|
|
|
|
24
|
|
|
use OCA\FaceRecognition\Migration\AddDefaultFaceModel; |
|
25
|
|
|
|
|
26
|
|
|
class PersonController extends Controller { |
|
27
|
|
|
|
|
28
|
|
|
private $config; |
|
29
|
|
|
private $rootFolder; |
|
30
|
|
|
private $userSession; |
|
31
|
|
|
private $urlGenerator; |
|
32
|
|
|
private $faceMapper; |
|
33
|
|
|
private $imageMapper; |
|
34
|
|
|
private $personMapper; |
|
35
|
|
|
private $userId; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct($AppName, |
|
38
|
|
|
IRequest $request, |
|
39
|
|
|
IConfig $config, |
|
40
|
|
|
IRootFolder $rootFolder, |
|
41
|
|
|
IUserSession $userSession, |
|
42
|
|
|
IURLGenerator $urlGenerator, |
|
43
|
|
|
FaceMapper $faceMapper, |
|
44
|
|
|
ImageMapper $imageMapper, |
|
45
|
|
|
PersonMapper $personmapper, |
|
46
|
|
|
$UserId) |
|
47
|
|
|
{ |
|
48
|
|
|
parent::__construct($AppName, $request); |
|
49
|
|
|
$this->config = $config; |
|
50
|
|
|
$this->rootFolder = $rootFolder; |
|
51
|
|
|
$this->userSession = $userSession; |
|
52
|
|
|
$this->urlGenerator = $urlGenerator; |
|
53
|
|
|
$this->imageMapper = $imageMapper; |
|
54
|
|
|
$this->faceMapper = $faceMapper; |
|
55
|
|
|
$this->personMapper = $personmapper; |
|
56
|
|
|
$this->userId = $UserId; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @NoAdminRequired |
|
61
|
|
|
*/ |
|
62
|
|
|
public function index() { |
|
63
|
|
|
$model = intval($this->config->getAppValue('facerecognition', 'model', AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID)); |
|
64
|
|
|
|
|
65
|
|
|
$resp = array(); |
|
66
|
|
|
$persons = $this->personMapper->findAll($this->userId); |
|
67
|
|
|
foreach ($persons as $person) { |
|
68
|
|
|
$cluster = []; |
|
69
|
|
|
$faces = []; |
|
70
|
|
|
$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $model, 14); |
|
71
|
|
|
foreach ($personFaces as $personFace) { |
|
72
|
|
|
$image = $this->imageMapper->find($this->userId, $personFace->getImage()); |
|
73
|
|
|
$face = []; |
|
74
|
|
|
$face['thumb-url'] = $this->getThumbUrl($personFace->getId()); |
|
75
|
|
|
$face['file-url'] = $this->getRedirectToFileUrl($image->getFile()); |
|
76
|
|
|
$faces[] = $face; |
|
77
|
|
|
} |
|
78
|
|
|
$cluster['name'] = $person->getName(); |
|
79
|
|
|
$cluster['id'] = $person->getId(); |
|
80
|
|
|
$cluster['faces'] = $faces; |
|
81
|
|
|
$resp[] = $cluster; |
|
82
|
|
|
} |
|
83
|
|
|
return new DataResponse($resp); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @NoAdminRequired |
|
88
|
|
|
*/ |
|
89
|
|
|
public function find($id) { |
|
90
|
|
|
$model = intval($this->config->getAppValue('facerecognition', 'model', AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID)); |
|
91
|
|
|
|
|
92
|
|
|
$person = $this->personMapper->find($this->userId, $id); |
|
93
|
|
|
|
|
94
|
|
|
$resp = []; |
|
95
|
|
|
$faces = []; |
|
96
|
|
|
$personFaces = $this->faceMapper->findFacesFromPerson($this->userId, $person->getId(), $model); |
|
97
|
|
|
foreach ($personFaces as $personFace) { |
|
98
|
|
|
$image = $this->imageMapper->find($this->userId, $personFace->getImage()); |
|
99
|
|
|
$face = []; |
|
100
|
|
|
$face['thumb-url'] = $this->getThumbUrl($personFace->getId()); |
|
101
|
|
|
$face['file-url'] = $this->getRedirectToFileUrl($image->getFile()); |
|
102
|
|
|
$faces[] = $face; |
|
103
|
|
|
} |
|
104
|
|
|
$resp['name'] = $person->getName(); |
|
105
|
|
|
$resp['id'] = $person->getId(); |
|
106
|
|
|
$resp['faces'] = $faces; |
|
107
|
|
|
|
|
108
|
|
|
return new DataResponse($resp); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @NoAdminRequired |
|
113
|
|
|
* |
|
114
|
|
|
* @param int $id |
|
115
|
|
|
* @param string $name |
|
116
|
|
|
*/ |
|
117
|
|
|
public function updateName($id, $name) { |
|
118
|
|
|
$person = $this->personMapper->find ($this->userId, $id); |
|
119
|
|
|
$person->setName($name); |
|
120
|
|
|
$this->personMapper->update($person); |
|
121
|
|
|
|
|
122
|
|
|
$newPerson = $this->personMapper->find($this->userId, $id); |
|
123
|
|
|
return new DataResponse($newPerson); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
private function getThumbUrl($faceId) { |
|
127
|
|
|
$params = []; |
|
128
|
|
|
$params['id'] = $faceId; |
|
129
|
|
|
$params['size'] = 50; |
|
130
|
|
|
return $this->urlGenerator->linkToRoute('facerecognition.face.getThumb', $params); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
private function getRedirectToFileUrl($fileId) { |
|
134
|
|
|
$uid = $this->userSession->getUser()->getUID(); |
|
135
|
|
|
$baseFolder = $this->rootFolder->getUserFolder($uid); |
|
136
|
|
|
$files = $baseFolder->getById($fileId); |
|
137
|
|
|
$file = current($files); |
|
138
|
|
|
|
|
139
|
|
|
$params = []; |
|
140
|
|
|
$params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath()); |
|
141
|
|
|
$params['scrollto'] = $file->getName(); |
|
142
|
|
|
|
|
143
|
|
|
return $this->urlGenerator->linkToRoute('files.view.index', $params); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|