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
|
26 |
|
public function __construct(IDBConnection $db) { |
13
|
26 |
|
parent::__construct($db, 'face_recognition_faces', '\OCA\FaceRecognition\Db\Face'); |
14
|
26 |
|
} |
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.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
|
|
|
$faces = $this->findEntities($qb); |
132
|
|
|
return $faces; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param int $imageId Image for which to delete faces for |
137
|
|
|
*/ |
138
|
2 |
|
public function removeFaces(int $imageId) { |
139
|
2 |
|
$qb = $this->db->getQueryBuilder(); |
140
|
2 |
|
$qb->delete($this->getTableName()) |
141
|
2 |
|
->where($qb->expr()->eq('image', $qb->createNamedParameter($imageId))) |
142
|
2 |
|
->execute(); |
143
|
2 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Deletes all faces from that user. |
147
|
|
|
* |
148
|
|
|
* @param string $userId User to drop faces from table. |
149
|
|
|
*/ |
150
|
26 |
|
public function deleteUserFaces(string $userId) { |
151
|
26 |
|
$sub = $this->db->getQueryBuilder(); |
152
|
26 |
|
$sub->select(new Literal('1')); |
153
|
26 |
|
$sub->from("face_recognition_images", "i") |
154
|
26 |
|
->where($sub->expr()->eq('i.id', '*PREFIX*' . $this->getTableName() .'.image')) |
155
|
26 |
|
->andWhere($sub->expr()->eq('i.user', $sub->createParameter('user'))); |
156
|
|
|
|
157
|
26 |
|
$qb = $this->db->getQueryBuilder(); |
158
|
26 |
|
$qb->delete($this->getTableName()) |
159
|
26 |
|
->where('EXISTS (' . $sub->getSQL() . ')') |
160
|
26 |
|
->setParameter('user', $userId) |
161
|
26 |
|
->execute(); |
162
|
26 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Insert one face to database. |
166
|
|
|
* Note: only reason we are not using (idiomatic) QBMapper method is |
167
|
|
|
* because "QueryBuilder::PARAM_DATE" cannot be set there |
168
|
|
|
* |
169
|
|
|
* @param Face $face Face to insert |
170
|
|
|
* @param IDBConnection $db Existing connection, if we need to reuse it. Null if we commit immediatelly. |
171
|
|
|
*/ |
172
|
15 |
|
public function insertFace(Face $face, IDBConnection $db = null) { |
173
|
15 |
|
if ($db !== null) { |
174
|
1 |
|
$qb = $db->getQueryBuilder(); |
175
|
|
|
} else { |
176
|
14 |
|
$qb = $this->db->getQueryBuilder(); |
177
|
|
|
} |
178
|
|
|
|
179
|
15 |
|
$qb->insert($this->getTableName()) |
180
|
15 |
|
->values([ |
181
|
15 |
|
'image' => $qb->createNamedParameter($face->image), |
182
|
15 |
|
'person' => $qb->createNamedParameter($face->person), |
183
|
15 |
|
'left' => $qb->createNamedParameter($face->left), |
184
|
15 |
|
'right' => $qb->createNamedParameter($face->right), |
185
|
15 |
|
'top' => $qb->createNamedParameter($face->top), |
186
|
15 |
|
'bottom' => $qb->createNamedParameter($face->bottom), |
187
|
15 |
|
'descriptor' => $qb->createNamedParameter(json_encode($face->descriptor)), |
188
|
15 |
|
'creation_time' => $qb->createNamedParameter($face->creationTime, IQueryBuilder::PARAM_DATE), |
189
|
|
|
]) |
190
|
15 |
|
->execute(); |
191
|
15 |
|
$face->setId((int) $qb->getLastInsertId()); |
192
|
|
|
} |
193
|
|
|
} |
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