1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2020, Matias De lellis <[email protected]> |
4
|
|
|
* @copyright Copyright (c) 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
|
|
|
|
25
|
|
|
namespace OCA\FaceRecognition\Service; |
26
|
|
|
|
27
|
|
|
use OCP\IUser; |
28
|
|
|
use OCP\IUserManager; |
29
|
|
|
|
30
|
|
|
use OCA\FaceRecognition\Db\FaceMapper; |
31
|
|
|
use OCA\FaceRecognition\Db\ImageMapper; |
32
|
|
|
use OCA\FaceRecognition\Db\PersonMapper; |
33
|
|
|
|
34
|
|
|
use OCA\FaceRecognition\Service\SettingsService; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Background service. Both command and cron job are calling this service for long-running background operations. |
38
|
|
|
* Background processing for face recognition is comprised of several steps, called tasks. Each task is independent, |
39
|
|
|
* idempotent, DI-aware logic unit that yields. Since tasks are non-preemptive, they should yield from time to time, so we son't end up |
40
|
|
|
* working for more than given timeout. |
41
|
|
|
* |
42
|
|
|
* Tasks can be seen as normal sequential functions, but they are easier to work with, |
43
|
|
|
* reason about them and test them independently. Other than that, they are really glorified functions. |
44
|
|
|
*/ |
45
|
|
|
class FaceManagementService { |
46
|
|
|
|
47
|
|
|
/** @var IUserManager */ |
48
|
|
|
private $userManager; |
49
|
|
|
|
50
|
|
|
/** @var FaceMapper */ |
51
|
|
|
private $faceMapper; |
52
|
|
|
|
53
|
|
|
/** @var ImageMapper */ |
54
|
|
|
private $imageMapper; |
55
|
|
|
|
56
|
|
|
/** @var PersonMapper */ |
57
|
|
|
private $personMapper; |
58
|
|
|
|
59
|
|
|
/** @var SettingsService */ |
60
|
|
|
private $settingsService; |
61
|
|
|
|
62
|
2 |
|
public function __construct(IUserManager $userManager, |
63
|
|
|
FaceMapper $faceMapper, |
64
|
|
|
ImageMapper $imageMapper, |
65
|
|
|
PersonMapper $personMapper, |
66
|
|
|
SettingsService $settingsService) |
67
|
|
|
{ |
68
|
2 |
|
$this->userManager = $userManager; |
69
|
2 |
|
$this->faceMapper = $faceMapper; |
70
|
2 |
|
$this->imageMapper = $imageMapper; |
71
|
2 |
|
$this->personMapper = $personMapper; |
72
|
2 |
|
$this->settingsService = $settingsService; |
73
|
2 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Deletes all faces, images and persons found. IF no user is given, resetting is executed for all users. |
77
|
|
|
* |
78
|
|
|
* @param IUser|null $user Optional user to execute resetting for |
79
|
|
|
*/ |
80
|
|
|
public function resetAll(IUser $user = null) { |
81
|
|
|
$eligible_users = $this->getEligiblesUserId($user); |
82
|
|
|
foreach($eligible_users as $user) { |
83
|
|
|
$this->resetAllForUser($user); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Deletes all faces, images and persons found for a given user. |
89
|
|
|
* |
90
|
|
|
* @param string $user ID of user to execute resetting for |
91
|
|
|
*/ |
92
|
28 |
|
public function resetAllForUser(string $userId) { |
93
|
28 |
|
$this->faceMapper->deleteUserFaces($userId); |
94
|
28 |
|
$this->personMapper->deleteUserPersons($userId); |
95
|
28 |
|
$this->imageMapper->deleteUserImages($userId); |
96
|
|
|
|
97
|
28 |
|
$this->settingsService->setUserFullScanDone(false, $userId); |
98
|
28 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Reset error in images in order to re-analyze again. |
102
|
|
|
* If no user is given, resetting is executed for all users. |
103
|
|
|
* |
104
|
|
|
* @param IUser|null $user Optional user to execute resetting for |
105
|
|
|
*/ |
106
|
|
|
public function resetImageErrors(IUser $user = null) { |
107
|
|
|
$eligible_users = $this->getEligiblesUserId($user); |
108
|
|
|
foreach($eligible_users as $userId) { |
109
|
|
|
$this->imageMapper->resetErrors($userId); |
110
|
|
|
$this->settingsService->setUserFullScanDone(false, $userId); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Eliminate all faces relations with person. |
116
|
|
|
* If no user is given, resetting is executed for all users. |
117
|
|
|
* |
118
|
|
|
* @param IUser|null $user Optional user to execute resetting for |
119
|
|
|
*/ |
120
|
|
|
public function resetClusters(IUser $user = null) { |
121
|
|
|
$eligible_users = $this->getEligiblesUserId($user); |
122
|
|
|
foreach($eligible_users as $user) { |
123
|
|
|
$this->resetClustersForUser($user); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Eliminate all faces relations with person. |
129
|
|
|
* |
130
|
|
|
* @param string $user ID of user to execute resetting for |
131
|
|
|
*/ |
132
|
|
|
public function resetClustersForUser(string $userId) { |
133
|
|
|
$model = $this->settingsService->getCurrentFaceModel(); |
134
|
|
|
|
135
|
|
|
$this->faceMapper->unsetPersonsRelationForUser($userId, $model); |
136
|
|
|
$this->personMapper->deleteUserPersons($userId); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get an array with the eligibles users taking into account the user argument, |
141
|
|
|
* or all users. |
142
|
|
|
* |
143
|
|
|
* @param IUser|null $user Optional user to get specific user. |
144
|
|
|
*/ |
145
|
|
|
private function getEligiblesUserId(IUser $user = null): array { |
146
|
|
|
$eligible_users = array(); |
147
|
|
|
if (is_null($user)) { |
148
|
|
|
$this->userManager->callForAllUsers(function (IUser $user) use (&$eligible_users) { |
149
|
|
|
$eligible_users[] = $user->getUID(); |
150
|
|
|
}); |
151
|
|
|
} else { |
152
|
|
|
$eligible_users[] = $user->getUID(); |
153
|
|
|
} |
154
|
|
|
return $eligible_users; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
} |