美文网首页
Baidu.Aip.Ocr

Baidu.Aip.Ocr

作者: 萧非子 | 来源:发表于2017-11-24 18:45 被阅读172次

    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using Baidu.Aip.Ocr;
    using System.IO;
    using Newtonsoft.Json.Linq;
    //百度图片文字识别接口调用——非子萧
    public class OCRTest : MonoBehaviour
    {

    public Text debugInfo;                            // 显示debug信息
    public Text debugInfo2;                            // 显示debug信息
    public Image image2;
    public RawImage portrait;                         // 显示图片
    public Texture2D texture;                         // 用以描绘关键点的图片
    
    private Ocr client;                              // 用来调用百度AI接口
    private Form form;                              // 用来调用百度AI接口
    private byte[] image;                             // 检测的图像数据
    private Dictionary<string, object> options;       // 返回的数据
    private JObject result;                           // 接收返回的结果
    private string ApiKey = "G7h0xBIkf4xfR6NE1mfpZvF3";//此处填写自己申请的key
    private string SecretKey = "gKsyBaTKp8GDAeiqG6eNrHkxbbsT4MAN";//此处填写自己申请的key
    
    private void Awake()
    {
        //网页端身份安全验证失败,我们需要在程序运行时手动添加安全证书,在Awake方法中加入
        System.Net.ServicePointManager.ServerCertificateValidationCallback +=
            delegate (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                        System.Security.Cryptography.X509Certificates.X509Chain chain,
                        System.Net.Security.SslPolicyErrors sslPolicyErrors)
            {
                return true;           // always accept
            };
        // 此处填写自己申请的key("Api Key", "Secret Key");
        client = new Ocr(ApiKey, SecretKey);
         form = new Baidu.Aip.Ocr.Form(ApiKey, SecretKey);
    
    }
    private void Start()
    {       
    }
    //通用文字识别
    public void GeneralBasic()
        {
        //var client = new Baidu.Aip.Ocr.Ocr("Api Key", "Secret Key");
        //var image = File.ReadAllBytes("图片文件路径");
        // 通用文字识别
        //result = client.GeneralBasic(image);
        // 图片url
        // result = client.GeneralBasic("https://www.baidu.com/img/bd_logo1.png");
    
        var path = Application.dataPath + "/Images/tw03.jpg";
    
        var image = File.ReadAllBytes(path);
    
        result = client.GeneralBasic(image);
    
    
        debugInfo.text = result.ToString();
    
       Debug.Log("通用文字识别");
    
     
        }
    //通用文字带生僻字识别
    public void GeneralEnhanced()
    {
    var path = Application.dataPath + "/Images/tw23.jpg";
    
    var image = File.ReadAllBytes(path);
    
    result = client.GeneralEnhanced(image);
    
    debugInfo.text = result.ToString();
    
    Debug.Log("带生僻字识别");
    }
    //通用文字识别带位置版本
    public static void GeneralWithLocatin()
        {
            var client = new Baidu.Aip.Ocr.Ocr("Api Key", "Secret Key");
            var image = File.ReadAllBytes("图片文件路径");
    
            // 带位置版本
            var result = client.GeneralWithLocatin(image, null);
        }
    //网络图片识别
    public  void WebImage()
        {
        //    var client = new Baidu.Aip.Ocr.Ocr("Api Key", "Secret Key");
        //    var image = File.ReadAllBytes("图片文件路径");
    
        //    // 网图识别
        //    var result = client.WebImage(image, null);
    
        //var path = Application.dataPath + "/Images/tw07.jpg";
        //var path = "http://p1.so.qhmsg.com/t01d24fedf46b88f4c6.jpg";
    
        //var image = File.ReadAllBytes(path);
    
        //StartCoroutine(DownLoadMovie());
    
        //var image = www.bytes;
    
        //result = client.WebImage(image, null);
    
        //debugInfo.text = result.ToString();
    
        //Debug.Log("网图识别");
    }
    //private WWW www;
    ////协程
    //IEnumerator<WWW> DownLoadMovie()
    //{
    //    var url = "http://img1.3lian.com/2015/a1/67/d/144.jpg";
    //     www = new WWW(url);
    //    yield return www;  
    //}
    
    // 高精度识别
    public  void Accurate()
        {
        var path = Application.dataPath + "/Images/tw22.jpg";
    
        var image = File.ReadAllBytes(path);
    
        result = client.Accurate(image);
    
    
        debugInfo.text = result.ToString();
    
        Debug.Log("高精度识别");
    }
    //高精度识别(带位置信息)
    public  void AccurateWithLocation()
        {
        //var client = new Baidu.Aip.Ocr.Ocr("Api Key", "Secret Key");
        //var image = File.ReadAllBytes("图片文件路径");
    
        //// 高精度识别(带位置信息)
        //var result = client.AccurateWithLocation(image);
    
        var path = Application.dataPath + "/Images/tw13.jpg";
    
        var image = File.ReadAllBytes(path);
    
        result = client.AccurateWithLocation(image);
    
        debugInfo.text = result.ToString();
    
        Debug.Log("高精度识别带位置");
    }
    //银行卡识别
    public  void BankCard()
        {
    
        var path = Application.dataPath + "/Images/tw32.jpg";
    
        var image = File.ReadAllBytes(path);
    
        result = client.BankCard(image);
    
    
        debugInfo.text = result.ToString();
    
        Debug.Log("银行卡识别");
    }
    //身份证识别
    public  void Idcard()
    {
        var options = new Dictionary<string, object>()
        {
            {"detect_direction", "true"} // 检测方向
        };
    
    var path = Application.dataPath + "/Images/tw42.jpg";
    
    var image = File.ReadAllBytes(path);
    // 身份证正面识别
    result = client.IdCardFront(image, options);
    // 身份证背面识别
    //result = client.IdCardBack(image);
    
    debugInfo.text = result.ToString();
    Debug.Log("身份证识别");
    }
    //驾驶证识别
    public void DrivingLicense()
    {
    var path = Application.dataPath + "/Images/tw53.jpg";
    var image = File.ReadAllBytes(path);
    
    result = client.DrivingLicense(image);
    
    debugInfo.text = result.ToString();
    Debug.Log("驾驶证识别");
    }
    //行驶证识别
    public  void VehicleLicense()
    {
        var path = Application.dataPath + "/Images/tw51.jpg";
        var image = File.ReadAllBytes(path);
    
        result = client.VehicleLicense(image);
    
        debugInfo.text = result.ToString();
        Debug.Log("行驶证识别");
    }
    //车牌识别
    public void PlateLicense()
        {
        var path = Application.dataPath + "/Images/tw59.jpg";
        var image = File.ReadAllBytes(path);
    
        result = client.PlateLicense(image);
    
        debugInfo.text = result.ToString();
        Debug.Log("车牌识别");
    }
    //通用票据识别//用户向服务请求识别医疗票据、发票、的士票、保险保单等票据类图片中的所有文字,并返回文字在图中的位置信息。
    public void Receipt()
        {
        var path = Application.dataPath + "/Images/tw72.jpg";
        var image = File.ReadAllBytes(path);
    
        var options = new Dictionary<string, object>()
            {
                {"recognize_granularity", "big"}  // 定位单字符位置
            };
        result = client.Receipt(image, options);
    
        debugInfo.text = result.ToString();
        Debug.Log("通用票据识别");
    }
    //营业执照识别
    public void BusinessLicense()
        {
        var path = Application.dataPath + "/Images/tw81.jpg";
        var image = File.ReadAllBytes(path);
    
        result = client.BusinessLicense(image, null);
    
        debugInfo.text = result.ToString();
        Debug.Log("营业执照识别");
    }
    //表格识别
    public  void FormBegin()
    {
        form.DebugLog = false;  // 是否开启调试日志
    // Console.Write(result);
    var path = Application.dataPath + "/Images/tw60.jpg";
    var image = File.ReadAllBytes(path);
    
    result = form.BeginRecognition(image);
    
    debugInfo.text = result.ToString();
    Debug.Log("/表格识别");
    }
    //表格识别结果获取
    public  void FormGetResult()
        {
        var options = new Dictionary<string, object>()
            {
                {"result_type", "json"}  // 或者为excel
            };
         result = form.GetRecognitionResult("10432578_70376", options);
        // Console.Write(result);
        debugInfo.text = result.ToString();
        Debug.Log("/表格识别结果");
    }
    //表格识别为json
    public  void FormToJson()
        {
        var path = Application.dataPath + "/Images/tw61.jpg";
        var image = File.ReadAllBytes(path);
    
        form.DebugLog = false;  // 是否开启调试日志
        // 识别为Json
            result = form.RecognizeToJson(image);
        //Console.Write(result);
        debugInfo.text = result.ToString();
        Debug.Log("/表格识别为json");
    }
    //表格识别为Excel
    public void FormToExcel()
    {
    var path = Application.dataPath + "/Images/tw61.jpg";
    var image = File.ReadAllBytes(path);
    
    form.DebugLog = false;  // 是否开启调试日志
    
        // 识别为Excel
       result = form.RecognizeToExcel(image);
        // Console.Write(result);
        debugInfo.text = result.ToString();
        Debug.Log("/表格识别为Excel");
    
    }
    

    }

    相关文章

      网友评论

          本文标题:Baidu.Aip.Ocr

          本文链接:https://www.haomeiwen.com/subject/hdbfbxtx.html