|
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 Branko Kokanovic <[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
|
|
|
namespace OCA\FaceRecognition\Db; |
|
25
|
|
|
|
|
26
|
|
|
use OCP\IDBConnection; |
|
27
|
|
|
use OCP\IUser; |
|
28
|
|
|
|
|
29
|
|
|
use OCP\AppFramework\Db\QBMapper; |
|
30
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
|
31
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
|
32
|
|
|
|
|
33
|
|
|
class ImageMapper extends QBMapper { |
|
34
|
|
|
/** @var FaceMapper Face mapper*/ |
|
35
|
|
|
private $faceMapper; |
|
36
|
|
|
|
|
37
|
1 |
|
public function __construct(IDBConnection $db, FaceMapper $faceMapper) { |
|
38
|
1 |
|
parent::__construct($db, 'facerecog_images', '\OCA\FaceRecognition\Db\Image'); |
|
39
|
1 |
|
$this->faceMapper = $faceMapper; |
|
40
|
1 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param string $userId Id of user |
|
44
|
|
|
* @param int $imageId Id of Image to get |
|
45
|
|
|
*/ |
|
46
|
|
|
public function find(string $userId, int $imageId) { |
|
47
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
48
|
|
|
$qb->select('id', 'file', 'is_processed', 'error', 'last_processed_time', 'processing_duration') |
|
49
|
|
|
->from($this->getTableName(), 'i') |
|
50
|
|
|
->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
51
|
|
|
->andWhere($qb->expr()->eq('id', $qb->createNamedParameter($imageId))); |
|
52
|
|
|
return $this->findEntity($qb); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $userId Id of user |
|
57
|
|
|
* @param int $modelId Id of model to get |
|
58
|
|
|
*/ |
|
59
|
|
|
public function findAll(string $userId, int $modelId) { |
|
60
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
61
|
|
|
$qb->select('id', 'file', 'is_processed', 'error', 'last_processed_time', 'processing_duration') |
|
62
|
|
|
->from($this->getTableName()) |
|
63
|
|
|
->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
64
|
|
|
->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($modelId))); |
|
65
|
|
|
return $this->findEntities($qb); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $userId Id of user |
|
70
|
|
|
* @param int $modelId Id of model |
|
71
|
|
|
* @param int $fileId Id of file to get Image |
|
72
|
|
|
*/ |
|
73
|
|
|
public function findFromFile(string $userId, int $modelId, int $fileId) { |
|
74
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
75
|
|
|
$qb->select('id', 'is_processed', 'error') |
|
76
|
|
|
->from($this->getTableName(), 'i') |
|
77
|
|
|
->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
78
|
|
|
->andwhere($qb->expr()->eq('model', $qb->createNamedParameter($modelId))) |
|
79
|
|
|
->andWhere($qb->expr()->eq('file', $qb->createNamedParameter($fileId))); |
|
80
|
|
|
|
|
81
|
|
|
try { |
|
82
|
|
|
return $this->findEntity($qb); |
|
83
|
|
|
} catch (DoesNotExistException $e) { |
|
84
|
|
|
return null; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
7 |
|
public function imageExists(Image $image) { |
|
89
|
7 |
|
$qb = $this->db->getQueryBuilder(); |
|
90
|
|
|
$query = $qb |
|
91
|
7 |
|
->select(['id']) |
|
92
|
7 |
|
->from($this->getTableName()) |
|
93
|
7 |
|
->where($qb->expr()->eq('user', $qb->createParameter('user'))) |
|
94
|
7 |
|
->andWhere($qb->expr()->eq('file', $qb->createParameter('file'))) |
|
95
|
7 |
|
->andWhere($qb->expr()->eq('model', $qb->createParameter('model'))) |
|
96
|
7 |
|
->setParameter('user', $image->getUser()) |
|
97
|
7 |
|
->setParameter('file', $image->getFile()) |
|
98
|
7 |
|
->setParameter('model', $image->getModel()); |
|
99
|
7 |
|
$resultStatement = $query->execute(); |
|
100
|
7 |
|
$row = $resultStatement->fetch(); |
|
101
|
7 |
|
$resultStatement->closeCursor(); |
|
102
|
7 |
|
return $row ? (int)$row['id'] : null; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function countImages(int $model): int { |
|
106
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
107
|
|
|
$query = $qb |
|
108
|
|
|
->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')')) |
|
109
|
|
|
->from($this->getTableName()) |
|
110
|
|
|
->where($qb->expr()->eq('model', $qb->createParameter('model'))) |
|
111
|
|
|
->setParameter('model', $model); |
|
112
|
|
|
$resultStatement = $query->execute(); |
|
113
|
|
|
$data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
114
|
|
|
$resultStatement->closeCursor(); |
|
115
|
|
|
|
|
116
|
|
|
return (int)$data[0]; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
public function countProcessedImages(int $model): int { |
|
120
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
121
|
|
|
$query = $qb |
|
122
|
|
|
->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')')) |
|
123
|
|
|
->from($this->getTableName()) |
|
124
|
|
|
->where($qb->expr()->eq('model', $qb->createParameter('model'))) |
|
125
|
|
|
->andWhere($qb->expr()->eq('is_processed', $qb->createParameter('is_processed'))) |
|
126
|
|
|
->setParameter('model', $model) |
|
127
|
|
|
->setParameter('is_processed', True); |
|
128
|
|
|
$resultStatement = $query->execute(); |
|
129
|
|
|
$data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
130
|
|
|
$resultStatement->closeCursor(); |
|
131
|
|
|
|
|
132
|
|
|
return (int)$data[0]; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function avgProcessingDuration(int $model): int { |
|
136
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
137
|
|
|
$query = $qb |
|
138
|
|
|
->select($qb->createFunction('AVG(' . $qb->getColumnName('processing_duration') . ')')) |
|
139
|
|
|
->from($this->getTableName()) |
|
140
|
|
|
->where($qb->expr()->eq('model', $qb->createParameter('model'))) |
|
141
|
|
|
->andWhere($qb->expr()->eq('is_processed', $qb->createParameter('is_processed'))) |
|
142
|
|
|
->setParameter('model', $model) |
|
143
|
|
|
->setParameter('is_processed', True); |
|
144
|
|
|
$resultStatement = $query->execute(); |
|
145
|
|
|
$data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
146
|
|
|
$resultStatement->closeCursor(); |
|
147
|
|
|
|
|
148
|
|
|
return (int)$data[0]; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
2 |
|
public function countUserImages(string $userId, int $model, $processed = false): int { |
|
152
|
2 |
|
$qb = $this->db->getQueryBuilder(); |
|
153
|
|
|
$query = $qb |
|
154
|
2 |
|
->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')')) |
|
155
|
2 |
|
->from($this->getTableName()) |
|
156
|
2 |
|
->where($qb->expr()->eq('user', $qb->createParameter('user'))) |
|
157
|
2 |
|
->andWhere($qb->expr()->eq('model', $qb->createParameter('model'))) |
|
158
|
2 |
|
->setParameter('user', $userId) |
|
159
|
2 |
|
->setParameter('model', $model); |
|
160
|
|
|
|
|
161
|
2 |
|
if ($processed) { |
|
162
|
|
|
$query->andWhere($qb->expr()->eq('is_processed', $qb->createParameter('is_processed'))) |
|
163
|
|
|
->setParameter('is_processed', true); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
2 |
|
$resultStatement = $query->execute(); |
|
167
|
2 |
|
$data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
168
|
2 |
|
$resultStatement->closeCursor(); |
|
169
|
|
|
|
|
170
|
2 |
|
return (int)$data[0]; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param IUser|null $user User for which to get images for. If not given, all images from instance are returned. |
|
175
|
|
|
* @param int $modelId Model Id to get images for. |
|
176
|
|
|
*/ |
|
177
|
9 |
|
public function findImagesWithoutFaces(IUser $user = null, int $modelId): array { |
|
178
|
9 |
|
$qb = $this->db->getQueryBuilder(); |
|
179
|
|
|
$qb |
|
180
|
9 |
|
->select(['id', 'user', 'file', 'model']) |
|
181
|
9 |
|
->from($this->getTableName()) |
|
182
|
9 |
|
->where($qb->expr()->eq('is_processed', $qb->createParameter('is_processed'))) |
|
183
|
9 |
|
->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($modelId))) |
|
184
|
9 |
|
->setParameter('is_processed', false, IQueryBuilder::PARAM_BOOL); |
|
185
|
9 |
|
if (!is_null($user)) { |
|
186
|
9 |
|
$qb->andWhere($qb->expr()->eq('user', $qb->createNamedParameter($user->getUID()))); |
|
187
|
|
|
} |
|
188
|
9 |
|
return $this->findEntities($qb); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
1 |
|
public function findImages(string $userId, int $model): array { |
|
192
|
1 |
|
$qb = $this->db->getQueryBuilder(); |
|
193
|
1 |
|
$qb->select('i.id', 'i.file') |
|
194
|
1 |
|
->from($this->getTableName(), 'i') |
|
195
|
1 |
|
->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
196
|
1 |
|
->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($model))); |
|
197
|
|
|
|
|
198
|
1 |
|
$images = $this->findEntities($qb); |
|
199
|
1 |
|
return $images; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function findFromPersonLike(string $userId, int $model, string $name, $offset = null, $limit = null): array { |
|
203
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
204
|
|
|
$qb->select('i.id', 'i.file') |
|
205
|
|
|
->from($this->getTableName(), 'i') |
|
206
|
|
|
->innerJoin('i', 'facerecog_faces', 'f', $qb->expr()->eq('f.image', 'i.id')) |
|
207
|
|
|
->innerJoin('i', 'facerecog_persons', 'p', $qb->expr()->eq('f.person', 'p.id')) |
|
208
|
|
|
->where($qb->expr()->eq('p.user', $qb->createNamedParameter($userId))) |
|
209
|
|
|
->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($model))) |
|
210
|
|
|
->andWhere($qb->expr()->eq('is_processed', $qb->createNamedParameter(True))) |
|
211
|
|
|
->andWhere($qb->expr()->like($qb->func()->lower('p.name'), $qb->createParameter('query'))); |
|
212
|
|
|
|
|
213
|
|
|
$query = '%' . $this->db->escapeLikeParameter(strtolower($name)) . '%'; |
|
214
|
|
|
$qb->setParameter('query', $query); |
|
215
|
|
|
|
|
216
|
|
|
$qb->setFirstResult($offset); |
|
217
|
|
|
$qb->setMaxResults($limit); |
|
218
|
|
|
|
|
219
|
|
|
return $this->findEntities($qb); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Writes to DB that image has been processed. Previously found faces are deleted and new ones are inserted. |
|
224
|
|
|
* If there is exception, its stack trace is also updated. |
|
225
|
|
|
* |
|
226
|
|
|
* @param Image $image Image to be updated |
|
227
|
|
|
* @param Face[] $faces Faces to insert |
|
228
|
|
|
* @param int $duration Processing time, in milliseconds |
|
229
|
|
|
* @param \Exception|null $e Any exception that happened during image processing |
|
230
|
|
|
*/ |
|
231
|
|
|
public function imageProcessed(Image $image, array $faces, int $duration, \Exception $e = null) { |
|
232
|
|
|
$this->db->beginTransaction(); |
|
233
|
|
|
try { |
|
234
|
|
|
// Update image itself |
|
235
|
|
|
// |
|
236
|
|
|
$error = null; |
|
237
|
|
|
if ($e !== null) { |
|
238
|
|
|
$error = substr($e->getMessage(), 0, 1024); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
242
|
|
|
$qb->update($this->getTableName()) |
|
243
|
|
|
->set("is_processed", $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL)) |
|
244
|
|
|
->set("error", $qb->createNamedParameter($error)) |
|
245
|
|
|
->set("last_processed_time", $qb->createNamedParameter(new \DateTime(), IQueryBuilder::PARAM_DATE)) |
|
246
|
|
|
->set("processing_duration", $qb->createNamedParameter($duration)) |
|
247
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($image->id))) |
|
248
|
|
|
->execute(); |
|
249
|
|
|
|
|
250
|
|
|
// Delete all previous faces |
|
251
|
|
|
// |
|
252
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
253
|
|
|
$qb->delete('facerecog_faces') |
|
254
|
|
|
->where($qb->expr()->eq('image', $qb->createNamedParameter($image->id))) |
|
255
|
|
|
->execute(); |
|
256
|
|
|
|
|
257
|
|
|
// Insert all faces |
|
258
|
|
|
// |
|
259
|
|
|
foreach ($faces as $face) { |
|
260
|
|
|
$this->faceMapper->insertFace($face, $this->db); |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
$this->db->commit(); |
|
264
|
|
|
} catch (\Exception $e) { |
|
265
|
|
|
$this->db->rollBack(); |
|
266
|
|
|
throw $e; |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Resets image by deleting all associated faces and prepares it to be processed again |
|
272
|
|
|
* |
|
273
|
|
|
* @param Image $image Image to reset |
|
274
|
|
|
*/ |
|
275
|
|
|
public function resetImage(Image $image) { |
|
276
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
277
|
|
|
$qb->update($this->getTableName()) |
|
278
|
|
|
->set("is_processed", $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)) |
|
279
|
|
|
->set("error", $qb->createNamedParameter(null)) |
|
280
|
|
|
->set("last_processed_time", $qb->createNamedParameter(null)) |
|
281
|
|
|
->where($qb->expr()->eq('user', $qb->createNamedParameter($image->getUser()))) |
|
282
|
|
|
->andWhere($qb->expr()->eq('file', $qb->createNamedParameter($image->getFile()))) |
|
283
|
|
|
->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($image->getModel()))) |
|
284
|
|
|
->execute(); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Resets all image with error from that user and prepares it to be processed again |
|
289
|
|
|
* |
|
290
|
|
|
* @param string $userId User to reset errors |
|
291
|
|
|
*/ |
|
292
|
|
|
public function resetErrors(string $userId) { |
|
293
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
294
|
|
|
$qb->update($this->getTableName()) |
|
295
|
|
|
->set("is_processed", $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL)) |
|
296
|
|
|
->set("error", $qb->createNamedParameter(null)) |
|
297
|
|
|
->set("last_processed_time", $qb->createNamedParameter(null)) |
|
298
|
|
|
->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
299
|
|
|
->andWhere($qb->expr()->isNotNull('error')) |
|
300
|
|
|
->execute(); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
/** |
|
304
|
|
|
* Deletes all images from that user. |
|
305
|
|
|
* |
|
306
|
|
|
* @param string $userId User to drop images from table. |
|
307
|
|
|
*/ |
|
308
|
28 |
|
public function deleteUserImages(string $userId) { |
|
309
|
28 |
|
$qb = $this->db->getQueryBuilder(); |
|
310
|
28 |
|
$qb->delete($this->getTableName()) |
|
311
|
28 |
|
->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
312
|
28 |
|
->execute(); |
|
313
|
28 |
|
} |
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Deletes all images from that user and Model |
|
317
|
|
|
* |
|
318
|
|
|
* @param string $userId User to drop images from table. |
|
319
|
|
|
* @param int $modelId model to drop images from table. |
|
320
|
|
|
*/ |
|
321
|
|
|
public function deleteUserModel(string $userId, $modelId) { |
|
322
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
323
|
|
|
$qb->delete($this->getTableName()) |
|
324
|
|
|
->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
325
|
|
|
->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($modelId))) |
|
326
|
|
|
->execute(); |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
} |
|
330
|
|
|
|