Face recognition of Golang WeChat public platform

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Transferred from: http://www.cnblogs.com/wlts/archive/2013/06/02/3113526.html

Well, in fact, the whole is based on the face++, without any technical content, I am just a diligent porter.

What can be achieved is simple, you send a picture, if there is a person, then tell you the analysis of the age, gender; if it is two people, tell you, these two people eyebrows, eyes, nose, mouth and overall similarity.

Public platform, how to say, or the traditional form of a question and answer, you send a message, I received a deal, and then give you back a message, it is so simple.

Simple you come and I go

First of all, the issue of information transmission, the public platform is post over an XML, server-side packaging of an XML sent back.

From the simplest, direct the user information back to get started.

Text messages

 <xml> <tousername><! [cdata[touser]]></tousername> <fromusername><! [cdata[fromuser]]></fromusername> <CreateTime>1348831860</CreateTime> <msgtype><! [cdata[text]]></msgtype> <content><! [Cdata[this is a test]]></content> <MsgId>1234567890123456</MsgId> </xml> 
Parameters Describe
Tousername Developer Number
Fromusername Sender account (one OpenID)
Createtime Message creation time (integer type)
Msgtype Text
Content Text message Content
MsgId Message id,64 bit integral type

The corresponding data structure will naturally come out:

struct {     stringstring            createtime time. Duration      string stringint               }

To decode the input XML:

Func decoderequest (data []byte) (req *request,err error) {      req=&request{}      Err =XML. Unmarshal (data,req)      return  }

Although the server is a post-delivery data, but actually also passed through the URL three parameters: Signature,timestamp,nonce.

These three parameters can verify whether a message was sent by the server.

Take the Post data:

Func Action (w http. Responsewriter,r *http. Request) {      postedmsg,err:=ioutil. ReadAll (r.body)      if err!=nil{          log. Fatal (Err)      }      r.body.close ()      msg,err:=decoderequest (postedmsg)     ...}

The next step is to answer the message

Reply text message

<xml> <tousername><! [cdata[touser]]></tousername> <fromusername><! [cdata[fromuser]]></fromusername> <CreateTime>12345678</CreateTime> <msgtype><! [cdata[text]]></msgtype> <content><! [cdata[content]]></content> <FuncFlag>0</FuncFlag> </xml>

Parameters

Describe
Tousername Recipient's account (OpenID received)

Fromusername

Developer Number
Createtime Message creation Time

Msgtype

Text
Content Reply to the message content, the length of not more than 2048 bytes
Funcflag When a bit 0x0001 is flagged, the star has just received a message

Under Simple encapsulation:

Type Responsestruct{xmlname XML. Name ' xml:"XML"' TousernamestringFromusernamestringcreatetime time. Duration MsgtypestringContentstringFuncflagint}func encoderesponse (resp Response) (data []byte, err Error) {resp. Createtime=Time . Second Data,err=XML. Marshal (RESP)return }

The code that sends the data back:

var resp responseresp.tousername=msg. Fromusernameresp.fromusername=msg. Tousernameresp.msgtype="text"resp. Content=msg. Contentresp.funcflag=0respdata,err:=encoderesponse (resp) fmt. fprintf (W,string(Respdata))

Human Face recognition

This is how to say, is the user by sending photos, photos are stored to the server, send me a picture URL, I send this URL to face++,face++ will analyze the results sent back to me, I put these data simple processing, feedback to the user (of course, the middle is also separated from the layer server).

The whole process, what I do is the simple JSON data processing, what high-end image processing is not the same with me, haha ~

First of all, of course, to http://cn.faceplusplus.com/registration, get Api_secret, Api_key.

Then recommended to read the document, http://cn.faceplusplus.com/dev/getting-started/api2info/, of course, directly follow me to do it again.

First face detection, detection of gender, age, race.

After looking at the sample document, the structure of the JSON that was returned after the detect call was found to be expressed presumably like this:

Type Faceslicestruct{face []struct{Attributestruct{ Agestruct{Range float64 Value float64} Genderstruct{Confidence float64 Valuestring} Racestruct{Confidence float64 vaulestring}} face_idstringPositionstruct{Centerstruct{X float64 Y float64} eye_leftstruct{X float64 Y float64} eye_rightstruct{X float64 Y float64} Height float64 Mouth_left struct{X float64 Y float64} mouth_rightstruct{X float64 Y float64} Nosestruct{X float64 Y float64} Width float64} Tagstring} img_heightintimg_idstringImg_widthintsession_idstringURLstring }

Parse JSON data:

Func decodedetect (data []byte) faceslice{     var  F faceslice     json. Unmarshal (data,&f)     return  F}

Then write a Get function:

Get string) (b []byte, err error) {     res,e:=http. Get (URL)     if e!=nil{         Err=e         return     }     Data,e:=ioutil. ReadAll (Res. Body)     if e!=nil{         Err=e         return     }     Res. Body.close ()     return  Data,nil}

Call the face++ interface and return the appropriate data:

ConstApiurl="https://apicn.faceplusplus.com"func detectiondetect (Picurlstring) detection. faceslice{URL:=apiurl+"/v2/detection/detect?url="+picurl+"&api_secret="+apisecret+"&api_key="+apikey tmp,_:=Get(URL)returndetection. Decodedetect (TMP)}

Just the above example simply considers the text information, now to pass the picture information, so make a simple modification:

struct {     stringstring            createtime time. Duration       stringstring                    int }

The action function also has to be modified to determine the next MSG. Msgtype, if it is text, then the same as the previous processing, if it is an image, there is a new processing method.

I did two simple processing, one is age, gender, race, and if the photo is two people, then give the facial features and the overall similarity value.

The similarity code is placed directly below:

Package Recognition Import (     "encoding/json"struct{      struct {eye         float64         Mouth float64         Nose float64 eyebrow         float64     }     string       similarity float64}

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.