golang微信公众平台之人脸识别
source link: https://studygolang.com/articles/306?fr=sidebar
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
golang微信公众平台之人脸识别
likai198981 · 2014-10-04 19:26:11 · 3237 次点击 · 预计阅读时间 12 分钟 · 大约8小时之前 开始浏览转自:http://www.cnblogs.com/wlts/archive/2013/06/02/3113526.html
所能实现的就是简单的,你发送一个图片过来,如果里面是一个人,则告诉你分析出来的年龄、性别;如果是两个人,就告诉你,这两个人眉毛、眼睛、鼻子、嘴巴及整体的相似度。
微信公众平台,怎么说呢,还是传统的一问一答的形式,你发个信息过来,我收到了处理下,再给你回馈一条信息,就是这么简单。
简单的你来我往
先说信息互传的问题,微信公众平台是post过来一个xml,服务器端打包一个xml发回去。
从最简单的,直接把用户信息返回去搞起吧。
<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>
相应的数据结构也就自然出来了:
type Request struct{ ToUserName string FromUserName string CreateTime time.Duration MsgType string Content string MsgId int }
将输入的xml解码:
func decodeRequest(data []byte)(req *Request,err error){ req=&Request{} err=xml.Unmarshal(data,req) return }
虽然微信服务器是用post方式传递的数据,不过实际还通过url传递过来了三个参数:signature,timestamp,nonce.
这三个参数可以验证消息是否微信服务器发送过来的。
取post过来的数据:
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) ... }
接下来就是回复信息
回复文本消息
<xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>12345678</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[content]]></Content> <FuncFlag>0</FuncFlag> </xml>
描述 ToUserName 接收方帐号(收到的OpenID)
FromUserName
开发者微信号 CreateTime 消息创建时间
MsgType
text Content 回复的消息内容,长度不超过2048字节 FuncFlag 位0x0001被标志时,星标刚收到的消息
简单封装下:
type Response struct{ XMLName xml.Name `xml:"xml"` ToUserName string FromUserName string CreateTime time.Duration MsgType string Content string FuncFlag int } func encodeResponse(resp Response)(data []byte,err error){ resp.CreateTime=time.Second data,err=xml.Marshal(resp) return }
将数据发送回去的代码:
var resp Response resp.ToUserName=msg.FromUserName resp.FromUserName=msg.ToUserName resp.MsgType="text" resp.Content=msg.Content resp.FuncFlag=0 respData,err:=encodeResponse(resp) fmt.Fprintf(w,string(respData))
人脸识别
这个怎么说,就是用户通过微信发送照片,照片是存到微信服务器的,微信给我发一个图片url,我再把这个url转给face++,face++将分析结果给我发回来,我再把这些数据简单处理下,反馈给微信用户(当然,中间还隔了层微信服务器)。
整个过程中,我所做的就是简单的json数据处理,什么高端的图像处理什么的都跟我不沾边,哈哈~
首先当然是到http://cn.faceplusplus.com/注册,获取API_SECRET、API_KEY。
而后推荐看文档,http://cn.faceplusplus.com/dev/getting-started/api2info/,当然直接跟着我来一遍也行。
先来个人脸检测吧,检测出性别、年龄、种族。
看了示例文档后,发现detect调用后返回的json的结构表示出来大概是这样:
type Faceslice struct{ Face []struct{ Attribute struct{ Age struct{ Range float64 Value float64 } Gender struct{ Confidence float64 Value string } Race struct{ Confidence float64 Vaule string } } Face_id string Position struct{ Center struct{ X float64 Y float64 } Eye_left struct{ X float64 Y float64 } Eye_right struct{ X float64 Y float64 } Height float64 Mouth_left struct{ X float64 Y float64 } Mouth_right struct{ X float64 Y float64 } Nose struct{ X float64 Y float64 } Width float64 } Tag string } Img_height int Img_id string Img_width int Session_id string url string }
解析json数据:
func DecodeDetect(data []byte) Faceslice{ var f Faceslice json.Unmarshal(data,&f) return f }
接着还是来写个get函数吧:
func get(url 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 }
调用face++接口并返回相应的数据:
const apiurl="https://apicn.faceplusplus.com" func DetectionDetect(picurl string)detection.Faceslice{ url:=apiurl+"/v2/detection/detect?url="+picurl+"&api_secret="+apisecret+"&api_key="+apikey tmp,_:=get(url) return detection.DecodeDetect(tmp) }
刚刚上面的示例只是简单考虑了文本信息,现在要传递的是图片信息,所以做个简单的修改:
type Request struct{ ToUserName string FromUserName string CreateTime time.Duration MsgType string Content string PicUrl string MsgId int }
Action函数里也该有所修改,判定下msg.MsgType,如果是text,则跟刚才一样处理,如果是image,则有新的处理方法。
我一个就做了两个简单的处理,一个是年龄、性别、种族,还有就是如果照片里是两个人,则给出五官及整体的相似度值。
相似度的代码直接放下面吧:
package recognition import( "encoding/json" ) type Compare struct{ Component_similarity struct{ Eye float64 Mouth float64 Nose float64 Eyebrow float64 } Session_id string Similarity float64 }
Recommend
-
166
人脸识别技术商用迎井喷期,专家建议完善行业标准保护隐私马晓澄张璇汪奥娜/经济参考报近期,“刷脸”成为了热词,人脸识别技术不断进入大众视野。苹果新机iPhoneX具备“刷脸”解锁功能,并且可运用到ApplePay以及各种需要身份验证的App中
-
121
妙投会员2017-10-30 17:26人脸识别技术将改写人类社会的游戏规则,这事会率先发生在中国文章所属专栏 深案例
-
121
奇客Solidot | 国有基金投资人脸识别创业公司 solidot新版网站常见问题,请点击这里查看。
-
132
website upgrading… 京ICP备1100...
-
123
从人脸识别到行人重识别,下一个风口 Original...
-
172
openFace 人脸识别框架测试 openface 人脸识别框架 ...
-
113
基于Emgu CV+百度人脸识别,实现视频动态 人脸抓取与识别 Original...
-
119
指纹识别真的要彻底被手机抛弃了吗?全面屏的流行似乎加速了指纹识别的死去,但如果屏下指纹能够快速量产并商用,或许是另外一番景象了。iPhoneX的推出已经说明了苹果的意图,那就是放弃TouchID,改用FaceID,而他们自己也表示,面容识别
-
120
近日,一位中国女子将iPhoneX退货,原因是她的iPhoneX竟然被同事轻松解锁了,有网友开玩笑表示,对于iPhoneX而言,每个中国人长得都一样。根据南华早报指出,这位南京女子致电苹果客服中心时,苹果并不相信她所说的。求助无门之下,她和同事一起前往附近的苹果商店...
-
111
上海的刘先生在iPhoneX手机发售不久后就为其太太购买了一台。之后发现,他们的儿子竟然刷脸就能解锁其母亲的手机。母子二人都能刷脸解锁。以下是来自上海电视台的报道:
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK