Home  >  Article  >  WeChat Applet  >  WeChat public platform messaging interface develops image recognition and face recognition

WeChat public platform messaging interface develops image recognition and face recognition

高洛峰
高洛峰Original
2017-03-01 09:35:575690browse

1. Foreword

The first few small applications seem to be weak, and the response is lukewarm. It seems that everyone is not interested. Today I will introduce a more eye-catching one for you: the WeChat public platform Face recognition.

I saw a report on the Internet some time ago that Wei Xiaoyong, associate professor and director of the Computer Science Department of Sichuan University, developed an image-based classroom attendance system. Face recognition is used to 'swipe your face' during class roll call. See the picture below.

WeChat public platform messaging interface develops image recognition and face recognition

During roll call, you only need to take photos of the students in the classroom from multiple angles, then upload the photos to the server, automatically stitch them into a whole picture, and the system will then compare the photos The student avatars in the app are automatically numbered and identified, and the student's personal information appears at the end, with two options: "It'sme (it's me)" and "notme (not me)". In this way, you can immediately know who is in class and who is skipping class

What’s even more amazing is the statistical significance of this thing: it can even analyze students’ interpersonal relationships and even personality interests. Where do you like to sit in class and who do you like to sit next to? After getting the raw data, after a period of time, you can analyze who is in love with whom, and who is with whom after breaking up. Who always likes to sit alone in the corner (autism), a certain boy and a certain boy have been sitting together every time for half a year (gay friends???), and instantly feel that this thing gives people unlimited space for imagination big. . . . . .

Let’s get down to business:

First, let’s take a look at what face recognition is, and look at the definition of Baidu Encyclopedia: Face recognition specifically refers to the use of analysis and comparison of face vision Computer technology for identification using characteristic information. Face recognition is a popular field of computer technology research. It can detect the light and dark of the face, automatically adjust the dynamic exposure compensation, track and detect the face, and automatically adjust the image magnification; it belongs to biometric identification technology and is a tool for biological identification. Generally refers to the biological characteristics of a person) to distinguish individual organisms.

The technical process of face recognition (from Baidu Encyclopedia)

It is generally divided into three steps:
(1) First create a face image file. That is, a camera is used to collect facial image files of the faces of unit personnel or take their photos to form facial image files, and these facial image files are generated into facial print (Faceprint) codes and stored.
(2) Get the current human face image. That is, use a camera to capture the face image of the current person entering or exiting, or take a photo to input, and generate a facial pattern code from the current face image file.
(3) Compare the current facial pattern code with the archive inventory. That is, the facial pattern code of the current facial image is retrieved and compared with the facial pattern code in the archive inventory. The above-mentioned "facial pattern encoding" method works based on the essential characteristics and beginnings of the human face. This facial pattern encoding is robust to changes in light, skin tone, facial hair, hairstyle, eyewear, expression and posture, allowing it to accurately identify an individual among millions of others. The face recognition process can be completed automatically, continuously, and in real time using ordinary image processing equipment.

2. Face recognition on the WeChat public platform

1. Obtain pictures

First of all, we must obtain the pictures sent by the users, WeChat public platform Supports receiving pictures. A picture sent by the

user is as follows:

WeChat public platform messaging interface develops image recognition and face recognition

The format of the picture message is as follows:

<xml>
    <ToUserName><![CDATA[gh_13d1a3a7x46a]]></ToUserName>
    <FromUserName><![CDATA[oKaHDjt60aAyPvQmUX3ddyix_zG8]]></FromUserName>
    <CreateTime>1357543196</CreateTime>
    <MsgType><![CDATA[image]]></MsgType>
    <PicUrl><![CDATA[http://www.php.cn/]]></PicUrl>
    <MsgId>5830603629728080261</MsgId></xml>

ToUserName WeChat ID of the message receiver, usually the public platform account WeChat ID
FromUserName WeChat ID of the message sender
CreateTime Message creation time
MsgType Message type; picture message Provide the face recognition interface for image
PicUrl image link address
MsgId message ID number

2. Interface

Face++,

Detect the positions of all faces (Face) in a given image (Image) and the corresponding facial attribute interface address is as follows:
http://apicn.faceplusplus.com/v2/detection/detect

Parameter

Is it required

Meaning

api_key

Required

App’s Face++ API Key

api_secret

Required

APP’s Face++ API Secret

url or img[POST]

must

The URL of the image to be detected or pass For binary data uploaded by POST method, the original image size needs to be less than 3M

mode

optional

检测模式可以是normal(默认) 或者 oneface 。在oneface模式中,检测器仅找出图片中最大的一张脸。

attribute

可选

可以是none或者由逗号分割的属性列表。默认为gender, age, race, smiling。目前支持的属性包括:gender, age, race, smiling, glass, pose

tag

可选

可以为图片中检测出的每一张Face指定一个不包含^@,&=*'"等非法字符且不超过255字节的字符串作为tag,tag信息可以通过 /info/get_face 查询

async

可选

如果置为true,该API将会以异步方式被调用;也就是立即返回一个session id,稍后可通过/info/get_session查询结果。默认值为false。


返回如下

{
    "face": [
        {
            "attribute": {
                "age": {
                    "range": 5,
                    "value": 17
                },
                "gender": {
                    "confidence": 99.9781,
                    "value": "Female"
                },
                "glass": {
                    "confidence": 99.9815,
                    "value": "None"
                },
                "pose": {
                    "pitch_angle": {
                        "value": 0.000019753399999999996
                    },
                    "roll_angle": {
                        "value": 1.75177
                    },
                    "yaw_angle": {
                        "value": 4
                    }
                },
                "race": {
                    "confidence": 99.471,
                    "value": "Asian"
                },
                "smiling": {
                    "value": 87.1365
                }
            },
            "face_id": "c772b4b66c00d46b15344eff74b56e48"
        }
    ],
    "img_height": 293,
    "img_id": "3005132383841edd08c9b500fb1fe2c4",
    "img_width": 440,
    "session_id": "4e64c73fec19442cbefde3cf9bd6b53d",
    "url": ""
}

参数:

gender

object

包含性别分析结果,value的值为Male/Female, confidence表示置信度

age

object

包含年龄分析结果,value的值为一个非负整数表示估计的年龄, range表示估计年龄的正负区间

race

object

包含人种分析结果,value的值为Asian/White/Black, confidence表示置信度

smiling

object

包含微笑程度分析结果,value的值为0-100的实数,越大表示微笑程度越高

实现方法

取结果参数中的gender,age,race,smiling等参数 ,如

"attribute": {
                "age": {
                    "range": 5,
                    "value": 17
                },
                "gender": {
                    "confidence": 99.9781,
                    "value": "Female"
                },
                "glass": {
                    "confidence": 99.9815,
                    "value": "None"
                },
                "pose": {
                    "pitch_angle": {
                        "value": 0.000019753399999999996
                    },
                    "roll_angle": {
                        "value": 1.75177
                    },
                    "yaw_angle": {
                        "value": 4
                    }
                },
                "race": {
                    "confidence": 99.471,
                    "value": "Asian"
                },
                "smiling": {
                    "value": 87.1365
                }

上述结果中,

年龄age为17,误差在5岁
性别为female,表示女,置信度为99%
种族为亚洲Asian,
微笑度 87%

和图片比较一下,还是比较一致的

WeChat public platform messaging interface develops image recognition and face recognition

 

3. 程序实现

SDK如下:

"attribute": {
                "age": {
                    "range": 5,
                    "value": 17
                },
                "gender": {
                    "confidence": 99.9781,
                    "value": "Female"
                },
                "glass": {
                    "confidence": 99.9815,
                    "value": "None"
                },
                "pose": {
                    "pitch_angle": {
                        "value": 0.000019753399999999996
                    },
                    "roll_angle": {
                        "value": 1.75177
                    },
                    "yaw_angle": {
                        "value": 4
                    }
                },
                "race": {
                    "confidence": 99.471,
                    "value": "Asian"
                },
                "smiling": {
                    "value": 87.1365
                }

调用方式如下:

<?PHP
/**
* Face++ PHP SDK
* author: Tianye
* since:  2013-12-11
**/
class Facepp{
    ######################################################
    ### If you choose Amazon(US) server,please use the ###
    ### http://apius.faceplusplus.com/v2               ###
    ### or                                             ###
    ### https://apius.faceplusplus.com/v2              ###
    ######################################################
    var $server = &#39;http://apicn.faceplusplus.com/v2&#39;;
    #var $server = &#39;https://apicn.faceplusplus.com/v2&#39;;
    #var $server = &#39;http://apius.faceplusplus.com/v2&#39;;
    #var $server = &#39;https://apius.faceplusplus.com/v2&#39;;

    #############################################
    ### set your api key and api secret here. ###
    #############################################
    var $api_key = &#39;{your API KEY}&#39;;
    var $api_secret = &#39;{your API SECRET}&#39;;

    public function __construct($api_key=NULL, $api_secret=NULL, $server=NULL){
        if($api_key){
            $this->api_key = $api_key;
        }
        if($api_secret){
            $this->api_secret = $api_secret;
        }
        if($server){
            $this->server = $server;
        }
    }

    /**
    * @param $method : The Face++ API 
    * @param $params : Request Parameters
    * @return : Array {&#39;http_code&#39;:&#39;Http Status Code&#39;, &#39;request_url&#39;:&#39;Http Request URL&#39;,&#39;body&#39;:&#39; JSON Response&#39;}
    **/
    public function execute($method,$params){
        if(empty($params)){
            $params=array();
        }
        $params[&#39;api_key&#39;] = $this->api_key;
        $params[&#39;api_secret&#39;] = $this->api_secret;

        return $this->request("{$this->server}{$method}",$params);
    }

    private function request($request_url , $request_body){
        $useragent = &#39;Faceplusplus PHP SDK/1.0&#39;;
        $curl_handle = curl_init();
        curl_setopt($curl_handle, CURLOPT_URL, $request_url);
        curl_setopt($curl_handle, CURLOPT_FILETIME, TRUE);
        curl_setopt($curl_handle, CURLOPT_FRESH_CONNECT, FALSE);
        curl_setopt($curl_handle, CURLOPT_CLOSEPOLICY, CURLCLOSEPOLICY_LEAST_RECENTLY_USED);
        curl_setopt($curl_handle, CURLOPT_MAXREDIRS, 5);
        curl_setopt($curl_handle, CURLOPT_HEADER, FALSE);
        curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl_handle, CURLOPT_TIMEOUT, 5184000);
        curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 120);
        curl_setopt($curl_handle, CURLOPT_NOSIGNAL, TRUE);
        curl_setopt($curl_handle, CURLOPT_REFERER, $request_url);
        curl_setopt($curl_handle, CURLOPT_USERAGENT, $useragent);
        if (extension_loaded(&#39;zlib&#39;)){
            curl_setopt($curl_handle, CURLOPT_ENCODING, &#39;&#39;);
        }
        curl_setopt($curl_handle, CURLOPT_POST, TRUE);
        if(array_key_exists(&#39;img&#39;,$request_body)){
            $request_body[&#39;img&#39;] = &#39;@&#39;.$request_body[&#39;img&#39;];
        }else{
            $request_body=http_build_query($request_body);
        }
        curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $request_body);
        $response_text = curl_exec($curl_handle);
        $reponse_header = curl_getinfo($curl_handle);
        curl_close($curl_handle);
        return array(&#39;http_code&#39;=>$reponse_header[&#39;http_code&#39;],&#39;request_url&#39;=>$request_url,&#39;body&#39;=>$response_text);
    }
}

在微信中再将结果整理成文本消息就可以了。

 

4. 效果展示

下面是实际使用结果:

漂亮/帅气指数鉴定技术

WeChat public platform messaging interface develops image recognition and face recognitionWeChat public platform messaging interface develops image recognition and face recognition

近亲及亲子鉴定技术

WeChat public platform messaging interface develops image recognition and face recognitionWeChat public platform messaging interface develops image recognition and face recognition

男人味/女人味鉴定技术

WeChat public platform messaging interface develops image recognition and face recognitionWeChat public platform messaging interface develops image recognition and face recognition

更多WeChat public platform messaging interface develops image recognition and face recognition相关文章请关注PHP中文网!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn