Merge pull request #20 from matiasdelellis/improve-hog

HOG detector also must return the rectangles of the detections.
This commit is contained in:
goodspb 2020-05-01 11:19:17 +08:00 committed by GitHub
commit b4f152f860
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 65 additions and 15 deletions

13
.gitignore vendored
View File

@ -28,12 +28,11 @@ missing
mkinstalldirs
modules
run-tests.php
tests/*/*.diff
tests/*/*.out
tests/*/*.php
tests/*/*.exp
tests/*/*.log
tests/*/*.sh
tests/*.diff
tests/*.out
tests/*.php
tests/*.exp
tests/*.log
tests/*.sh
.idea
cmake-build-debug

View File

@ -100,9 +100,8 @@ If you want to use HOG based approach:
<?php
// face detection
$faceCount = dlib_face_detection("~/a.jpg");
// how mary face in the picture.
var_dump($faceCount);
detected_faces = dlib_face_detection("image.jpg");
// $detected_faces is indexed array, where values are assoc arrays with "top", "bottom", "left" and "right" values
```
If you want to use CNN approach (and CNN model):

View File

@ -31,7 +31,7 @@ extern "C" {
extern zend_module_entry pdlib_module_entry;
#define phpext_pdlib_ptr &pdlib_module_entry
#define PHP_PDLIB_VERSION "0.1.0" /* Replace with version number for your extension */
#define PHP_PDLIB_VERSION "1.0.1" /* Replace with version number for your extension */
#ifdef PHP_WIN32
# define PHP_PDLIB_API __declspec(dllexport)

View File

@ -51,7 +51,7 @@ PHP_METHOD(CnnFaceDetection, detect)
{
char *img_path;
size_t img_path_len;
long upsample_num = 1;
long upsample_num = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &img_path, &img_path_len, &upsample_num) == FAILURE){
zend_throw_exception_ex(zend_ce_exception, 0 TSRMLS_CC, "Unable to parse detect arguments");

View File

@ -2,6 +2,7 @@
#include "../php_pdlib.h"
#include "face_detection.h"
#include <zend_exceptions.h>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/gui_widgets.h>
#include <dlib/image_io.h>
@ -14,19 +15,41 @@ PHP_FUNCTION(dlib_face_detection)
{
char *img_path;
size_t img_path_len;
long upsample_num = 0;
if(zend_parse_parameters(ZEND_NUM_ARGS(), "s", &img_path, &img_path_len) == FAILURE){
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &img_path, &img_path_len, &upsample_num) == FAILURE) {
zend_throw_exception_ex(zend_ce_exception, 0 TSRMLS_CC, "Unable to parse dlib_face_detection arguments");
RETURN_FALSE;
}
try {
frontal_face_detector detector = get_frontal_face_detector();
pyramid_down<2> pyr;
array2d<unsigned char> img;
load_image(img, img_path);
pyramid_up(img);
unsigned int levels = upsample_num;
while (levels > 0) {
levels--;
pyramid_up(img, pyr);
}
array_init(return_value);
std::vector<rectangle> dets = detector(img);
RETURN_LONG(dets.size());
for (unsigned long i = 0; i < dets.size(); ++i) {
rectangle rect = pyr.rect_down(dets[i], upsample_num);
zval rect_arr;
array_init(&rect_arr);
add_assoc_long(&rect_arr, "left", rect.left());
add_assoc_long(&rect_arr, "top", rect.top());
add_assoc_long(&rect_arr, "right", rect.right());
add_assoc_long(&rect_arr, "bottom", rect.bottom());
// Add this assoc array to returned array
//
add_next_index_zval(return_value, &rect_arr);
}
}
catch (exception& e)
{

View File

@ -7,6 +7,7 @@
ZEND_BEGIN_ARG_INFO_EX(dlib_face_detection_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, img_path)
ZEND_ARG_INFO(0, upsample_num)
ZEND_END_ARG_INFO()
PHP_FUNCTION(dlib_face_detection);

View File

@ -0,0 +1,28 @@
--TEST--
Frontal face detection.
--SKIPIF--
<?php if (!extension_loaded("pdlib") print "skip"; ?>
--FILE--
<?php
printf("Simple detection\n");
$detected_faces = dlib_face_detection(__DIR__ . "/lenna.jpg");
printf("Faces found = %d\n", count($detected_faces));
foreach($detected_faces as $index => $detected_face) {
printf("Face[%d] in bounding box (left=%d, top=%d, right=%d, bottom=%d)\n", $index,
$detected_face["left"], $detected_face["top"], $detected_face["right"], $detected_face["bottom"]);
}
printf("Detection with upsampling\n");
$detected_faces = dlib_face_detection(__DIR__ . "/lenna.jpg", 1);
printf("Faces found = %d\n", count($detected_faces));
foreach($detected_faces as $index => $detected_face) {
printf("Face[%d] in bounding box (left=%d, top=%d, right=%d, bottom=%d)\n", $index,
$detected_face["left"], $detected_face["top"], $detected_face["right"], $detected_face["bottom"]);
}
?>
--EXPECT--
Simple detection
Faces found = 1
Face[0] in bounding box (left=214, top=194, right=393, bottom=373)
Detection with upsampling
Faces found = 1
Face[0] in bounding box (left=201, top=180, right=386, bottom=366)