Skip to content

Commit a17029b

Browse files
committed
Merge branch 'dev'
2 parents 868c5d6 + 5fb7278 commit a17029b

File tree

12 files changed

+320
-69
lines changed

12 files changed

+320
-69
lines changed

.travis.yml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ before_install:
2424
install:
2525
# OpenCV dependencies - Details available at: http://docs.opencv.org/trunk/doc/tutorials/introduction/linux_install/linux_install.html
2626
- sudo apt-get install -y build-essential
27-
# Install cmake
27+
# Install Cmake
2828
- wget https://cmake.org/files/v3.8/cmake-3.8.2.tar.gz
2929
- tar -zxf cmake-3.8.2.tar.gz
3030
- cd cmake-3.8.2
@@ -36,12 +36,6 @@ install:
3636
- sudo apt-get install -y git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev -qq
3737
- sudo apt-get install -y python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev -qq
3838

39-
40-
41-
42-
43-
#- sudo apt-get install -y gcc-4.8 g++-4.8
44-
4539
# Download OpenCV-contrib v3.3.0-rc
4640
- git clone https://github.com/opencv/opencv_contrib.git
4741
- cd opencv_contrib

README.md

Lines changed: 21 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ extension="opencv.so path"
2929
```
3030
## Example
3131

32+
LBPH face recognition
33+
34+
```php
35+
use CV\Face\LBPHFaceRecognizer;
36+
//use ...;
37+
38+
$src = imread('facePic.jpg');
39+
$gray = cvtColor($src, COLOR_BGR2GRAY);
40+
equalizeHist($gray, $gray);
41+
$faceRecognizer = LBPHFaceRecognizer::create();
42+
/* ... */ //get $images and $labels for train
43+
$faceRecognizer->train($images, $labels);//How to get $image and $labels, see the document
44+
/* ... */ //Face detection using CascadeClassifier
45+
$faceLabel = $faceRecognizer->predict($gray);
46+
/* ... */ //draw face and name
47+
```
48+
49+
result:
50+
51+
![predict](tests/face_recognizer.jpg)
52+
3253
```php
3354
//Obama.php
3455
use function CV\{ imread, imshow, waitkey, namedWindow};
@@ -112,66 +133,6 @@ result:
112133

113134
[PHP OpenCV Api](http://php-opencv.hihozhou.com/zh-cn/api/)
114135

115-
## Structure
116-
- CV(namespace)
117-
- CONSTANTS
118-
- CLASSES
119-
- Exception(class)
120-
- Mat(class)
121-
- properties
122-
- public int $rows
123-
- public int $cols
124-
- private int $type
125-
- functions
126-
- public __construct(int $rows, int $cols, int $type, Scalar $scalar)
127-
- public print(int $type)
128-
- public static zeros(int $rows, int $cols, int $type)
129-
- public type()
130-
- public depth()
131-
- public channels()
132-
- public isContinuous()
133-
- public row(int $y)
134-
- public col(int $x)
135-
- public clone()
136-
- Point(class)
137-
- properties
138-
- public int $x
139-
- public int $y
140-
- functions
141-
- public __construct(int $x, int $y)
142-
- public print()
143-
- Scalar(class)
144-
- public array $val
145-
- public __construct(int $val1, int $val2, int $val3, int $valu4)
146-
- public print()
147-
- Size(class)
148-
- functions
149-
- public __construct(int $width, int $height);
150-
- public print()
151-
- Rect(class)
152-
- functions
153-
- public __construct(int $x, int $y, int $width, int $height)
154-
- public print()
155-
- pubilc tl()
156-
- public br()
157-
- public size()
158-
- public area()
159-
- Formatter(class)
160-
161-
- FUNCTIONS
162-
- imread(string $path, int $flags = CV\IMREAD_COLOR)
163-
- imwrite(string $path, Mat $img)
164-
- namedWindow(string $name, int $flags = 2)
165-
- imshow(string $windowName, Mat $img)
166-
- waitKey(int $sec)
167-
- cvtColor(Mat $img, int $code)
168-
- ellipse(Mat $img, Point $center, Size $size, int $angle, int $startAngle, int $endAngle, Scalar $color, int $thickness = 1, int $lineType = CV\LINE_8, int $shift = 0)
169-
- circle(Mat $img, Point $center, int $radius, Scalar $color, int $thickness = 1, int $lineType = CV\LINE_8, int $shift = 0)
170-
- line(Mat $img, Point $start, Point $end, Scalar $color, int $thickness = 1, int $lineType = CV\LINE_8, int $shift = 0)
171-
- rectangle(Mat $img, int $startX, int $startY, int $endX, int $endY, Scalar $color, int $thickness = 1, int $lineType = CV\LINE_8, int $shift = 0)
172-
- rectangleByPoint(Mat $img, Point $start, Point $end, Scalar $color, int $thickness = 1, int $lineType = CV\LINE_8, int $shift = 0)
173-
- rectangleByRect(Mat $img, Rect $rect, Scalar $color, int $thickness = 1, int $lineType = CV\LINE_8, int $shift = 0)
174-
175136

176137

177138
## 感谢

config.m4

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ if test "$PHP_OPENCV" != "no"; then
4545
source/opencv2/core/opencv_base.cc \
4646
source/opencv2/core/opencv_persistence.cc \
4747
source/opencv2/opencv_objdetect.cc \
48-
source/opencv2/opencv_videoio.cc"
48+
source/opencv2/opencv_videoio.cc \
49+
source/opencv2/opencv_face.cc \
50+
source/opencv2/face/opencv_facerec.cc"
4951

5052

5153
PHP_NEW_EXTENSION(opencv, $opencv_source_file, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)

opencv.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extern "C" {
3939
#include "source/opencv2/core/opencv_persistence.h"
4040
#include "source/opencv2/opencv_objdetect.h"
4141
#include "source/opencv2/opencv_videoio.h"
42+
#include "source/opencv2/opencv_face.h"
4243

4344
/* If you declare any globals in php_opencv.h uncomment this:
4445
ZEND_DECLARE_MODULE_GLOBALS(opencv)
@@ -119,6 +120,7 @@ PHP_MINIT_FUNCTION(opencv)
119120
opencv_file_storage_init(module_number);
120121
opencv_objdetect_init(module_number);
121122
opencv_videoio_init(module_number);
123+
opencv_face_init(module_number);
122124

123125
return SUCCESS;
124126
}
@@ -209,6 +211,7 @@ const zend_function_entry opencv_functions[] = {
209211
ZEND_NS_NAMED_FE(OPENCV_NS, normalize, ZEND_FN(opencv_normalize), opencv_normalize_arginfo)
210212
ZEND_NS_NAMED_FE(OPENCV_NS, equalizeHist, ZEND_FN(opencv_equalize_hist), opencv_equalize_hist_arginfo)
211213
ZEND_NS_NAMED_FE(OPENCV_NS, resize, ZEND_FN(opencv_resize), opencv_resize_arginfo)
214+
ZEND_NS_NAMED_FE(OPENCV_NS, putText, ZEND_FN(opencv_put_text), NULL)
212215
PHP_FE_END /* Must be the last line in opencv_functions[] */
213216
};
214217
/* }}} */

php_opencv.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ extern zend_module_entry opencv_module_entry;
4747
using namespace cv;
4848

4949
#define OPENCV_NS "CV"
50+
#define OPENCV_FACE_NS ZEND_NS_NAME(OPENCV_NS,"Face")
5051

5152
#define OPENCV_CONNECT(text1,text2) text1##text2
5253

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP-OpenCV |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 2.0 of the Apache license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| http://www.apache.org/licenses/LICENSE-2.0.html |
9+
| If you did not receive a copy of the Apache2.0 license and are unable|
10+
| to obtain it through the world-wide-web, please send a note to |
11+
| hihozhou@gmail.com so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Author: HaiHao Zhou <hihozhou@gmail.com> |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
18+
#include "opencv_facerec.h"
19+
#include "../opencv_face.h"
20+
#include "../core/opencv_mat.h"
21+
#include "../../../opencv_exception.h"
22+
23+
zend_object_handlers opencv_lbph_face_recognizer_object_handlers;
24+
25+
zend_class_entry *opencv_lbph_face_recognizer_ce;
26+
27+
PHP_METHOD(opencv_lbph_face_recognizer, create)
28+
{
29+
zval instance;
30+
object_init_ex(&instance, opencv_lbph_face_recognizer_ce);
31+
opencv_lbph_face_recognizer_object *obj = Z_PHP_LBPH_FACE_RECOGNIZER_OBJ_P(&instance);
32+
obj->faceRecognizer = LBPHFaceRecognizer::create();
33+
RETURN_ZVAL(&instance,0,0);
34+
}
35+
36+
PHP_METHOD(opencv_lbph_face_recognizer, train)
37+
{
38+
zval *src_zval, *labels_zval;
39+
zend_ulong _h;
40+
zval *array_val_zval;
41+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "aa", &src_zval, &labels_zval) == FAILURE) {
42+
RETURN_NULL();
43+
}
44+
std::vector<Mat> src;
45+
std::vector<int> labels;
46+
//check
47+
opencv_lbph_face_recognizer_object *obj = Z_PHP_LBPH_FACE_RECOGNIZER_OBJ_P(getThis());
48+
unsigned long src_count = zend_hash_num_elements(Z_ARRVAL_P(src_zval));
49+
src.reserve(src_count);//指定长度
50+
opencv_mat_object *mat_obj;
51+
52+
ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(src_zval),_h,array_val_zval){
53+
//check array_val_zval is Mat object
54+
again1:
55+
if(Z_TYPE_P(array_val_zval) == IS_OBJECT && Z_OBJCE_P(array_val_zval)==opencv_mat_ce){
56+
mat_obj = Z_PHP_MAT_OBJ_P(array_val_zval);
57+
src.push_back(*mat_obj->mat);
58+
}else if(Z_TYPE_P(array_val_zval) == IS_REFERENCE){
59+
array_val_zval = Z_REFVAL_P(array_val_zval);
60+
goto again1;
61+
} else {
62+
char *error_message = (char*)malloc(strlen("array value just Mat object") + 1);
63+
strcpy(error_message,"array value just Mat object");
64+
opencv_throw_exception(error_message);
65+
free(error_message);
66+
RETURN_NULL();
67+
}
68+
}ZEND_HASH_FOREACH_END();
69+
ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(labels_zval),_h,array_val_zval){
70+
again2:
71+
if(Z_TYPE_P(array_val_zval) == IS_LONG){
72+
labels.push_back((int)zval_get_long(array_val_zval));
73+
}else if(Z_TYPE_P(array_val_zval) == IS_REFERENCE){
74+
array_val_zval = Z_REFVAL_P(array_val_zval);
75+
goto again2;
76+
} else {
77+
char *error_message = (char*)malloc(strlen("array value just number") + 1);
78+
strcpy(error_message,"array value just number");
79+
opencv_throw_exception(error_message);
80+
free(error_message);
81+
RETURN_NULL();
82+
}
83+
}ZEND_HASH_FOREACH_END();
84+
obj->faceRecognizer->train(src,labels);
85+
RETURN_NULL();
86+
87+
}
88+
89+
PHP_METHOD(opencv_lbph_face_recognizer, predict)
90+
{
91+
zval *src_zval;
92+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "o", &src_zval, opencv_mat_ce) == FAILURE) {
93+
RETURN_NULL();
94+
}
95+
opencv_lbph_face_recognizer_object *obj = Z_PHP_LBPH_FACE_RECOGNIZER_OBJ_P(getThis());
96+
opencv_mat_object *src_object = Z_PHP_MAT_OBJ_P(src_zval);
97+
int predict_label = obj->faceRecognizer->predict(*src_object->mat);
98+
RETURN_LONG(predict_label);
99+
}
100+
101+
102+
/**
103+
* opencv_lbph_face_recognizer_methods[]
104+
*/
105+
const zend_function_entry opencv_lbph_face_recognizer_methods[] = {
106+
PHP_ME(opencv_lbph_face_recognizer, create, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
107+
PHP_ME(opencv_lbph_face_recognizer, train, NULL, ZEND_ACC_PUBLIC)
108+
PHP_ME(opencv_lbph_face_recognizer, predict, NULL, ZEND_ACC_PUBLIC)
109+
PHP_FE_END
110+
};
111+
/* }}} */
112+
113+
/**
114+
* @param type
115+
* @return
116+
*/
117+
zend_object* opencv_lbph_face_recognizer_handler(zend_class_entry *type)
118+
{
119+
size_t size = sizeof(opencv_lbph_face_recognizer_object);
120+
opencv_lbph_face_recognizer_object *obj = (opencv_lbph_face_recognizer_object *)ecalloc(1,size);
121+
memset(obj, 0, sizeof(opencv_lbph_face_recognizer_object));
122+
zend_object_std_init(&obj->std, type);
123+
object_properties_init(&obj->std, type);
124+
obj->std.ce = type;
125+
obj->std.handlers = &opencv_lbph_face_recognizer_object_handlers;
126+
return &obj->std;
127+
}
128+
129+
130+
void opencv_lbph_face_recognizer_free_obj(zend_object *object)
131+
{
132+
opencv_lbph_face_recognizer_object *obj;
133+
obj = get_lbph_face_recognizer_obj(object);
134+
delete obj->faceRecognizer;
135+
zend_object_std_dtor(object);
136+
}
137+
138+
extern void opencv_lbph_face_recognizer_init(int module_number){
139+
zend_class_entry ce;
140+
INIT_NS_CLASS_ENTRY(ce,OPENCV_FACE_NS, "LBPHFaceRecognizer", opencv_lbph_face_recognizer_methods);
141+
opencv_lbph_face_recognizer_ce = zend_register_internal_class_ex(&ce, opencv_face_recognizer_ce);
142+
143+
opencv_lbph_face_recognizer_ce->create_object = opencv_lbph_face_recognizer_handler;
144+
memcpy(&opencv_lbph_face_recognizer_object_handlers,
145+
zend_get_std_object_handlers(), sizeof(zend_object_handlers));
146+
opencv_lbph_face_recognizer_object_handlers.clone_obj = NULL;
147+
opencv_lbph_face_recognizer_object_handlers.free_obj = opencv_lbph_face_recognizer_free_obj;
148+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP-OpenCV |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 2.0 of the Apache license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| http://www.apache.org/licenses/LICENSE-2.0.html |
9+
| If you did not receive a copy of the Apache2.0 license and are unable|
10+
| to obtain it through the world-wide-web, please send a note to |
11+
| hihozhou@gmail.com so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Author: HaiHao Zhou <hihozhou@gmail.com> |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
18+
#ifndef PHP_OPENCV_FACEREC_H
19+
#define PHP_OPENCV_FACEREC_H
20+
21+
#include "../../../php_opencv.h"
22+
23+
#include <opencv2/face.hpp>
24+
using namespace cv::face;
25+
26+
extern void opencv_lbph_face_recognizer_init(int module_number);
27+
28+
#define Z_PHP_LBPH_FACE_RECOGNIZER_OBJ_P(zv) get_lbph_face_recognizer_obj(Z_OBJ_P(zv))
29+
30+
typedef struct _opencv_lbph_face_recognizer_object{
31+
zend_object std;
32+
Ptr<LBPHFaceRecognizer> faceRecognizer;
33+
}opencv_lbph_face_recognizer_object;
34+
35+
36+
/**
37+
* @param obj
38+
* @return
39+
*/
40+
static inline opencv_lbph_face_recognizer_object* get_lbph_face_recognizer_obj(zend_object *obj) {
41+
return (opencv_lbph_face_recognizer_object*)((char*)(obj) - XtOffsetOf(opencv_lbph_face_recognizer_object, std));
42+
}
43+
44+
#endif //PHP_OPENCV_FACEREC_H

source/opencv2/opencv_face.cc

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP-OpenCV |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 2.0 of the Apache license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| http://www.apache.org/licenses/LICENSE-2.0.html |
9+
| If you did not receive a copy of the Apache2.0 license and are unable|
10+
| to obtain it through the world-wide-web, please send a note to |
11+
| hihozhou@gmail.com so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Author: HaiHao Zhou <hihozhou@gmail.com> |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#include "opencv_face.h"
18+
#include "face/opencv_facerec.h"
19+
20+
zend_class_entry *opencv_face_recognizer_ce;
21+
22+
/**
23+
* opencv_face_recognizer_methods[]
24+
*/
25+
const zend_function_entry opencv_face_recognizer_methods[] = {
26+
PHP_FE_END
27+
};
28+
/* }}} */
29+
30+
31+
void opencv_face_recognizer_init(int module_number){
32+
zend_class_entry ce;
33+
INIT_NS_CLASS_ENTRY(ce,OPENCV_FACE_NS, "FaceRecognizer", opencv_face_recognizer_methods);
34+
opencv_face_recognizer_ce = zend_register_internal_class(&ce);
35+
}
36+
37+
void opencv_face_init(int module_number){
38+
opencv_face_recognizer_init(module_number);
39+
opencv_lbph_face_recognizer_init(module_number);
40+
}

0 commit comments

Comments
 (0)