Landmark detection (custom model and class-based)

This change extends existing landmark detection in new ways:
1. Existing logic is hiding HOG model (frontal_face_detector)
underneath and user cannot use other models (CNN model, for example).
2. Bounding box is exposed as additional argument, and user
can define custom bounding box (which is needed, if image used to detect
faces is changed (for example scaled), and we want to crop only face
from original image to feed into shape predictor).
3. This approach is class-based, so no need for multiple loadings of
shape predictor model (only once, in ctor)
This commit is contained in:
Branko Kokanovic
2018-08-25 00:51:46 +02:00
parent 3f60326661
commit 7de8d060b3
5 changed files with 205 additions and 5 deletions

View File

@@ -2,6 +2,8 @@
#include "../php_pdlib.h"
#include "face_landmark_detection.h"
#include <zend_exceptions.h>
#include <dlib/image_processing/frontal_face_detector.h>
#include <dlib/image_processing/render_face_detections.h>
#include <dlib/image_processing.h>
@@ -13,6 +15,12 @@
using namespace dlib;
using namespace std;
static inline face_landmark_detection *php_face_landmark_detection_from_obj(zend_object *obj) {
return (face_landmark_detection*)((char*)(obj) - XtOffsetOf(face_landmark_detection, std));
}
#define Z_FACE_LANDMARK_DETECTION_P(zv) php_face_landmark_detection_from_obj(Z_OBJ_P((zv)))
PHP_FUNCTION(dlib_face_landmark_detection)
{
char *shape_predictor_file_path;
@@ -60,4 +68,106 @@ PHP_FUNCTION(dlib_face_landmark_detection)
{
RETURN_FALSE;
}
}
PHP_METHOD(FaceLandmarkDetection, __construct)
{
char *sz_shape_predictor_file_path;
size_t shape_predictor_file_path_len;
face_landmark_detection *fld = Z_FACE_LANDMARK_DETECTION_P(getThis());
if (nullptr == fld) {
php_error_docref(NULL TSRMLS_CC, E_ERROR, "Unable to find obj in FaceLandmarkDetection::__construct()");
return;
}
// Parse predictor model's path
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s",
&sz_shape_predictor_file_path, &shape_predictor_file_path_len) == FAILURE){
zend_throw_exception_ex(zend_ce_exception, 0 TSRMLS_CC, "Unable to parse shape_predictor_file_path");
return;
}
// Load predictor model from given path
try {
string shape_predictor_file_path(sz_shape_predictor_file_path, shape_predictor_file_path_len);
fld->sp = new shape_predictor;
deserialize(shape_predictor_file_path) >> *(fld->sp);
} catch (exception& e) {
zend_throw_exception_ex(zend_ce_exception, 0 TSRMLS_CC, e.what());
return;
}
}
// Helper macro to automatically have parsing of "top"/"bottom"/"left"/"right"
#define PARSE_BOUNDING_BOX_EDGE(side) \
zval* data##side; \
/* Tries to find given key in array */ \
data##side = zend_hash_str_find(bounding_box_hash, #side, sizeof(#side)-1); \
if (data##side == nullptr) { \
zend_throw_exception_ex(zend_ce_exception, 0 TSRMLS_CC, "Bounding box (second argument) is missing " #side "key"); \
return; \
} \
\
/* We also need to check proper type of value in associative array */ \
if (Z_TYPE_P(data##side) != IS_LONG) { \
zend_throw_exception_ex(zend_ce_exception, 0 TSRMLS_CC, "Value of bounding box's (second argument) " #side " key is not long type"); \
return; \
} \
zend_long side = Z_LVAL_P(data##side); \
PHP_METHOD(FaceLandmarkDetection, detect)
{
char *img_path;
size_t img_path_len;
zval *bounding_box;
array2d<rgb_pixel> img;
// Parse path to image and bounding box. Bounding box is associative array of 4 elements - "top", "bottom", "left" and "right".
//
if (zend_parse_parameters(ZEND_NUM_ARGS(), "sa", &img_path, &img_path_len, &bounding_box) == FAILURE){
zend_throw_exception_ex(zend_ce_exception, 0 TSRMLS_CC, "Unable to parse detect arguments");
return;
}
// Check that bounding box have exactly 4 elements
HashTable *bounding_box_hash = Z_ARRVAL_P(bounding_box);
uint32_t bounding_box_num_elements = zend_hash_num_elements(bounding_box_hash);
if (bounding_box_num_elements != 4) {
zend_throw_exception_ex(zend_ce_exception, 0 TSRMLS_CC, "Bounding box (second argument) needs to have exactly 4 elements");
return;
}
// Retrieve all 4 edges of bounding box
//
PARSE_BOUNDING_BOX_EDGE(top)
PARSE_BOUNDING_BOX_EDGE(bottom)
PARSE_BOUNDING_BOX_EDGE(left)
PARSE_BOUNDING_BOX_EDGE(right)
try {
// Load image and execute shape predictor on it.
//
face_landmark_detection *fld = Z_FACE_LANDMARK_DETECTION_P(getThis());
load_image(img, img_path);
rectangle rct(left, top, right, bottom);
full_object_detection shape = fld->sp->operator()(img, rct);
// Return value is regular array with integer keys.
// Each key is one part from shape. Value of each part is associative array of keys "x" and "y".
//
array_init(return_value);
for (int i = 0; i < shape.num_parts(); i++) {
zval part;
array_init(&part);
dlib::point p = shape.part(i);
add_assoc_long(&part, "x", p.x());
add_assoc_long(&part, "y", p.y());
add_next_index_zval(return_value, &part);
}
} catch (exception& e) {
zend_throw_exception_ex(zend_ce_exception, 0 TSRMLS_CC, e.what());
return;
}
}

View File

@@ -5,10 +5,30 @@
#ifndef PDLIB_FACE_LANDMARK_DETECTION_H
#define PDLIB_FACE_LANDMARK_DETECTION_H
#include <dlib/dnn.h>
using namespace dlib;
ZEND_BEGIN_ARG_INFO_EX(dlib_face_landmark_detection_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, shape_predictor_file_path)
ZEND_ARG_INFO(0, img_path)
ZEND_END_ARG_INFO()
PHP_FUNCTION(dlib_face_landmark_detection);
typedef struct _face_landmark_detection {
shape_predictor *sp;
zend_object std;
} face_landmark_detection;
ZEND_BEGIN_ARG_INFO_EX(face_landmark_detection_ctor_arginfo, 0, 0, 1)
ZEND_ARG_INFO(0, shape_predictor_file_path)
ZEND_END_ARG_INFO()
PHP_METHOD(FaceLandmarkDetection, __construct);
ZEND_BEGIN_ARG_INFO_EX(face_landmark_detection_detect_arginfo, 0, 0, 2)
ZEND_ARG_INFO(0, img_path)
ZEND_ARG_INFO(0, bounding_box)
ZEND_END_ARG_INFO()
PHP_METHOD(FaceLandmarkDetection, detect);
#endif //PDLIB_FACE_LANDMARK_DETECTION_H