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
|
|
|
class FaceMapper extends QBMapper { |
35
|
|
|
|
36
|
1 |
|
public function __construct(IDBConnection $db) { |
37
|
1 |
|
parent::__construct($db, 'facerecog_faces', '\OCA\FaceRecognition\Db\Face'); |
38
|
1 |
|
} |
39
|
|
|
|
40
|
1 |
|
public function find (int $faceId) { |
41
|
1 |
|
$qb = $this->db->getQueryBuilder(); |
42
|
1 |
|
$qb->select('id', 'image', 'person', 'left', 'right', 'top', 'bottom', 'landmarks', 'descriptor', 'confidence') |
43
|
1 |
|
->from($this->getTableName(), 'f') |
44
|
1 |
|
->andWhere($qb->expr()->eq('id', $qb->createNamedParameter($faceId))); |
45
|
1 |
|
return $this->findEntity($qb); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Based on a given fileId, takes all faces that belong to that file |
50
|
|
|
* and return an array with that. |
51
|
|
|
* |
52
|
|
|
* @param string $userId ID of the user that faces belong to |
53
|
|
|
* @param int $modelId ID of the model that faces belgon to |
54
|
|
|
* @param int $fileId ID of file for which to search faces. |
55
|
|
|
* |
56
|
|
|
* @return array Array of faces on that file |
57
|
|
|
*/ |
58
|
|
|
public function findFromFile(string $userId, int $modelId, int $fileId): array { |
59
|
|
|
$qb = $this->db->getQueryBuilder(); |
60
|
|
|
$qb->select('f.id', 'left', 'right', 'top', 'bottom', 'person', 'confidence', 'creation_time') |
61
|
|
|
->from($this->getTableName(), 'f') |
62
|
|
|
->innerJoin('f', 'facerecog_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
63
|
|
|
->where($qb->expr()->eq('i.user', $qb->createParameter('user_id'))) |
64
|
|
|
->andWhere($qb->expr()->eq('model', $qb->createParameter('model_id'))) |
65
|
|
|
->andWhere($qb->expr()->eq('file', $qb->createParameter('file_id'))) |
66
|
|
|
->setParameter('user_id', $userId) |
67
|
|
|
->setParameter('model_id', $modelId) |
68
|
|
|
->setParameter('file_id', $fileId) |
69
|
|
|
->orderBy('confidence', 'DESC'); |
70
|
|
|
return $this->findEntities($qb); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Counts all the faces that belong to images of a given user, created using given model |
75
|
|
|
* |
76
|
|
|
* @param string $userId User to which faces and associated images belongs to |
77
|
|
|
* @param int $model Model ID |
78
|
|
|
* @param bool $onlyWithoutPersons True if we need to count only faces which are not having person associated for it. |
79
|
|
|
* If false, all faces are counted. |
80
|
|
|
*/ |
81
|
14 |
|
public function countFaces(string $userId, int $model, bool $onlyWithoutPersons=false): int { |
82
|
14 |
|
$qb = $this->db->getQueryBuilder(); |
83
|
|
|
$qb = $qb |
84
|
14 |
|
->select($qb->createFunction('COUNT(' . $qb->getColumnName('f.id') . ')')) |
85
|
14 |
|
->from($this->getTableName(), 'f') |
86
|
14 |
|
->innerJoin('f', 'facerecog_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
87
|
14 |
|
->where($qb->expr()->eq('user', $qb->createParameter('user'))) |
88
|
14 |
|
->andWhere($qb->expr()->eq('model', $qb->createParameter('model'))); |
89
|
14 |
|
if ($onlyWithoutPersons) { |
90
|
|
|
$qb = $qb->andWhere($qb->expr()->isNull('person')); |
91
|
|
|
} |
92
|
|
|
$query = $qb |
93
|
14 |
|
->setParameter('user', $userId) |
94
|
14 |
|
->setParameter('model', $model); |
95
|
14 |
|
$resultStatement = $query->execute(); |
96
|
14 |
|
$data = $resultStatement->fetch(\PDO::FETCH_NUM); |
97
|
14 |
|
$resultStatement->closeCursor(); |
98
|
|
|
|
99
|
14 |
|
return (int)$data[0]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Gets oldest created face from database, for a given user and model, that is not associated with a person. |
104
|
|
|
* |
105
|
|
|
* @param string $userId User to which faces and associated images belongs to |
106
|
|
|
* @param int $model Model ID |
107
|
|
|
* |
108
|
|
|
* @return Face Oldest face, if any is found |
109
|
|
|
* @throws DoesNotExistException If there is no faces in database without person for a given user and model. |
110
|
|
|
*/ |
111
|
|
|
public function getOldestCreatedFaceWithoutPerson(string $userId, int $model) { |
112
|
|
|
$qb = $this->db->getQueryBuilder(); |
113
|
|
|
$qb |
114
|
|
|
->select('f.id', 'f.creation_time') |
115
|
|
|
->from($this->getTableName(), 'f') |
116
|
|
|
->innerJoin('f', 'facerecog_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
117
|
|
|
->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
118
|
|
|
->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($model))) |
119
|
|
|
->andWhere($qb->expr()->isNull('person')) |
120
|
|
|
->orderBy('f.creation_time', 'ASC'); |
121
|
|
|
$cursor = $qb->execute(); |
122
|
|
|
$row = $cursor->fetch(); |
123
|
|
|
if($row === false) { |
124
|
|
|
$cursor->closeCursor(); |
125
|
|
|
throw new DoesNotExistException("No faces found and we should have at least one"); |
126
|
|
|
} |
127
|
|
|
$face = $this->mapRowToEntity($row); |
128
|
|
|
$cursor->closeCursor(); |
129
|
|
|
return $face; |
130
|
|
|
} |
131
|
|
|
|
132
|
17 |
|
public function getFaces(string $userId, $model): array { |
133
|
17 |
|
$qb = $this->db->getQueryBuilder(); |
134
|
17 |
|
$qb->select('f.id', 'f.person', 'f.left', 'f.right', 'f.top', 'f.bottom', 'f.confidence', 'f.descriptor') |
135
|
17 |
|
->from($this->getTableName(), 'f') |
136
|
17 |
|
->innerJoin('f', 'facerecog_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
137
|
17 |
|
->where($qb->expr()->eq('user', $qb->createParameter('user'))) |
138
|
17 |
|
->andWhere($qb->expr()->eq('model', $qb->createParameter('model'))) |
139
|
17 |
|
->setParameter('user', $userId) |
140
|
17 |
|
->setParameter('model', $model); |
141
|
17 |
|
return $this->findEntities($qb); |
142
|
|
|
} |
143
|
|
|
|
144
|
13 |
|
public function findFacesFromPerson(string $userId, int $personId, int $model, $limit = null, $offset = null): array { |
145
|
13 |
|
$qb = $this->db->getQueryBuilder(); |
146
|
13 |
|
$qb->select('f.id', 'f.image', 'f.person') |
147
|
13 |
|
->from($this->getTableName(), 'f') |
148
|
13 |
|
->innerJoin('f', 'facerecog_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
149
|
13 |
|
->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
150
|
13 |
|
->andWhere($qb->expr()->eq('person', $qb->createNamedParameter($personId))) |
151
|
13 |
|
->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($model))); |
152
|
|
|
|
153
|
13 |
|
$qb->setMaxResults($limit); |
154
|
13 |
|
$qb->setFirstResult($offset); |
155
|
|
|
|
156
|
13 |
|
$faces = $this->findEntities($qb); |
157
|
13 |
|
return $faces; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Finds all faces contained in one image |
162
|
|
|
* Note that this is independent of any Model |
163
|
|
|
* |
164
|
|
|
* @param int $imageId Image for which to find all faces for |
165
|
|
|
*/ |
166
|
|
|
public function findByImage(int $imageId) { |
167
|
|
|
$qb = $this->db->getQueryBuilder(); |
168
|
|
|
$qb->select('id', 'image', 'person') |
169
|
|
|
->from($this->getTableName()) |
170
|
|
|
->where($qb->expr()->eq('image', $qb->createNamedParameter($imageId))); |
171
|
|
|
$faces = $this->findEntities($qb); |
172
|
|
|
return $faces; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Removes all faces contained in one image. |
177
|
|
|
* Note that this is independent of any Model |
178
|
|
|
* |
179
|
|
|
* @param int $imageId Image for which to delete faces for |
180
|
|
|
*/ |
181
|
2 |
|
public function removeFromImage(int $imageId) { |
182
|
2 |
|
$qb = $this->db->getQueryBuilder(); |
183
|
2 |
|
$qb->delete($this->getTableName()) |
184
|
2 |
|
->where($qb->expr()->eq('image', $qb->createNamedParameter($imageId))) |
185
|
2 |
|
->execute(); |
186
|
2 |
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Deletes all faces from that user. |
190
|
|
|
* |
191
|
|
|
* @param string $userId User to drop faces from table. |
192
|
|
|
*/ |
193
|
28 |
|
public function deleteUserFaces(string $userId) { |
194
|
28 |
|
$sub = $this->db->getQueryBuilder(); |
195
|
28 |
|
$sub->select(new Literal('1')); |
196
|
28 |
|
$sub->from('facerecog_images', 'i') |
197
|
28 |
|
->where($sub->expr()->eq('i.id', '*PREFIX*' . $this->getTableName() .'.image')) |
198
|
28 |
|
->andWhere($sub->expr()->eq('i.user', $sub->createParameter('user'))); |
199
|
|
|
|
200
|
28 |
|
$qb = $this->db->getQueryBuilder(); |
201
|
28 |
|
$qb->delete($this->getTableName()) |
202
|
28 |
|
->where('EXISTS (' . $sub->getSQL() . ')') |
203
|
28 |
|
->setParameter('user', $userId) |
204
|
28 |
|
->execute(); |
205
|
28 |
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Deletes all faces from that user and model |
209
|
|
|
* |
210
|
|
|
* @param string $userId User to drop faces from table. |
211
|
|
|
* @param int $modelId model to drop faces from table. |
212
|
|
|
*/ |
213
|
|
|
public function deleteUserModel(string $userId, $modelId) { |
214
|
|
|
$sub = $this->db->getQueryBuilder(); |
215
|
|
|
$sub->select(new Literal('1')); |
216
|
|
|
$sub->from('facerecog_images', 'i') |
217
|
|
|
->where($sub->expr()->eq('i.id', '*PREFIX*' . $this->getTableName() .'.image')) |
218
|
|
|
->andWhere($sub->expr()->eq('i.user', $sub->createParameter('user'))) |
219
|
|
|
->andWhere($sub->expr()->eq('i.model', $sub->createParameter('model'))); |
220
|
|
|
|
221
|
|
|
$qb = $this->db->getQueryBuilder(); |
222
|
|
|
$qb->delete($this->getTableName()) |
223
|
|
|
->where('EXISTS (' . $sub->getSQL() . ')') |
224
|
|
|
->setParameter('user', $userId) |
225
|
|
|
->setParameter('model', $modelId) |
226
|
|
|
->execute(); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* Unset relation beetwen faces and persons from that user in order to reset clustering |
231
|
|
|
* |
232
|
|
|
* @param string $userId User to drop fo unset relation. |
233
|
|
|
*/ |
234
|
|
|
public function unsetPersonsRelationForUser(string $userId, int $model) { |
235
|
|
|
$sub = $this->db->getQueryBuilder(); |
236
|
|
|
$sub->select(new Literal('1')); |
237
|
|
|
$sub->from('facerecog_images', 'i') |
238
|
|
|
->where($sub->expr()->eq('i.id', '*PREFIX*' . $this->getTableName() .'.image')) |
239
|
|
|
->andWhere($sub->expr()->eq('i.model', $sub->createParameter('model'))) |
240
|
|
|
->andWhere($sub->expr()->eq('i.user', $sub->createParameter('user'))); |
241
|
|
|
|
242
|
|
|
$qb = $this->db->getQueryBuilder(); |
243
|
|
|
$qb->update($this->getTableName()) |
244
|
|
|
->set("person", $qb->createNamedParameter(null)) |
245
|
|
|
->where('EXISTS (' . $sub->getSQL() . ')') |
246
|
|
|
->setParameter('model', $model) |
247
|
|
|
->setParameter('user', $userId) |
248
|
|
|
->execute(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Insert one face to database. |
253
|
|
|
* Note: only reason we are not using (idiomatic) QBMapper method is |
254
|
|
|
* because "QueryBuilder::PARAM_DATE" cannot be set there |
255
|
|
|
* |
256
|
|
|
* @param Face $face Face to insert |
257
|
|
|
* @param IDBConnection $db Existing connection, if we need to reuse it. Null if we commit immediatelly. |
258
|
|
|
*/ |
259
|
15 |
|
public function insertFace(Face $face, IDBConnection $db = null) { |
260
|
15 |
|
if ($db !== null) { |
261
|
1 |
|
$qb = $db->getQueryBuilder(); |
262
|
|
|
} else { |
263
|
14 |
|
$qb = $this->db->getQueryBuilder(); |
264
|
|
|
} |
265
|
|
|
|
266
|
15 |
|
$qb->insert($this->getTableName()) |
267
|
15 |
|
->values([ |
268
|
15 |
|
'image' => $qb->createNamedParameter($face->image), |
269
|
15 |
|
'person' => $qb->createNamedParameter($face->person), |
270
|
15 |
|
'left' => $qb->createNamedParameter($face->left), |
271
|
15 |
|
'right' => $qb->createNamedParameter($face->right), |
272
|
15 |
|
'top' => $qb->createNamedParameter($face->top), |
273
|
15 |
|
'bottom' => $qb->createNamedParameter($face->bottom), |
274
|
15 |
|
'confidence' => $qb->createNamedParameter($face->confidence), |
275
|
15 |
|
'landmarks' => $qb->createNamedParameter(json_encode($face->landmarks)), |
276
|
15 |
|
'descriptor' => $qb->createNamedParameter(json_encode($face->descriptor)), |
277
|
15 |
|
'creation_time' => $qb->createNamedParameter($face->creationTime, IQueryBuilder::PARAM_DATE), |
278
|
|
|
]) |
279
|
15 |
|
->execute(); |
280
|
|
|
|
281
|
15 |
|
$face->setId((int) $qb->getLastInsertId()); |
282
|
|
|
|
283
|
15 |
|
return $face; |
284
|
|
|
} |
285
|
|
|
} |
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