|
1
|
|
|
<?php |
|
2
|
|
|
namespace OCA\FaceRecognition\Db; |
|
3
|
|
|
|
|
4
|
|
|
use OC\DB\QueryBuilder\Literal; |
|
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use OCP\IDBConnection; |
|
7
|
|
|
use OCP\AppFramework\Db\QBMapper; |
|
8
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
|
9
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
|
10
|
|
|
|
|
11
|
|
|
class FaceMapper extends QBMapper { |
|
12
|
28 |
|
public function __construct(IDBConnection $db) { |
|
13
|
28 |
|
parent::__construct($db, 'face_recognition_faces', '\OCA\FaceRecognition\Db\Face'); |
|
14
|
28 |
|
} |
|
15
|
|
|
|
|
16
|
1 |
|
public function find (int $faceId): Face { |
|
17
|
1 |
|
$qb = $this->db->getQueryBuilder(); |
|
18
|
1 |
|
$qb->select('id', 'image', 'person', 'left', 'right', 'top', 'bottom', 'descriptor') |
|
19
|
1 |
|
->from('face_recognition_faces', 'f') |
|
20
|
1 |
|
->andWhere($qb->expr()->eq('id', $qb->createNamedParameter($faceId))); |
|
21
|
1 |
|
$faces = $this->findEntity($qb); |
|
22
|
1 |
|
return $faces; |
|
|
|
|
|
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Counts all the faces that belong to images of a given user, created using given model |
|
27
|
|
|
* |
|
28
|
|
|
* @param string $userId User to which faces and associated images belongs to |
|
29
|
|
|
* @param int $model Model ID |
|
30
|
|
|
* @param bool $onlyWithoutPersons True if we need to count only faces which are not having person associated for it. |
|
31
|
|
|
* If false, all faces are counted. |
|
32
|
|
|
*/ |
|
33
|
14 |
|
public function countFaces(string $userId, int $model, bool $onlyWithoutPersons=false): int { |
|
34
|
14 |
|
$qb = $this->db->getQueryBuilder(); |
|
35
|
|
|
$qb = $qb |
|
36
|
14 |
|
->select($qb->createFunction('COUNT(' . $qb->getColumnName('f.id') . ')')) |
|
37
|
14 |
|
->from('face_recognition_faces', 'f') |
|
38
|
14 |
|
->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
|
39
|
14 |
|
->where($qb->expr()->eq('user', $qb->createParameter('user'))) |
|
40
|
14 |
|
->andWhere($qb->expr()->eq('model', $qb->createParameter('model'))); |
|
41
|
14 |
|
if ($onlyWithoutPersons) { |
|
42
|
|
|
$qb = $qb->andWhere($qb->expr()->isNull('person')); |
|
43
|
|
|
} |
|
44
|
|
|
$query = $qb |
|
45
|
14 |
|
->setParameter('user', $userId) |
|
46
|
14 |
|
->setParameter('model', $model); |
|
47
|
14 |
|
$resultStatement = $query->execute(); |
|
48
|
14 |
|
$data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
49
|
14 |
|
$resultStatement->closeCursor(); |
|
50
|
|
|
|
|
51
|
14 |
|
return (int)$data[0]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Gets oldest created face from database, for a given user and model, that is not associated with a person. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $userId User to which faces and associated images belongs to |
|
58
|
|
|
* @param int $model Model ID |
|
59
|
|
|
* |
|
60
|
|
|
* @return Face Oldest face, if any is found |
|
61
|
|
|
* @throws DoesNotExistException If there is no faces in database without person for a given user and model. |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getOldestCreatedFaceWithoutPerson(string $userId, int $model): Face { |
|
64
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
65
|
|
|
$qb |
|
66
|
|
|
->select('f.id', 'f.creation_time') |
|
67
|
|
|
->from('face_recognition_faces', 'f') |
|
68
|
|
|
->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
|
69
|
|
|
->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
70
|
|
|
->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($model))) |
|
71
|
|
|
->andWhere($qb->expr()->isNull('person')) |
|
72
|
|
|
->orderBy('f.creation_time', 'ASC'); |
|
73
|
|
|
$cursor = $qb->execute(); |
|
74
|
|
|
$row = $cursor->fetch(); |
|
75
|
|
|
if($row === false) { |
|
76
|
|
|
$cursor->closeCursor(); |
|
77
|
|
|
throw new DoesNotExistException("No faces found and we should have at least one"); |
|
78
|
|
|
} |
|
79
|
|
|
$face = $this->mapRowToEntity($row); |
|
80
|
|
|
$cursor->closeCursor(); |
|
81
|
|
|
return $face; |
|
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
17 |
|
public function getFaces(string $userId, $model): array { |
|
85
|
17 |
|
$qb = $this->db->getQueryBuilder(); |
|
86
|
|
|
$query = $qb |
|
|
|
|
|
|
87
|
17 |
|
->select('f.id', 'f.person', 'f.confidence', 'f.descriptor') |
|
88
|
17 |
|
->from('face_recognition_faces', 'f') |
|
89
|
17 |
|
->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
|
90
|
17 |
|
->where($qb->expr()->eq('user', $qb->createParameter('user'))) |
|
91
|
17 |
|
->andWhere($qb->expr()->eq('model', $qb->createParameter('model'))) |
|
92
|
17 |
|
->setParameter('user', $userId) |
|
93
|
17 |
|
->setParameter('model', $model); |
|
94
|
17 |
|
$faces = $this->findEntities($qb); |
|
95
|
17 |
|
return $faces; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
13 |
|
public function findFacesFromPerson(string $userId, int $personId, int $model, $limit = null, $offset = null): array { |
|
99
|
13 |
|
$qb = $this->db->getQueryBuilder(); |
|
100
|
13 |
|
$qb->select('f.id', 'f.image', 'f.person') |
|
101
|
13 |
|
->from('face_recognition_faces', 'f') |
|
102
|
13 |
|
->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
|
103
|
13 |
|
->where($qb->expr()->eq('user', $qb->createNamedParameter($userId))) |
|
104
|
13 |
|
->andWhere($qb->expr()->eq('person', $qb->createNamedParameter($personId))) |
|
105
|
13 |
|
->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($model))); |
|
106
|
|
|
|
|
107
|
13 |
|
$qb->setMaxResults($limit); |
|
108
|
13 |
|
$qb->setFirstResult($offset); |
|
109
|
|
|
|
|
110
|
13 |
|
$faces = $this->findEntities($qb); |
|
111
|
13 |
|
return $faces; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
public function getPersonOnFile(string $userId, int $personId, int $fileId, int $model): array { |
|
116
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
117
|
|
|
$qb->select('f.id', 'left', 'right', 'top', 'bottom') |
|
118
|
|
|
->from('face_recognition_faces', 'f') |
|
119
|
|
|
->innerJoin('f', 'face_recognition_persons' ,'p', $qb->expr()->eq('f.person', 'p.id')) |
|
120
|
|
|
->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
|
121
|
|
|
->where($qb->expr()->eq('p.user', $qb->createParameter('user'))) |
|
122
|
|
|
->andWhere($qb->expr()->eq('person', $qb->createParameter('person'))) |
|
123
|
|
|
->andWhere($qb->expr()->eq('file', $qb->createParameter('file_id'))) |
|
124
|
|
|
->andWhere($qb->expr()->eq('model', $qb->createParameter('model'))) |
|
125
|
|
|
->andWhere($qb->expr()->eq('p.is_valid', $qb->createParameter('is_valid'))) |
|
126
|
|
|
->setParameter('user', $userId) |
|
127
|
|
|
->setParameter('person', $personId) |
|
128
|
|
|
->setParameter('file_id', $fileId) |
|
129
|
|
|
->setParameter('model', $model) |
|
130
|
|
|
->setParameter('is_valid', true) |
|
131
|
|
|
->orderBy('confidence', 'DESC'); |
|
132
|
|
|
$faces = $this->findEntities($qb); |
|
133
|
|
|
return $faces; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Finds all faces contained in one image |
|
138
|
|
|
* |
|
139
|
|
|
* @param int $imageId Image for which to find all faces for |
|
140
|
|
|
*/ |
|
141
|
|
|
public function findByImage(int $imageId) { |
|
142
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
143
|
|
|
$qb->select('id', 'image', 'person') |
|
144
|
|
|
->from($this->getTableName()) |
|
145
|
|
|
->where($qb->expr()->eq('image', $qb->createNamedParameter($imageId))); |
|
146
|
|
|
$faces = $this->findEntities($qb); |
|
147
|
|
|
return $faces; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param int $imageId Image for which to delete faces for |
|
152
|
|
|
*/ |
|
153
|
2 |
|
public function removeFaces(int $imageId) { |
|
154
|
2 |
|
$qb = $this->db->getQueryBuilder(); |
|
155
|
2 |
|
$qb->delete($this->getTableName()) |
|
156
|
2 |
|
->where($qb->expr()->eq('image', $qb->createNamedParameter($imageId))) |
|
157
|
2 |
|
->execute(); |
|
158
|
2 |
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Deletes all faces from that user. |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $userId User to drop faces from table. |
|
164
|
|
|
*/ |
|
165
|
28 |
|
public function deleteUserFaces(string $userId) { |
|
166
|
28 |
|
$sub = $this->db->getQueryBuilder(); |
|
167
|
28 |
|
$sub->select(new Literal('1')); |
|
168
|
28 |
|
$sub->from("face_recognition_images", "i") |
|
169
|
28 |
|
->where($sub->expr()->eq('i.id', '*PREFIX*' . $this->getTableName() .'.image')) |
|
170
|
28 |
|
->andWhere($sub->expr()->eq('i.user', $sub->createParameter('user'))); |
|
171
|
|
|
|
|
172
|
28 |
|
$qb = $this->db->getQueryBuilder(); |
|
173
|
28 |
|
$qb->delete($this->getTableName()) |
|
174
|
28 |
|
->where('EXISTS (' . $sub->getSQL() . ')') |
|
175
|
28 |
|
->setParameter('user', $userId) |
|
176
|
28 |
|
->execute(); |
|
177
|
28 |
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Insert one face to database. |
|
181
|
|
|
* Note: only reason we are not using (idiomatic) QBMapper method is |
|
182
|
|
|
* because "QueryBuilder::PARAM_DATE" cannot be set there |
|
183
|
|
|
* |
|
184
|
|
|
* @param Face $face Face to insert |
|
185
|
|
|
* @param IDBConnection $db Existing connection, if we need to reuse it. Null if we commit immediatelly. |
|
186
|
|
|
*/ |
|
187
|
15 |
|
public function insertFace(Face $face, IDBConnection $db = null) { |
|
188
|
15 |
|
if ($db !== null) { |
|
189
|
1 |
|
$qb = $db->getQueryBuilder(); |
|
190
|
|
|
} else { |
|
191
|
14 |
|
$qb = $this->db->getQueryBuilder(); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
15 |
|
$qb->insert($this->getTableName()) |
|
195
|
15 |
|
->values([ |
|
196
|
15 |
|
'image' => $qb->createNamedParameter($face->image), |
|
197
|
15 |
|
'person' => $qb->createNamedParameter($face->person), |
|
198
|
15 |
|
'left' => $qb->createNamedParameter($face->left), |
|
199
|
15 |
|
'right' => $qb->createNamedParameter($face->right), |
|
200
|
15 |
|
'top' => $qb->createNamedParameter($face->top), |
|
201
|
15 |
|
'bottom' => $qb->createNamedParameter($face->bottom), |
|
202
|
15 |
|
'confidence' => $qb->createNamedParameter($face->confidence), |
|
203
|
15 |
|
'landmarks' => $qb->createNamedParameter(json_encode($face->landmarks)), |
|
204
|
15 |
|
'descriptor' => $qb->createNamedParameter(json_encode($face->descriptor)), |
|
205
|
15 |
|
'creation_time' => $qb->createNamedParameter($face->creationTime, IQueryBuilder::PARAM_DATE), |
|
206
|
|
|
]) |
|
207
|
15 |
|
->execute(); |
|
208
|
15 |
|
$face->setId((int) $qb->getLastInsertId()); |
|
209
|
|
|
} |
|
210
|
|
|
} |
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