Skip to content

Commit

Permalink
recognize_face_by_dnn_insightface.php
Browse files Browse the repository at this point in the history
  • Loading branch information
morozovsk committed Jan 30, 2021
1 parent 3a27a00 commit dda7cd1
Show file tree
Hide file tree
Showing 7 changed files with 10,005 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

![detect_facemarks_by_lbf.jpg](https://raw.githubusercontent.com/php-opencv/php-opencv-examples/master/results/detect_facemarks_by_lbf.jpg)

- [recognize face by LBPH algorithm](https://github.com/php-opencv/php-opencv-examples/blob/master/recognize_face_by_lbph.php) / [recognize face by DNN openface](https://github.com/php-opencv/php-opencv-examples/blob/master/recognize_face_by_dnn_openface.php)
- [recognize face by LBPH algorithm](https://github.com/php-opencv/php-opencv-examples/blob/master/recognize_face_by_lbph.php) / [recognize face by DNN openface](https://github.com/php-opencv/php-opencv-examples/blob/master/recognize_face_by_dnn_openface.php) / [recognize face by DNN insightface](https://github.com/php-opencv/php-opencv-examples/blob/master/recognize_face_by_dnn_insightface.php)

![recognize_face_by_lbph.jpg](https://raw.githubusercontent.com/php-opencv/php-opencv-examples/master/results/recognize_face_by_lbph.jpg)

Expand Down
48 changes: 48 additions & 0 deletions face_to_vector_by_dnn_insightface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use CV\Scalar, CV\Size;
use function CV\{imread, imwrite};

$netDet = \CV\DNN\readNetFromCaffe('models/ssd/res10_300x300_ssd_deploy.prototxt', 'models/ssd/res10_300x300_ssd_iter_140000.caffemodel');
$netRecogn = \CV\DNN\readNetFromONNX('models/insightface/arcface_mobilefacenet.onnx');

$src = imread("images/faces.jpg");
$size = $src->size(); // 2000x500

$minSide = min($size->width, $size->height);
$divider = $minSide / 300;
\CV\resize($src, $resized, new Size($size->width / $divider, $size->height / $divider)); // 1200x300

//var_export($resized);

$blob = \CV\DNN\blobFromImage($resized, 1, new Size(), new Scalar(104, 177, 123), true, false);

$netDet->setInput($blob);

$r = $netDet->forward();

//var_export($r->shape);

$faces = [];
$scalar = new Scalar(0, 0, 255);
for ($i = 0; $i < $r->shape[2]; $i++) {
$confidence = $r->atIdx([0,0,$i,2]);
if ($confidence > 0.9) {
var_export($confidence);echo "\n";
$startX = $r->atIdx([0,0,$i,3]) * $src->cols;
$startY = $r->atIdx([0,0,$i,4]) * $src->rows;
$endX = $r->atIdx([0,0,$i,5]) * $src->cols;
$endY = $r->atIdx([0,0,$i,6]) * $src->rows;

$face = $src->getImageROI(new \CV\Rect($startX, $startY, $endX - $startX, $endY - $startY));
//imwrite("results/_face.jpg", $face);

$blob = \CV\DNN\blobFromImage($face, 1.0 / 255, new Size(112, 112), new Scalar(), true, false);
$netRecogn->setInput($blob);
$vec = $netRecogn->forward();
\CV\normalize($r, $r);

//$vec->print();
var_export($vec->data());
}
}
1 change: 1 addition & 0 deletions models/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ Sources:
* ssd_mobilenet_v1_coco - https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md
* ssdlite_mobilenet_v2_coco - https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md
* ssd_mobilenet_v2_coco - https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md
* insightface - https://github.com/axinc-ai/ailia-models/tree/master/face_identification/insightface
Binary file added models/insightface/arcface_mobilefacenet.onnx
Binary file not shown.
Loading

0 comments on commit dda7cd1

Please sign in to comment.