|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2017-2020, Matias De lellis <[email protected]> |
|
4
|
|
|
* @copyright Copyright (c) 2018-2019, Branko Kokanovic <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* @author Matias De lellis <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @license GNU AGPL version 3 or any later version |
|
9
|
|
|
* |
|
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
13
|
|
|
* License, or (at your option) any later version. |
|
14
|
|
|
* |
|
15
|
|
|
* This program is distributed in the hope that it will be useful, |
|
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
* GNU Affero General Public License for more details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace OCA\FaceRecognition\Db; |
|
26
|
|
|
|
|
27
|
|
|
use OC\DB\QueryBuilder\Literal; |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
use OCP\IDBConnection; |
|
30
|
|
|
use OCP\AppFramework\Db\QBMapper; |
|
31
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
|
32
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
|
33
|
|
|
|
|
34
|
|
|
use OCA\FaceRecognition\Helper\Euclidean; |
|
35
|
|
|
|
|
36
|
|
|
class FaceMapper extends QBMapper { |
|
37
|
1 |
|
public function __construct(IDBConnection $db) { |
|
38
|
1 |
|
parent::__construct($db, 'facerecog_faces', '\OCA\FaceRecognition\Db\Face'); |
|
39
|
1 |
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
public function find (int $faceId) { |
|
42
|
1 |
|
$qb = $this->db->getQueryBuilder(); |
|
43
|
1 |
|
$qb->select('id', 'image', 'person', 'left', 'right', 'top', 'bottom', 'landmarks', 'descriptor', 'confidence') |
|
44
|
1 |
|
->from($this->getTableName(), 'f') |
|
45
|
1 |
|
->andWhere($qb->expr()->eq('id', $qb->createNamedParameter($faceId))); |
|
46
|
1 |
|
return $this->findEntity($qb); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Counts all the faces that belong to images of a given user, created using given model |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $userId User to which faces and associated images belongs to |
|
53
|
|
|
* @param int $model Model ID |
|
54
|
|
|
* @param bool $onlyWithoutPersons True if we need to count only faces which are not having person associated for it. |
|
55
|
|
|
* If false, all faces are counted. |
|
56
|
|
|
*/ |
|
57
|
14 |
|
public function countFaces(string $userId, int $model, bool $onlyWithoutPersons=false): int { |
|
58
|
14 |
|
$qb = $this->db->getQueryBuilder(); |
|
59
|
|
|
$qb = $qb |
|
60
|
14 |
|
->select($qb->createFunction('COUNT(' . $qb->getColumnName('f.id') . ')')) |
|
61
|
14 |
|
->from($this->getTableName(), 'f') |
|
62
|
14 |
|
->innerJoin('f', 'facerecog_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
|
63
|
14 |
|
->where($qb->expr()->eq('user', $qb->createParameter('user'))) |
|
64
|
14 |
|
->andWhere($qb->expr()->eq('model', $qb->createParameter('model'))); |
|
65
|
14 |
|
if ($onlyWithoutPersons) { |
|
66
|
|
|
$qb = $qb->andWhere($qb->expr()->isNull('person')); |
|
67
|
|
|
} |
|
68
|
|
|
$query = $qb |
|
69
|
14 |
|
->setParameter('user', $userId) |
|
70
|
14 |
|
->setParameter('model', $model); |
|
71
|
14 |
|
$resultStatement = $query->execute(); |
|
72
|
14 |
|
$data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
73
|
14 |
|
$resultStatement->closeCursor(); |
|
74
|
|
|
|
|
75
|
14 |
|
return (int)$data[0]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Gets oldest created face from database, for a given user and model, that is not associated with a person. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $userId User to which faces and associated images belongs to |
|
82
|
|
|
* @param int $model Model ID |
|
83
|
|
|
* |
|
84
|
|
|
* @return Face Oldest face, if any is found |
|
85
|
|
|
* @throws DoesNotExistException If there is no faces in database without person for a given user and model. |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getOldestCreatedFaceWithoutPerson(string $userId, int $model) { |
|
88
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
89
|
|
|
$qb |
|
90
|
|
|
->select('f.id', 'f.creation_time') |
|
91
|
|
|
->from($this->getTableName(), 'f') |
|
92
|
|
|
->innerJoin('f', 'facerecog_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
|
93
|
|
|
->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
94
|
|
|
->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($model))) |
|
95
|
|
|
->andWhere($qb->expr()->isNull('person')) |
|
96
|
|
|
->orderBy('f.creation_time', 'ASC'); |
|
97
|
|
|
$cursor = $qb->execute(); |
|
98
|
|
|
$row = $cursor->fetch(); |
|
99
|
|
|
if($row === false) { |
|
100
|
|
|
$cursor->closeCursor(); |
|
101
|
|
|
throw new DoesNotExistException("No faces found and we should have at least one"); |
|
102
|
|
|
} |
|
103
|
|
|
$face = $this->mapRowToEntity($row); |
|
104
|
|
|
$cursor->closeCursor(); |
|
105
|
|
|
return $face; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
17 |
|
public function getFaces(string $userId, $model): array { |
|
109
|
17 |
|
$qb = $this->db->getQueryBuilder(); |
|
110
|
17 |
|
$qb->select('f.id', 'f.person', 'f.confidence', 'f.descriptor') |
|
111
|
17 |
|
->from($this->getTableName(), 'f') |
|
112
|
17 |
|
->innerJoin('f', 'facerecog_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
|
113
|
17 |
|
->where($qb->expr()->eq('user', $qb->createParameter('user'))) |
|
114
|
17 |
|
->andWhere($qb->expr()->eq('model', $qb->createParameter('model'))) |
|
115
|
17 |
|
->setParameter('user', $userId) |
|
116
|
17 |
|
->setParameter('model', $model); |
|
117
|
17 |
|
return $this->findEntities($qb); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
13 |
|
public function findFacesFromPerson(string $userId, int $personId, int $model, $limit = null, $offset = null): array { |
|
121
|
13 |
|
$qb = $this->db->getQueryBuilder(); |
|
122
|
13 |
|
$qb->select('f.id', 'f.image', 'f.person') |
|
123
|
13 |
|
->from($this->getTableName(), 'f') |
|
124
|
13 |
|
->innerJoin('f', 'facerecog_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
|
125
|
13 |
|
->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
126
|
13 |
|
->andWhere($qb->expr()->eq('person', $qb->createNamedParameter($personId))) |
|
127
|
13 |
|
->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($model))); |
|
128
|
|
|
|
|
129
|
13 |
|
$qb->setMaxResults($limit); |
|
130
|
13 |
|
$qb->setFirstResult($offset); |
|
131
|
|
|
|
|
132
|
13 |
|
$faces = $this->findEntities($qb); |
|
133
|
13 |
|
return $faces; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function findRepresentativeFromPerson(string $userId, int $personId, float $sensitivity, int $model) { |
|
137
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
138
|
|
|
$qb->select('f.id', 'f.image', 'f.person', 'f.descriptor') |
|
139
|
|
|
->from($this->getTableName(), 'f') |
|
140
|
|
|
->innerJoin('f', 'facerecog_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
|
141
|
|
|
->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
142
|
|
|
->andWhere($qb->expr()->eq('person', $qb->createNamedParameter($personId))) |
|
143
|
|
|
->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($model))); |
|
144
|
|
|
$faces = $this->findEntities($qb); |
|
145
|
|
|
|
|
146
|
|
|
$facesCount = array(); |
|
147
|
|
|
$euclidean = new Euclidean(); |
|
148
|
|
|
for ($i = 0, $face_count1 = count($faces); $i < $face_count1; $i++) { |
|
149
|
|
|
$face1 = $faces[$i]; |
|
150
|
|
|
for ($j = $i, $face_count2 = count($faces); $j < $face_count2; $j++) { |
|
151
|
|
|
$face2 = $faces[$j]; |
|
152
|
|
|
$distance = $euclidean->distance($face1->descriptor, $face2->descriptor); |
|
153
|
|
|
if ($distance < $sensitivity) { |
|
154
|
|
|
if (!array_key_exists($i, $facesCount)) { |
|
155
|
|
|
$facesCount[$i] = 1; |
|
156
|
|
|
} else { |
|
157
|
|
|
$facesCount[$i] = $facesCount[$i]+1; |
|
158
|
|
|
} |
|
159
|
|
|
if (!array_key_exists($j, $facesCount)) { |
|
160
|
|
|
$facesCount[$j] = 1; |
|
161
|
|
|
} else { |
|
162
|
|
|
$facesCount[$j] = $facesCount[$j]+1; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
$bestFaceId = -1; |
|
169
|
|
|
$bestFaceCount = -1; |
|
170
|
|
|
foreach ($facesCount as $faceId => $faceCount) { |
|
171
|
|
|
if ($faceCount > $bestFaceCount) { |
|
172
|
|
|
$bestFaceId = $faceId; |
|
173
|
|
|
$bestFaceCount = $faceCount; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
return $faces[$bestFaceId]; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
public function getPersonOnFile(string $userId, int $personId, int $fileId, int $model): array { |
|
180
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
181
|
|
|
$qb->select('f.id', 'left', 'right', 'top', 'bottom') |
|
182
|
|
|
->from($this->getTableName(), 'f') |
|
183
|
|
|
->innerJoin('f', 'facerecog_persons' ,'p', $qb->expr()->eq('f.person', 'p.id')) |
|
184
|
|
|
->innerJoin('f', 'facerecog_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
|
185
|
|
|
->where($qb->expr()->eq('p.user', $qb->createParameter('user'))) |
|
186
|
|
|
->andWhere($qb->expr()->eq('person', $qb->createParameter('person'))) |
|
187
|
|
|
->andWhere($qb->expr()->eq('file', $qb->createParameter('file_id'))) |
|
188
|
|
|
->andWhere($qb->expr()->eq('model', $qb->createParameter('model'))) |
|
189
|
|
|
->andWhere($qb->expr()->eq('p.is_valid', $qb->createParameter('is_valid'))) |
|
190
|
|
|
->setParameter('user', $userId) |
|
191
|
|
|
->setParameter('person', $personId) |
|
192
|
|
|
->setParameter('file_id', $fileId) |
|
193
|
|
|
->setParameter('model', $model) |
|
194
|
|
|
->setParameter('is_valid', true) |
|
195
|
|
|
->orderBy('confidence', 'DESC'); |
|
196
|
|
|
$faces = $this->findEntities($qb); |
|
197
|
|
|
return $faces; |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Finds all faces contained in one image |
|
202
|
|
|
* Note that this is independent of any Model |
|
203
|
|
|
* |
|
204
|
|
|
* @param int $imageId Image for which to find all faces for |
|
205
|
|
|
*/ |
|
206
|
|
|
public function findByImage(int $imageId) { |
|
207
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
208
|
|
|
$qb->select('id', 'image', 'person') |
|
209
|
|
|
->from($this->getTableName()) |
|
210
|
|
|
->where($qb->expr()->eq('image', $qb->createNamedParameter($imageId))); |
|
211
|
|
|
$faces = $this->findEntities($qb); |
|
212
|
|
|
return $faces; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Removes all faces contained in one image. |
|
217
|
|
|
* Note that this is independent of any Model |
|
218
|
|
|
* |
|
219
|
|
|
* @param int $imageId Image for which to delete faces for |
|
220
|
|
|
*/ |
|
221
|
2 |
|
public function removeFromImage(int $imageId) { |
|
222
|
2 |
|
$qb = $this->db->getQueryBuilder(); |
|
223
|
2 |
|
$qb->delete($this->getTableName()) |
|
224
|
2 |
|
->where($qb->expr()->eq('image', $qb->createNamedParameter($imageId))) |
|
225
|
2 |
|
->execute(); |
|
226
|
2 |
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Deletes all faces from that user. |
|
230
|
|
|
* |
|
231
|
|
|
* @param string $userId User to drop faces from table. |
|
232
|
|
|
*/ |
|
233
|
28 |
|
public function deleteUserFaces(string $userId) { |
|
234
|
28 |
|
$sub = $this->db->getQueryBuilder(); |
|
235
|
28 |
|
$sub->select(new Literal('1')); |
|
236
|
28 |
|
$sub->from('facerecog_images', 'i') |
|
237
|
28 |
|
->where($sub->expr()->eq('i.id', '*PREFIX*' . $this->getTableName() .'.image')) |
|
238
|
28 |
|
->andWhere($sub->expr()->eq('i.user', $sub->createParameter('user'))); |
|
239
|
|
|
|
|
240
|
28 |
|
$qb = $this->db->getQueryBuilder(); |
|
241
|
28 |
|
$qb->delete($this->getTableName()) |
|
242
|
28 |
|
->where('EXISTS (' . $sub->getSQL() . ')') |
|
243
|
28 |
|
->setParameter('user', $userId) |
|
244
|
28 |
|
->execute(); |
|
245
|
28 |
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Unset relation beetwen faces and persons from that user in order to reset clustering |
|
249
|
|
|
* |
|
250
|
|
|
* @param string $userId User to drop fo unset relation. |
|
251
|
|
|
*/ |
|
252
|
|
|
public function unsetPersonsRelationForUser(string $userId, int $model) { |
|
253
|
|
|
$sub = $this->db->getQueryBuilder(); |
|
254
|
|
|
$sub->select(new Literal('1')); |
|
255
|
|
|
$sub->from('facerecog_images', 'i') |
|
256
|
|
|
->where($sub->expr()->eq('i.id', '*PREFIX*' . $this->getTableName() .'.image')) |
|
257
|
|
|
->andWhere($sub->expr()->eq('i.model', $sub->createParameter('model'))) |
|
258
|
|
|
->andWhere($sub->expr()->eq('i.user', $sub->createParameter('user'))); |
|
259
|
|
|
|
|
260
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
261
|
|
|
$qb->update($this->getTableName()) |
|
262
|
|
|
->set("person", $qb->createNamedParameter(null)) |
|
263
|
|
|
->where('EXISTS (' . $sub->getSQL() . ')') |
|
264
|
|
|
->setParameter('model', $model) |
|
265
|
|
|
->setParameter('user', $userId) |
|
266
|
|
|
->execute(); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Insert one face to database. |
|
271
|
|
|
* Note: only reason we are not using (idiomatic) QBMapper method is |
|
272
|
|
|
* because "QueryBuilder::PARAM_DATE" cannot be set there |
|
273
|
|
|
* |
|
274
|
|
|
* @param Face $face Face to insert |
|
275
|
|
|
* @param IDBConnection $db Existing connection, if we need to reuse it. Null if we commit immediatelly. |
|
276
|
|
|
*/ |
|
277
|
15 |
|
public function insertFace(Face $face, IDBConnection $db = null) { |
|
278
|
15 |
|
if ($db !== null) { |
|
279
|
1 |
|
$qb = $db->getQueryBuilder(); |
|
280
|
|
|
} else { |
|
281
|
14 |
|
$qb = $this->db->getQueryBuilder(); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
15 |
|
$qb->insert($this->getTableName()) |
|
285
|
15 |
|
->values([ |
|
286
|
15 |
|
'image' => $qb->createNamedParameter($face->image), |
|
287
|
15 |
|
'person' => $qb->createNamedParameter($face->person), |
|
288
|
15 |
|
'left' => $qb->createNamedParameter($face->left), |
|
289
|
15 |
|
'right' => $qb->createNamedParameter($face->right), |
|
290
|
15 |
|
'top' => $qb->createNamedParameter($face->top), |
|
291
|
15 |
|
'bottom' => $qb->createNamedParameter($face->bottom), |
|
292
|
15 |
|
'confidence' => $qb->createNamedParameter($face->confidence), |
|
293
|
15 |
|
'landmarks' => $qb->createNamedParameter(json_encode($face->landmarks)), |
|
294
|
15 |
|
'descriptor' => $qb->createNamedParameter(json_encode($face->descriptor)), |
|
295
|
15 |
|
'creation_time' => $qb->createNamedParameter($face->creationTime, IQueryBuilder::PARAM_DATE), |
|
296
|
|
|
]) |
|
297
|
15 |
|
->execute(); |
|
298
|
|
|
|
|
299
|
15 |
|
$face->setId((int) $qb->getLastInsertId()); |
|
300
|
|
|
|
|
301
|
15 |
|
return $face; |
|
302
|
|
|
} |
|
303
|
|
|
} |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths