forked from infusion/PHP-Facedetect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacedetect.c
155 lines (131 loc) · 4.21 KB
/
facedetect.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_0.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Robert Eisele <[email protected]> |
+----------------------------------------------------------------------+
*/
// http://www.santyago.pl/files/facedetect-1.0.1-opencv-2.2.0.patch
// - face recog lib embedden http://libface.sourceforge.net/file/Examples.html
// - http://www.cognotics.com/opencv/servo_2007_series/part_5/index.html
// - opencv 2.2 kompatibel
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_facedetect.h"
#include <opencv/cv.h>
#include <opencv/highgui.h>
/* {{{ facedetect_functions[]
*
* Every user visible function must have an entry in facedetect_functions[].
*/
static zend_function_entry facedetect_functions[] = {
PHP_FE(face_detect, NULL)
PHP_FE(face_count, NULL)
{NULL, NULL, NULL}
};
/* }}} */
/* {{{ facedetect_module_entry
*/
zend_module_entry facedetect_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_FACEDETECT_EXTNAME,
facedetect_functions,
NULL,
NULL,
NULL,
NULL,
PHP_MINFO(facedetect),
#if ZEND_MODULE_API_NO >= 20010901
PHP_FACEDETECT_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_FACEDETECT
ZEND_GET_MODULE(facedetect)
#endif
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINFO_FUNCTION(facedetect)
{
php_info_print_table_start();
php_info_print_table_row(2, "facedetect support", "enabled");
php_info_print_table_row(2, "facedetect version", PHP_FACEDETECT_VERSION);
php_info_print_table_row(2, "OpenCV version", CV_VERSION);
php_info_print_table_end();
}
/* }}} */
static void php_facedetect(INTERNAL_FUNCTION_PARAMETERS, int return_type)
{
char *file, *casc;
long flen, clen;
zval *array;
CvHaarClassifierCascade* cascade;
IplImage *img, *gray;
CvMemStorage *storage;
CvSeq *faces;
CvRect *rect;
if(zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &file, &flen, &casc, &clen) == FAILURE) {
RETURN_NULL();
}
img = cvLoadImage(file, 1);
if(!img) {
RETURN_FALSE;
}
cascade = (CvHaarClassifierCascade*)cvLoad(casc, 0, 0, 0);
if(!cascade) {
RETURN_FALSE;
}
gray = cvCreateImage(cvSize(img->width, img->height), 8, 1);
cvCvtColor(img, gray, CV_BGR2GRAY);
cvEqualizeHist(gray, gray);
storage = cvCreateMemStorage(0);
faces = cvHaarDetectObjects(gray, cascade, storage, 1.1, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize(0, 0), cvSize(0, 0));
if(return_type) {
array_init(return_value);
if(faces && faces->total > 0) {
int i;
for(i = 0; i < faces->total; i++) {
MAKE_STD_ZVAL(array);
array_init(array);
rect = (CvRect *)cvGetSeqElem(faces, i);
add_assoc_long(array, "x", rect->x);
add_assoc_long(array, "y", rect->y);
add_assoc_long(array, "w", rect->width);
add_assoc_long(array, "h", rect->height);
add_next_index_zval(return_value, array);
}
}
} else {
RETVAL_LONG(faces ? faces->total : 0);
}
cvReleaseMemStorage(&storage);
cvReleaseImage(&gray);
cvReleaseImage(&img);
}
/* {{{ proto array face_detect(string picture_path, string cascade_file)
Finds coordinates of faces (or in gernal "objects") on the given picture */
PHP_FUNCTION(face_detect)
{
php_facedetect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
}
/* }}} */
/* {{{ proto int face_count(string picture_path, string cascade_file)
Finds number of faces (or in gernal "objects") on the given picture*/
PHP_FUNCTION(face_count)
{
php_facedetect(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */