您的位置:首页 > 移动开发 > Unity3D

Unity 图像识别 接入Face++ 进行 场景识别 和 文字识别

2017-04-03 19:02 501 查看
首先需要去Face++注册一个账号:(https://console.faceplusplus.com.cn/login)注册非常简单这里就不讲了。创建完我们登录后悔进入如下界面。

接着我们去创建一个应用(也就是创建一个API Key,这个非常重要)如下:

现在我们去找到我们需要的API,找到图像识别中的Detect
Scene & Object API (Beta)如图:

点击
API文档进入当前API介绍和如何使用界面在这里我们只需要知道如何使用和返回值就行了:如图(它使用的是post方法)

到这一步准备工作就完成了,下面打开unity创建场景,这个过程就不讲了,如图:

直接上代码:

using UnityEngine;
using System.Collections;
using System.IO;
using LitJson;
using Vuforia;
using UnityEngine.UI;
using System.Text.RegularExpressions;

public class Demo : MonoBehaviour
{
public bool ImageOrWord = true;

public string ImageOrWordURL="";

//中英互译
string CHENUrl = "http://fanyi.youdao.com/openapi.do?keyfrom=UnityFY&key=自己的有道key&type=data&doctype=json&version=1.1&q=";

//相机/用于截图使用
public Camera Cam;

//按钮上的文本
public Text Btn_PaiZhaoText;

//显示结果
public GameObject ShowResult;

//物体信息数组
public ArrayList ObjArr = new ArrayList();
public ArrayList ObjArrKXD = new ArrayList();

//结果显示时间
public float myTimer = 2.0f;

ArrayList TempArr = new ArrayList ();
bool QueRen = false;
// Use this for initialization
void Start ()
{
CameraDevice.Instance.SetFocusMode(CameraDevice.FocusMode.FOCUS_MODE_CONTINUOUSAUTO);
}

// Update is called once per frame
void Update () {
if (myTimer > 0)
{
myTimer = myTimer - Time.deltaTime;
}
if (myTimer <= 0)
{
ShowResult.SetActive(false);
}
}

//提交数据进行识别
IEnumerator SendPost(string _url, WWWForm _wForm)
{
WWW postData = new WWW(_url, _wForm);
yield return postData;
if (postData.error != null)
{
Debug.Log(postData.error);
ShowResult.SetActive (true);
Btn_PaiZhaoText.text = "识别";
ShowResult.transform.Find ("Text").GetComponent<Text> ().text = "识别失败!";
GameObject.Find("DebugText").GetComponent<Text>().text = postData.error;
myTimer = 2.0f;
}
else
{
Btn_PaiZhaoText.text = "识别";
Debug.Log(postData.text);
GameObject.Find ("DebugText").GetComponent<Text> ().text = postData.text;
JsonJieXi (postData.text);
}
}

public void TestHttpSend()
{
if (ImageOrWord == true)
{
//识别图片场景中的物体
WWWForm form = new WWWForm ();
form.AddField("api_key", "你自己申请的api_key");
form.AddField("api_secret", "你自己申请的api_secret");
//form.AddField ("image_url", ImageOrWordURL);
form.AddField ("image_file", ImageOrWordURL);
StartCoroutine (SendPost ("https://api-cn.faceplusplus.com/imagepp/beta/detectsceneandobject", form));
}
else
{
//识别文字
WWWForm form = new WWWForm ();
form
4000
.AddField("api_key", "你自己申请的api_key");
form.AddField("api_secret", "你自己申请的api_secret");
form.AddField ("image_url", ImageOrWordURL);
StartCoroutine (SendPost ("https://api-cn.faceplusplus.com/imagepp/beta/recognizetext", form));
}
}

void JsonJieXi(string str)
{
JsonData jd = JsonMapper.ToObject (str);
//图片
if (QieHuanShiBieMoShiBool == true)
{
Debug.Log ("使用时间(time_used)" + jd ["time_used"].ToString ());
/*
if (jd ["scenes"].Count>0)
{
for (int i = 0; i < jd ["scenes"].Count; i++) {
Debug.Log ("场景信息数组-名称(scenes)" + jd ["scenes"] [i] ["value"].ToString ());
Debug.Log ("场景信息数组-置信度(scenes)" + jd ["scenes"] [i] ["confidence"].ToString ());
Debug.Log ("");
}
} else {
Debug.Log ("没有识别出场景");
}
*/
if (jd ["objects"].Count > 0) {
for (int i = 0; i < jd ["objects"].Count; i++) {
Debug.Log ("物体信息数组-名称(objects)" + jd ["objects"] [i] ["value"].ToString ());
Debug.Log ("物体信息数组-置信度(objects)" + jd ["objects"] [i] ["confidence"].ToString ());
Debug.Log ("");
ObjArr.Add (jd ["objects"] [i] ["value"].ToString ());//物体名称
ObjArrKXD.Add (float.Parse (jd ["objects"] [i] ["confidence"].ToString ()));//对应的可信度
}
//TempArr.Clear ();
ShowResultToUser (ObjArr, ObjArrKXD);

} else {
Debug.Log ("没有识别物体");
ShowResult.SetActive (true);
ShowResult.transform.Find ("Text").GetComponent<Text> ().text = "我不知道这是什么呢";
myTimer = 2.0f;
}
}
//文字
else
{
Debug.Log (jd ["result"].Count);
for (int i = 0; i < jd ["result"].Count; i++)
{
for (int j = 0; j < jd ["result"] [i] ["child-objects"].Count; j++)
{
Debug.Log (jd ["result"] [i] ["child-objects"][j]["type"].ToString());
Debug.Log (jd ["result"] [i] ["child-objects"][j]["value"].ToString());
}
//Debug.Log ("__________"+jd ["result"] [i] ["child-objects"]["type"].ToString());
//Debug.Log ("----------"+jd ["result"] [i] ["child-objects"]["value"].ToString());
}
}
}

/// <summary>
/// 正则表达式删除指定的字符串
/// <并且返回一个string类型>
/// <"value">原字符串>
/// <"delStr">需要去除的字符>
/// <"b">布尔值 如果为真表示去除标点
/// <@"\d"-表示删除字符串中的所有数字>
/// <@"[^\d]*"-表示删除字符串中的非数字>
/// </summary>
public static string RegexStringManipulation(string value,string delStr,bool b)
{
string str;
Regex re = new Regex (delStr);
str = re.Replace (value, "");
if (b == true) //去标点
{
str = Regex.Replace (str,@"\w+","");
}

return str;
}

void ShowResultToUser(ArrayList Arr,ArrayList Arra)
{

TempArr = Arra;

for (int i =0 ;i<Arr.Count;i++) {
Debug.Log ("Arr = "+Arr[i].ToString()+" 下标:"+i);
}

for (int j =0 ;j<Arra.Count;j++) {
Debug.Log ("Arra = "+Arra[j].ToString()+" 下标:"+j);
}

//排序取最大值
Arra.Sort ();
float Max =float.Parse( Arra [Arra.Count - 1].ToString());

Debug.Log ("MAX = "+Max);
if (Max > 92) {
QueRen = true;
} else {
QueRen = false;
}
if (Arra.Count > 1) {
for (int i = 0; i < TempArr.Count; i++) {
if (float.Parse (TempArr [i].ToString ()) == Max) {
Debug.Log (float.Parse (TempArr [i].ToString ()) + "在TempArr数组中 下标为 " + i);
if (i > 0) {
StartCoroutine (FanYi (Arr [i - 1].ToString ()));
}
break;
}
}
} else {
Debug.Log ("----------------"+Arr [0].ToString ());
StartCoroutine (FanYi (Arr [0].ToString ()));
}
}

//翻译
IEnumerator FanYi(string str)
{
WWW www = new WWW (CHENUrl + str);
yield return www;
if (www.error != null)
{
Debug.Log (">>>>>>>>>>>>>>"+www.error);
} else {
Debug.Log ("++++++++++++++"+www.text);
FanYiJSONJieXi (www.text);
}
}

/// <summary>
/// 翻译xml解析
/// </summary>
void FanYiJSONJieXi(string str)
{
//Debug.Log (str);
ShowResult.SetActive (true);
JsonData jd = JsonMapper.ToObject (str);
Debug.Log ("翻译结果:"+jd["translation"][0].ToString ());
if (QueRen == true) {
ShowResult.transform.Find ("Text").GetComponent<Text> ().text = "这一定是 " + jd ["translation"] [0].ToString () + " 呢!";
} else {
ShowResult.transform.Find ("Text").GetComponent<Text> ().text = "我猜这是 " + jd ["translation"] [0].ToString () + " 呢!";
}

myTimer = 2.0f;
}

/// <summary>
/// 拍照按钮
/// </summary>
public void Btn_JieTu()
{
Btn_PaiZhaoText.text = "识别中";
TempArr.Clear ();
ObjArr.Clear ();
ObjArrKXD.Clear ();
CaptureCamera (Cam,new Rect(0,0,Screen.width*0.5f,Screen.height*0.5f));
}

/// <summary>
/// 显示debug信息在屏幕上
/// </summary>
bool Btn_DebugBool = false;
public void Btn_Debug()
{
if (Btn_DebugBool == true) {
GameObject.Find ("DebugText").GetComponent<Text> ().enabled = false;
Btn_DebugBool = false;
} else {
GameObject.Find ("DebugText").GetComponent<Text> ().enabled = true;
Btn_DebugBool = true;
}
}

/// <summary>
/// 切换识别模式
/// </summary>
bool QieHuanShiBieMoShiBool = true;
public void Btn_QieHuanShiBieMoShi()
{
QieHuanShiBieMoShiBool =! QieHuanShiBieMoShiBool;
Debug.Log ("------"+QieHuanShiBieMoShiBool);

if (QieHuanShiBieMoShiBool == true) {
GameObject.Find ("Btn_ImageOrWord").transform.Find ("Text").GetComponent<Text> ().text = "图片";
} else {
GameObject.Find ("Btn_ImageOrWord").transform.Find ("Text").GetComponent<Text> ().text = "文字";
}
}

//截图并且识别
void CaptureCamera(Camera camera, Rect rect)
{
// 创建一个RenderTexture对象
RenderTexture rt = new RenderTexture((int)rect.width, (int)rect.height, 0);
// 临时设置相关相机的targetTexture为rt, 并手动渲染相关相机
camera.targetTexture = rt;
camera.Render();
//ps: --- 如果这样加上第二个相机,可以实现只截图某几个指定的相机一起看到的图像。
//ps: camera2.targetTexture = rt;
//ps: camera2.Render();
//ps: -------------------------------------------------------------------

RenderTexture.active = rt;
Texture2D screenShot = new Texture2D((int)rect.width, (int)rect.height, TextureFormat.RGB24,false);
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();

// 重置相关参数,以使用camera继续在屏幕上显示
camera.targetTexture = null;
RenderTexture.active = null;
GameObject.Destroy(rt);
// // 最后将这些纹理数据,成一个png图片文件
byte[] bytes = screenShot.EncodeToPNG();
string filename = Application.dataPath + "/Screenshot.png";
System.IO.File.WriteAllBytes(filename, bytes);
Debug.Log(string.Format("截屏了一张照片: {0}", filename));

if (QieHuanShiBieMoShiBool == true)
{
Debug.Log ("图片");
//识别图片场景中的物体
WWWForm form = new WWWForm ();
form.AddField("api_key", "你自己申请的api_key");
form.AddField("api_secret", "你自己申请的api_secret");
form.AddBinaryData ("image_file", screenShot.EncodeToPNG());
StartCoroutine (SendPost ("https://api-cn.faceplusplus.com/imagepp/beta/detectsceneandobject", form));
}
else
{
Debug.Log ("文字");
//识别文字
WWWForm form = new WWWForm ();
form.AddField("api_key", "你自己申请的api_key");
form.AddField("api_secret", "你自己申请的api_secret");
form.AddBinaryData ("image_file",screenShot.EncodeToPNG());
StartCoroutine (SendPost ("https://api-cn.faceplusplus.com/imagepp/beta/recognizetext", form));
}

}

}


这里我们用到LitJson.dll来解析返回的Json数据:(LitJson下载地址:链接: http://pan.baidu.com/s/1miOB6go 密码: ktcm)

完美!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息