-
Notifications
You must be signed in to change notification settings - Fork 88
/
classify_image_by_dnn_mobilenet.php
34 lines (24 loc) · 985 Bytes
/
classify_image_by_dnn_mobilenet.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
<?php
use CV\Scalar;
use function CV\{imread, cvtColor};
$categories = explode("\n", file_get_contents('models/mobilenet/classes.txt'));
$src = imread("images/cat.jpg"); // opencv loads image to matrix with BGR order
$src = cvtColor($src, CV\COLOR_BGR2RGB); // convert BGR to RGB
//var_export($src);
$blob = \CV\DNN\blobFromImage($src, 0.017, new \CV\Size(224,224), new Scalar(103.94, 116.78, 123.68)); // convert image to 4 dimensions matrix
//var_export($blob);
$net = \CV\DNN\readNetFromCaffe('models/mobilenet/mobilenet_deploy.prototxt', 'models/mobilenet/mobilenet.caffemodel');
$net->setInput($blob, "");
$r = $net->forward();
//var_export($r);
$maxConfidence = 0;
$confidences = [];
for ($i = 0; $i < 1000; $i++) {
$confidences[$i] = intval($r->atIdx([0,$i,0,0], 1) * 100);
}
// top 5:
arsort($confidences);
$confidences = array_slice($confidences, 0, 5, true);
foreach ($confidences as $label => $confidence) {
echo "$confidence%: {$categories[$label]}\n";
}