Adding support for cnn face detector

CNN face detector (deep learning face detection) is modeled as
PHP class. Currently, it only has constructor (which loads a model)
and detect() method. I tried to make it resamble to Python implementation.
Similar to Python, also added support for upsampling. Returned value
is array with numbers as keys where number of keys is equal to number
of found faced. Each element is associative array that has "top", "left",
"right", "bottom" and "detection_confidence" keys.

All errors are propagated using exceptions.

Added two new error tests. I didn't want to rely on presence of model,
so tests are not having great coverage. If we are OK to download models
in tests, it would allow us to have far better coverage.

What is missing:
* Testing with models (should we test that?)
* detect_mult method (similar to what Python have; however, I consider
  that this should not block pushing this change)
This commit is contained in:
Branko Kokanovic
2018-07-16 22:34:55 +02:00
parent d12d75c69e
commit f8cc0e48d3
7 changed files with 213 additions and 3 deletions
+36
View File
@@ -29,6 +29,7 @@ extern "C" {
}
#include "php_pdlib.h"
#include "src/face_detection.h"
#include "src/cnn_face_detection.h"
#include "src/face_landmark_detection.h"
/* If you declare any globals in php_pdlib.h uncomment this:
@@ -38,6 +39,9 @@ ZEND_DECLARE_MODULE_GLOBALS(pdlib)
/* True global resources - no need for thread safety here */
static int le_pdlib;
static zend_class_entry *cnn_face_detection_ce = nullptr;
static zend_object_handlers cnn_face_detection_obj_handlers;
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
@@ -88,15 +92,47 @@ static void php_pdlib_init_globals(zend_pdlib_globals *pdlib_globals)
*/
/* }}} */
const zend_function_entry cnn_face_detection_class_methods[] = {
PHP_ME(CnnFaceDetection, __construct, cnn_face_detection_ctor_arginfo, ZEND_ACC_PUBLIC)
PHP_ME(CnnFaceDetection, detect, cnn_face_detection_detect_arginfo, ZEND_ACC_PUBLIC)
PHP_FE_END
};
zend_object* php_cnn_face_detection_new(zend_class_entry *class_type TSRMLS_DC)
{
cnn_face_detection *cfd = (cnn_face_detection*)ecalloc(1, sizeof(cnn_face_detection));
zend_object_std_init(&cfd->std, class_type TSRMLS_CC);
object_properties_init(&cfd->std, class_type);
cfd->std.handlers = &cnn_face_detection_obj_handlers; //zend_get_std_object_handlers();
return &cfd->std;
}
static void php_cnn_face_detection_free(zend_object *object)
{
cnn_face_detection *cfd = (cnn_face_detection*)((char*)object - XtOffsetOf(cnn_face_detection, std));
delete cfd->net;
zend_object_std_dtor(object);
}
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(pdlib)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "CnnFaceDetection", cnn_face_detection_class_methods);
cnn_face_detection_ce = zend_register_internal_class(&ce TSRMLS_CC);
cnn_face_detection_ce->create_object = php_cnn_face_detection_new;
memcpy(&cnn_face_detection_obj_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
cnn_face_detection_obj_handlers.offset = XtOffsetOf(cnn_face_detection, std);
cnn_face_detection_obj_handlers.free_obj = php_cnn_face_detection_free;
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION