Unity技术分享之调用Youtu实现智能AI图像处理人脸识别,证照识别等

最近因在研究图像解析的过程当中无心看到了腾讯优图免费API的事情,就是每天P图的技术支持,感受挺有意思的,官方的C#API是:根(qq418871901)在维护.web

腾讯优图是腾讯旗下顶级的机器学习研发团队,专一于图像处理、模式识别、深度学习。
在人脸识别、图像识别、医疗AI、OCR、哼唱识别、语音合成等领域都积累了领先的技术水平和完整解决方案。json

根据官方提供的C#API针对Unity作了一下方法封装,而且测试是支持Unity的平台的,个人就是应用在Android端了,
这里简单说明部分代码,项目工程还请点击下方连接下载.app


优图支持的API : ↓

人脸识别 人体识别 图像识别 文字识别 音频技术 图像处理
人脸检测与分析 人像分割 图像内容识别 通用印刷体文字识别 歌曲识别 人脸融合
人脸验证 自拍场景手势识别 智能鉴黄 身份证识别 声伴分离 智能P图
人脸检索 暴恐图片识别 名片识别 语音合成
人脸聚类 车辆属性识别 营业执照识别
活体检测 AlphaAR 银行卡识别
人脸关键点定位与追踪 车牌识别
驾驶证识别
行驶证识别
通用手写体文字识别

Unity中面部融合的演示效果 ↓

面部融合案例展现

代码展现(仅展现部份内容) ↓

/************************************************************************* *****开发人员 : #月儿圆# *****修改日期 : 2018.02.23 *****描述信息 : 面部识别应用 *************************************************************************/

using UnityEngine;
using TencentYoutuYun.SDK.Csharp;
using UnityEngine.UI;
using System.Collections.Generic;

public class FacialRecognition : MonoBehaviour {

    private static FacialRecognition instance;
    public static FacialRecognition Instance {
        get {
            return instance;
        } 
    }

    public Text textLog;
    public RawImage rawImage;
    public string template= "cf_yuren_cungu";

    private void Awake()
    {
        instance=this;
    }

    void Start () {
        // 设置为你本身的密钥对
        string appid = "这里是你的APPID";
        string secretId = "这里是你的secretId";
        string secretKey = "这里是你的secretKey";
        string userid = "这里是你的QQ号";

        Conf.Instance().setAppInfo(appid, secretId, secretKey, userid, Conf.Instance().YOUTU_END_POINT);

        string path =
#if UNITY_ANDROID
            Application.streamingAssetsPath + "/test.jpg";
#else
        "file:///"+Application.streamingAssetsPath+ "/test.jpg";
#endif
        if (Application.platform == RuntimePlatform .WindowsEditor)
        {
        YoutuFunc(Application.streamingAssetsPath + "/test.jpg");
        }
    }

    public void YoutuFunc(string path) {

        //// 人脸对比
        //result = Youtu.facecompare(path, path2);
        //print(result);

        //// 人脸关键点定位 调用demo
        //result = Youtu.faceshape(path);
        //print(result);

        //result = Youtu.getpersonids("group");
        //print(result);

        //// 名片OCR
        //path = System.IO.Directory.GetCurrentDirectory() + "\\ocr_card_01.jpg";
        //result = Youtu.bcocr(path);
        //print(result);

        //// 通用OCR
        //result = Youtu.generalocrurl("http://open.youtu.qq.com/app/img/experience/char_general/ocr_common01.jpg");
        //print(result);

        //// 行驶证OCR
        //path = System.IO.Directory.GetCurrentDirectory() + "\\ocr_xsz_01.jpg";
        //result = Youtu.driverlicenseocr(path, 0);
        //print(result);

        ////多人脸检索
        //List<string> group_ids = new List<string>();
        //result = Youtu.multifaceidentifyurl("http://open.youtu.qq.com/app/img/experience/face_img/face_05.jpg?v=1.0", "test", group_ids, 5, 40);
        //print(result);

        /////识别一个图像是否为暴恐图像
        //result = Youtu.imageterrorismurl("http://open.youtu.qq.com/app/img/experience/terror/img_terror01.jpg");
        //print(result);

        /////自动地检测图片车身以及识别车辆属性
        //result = Youtu.carcalssifyurl("http://open.youtu.qq.com/app/img/experience/car/car_01.jpg");
        //print(result);

        /////银行卡OCR识别,根据用户上传的银行卡图像,返回识别出的银行卡字段信息。
        //result = Youtu.creditcardocrurl("http://open.youtu.qq.com/app/img/experience/char_general/ocr_card_1.jpg");
        //print(result);

        /////营业执照OCR 识别,根据用户上传的营业执照图像,返回识别出的注册号、公司名称、地址字段信息
        //result = Youtu.bizlicenseocrurl("http://open.youtu.qq.com/app/img/experience/char_general/ocr_yyzz_01.jpg");
        //print(result);

        /// 车牌OCR识别,根据用户上传的图像,返回识别出的图片中的车牌号。
        //result = Youtu.plateocrurl("http://open.youtu.qq.com/app/img/experience/char_general/ocr_license_1.jpg");
        //print(result);

        /// 人脸融合,根据用户上传的图像,返回融合后的图像。
        string result = string.Empty;
        try
        {
            result = Youtu.faceMerge(path, "base64", "[{\"cmd\":\"doFaceMerge\",\"params\":{\"model_id\":\"" + template + "\"}}]");

            ShowRawimage(result, rawImage);
        }
        catch (System.Exception e)
        {
            textLog.text = e.Message;
        }

        List<string> group_ids = new List<string>();
        result = Youtu.multifaceidentify(path, "test", group_ids, 5, 40);

        JsonParse.Multifaceidentify mu=JsonParse.Multifaceidentify.ParseMultifaceidentify(result);

        textLog.text += "\n面部坐标:"+ mu.results[0].face_rect.x.ToString()+";"+ mu.results[0].face_rect.y;
        textLog.text += "\n面部宽高:"+ mu.results[0].face_rect.width.ToString()+";"+ mu.results[0].face_rect.height;
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit();
        }
    }

    void ShowRawimage(string json,RawImage raw) {
        JsonParse.FaceMerge faceMerge = JsonParse.FaceMerge.ParseJsonFaceMerge(json);
        if (faceMerge.img_base64!=null)
        {
            raw.texture = SCY.Utility.Base64Img(faceMerge.img_base64);
            raw.SetNativeSize();
        }
        else if (faceMerge.img_url!=null)
        {
            Debug.Log("图片地址");
            Application.OpenURL(faceMerge.img_url);
        }
        textLog.text += faceMerge.msg;
    }


    void LoadTextFromBase64(string base64,RawImage rawImage) {

        Texture2D pic = new Texture2D(500, 500);
        byte[] data = System.Convert.FromBase64String(base64);
        pic.LoadImage(data);

        rawImage.texture = pic;
        rawImage.SetNativeSize();
        rawImage.transform.localScale = Vector3.one * 0.3f;
    }
}
/************************************************************************* *****开发人员 : #月儿圆# *****修改日期 : 2018.02.23 *****描述信息 : 功能脚本 *************************************************************************/
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Text;
using UnityEngine;
using UnityEngine.UI;

namespace SCY
{

    public class Utility
    {
        public static Texture2D Base64Img(string Base64String)
        {
            Texture2D pic = new Texture2D(500, 500);
            byte[] data = System.Convert.FromBase64String(Base64String);
            pic.LoadImage(data);
            return pic;
        }

        public static string ByteArrToStr(byte[] byteArray) {
            return Encoding.UTF8.GetString(byteArray);
        }

        //public static Bitmap GetWebImage(string url)
        //{
        // Bitmap bitmap = null;
        // HttpWebResponse response = null;
        // try
        // {
        // Uri requestUri = new Uri(url);
        // HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
        // request.Timeout = 0x2bf20;
        // request.Method = "GET";
        // response = (HttpWebResponse)request.GetResponse();
        // bitmap = new Bitmap(response.GetResponseStream());
        // }
        // catch (Exception)
        // {
        // throw new Exception("图片读取出错!");
        // }
        // finally
        // {
        // response.Close();
        // }
        // return bitmap;
        //}

        public static byte[] JoinByteArr(byte[] byte1, byte[] byte2)
        {
            byte[] buffer = new byte[byte1.Length + byte2.Length];
            Stream stream = new MemoryStream();
            stream.Write(byte1, 0, byte1.Length);
            stream.Write(byte2, 0, byte2.Length);
            stream.Position = 0L;
            if (stream.Read(buffer, 0, buffer.Length) <= 0)
            {
                throw new Exception("读取错误!");
            }
            return buffer;
        }

        public static byte[] StrToByteArr(string str) {
        return    Encoding.UTF8.GetBytes(str);
        }

        public static string UnixTime(double expired = 0.0)
        {
            long num = (DateTime.Now.AddSeconds(expired).ToUniversalTime().Ticks - 0x89f7ff5f7b58000L) / 0x989680L;
            return num.ToString();
        }
        public static string ImgBase64(string path, bool isWebImg = false)
        {
            string base64Info = "";
            //if (!System.IO.File.Exists(path))
            //{
            // throw new Exception("文件不存在!");
            //}
            if (Application.platform == RuntimePlatform.Android)
            {
                base64Info = Convert.ToBase64String(GetPictureData(path.Replace("//jar:file://", "jar:file:///").Replace("!","//!")));
            }
            else if (Application.platform == RuntimePlatform.WindowsEditor)
            {
                base64Info= Convert.ToBase64String(GetPictureData(path));
            }
            return base64Info;
        }

        //参数是图片的路径 
        public static byte[] GetPictureData(string imagePath)
        {
            FileStream fs = new FileStream(imagePath, FileMode.Open);
            byte[] byteData = new byte[fs.Length];
            fs.Read(byteData, 0, byteData.Length);
            fs.Close();
            return byteData;
        }

        public static IEnumerator LoadTextureFromPath(string path , RawImage raw)
        {
                WWW www = new WWW(path);
                yield return www;
                raw.texture = www.texture;
        }

        public static IEnumerator LoadTextureFromBase64(string bese64, RawImage raw)
        {
            WWW www = new WWW(bese64);
            yield return www;
            raw.texture = www.texture;
        }
    }
}

工程下载 ↓

https://download.csdn.net/download/s15100007883/10406790机器学习