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