-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathFaceDetectionClient.php
executable file
·353 lines (303 loc) · 9.65 KB
/
FaceDetectionClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
namespace FaceDetection;
/**
* Represents a client with vendor specific data and functions to process the
* defined image dataset as well as exporting the results into a CSV file.
*/
class FaceDetectionClient {
/**
* Defines the full path to where processed images will be saved.
*
* @var string
*/
protected $outputDir;
/**
* The vendor name to use on the printed image metadata.
*
* @var string
*/
protected $vendorName;
/**
* An array containing the RGB integers for the color of the text
* and bounding boxes to use.
*
* @var array
*/
protected $textColor;
/**
* Defines the full path to our image dataset to be processed.
*
* @var string
*/
protected $datasetDir;
/**
* Defines the directory name where to save images with bounding boxes only.
*
* @var string
*/
protected $rawDir;
/**
* Our image dataset represented as an array of \FaceDetectionImage objects.
*
* @var \FaceDetection\FaceDetectionImage[]
*/
protected $images;
/**
* Helper to measure how long a run took.
*
* @var int
*/
protected $startTime;
/**
* Helper which contains the expected face counts per image.
*
* @var array
*/
protected $expectedFaceCounts;
/**
* Constructs a FaceDetectionClient object.
*
* @param string $outputDir
* The full path to where processed images will be saved.
* @param null $vendorName
* The vendor name to use on the printed image metadata.
* @param array $textColor
* An array containing the RGB integers for the color of the text
* and bounding boxes to use.
* @param string $datasetDir
* The full path to the image dataset to be processed.
* @param string $rawDir
* The directory name where to save images with bounding boxes only.
*/
public function __construct($outputDir, $vendorName=NULL, $textColor=[255, 0, 0], $datasetDir=__DIR__ . '/../dataset/', $rawDir='raw/') {
$this->outputDir = __DIR__ . '/../dataset-output/' . $outputDir . '/';
$this->vendorName = empty($vendorName) ? dirname($outputDir) : $vendorName;
$this->textColor = $textColor;
$this->datasetDir = $datasetDir;
$this->rawDir = $rawDir;
$this->images = [];
$this->startTime = 0;
$this->expectedFaceCounts = [];
// Ensure the output directories exists.
$this->createDirectory($this->outputDir);
$this->createDirectory($this->outputDir . $this->rawDir);
// Parse face counts file.
$this->parseFaceCounts();
}
/**
* Makes sure that the given directory exists.
*
* @param $directory
* The directory to create (if not existing yet).
*/
protected function createDirectory($directory) {
if (!file_exists($directory)) {
mkdir($directory, 0755, TRUE);
}
}
/**
* Parses the face_counts.ini file located in the specified dataset directory
* and saves the result in a local array.
*/
protected function parseFaceCounts() {
if (file_exists($this->datasetDir . 'face_counts.ini')) {
$this->expectedFaceCounts = parse_ini_file($this->datasetDir . 'face_counts.ini', TRUE);
}
}
/**
* Checks if an expected face count was defined for a given filename.
* The expected face count number is read from the face_counts.ini file.
*
* @param $filename
* The image filename for which to check if an expected face count has been
* defined.
*
* @return int|null
* The expected face count for this filename defined in the face_counts.ini
* file or NULL otherwise.
*
* @see \FaceDetectionClient::parseFaceCounts()
* How the face_counts.ini file is being read.
*/
protected function getFaceCountForFilename($filename) {
if (array_key_exists($filename, $this->expectedFaceCounts)) {
return $this->expectedFaceCounts[$filename];
}
return NULL;
}
/**
* Returns the CSV header row columns according to the images array
* defined in the object.
*
* @return array
* The header row column names as an array.
*/
protected function getCSVHeaders() {
$headers = ['Vendor'];
foreach ($this->images as &$image) {
$headers[] = $image->getFilename() . ' (Success rate)';
$headers[] = $image->getFilename() . ' (Faces detected)';
$headers[] = $image->getFilename() . ' (Faces expected)';
$headers[] = $image->getFilename() . ' (Time)';
}
$headers[] = 'Total success rate';
$headers[] = 'Total faces detected';
$headers[] = 'Total faces expected';
$headers[] = 'Total processing time (ms)';
$headers[] = 'Average processing time (ms)';
return $headers;
}
/**
* Returns the false positives CSV header row columns according to
* the images array defined in the object.
*
* @return array
* The false positives header row column names as an array.
*/
protected function getFalsePositivesCSVHeaders() {
$headers = ['Vendor'];
foreach ($this->images as &$image) {
$headers[] = $image->getFilename() . ' (Faces detected)';
$headers[] = $image->getFilename() . ' (False positives)';
$headers[] = $image->getFilename() . ' (False positives %)';
}
$headers[] = 'Total faces detected';
$headers[] = 'Total false positives';
$headers[] = 'Average false positives';
return $headers;
}
/**
* Loops over all images to get the total count of faces detected.
*
* @return int
* The total count of all faces detected.
*/
public function getTotalDetectedFaceCount() {
$totalFaceCount = 0;
foreach ($this->images as &$image) {
$totalFaceCount += $image->getDetectedFaceCount();
}
return $totalFaceCount;
}
/**
* Loops over all images to get the total count of faces expected.
*
* @return int
* The total count of all faces expected.
*/
public function getTotalExpectedFaceCount() {
$totalExpectedFaceCount = 0;
foreach ($this->images as &$image) {
$totalExpectedFaceCount += $image->getExpectedFaceCount();
}
return $totalExpectedFaceCount;
}
/**
* Loops over all images to get the total processing time.
*
* @return int
* The total processing time.
*/
public function getTotalProcessingTime() {
$totalProcessingTime = 0;
foreach ($this->images as &$image) {
$totalProcessingTime += $image->getProcessingTime();
}
return $totalProcessingTime;
}
/**
* Loops over all images to get the total success rate.
*
* @return int
* The success rate with two decimal points.
*/
public function getTotalSuccessRate() {
$totalDetectedFaceCount = 0;
$totalExpectedFaceCount = 0;
foreach ($this->images as &$image) {
if ($image->getExpectedFaceCount() !== '-') {
$totalDetectedFaceCount += $image->getDetectedFaceCount();
$totalExpectedFaceCount += $image->getExpectedFaceCount();
}
}
return number_format($totalDetectedFaceCount / $totalExpectedFaceCount * 100, 2);
}
/**
* Lists all images in the defined dataset directory and creates
* FaceDetectionImage objects from them.
*
* @return \FaceDetection\FaceDetectionImage[]
* An array of FaceDetectionImage objects.
*/
public function loadImages() {
$filenames = glob($this->datasetDir . '*.{jpg,jpeg,png}', GLOB_BRACE);
foreach ($filenames as &$fullFilePath) {
$filename = basename($fullFilePath);
try {
$this->images[] = new FaceDetectionImage($fullFilePath, $this->outputDir, $this->rawDir, $this->vendorName, $this->textColor, $this->getFaceCountForFilename($filename));
} catch (\Exception $e) {
print $e->getMessage();
}
}
return $this->images;
}
/**
* Writes analytical results to a CSV file.
*
* If the CSV file already exists, it will only append it's data into a new row.
* In case the CSV file doesn't exist yet, it will create the CSV file
* including the CSV header row.
*/
public function exportCSV() {
$columns = [$this->vendorName];
foreach ($this->images as &$image) {
$columns[] = $image->getSuccessRate();
$columns[] = $image->getDetectedFaceCount();
$columns[] = $image->getExpectedFaceCount();
$columns[] = $image->getProcessingTime();
}
$columns[] = $this->getTotalSuccessRate();
$columns[] = $this->getTotalDetectedFaceCount();
$columns[] = $this->getTotalExpectedFaceCount();
$columns[] = $this->getTotalProcessingTime();
$columns[] = $this->getTotalProcessingTime() / count($this->images);
if (file_exists($this->outputDir . '../results.csv')) {
$fp = fopen($this->outputDir . '../results.csv', 'a');
}
else {
$fp = fopen($this->outputDir . '../results.csv', 'w');
fputcsv($fp, $this->getCSVHeaders());
}
fputcsv($fp, $columns);
fclose($fp);
}
/**
* Prepares the a false positives CSV file with a header row but no data.
*
* As for now, the data has to be entered manually to the CSV.
* This might change in the future.
*/
public function exportFalsePositivesCSVHeaders() {
if (!file_exists($this->outputDir . '../false_positives.csv')) {
// Only overwrite the file if it's not existing yet.
$fp = fopen($this->outputDir . '../false_positives.csv', 'w');
fputcsv($fp, $this->getFalsePositivesCSVHeaders());
fclose($fp);
}
}
/**
* Sets the start time to just now for tracking process duration.
*/
public function startTimer() {
$this->startTime = round(microtime(TRUE) * 1000);
}
/**
* Stops the timer and returns the difference to the start time.
*
* @return float|int
* The duration since the start time.
*/
public function stopTimer() {
return round(microtime(TRUE) * 1000) - $this->startTime;
}
}