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

本文分享如何使用腾讯优图的免费API在Unity中进行面部识别和证照识别。优图是腾讯的机器学习团队,提供包括人脸识别、图像识别在内的多种先进技术。作者通过封装官方C# API,成功应用于Unity的Android平台,并提供了部分代码示例和工程下载链接。

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

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

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


优图支持的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

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值