How to define global object for thread security in php extension

Source: Internet
Author: User
Traditional php cropping classes can only be tailored by position. For example, crop by the center position. This cropping method may cause a person to be cut into two segments. To solve this problem, we recently developed a php extension (tclip) that can automatically recognize faces or other important areas of images and then crop them. Crop the source image as follows:

Traditional php cropping classes can only be tailored by position. For example, crop by the center position. This cropping method may cause a person to be cut into two segments. To solve this problem, we recently developed a php extension (tclip) that can automatically recognize faces or other important areas of images and then crop them. The cropping effect is as follows:

SOURCE image:

If the image size is 400*225 in the middle. The effect is as follows:

You can use tclip to expand and crop the image as follows:

In the face recognition process, you need to use CascadeClassifier in opencv to load an xml configuration document. Because the file size is large, the loading time is relatively long. To improve the cropping efficiency, we decided to load the object during php extension initialization and use global variables to store the CascadeClassifier object. In this way, you can directly use this configuration file during each cropping without loading it.

Modify the global variable declaration section in php_tclip.h to the following:

ZEND_BEGIN_MODULE_GLOBALS(tclip)CascadeClassifier face_cascade;char *face_config_path;ZEND_END_MODULE_GLOBALS(tclip)

The following error is reported:
Error: 'cascadeclassifier' does not name a type
It seems that this class is not declared in the header file. That's another way. Change the code:

ZEND_BEGIN_MODULE_GLOBALS(tclip)void *face_cascade;char *face_config_path;ZEND_END_MODULE_GLOBALS(tclip)

Then, modify tclip. c as follows:
1. define a global variable first.

static CascadeClassifier face_cascade;

2. modify PHP_MINIT_FUNCTION (tclip ). The code is as follows:

PHP_MINIT_FUNCTION(tclip){/* If you have INI entries, uncomment these lines */REGISTER_INI_ENTRIES();string face_config_path = (TCLIP_G(face_config_path) == "" || TCLIP_G(face_config_path) == NULL)? "/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" :TCLIP_G(face_config_path);if( !face_cascade.load( face_config_path ) ){php_error_docref(NULL TSRMLS_CC, E_WARNING, "can not load classifier file!%s", face_config_path.c_str());        return FAILURE;    }TCLIP_G(face_cascade) = &face_cascade;return SUCCESS;}

You can call a global object as follows:

((CascadeClassifier *)TCLIP_G(face_cascade))->detectMultiScale( img_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) );

Note: To ensure thread security for php extensions, use related macros to access global variables. The above TCLIP_G (face_cascade ).
References http://www.laruence.com/2009/04/28/719.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.