From afc91641271bbd9dcf9384ddbd304291e1a03090 Mon Sep 17 00:00:00 2001 From: Matias De lellis Date: Sat, 29 Feb 2020 14:04:41 -0300 Subject: [PATCH 1/3] HOG detector also must return the rectangles of the detections. --- .gitignore | 13 ++++++------- README.md | 5 ++--- php_pdlib.h | 2 +- src/face_detection.cc | 17 ++++++++++++++--- tests/dlib_face_detection.phpt | 18 ++++++++++++++++++ 5 files changed, 41 insertions(+), 14 deletions(-) create mode 100644 tests/dlib_face_detection.phpt diff --git a/.gitignore b/.gitignore index 54983d3..2f6ab2a 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/README.md b/README.md index 81ca51b..f2a800e 100644 --- a/README.md +++ b/README.md @@ -100,9 +100,8 @@ If you want to use HOG based approach: img; load_image(img, img_path); - pyramid_up(img); + array_init(return_value); + std::vector dets = detector(img); - RETURN_LONG(dets.size()); + for (unsigned long i = 0; i < dets.size(); ++i) { + zval rect_arr; + array_init(&rect_arr); + add_assoc_long(&rect_arr, "left", dets[i].left()); + add_assoc_long(&rect_arr, "top", dets[i].top()); + add_assoc_long(&rect_arr, "right", dets[i].right()); + add_assoc_long(&rect_arr, "bottom", dets[i].bottom()); + // Add this assoc array to returned array + // + add_next_index_zval(return_value, &rect_arr); + } } catch (exception& e) { diff --git a/tests/dlib_face_detection.phpt b/tests/dlib_face_detection.phpt new file mode 100644 index 0000000..8057bf4 --- /dev/null +++ b/tests/dlib_face_detection.phpt @@ -0,0 +1,18 @@ +--TEST-- +Frontal face detection. +--SKIPIF-- + +--FILE-- + $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-- +Detection +Faces found = 1 +Face[0] in bounding box (left=214, top=194, right=393, bottom=373) \ No newline at end of file From cd4e7bcbdfc50d80bb2f84985e9b14eea22fa6b9 Mon Sep 17 00:00:00 2001 From: Matias De lellis Date: Sat, 7 Mar 2020 10:46:12 -0300 Subject: [PATCH 2/3] Dont upsamply by default in CNN, and optional argument on fhog for that. --- src/cnn_face_detection.cc | 2 +- src/face_detection.cc | 22 +++++++++++++++++----- tests/dlib_face_detection.phpt | 16 +++++++++++++--- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/cnn_face_detection.cc b/src/cnn_face_detection.cc index 16f9d66..83ea66f 100644 --- a/src/cnn_face_detection.cc +++ b/src/cnn_face_detection.cc @@ -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"); diff --git a/src/face_detection.cc b/src/face_detection.cc index 3a2563b..3d2bd17 100644 --- a/src/face_detection.cc +++ b/src/face_detection.cc @@ -2,6 +2,7 @@ #include "../php_pdlib.h" #include "face_detection.h" +#include #include #include #include @@ -14,26 +15,37 @@ 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 img; load_image(img, img_path); + unsigned int levels = upsample_num; + while (levels > 0) { + levels--; + pyramid_up(img, pyr); + } + array_init(return_value); std::vector dets = detector(img); 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", dets[i].left()); - add_assoc_long(&rect_arr, "top", dets[i].top()); - add_assoc_long(&rect_arr, "right", dets[i].right()); - add_assoc_long(&rect_arr, "bottom", dets[i].bottom()); + 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); diff --git a/tests/dlib_face_detection.phpt b/tests/dlib_face_detection.phpt index 8057bf4..75fb99f 100644 --- a/tests/dlib_face_detection.phpt +++ b/tests/dlib_face_detection.phpt @@ -4,15 +4,25 @@ Frontal face detection. --FILE-- $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-- -Detection +Simple detection Faces found = 1 -Face[0] in bounding box (left=214, top=194, right=393, bottom=373) \ No newline at end of file +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) \ No newline at end of file From dfd6a952fb259525b9adf03968b129c318799c07 Mon Sep 17 00:00:00 2001 From: Matias De lellis Date: Sat, 7 Mar 2020 12:55:39 -0300 Subject: [PATCH 3/3] upsample_num as references --- src/face_detection.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/face_detection.h b/src/face_detection.h index aaeb2fd..a95667e 100644 --- a/src/face_detection.h +++ b/src/face_detection.h @@ -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);