作者:小5聊基础
简介:一只喜欢全栈方向的程序员,欢迎咨询,尽绵薄之力答疑解惑
编程原则:Write Less Do More
源码-camera组件-1526881707172
3、在默认组件的拍照按钮的单击事件上添加如下代码
wx.uploadFile({
url: 'http://166.61.66.66/Home/Upload',
filePath: res.tempImagePath,
name: 'file',
header: { 'content-type': 'multipart/form-data' },
method: 'POST',
formData: {'user': 'test'},
success: function (res) {
var data = res.data;
console.log(data);
var _json = JSON.parse(JSON.parse(data));
tishi(1,_json);
},
fail: function (res) {
tishi(3,res);
}
})
function tishi(_t,_v){
if (_t == 1 && _v.result!=null){
wx.showModal({
title: '提示',
content: '你的颜值分:' + parseInt(_v.result.face_list[0].beauty),
success: function (res) {
}
})
}
else{
wx.showModal({
title: '提示',
content: 'type:' + _t + "=" + (_v.errMsg == undefined ? _v.error_msg : _v.errMsg),
success: function (res) {
}
})
}
}
4、下载百度人脸识别
1)人脸识别技术文档
http://ai.baidu.com/docs#/Face-Detect-V3/top\
https://cloud.baidu.com/doc/FACE/Face-Csharp-SDK.html#.E4.BA.BA.E8.84.B8.E6.A3.80.E6.B5.8B\
2)百度云 - 接口需要进入这里创建
5、C# MVC 调用人脸识别源码(api调用暂时未实现)
#region 接收Base64编码格式的图片
[HttpPost]
public JsonResult Upload()
{
string _fileAllPath = "";
HttpPostedFileBase flogo = Request.Files["file"];
if (flogo.ContentLength != 0) {
string name = flogo.FileName; //获取后缀名
string namejpg = Path.GetExtension(name).ToLower();
string path = Server.MapPath("../upload/"); //获取上传的路径
string gid = System.Guid.NewGuid().ToString(); //生成一个新的文件名
string newname = gid + namejpg; //上传
_fileAllPath = path + newname;
flogo.SaveAs(_fileAllPath);
}
JObject _result= DetectDemo(_fileAllPath); //人脸识别验证
return Json(_result.ToString());
}
#endregion
public JObject DetectDemo(string _imgpath)
{
// 设置APPID/AK/SK
//var APP_ID = "11116994";
var API_KEY = "百度云申请得到的KEY";
var SECRET_KEY = "百度云申请得到的KEY";
var client = new Baidu.Aip.Face.Face(API_KEY, SECRET_KEY);
client.Timeout = 60000; // 修改超时时间
var image = _imgpath;
image = ImgToBase64String(_imgpath);
var imageType = "URL"; //需要公网可访问的图片
imageType = "BASE64";
// 调用人脸检测,可能会抛出网络等异常,请使用try/catch捕获
var result = client.Detect(image, imageType);
Console.WriteLine(result);
// 如果有可选参数 {"face_field", "age","beauty","expression","face_type","glasses","gender"},
var options = new Dictionary < string, object>{
{"face_field", "beauty" },
{ "max_face_num", 2 },
{ "face_type", "LIVE" },
};
// 带参数调用人脸检测
result = client.Detect(image, imageType, options);
return result;
}
//imgage图片转base64字符
protected string ImgToBase64String(string Imagefilename)
{
try {
Bitmap bmp = new Bitmap(Imagefilename);
MemoryStream ms = new MemoryStream();
bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] arr = new byte[ms.Length];
ms.Position = 0;
ms.Read(arr, 0, (int)ms.Length);
ms.Close();
return Convert.ToBase64String(arr);
}
catch (Exception ex) {
return null;
}
}
6、小程序本地开发调试和手机预览,建议进行如下操作
1)微信开发者工具-点击详情-在项目设置:勾选上-不校验安全域名
2)手机预览,需要打开调试模式才能请求到IP地址
3)如果以上两者都无法访问,需要检查ip设置是否正确,以及IP服务器是否被防火墙屏蔽了