Unity教程之faceplusplus免费人脸识别云服务

 

本篇教程我们来学习一篇高大上的技术,在unity3d中实现人脸识别,duang是不是很流弊,当然我们是借助第三方插件来实现的,嘿嘿!进入正文,先介绍下faceplusplus免费人脸识别云服务人脸实验室是 Face++ 联合开发者共同推出的产品展示平台,旨在为广大用户提供好玩有趣的人脸识别技术应用。

下面我们来看下在unity3d中人脸识别时如何调用api,废话不说,贴上代码

FaceTest类用作测试人脸识别


using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using System.IO;

public class FaceTest : MonoBehaviour
{
private string api_key = "yourkey";
private string api_secret = "yoursecret";
private string url = "http://apicn.faceplusplus.com/v2/detection/detect?";
private string parameters = "&attribute=glass,pose,gender,age,race,smiling";

private string imgName = "temp.jpg";

public Texture2D img = null;

// Use this for initialization
void Start()
{

}

// Update is called once per frame
void Update()
{

}

void OnGUI()
{
if (img != null)
{
GUI.DrawTexture(new Rect(0, 35, img.width, img.height), img);
}

if (GUI.Button(new Rect(0, 0, 100, 35), "OpenDialog"))
{
OpenFileName ofn = new OpenFileName();

ofn.structSize = Marshal.SizeOf(ofn);

ofn.filter = "图片文件(*.jpg*.png)\0*.jpg;*.png";

ofn.file = new string(new char[256]);

ofn.maxFile = ofn.file.Length;

ofn.fileTitle = new string(new char[64]);

ofn.maxFileTitle = ofn.fileTitle.Length;

ofn.initialDir = UnityEngine.Application.dataPath;//默认路径

ofn.title = "Open Project";

ofn.defExt = "JPG";

ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR

if (WindowDll.GetOpenFileName(ofn))
{
Debug.Log("Selected file with full path: {0}" + ofn.file);
File.Copy(ofn.file, UnityEngine.Application.streamingAssetsPath + "/temp.jpg");
Debug.Log("file://" + ofn.file);
StartCoroutine(LoadTexture("file://" + UnityEngine.Application.streamingAssetsPath + "/temp.jpg"));
}

}
}

IEnumerator LoadTexture(string localUrl)
{
WWW www = new WWW(localUrl);
yield return www;
img = www.texture;
byte[] pngData = www.texture.EncodeToJPG();
File.Delete(UnityEngine.Application.streamingAssetsPath + "/temp.jpg");
StartCoroutine(PostImage(pngData));
}

IEnumerator PostImage(byte[] imgBytes)
{
WWWForm form = new WWWForm();
form.AddField("api_key", api_key);
form.AddField("api_secret", api_secret);
form.AddBinaryData("img", imgBytes, "temp.jpg", "image/jpg");
form.AddField("attribute", "glass,pose,gender,age,race,smiling");
WWW www = new WWW(url,form);
yield return www;
Debug.Log(www.text);
}
}

OpenFileName类,实现打开图片功能


using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]

public class OpenFileName
{
public int structSize = 0;
public IntPtr dlgOwner = IntPtr.Zero;
public IntPtr instance = IntPtr.Zero;
public String filter = null;
public String customFilter = null;
public int maxCustFilter = 0;
public int filterIndex = 0;
public String file = null;
public int maxFile = 0;
public String fileTitle = null;
public int maxFileTitle = 0;
public String initialDir = null;
public String title = null;
public int flags = 0;
public short fileOffset = 0;
public short fileExtension = 0;
public String defExt = null;
public IntPtr custData = IntPtr.Zero;
public IntPtr hook = IntPtr.Zero;
public String templateName = null;
public IntPtr reservedPtr = IntPtr.Zero;
public int reservedInt = 0;
public int flagsEx = 0;
}

public class WindowDll
{
[DllImport("Comdlg32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Auto)]
public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
public static bool GetOpenFileName1([In, Out] OpenFileName ofn)
{
return GetOpenFileName(ofn);
}
}

ok,本篇unity教程到此结束,感谢围观!