Passed
Push — translation ( b6530c...f6a5f9 )
by Matias
02:15
created

CreateClustersTask::getCurrentClusters()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 11
ccs 0
cts 8
cp 0
crap 20
rs 10
c 0
b 0
f 0
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\BackgroundJob\Tasks;
25
26
use OCP\IConfig;
27
use OCP\IUser;
28
29
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionBackgroundTask;
30
use OCA\FaceRecognition\BackgroundJob\FaceRecognitionContext;
31
use OCA\FaceRecognition\BackgroundJob\Tasks\AddMissingImagesTask;
32
33
use OCA\FaceRecognition\Db\FaceMapper;
34
use OCA\FaceRecognition\Db\ImageMapper;
35
use OCA\FaceRecognition\Db\PersonMapper;
36
37
use OCA\FaceRecognition\Helper\Euclidean;
38
39
use OCA\FaceRecognition\Migration\AddDefaultFaceModel;
40
41
/**
42
 * Taks that, for each user, creates person clusters for each.
43
 */
44
class CreateClustersTask extends FaceRecognitionBackgroundTask {
45
	/** @var IConfig Config */
46
	private $config;
47
48
	/** @var PersonMapper Person mapper*/
49
	private $personMapper;
50
51
	/** @var ImageMapper Image mapper*/
52
	private $imageMapper;
53
54
	/** @var FaceMapper Face mapper*/
55
	private $faceMapper;
56
57
	/**
58
	 * @param IConfig $config Config
59
	 */
60 2
	public function __construct(IConfig      $config,
61
	                            PersonMapper $personMapper,
62
	                            ImageMapper  $imageMapper,
63
	                            FaceMapper   $faceMapper)
64
	{
65 2
		parent::__construct();
66 2
		$this->config = $config;
67 2
		$this->personMapper = $personMapper;
68 2
		$this->imageMapper = $imageMapper;
69 2
		$this->faceMapper = $faceMapper;
70 2
	}
71
72
	/**
73
	 * @inheritdoc
74
	 */
75
	public function description() {
76
		return "Create new persons or update existing persons";
77
	}
78
79
	/**
80
	 * @inheritdoc
81
	 */
82
	public function execute(FaceRecognitionContext $context) {
83
		$this->setContext($context);
84
85
		$fullImageScanDone = $this->config->getAppValue('facerecognition', AddMissingImagesTask::FULL_IMAGE_SCAN_DONE_KEY, 'false');
86
		if ($fullImageScanDone != 'true') {
87
			// If not all images are not interested in the database, we cannot determine when we should start clustering.
88
			// Since this is step in beggining, just bail out.
89
			$this->logInfo('Skipping cluster creation, as not even existing images are found and inserted in database');
90
			return true;
91
		}
92
93
		// We cannot yield inside of Closure, so we need to extract all users and iterate outside of closure.
94
		// However, since we don't want to do deep copy of IUser, we keep only UID in this array.
95
		//
96
		$eligable_users = array();
97
		if (is_null($this->context->user)) {
98
			$this->context->userManager->callForSeenUsers(function (IUser $user) use (&$eligable_users) {
99
				$eligable_users[] = $user->getUID();
100
			});
101
		} else {
102
			$eligable_users[] = $this->context->user->getUID();
103
		}
104
105
		foreach($eligable_users as $user) {
106
			$this->createClusterIfNeeded($user);
107
		}
108
109
		return true;
110
	}
111
112
	private function createClusterIfNeeded(string $userId) {
113
		// Check that we processed enough images to start creating clusters
114
		//
115
		$modelId = intval($this->config->getAppValue('facerecognition', 'model', AddDefaultFaceModel::DEFAULT_FACE_MODEL_ID));
116
117
		$hasPersons = $this->personMapper->countPersons($userId) > 0;
118
119
		// Depending on whether we already have clusters, decide if we should create/recreate them.
120
		//
121
		if ($hasPersons) {
122
			// OK, we already got some persons. We now need to evaluate whether we want to recreate clusters.
123
			// We want to recreate clusters/persons if:
124
			// * Some cluster/person is invalidated (is_valid is false for someone)
125
			//     This means some image that belonged to this user is changed, deleted etc.
126
			// * There are some new faces. Now, we don't want to jump the gun here. We want to either have:
127
			// ** more than 10 new faces, or
128
			// ** less than 10 new faces, but they are older than 2h
129
			//  (basically, we want to avoid recreating cluster for each new face being uploaded,
130
			//  however, we don't want to wait too much as clusters could be changed a lot)
131
			//
132
			$haveNewFaces = false;
133
			$facesWithoutPersons = $this->faceMapper->countFaces($userId, $modelId, true);
134
			$this->logDebug(sprintf('Found %d faces without associated persons for user %s and model %d',
135
				$facesWithoutPersons, $userId, $modelId));
136
			// todo: get rid of magic numbers (move to config)
137
			if ($facesWithoutPersons >= 10) {
138
				$haveNewFaces = true;
139
			} else if ($facesWithoutPersons > 0) {
140
				// We have some faces, but not that many, let's see when oldest one is generated.
141
				$face = $this->faceMapper->getOldestCreatedFaceWithoutPerson($userId, $modelId);
142
				$oldestFaceTimestamp = $face->creationTime->getTimestamp();
143
				$currentTimestamp = (new \DateTime())->getTimestamp();
144
				$this->logDebug(sprintf('Oldest face without persons for user %s and model %d is from %s',
145
					$userId, $modelId, $face->creationTime->format('Y-m-d H:i:s')));
146
				// todo: get rid of magic numbers (move to config)
147
				if ($currentTimestamp - $oldestFaceTimestamp > 2 * 60 * 60) {
148
					$haveNewFaces = true;
149
				}
150
			}
151
152
			$stalePersonsCount = $this->personMapper->countPersons($userId, true);
153
			$this->logDebug(sprintf('Found %d stale persons for user %s and model %d', $stalePersonsCount, $userId, $modelId));
154
			$haveStalePersons = $stalePersonsCount > 0;
155
156
			if ($haveStalePersons === false && $haveNewFaces === false) {
157
				// If there is no invalid persons, and there is no recent new faces, no need to recreate cluster
158
				$this->logInfo('Clusters already exist, calculated there is no need to recreate them');
159
				return;
160
			}
161
		} else {
162
			// These are basic criteria without which we should not even consider creating clusters.
163
			// These clusters will be small and not "stable" enough and we should better wait for more images to come.
164
			// todo: 2 queries to get these 2 counts, can we do this smarter?
165
			$imageCount = $this->imageMapper->countUserImages($userId, $modelId);
166
			$imageProcessed = $this->imageMapper->countUserProcessedImages($userId, $modelId);
167
			$percentImagesProcessed = $imageProcessed / floatval($imageCount);
168
			$facesCount = $this->faceMapper->countFaces($userId, $modelId);
169
			// todo: get rid of magic numbers (move to config)
170
			if (($facesCount < 1000) && ($imageCount < 100) && ($percentImagesProcessed < 0.95)) {
171
				$this->logInfo(
172
					'Skipping cluster creation, not enough data (yet) collected. ' .
173
					'For cluster creation, you need either one of the following:');
174
				$this->logInfo(sprintf('* have 1000 faces already processed (you have %d),', $facesCount));
175
				$this->logInfo(sprintf('* have 100 images (you have %d),', $imageCount));
176
				$this->logInfo(sprintf('* or you need to have 95%% of you images processed (you have %.2f%%)', $percentImagesProcessed));
177
				return;
178
			}
179
		}
180
181
		$faces = $this->faceMapper->getFaces($userId, $modelId);
182
		$this->logInfo(count($faces) . ' faces found for clustering');
183
184
		// Cluster is associative array where key is person ID.
185
		// Value is array of face IDs. For old clusters, person IDs are some existing person IDs,
186
		// and for new clusters is whatever chinese whispers decides to identify them.
187
		//
188
		$currentClusters = $this->getCurrentClusters($faces);
189
		$newClusters = $this->getNewClusters($faces);
190
		$this->logInfo(count($newClusters) . ' clusters found for clustering');
191
		// New merge
192
		$mergedClusters = $this->mergeClusters($currentClusters, $newClusters);
193
		$this->personMapper->mergeClusterToDatabase($userId, $currentClusters, $mergedClusters);
194
	}
195
196
	private function getCurrentClusters(array $faces): array {
197
		$chineseClusters = array();
198
		foreach($faces as $face) {
199
			if ($face->person != null) {
200
				if (!isset($chineseClusters[$face->person])) {
201
					$chineseClusters[$face->person] = array();
202
				}
203
				$chineseClusters[$face->person][] = $face->id;
204
			}
205
		}
206
		return $chineseClusters;
207
	}
208
209
	private function getNewClusters(array $faces): array {
210
		// Create edges for chinese whispers
211
		$euclidean = new Euclidean();
212
		$edges = array();
213
		for ($i = 0, $face_count1 = count($faces); $i < $face_count1; $i++) {
214
			$face1 = $faces[$i];
215
			for ($j = $i, $face_count2 = count($faces); $j < $face_count2; $j++) {
216
				$face2 = $faces[$j];
217
				// todo: can't this distance be a method in $face1->distance($face2)?
218
				$distance = $euclidean->distance($face1->descriptor, $face2->descriptor);
219
				// todo: extract this magic number to app param
220
				if ($distance < 0.5) {
221
					$edges[] = array($i, $j);
222
				}
223
			}
224
		}
225
226
		$newChineseClustersByIndex = dlib_chinese_whispers($edges);
0 ignored issues
show
Bug introduced by
The function dlib_chinese_whispers was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

226
		$newChineseClustersByIndex = /** @scrutinizer ignore-call */ dlib_chinese_whispers($edges);
Loading history...
227
		$newClusters = array();
228
		for ($i = 0, $c = count($newChineseClustersByIndex); $i < $c; $i++) {
229
			if (!isset($newClusters[$newChineseClustersByIndex[$i]])) {
230
				$newClusters[$newChineseClustersByIndex[$i]] = array();
231
			}
232
			$newClusters[$newChineseClustersByIndex[$i]][] = $faces[$i]->id;
233
		}
234
235
		return $newClusters;
236
	}
237
238
	/**
239
	 * todo: only reason this is public is because of tests. Go figure it out better.
240
	 */
241 2
	public function mergeClusters(array $oldCluster, array $newCluster): array {
242
		// Create map of face transitions
243 2
		$transitions = array();
244 2
		foreach ($newCluster as $newPerson=>$newFaces) {
245 2
			foreach ($newFaces as $newFace) {
246 2
				$oldPersonFound = null;
247 2
				foreach ($oldCluster as $oldPerson => $oldFaces) {
248 2
					if (in_array($newFace, $oldFaces)) {
249 2
						$oldPersonFound = $oldPerson;
250 2
						break;
251
					}
252
				}
253 2
				$transitions[$newFace] = array($oldPersonFound, $newPerson);
254
			}
255
		}
256
		// Count transitions
257 2
		$transitionCount = array();
258 2
		foreach ($transitions as $transition) {
259 2
			$key = $transition[0] . ':' . $transition[1];
260 2
			if (array_key_exists($key, $transitionCount)) {
261 2
				$transitionCount[$key]++;
262
			} else {
263 2
				$transitionCount[$key] = 1;
264
			}
265
		}
266
		// Create map of new person -> old persion transitions
267 2
		$newOldPersonMapping = array();
268 2
		$oldPersonProcessed = array(); // store this, so we don't waste cycles for in_array()
269 2
		arsort($transitionCount);
270 2
		foreach ($transitionCount as $transitionKey => $count) {
271 2
			$transition = explode(":", $transitionKey);
272 2
			$oldPerson = intval($transition[0]);
273 2
			$newPerson = intval($transition[1]);
274 2
			if (!array_key_exists($newPerson, $newOldPersonMapping)) {
275 2
				if (($oldPerson == 0) || (!array_key_exists($oldPerson, $oldPersonProcessed))) {
276 2
					$newOldPersonMapping[$newPerson] = $oldPerson;
277 2
					$oldPersonProcessed[$oldPerson] = 0;
278
				} else {
279 2
					$newOldPersonMapping[$newPerson] = 0;
280
				}
281
			}
282
		}
283
		// Starting with new cluster, convert all new person IDs with old person IDs
284 2
		$maxOldPersonId = 1;
285 2
		if (count($oldCluster) > 0) {
286 2
			$maxOldPersonId = max(array_keys($oldCluster)) + 1;
287
		}
288
289 2
		$result = array();
290 2
		foreach ($newCluster as $newPerson => $newFaces) {
291 2
			$oldPerson = $newOldPersonMapping[$newPerson];
292 2
			if ($oldPerson == 0) {
293 2
				$result[$maxOldPersonId] = $newFaces;
294 2
				$maxOldPersonId++;
295
			} else {
296 2
				$result[$oldPerson] = $newFaces;
297
			}
298
		}
299 2
		return $result;
300
	}
301
}
302