百度AI接入身份证识别

首先你得注册一个账号,然后再认证,其实有一个《接入指南》的

http://ai.baidu.com/docs#/Begin/top

然后创建你想要的应用,记录appid, api_key, secret_key


再新建项目,并NgGet “baidu.ai”,安装


添加身份证识别的选项

代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp2_test_baidu_idcard_demo
{
    public partial class Form1 : Form
    {

        // 设置APPID/AK/SK
        string APP_ID = "114****11";
        string API_KEY = "c2jmHjrt4xL*****mLhGs";
        string SECRET_KEY = "57Z7HlxuYtymZbZEhLIjn*****9TC";

        Baidu.Aip.Ocr.Ocr client = null;


        public Form1()
        {
            InitializeComponent();


            client = new Baidu.Aip.Ocr.Ocr(API_KEY, SECRET_KEY);
            client.Timeout = 60000;  // 修改超时时间
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var token = getAccessToken(API_KEY, SECRET_KEY);
            IdcardDemo();
        }


        public String getAccessToken(string clientId, string clientSecret)
        {
            String authHost = "https://aip.baidubce.com/oauth/2.0/token";
            HttpClient client2 = new HttpClient();
            List<KeyValuePair<String, String>> paraList = new List<KeyValuePair<string, string>>();
            paraList.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
            paraList.Add(new KeyValuePair<string, string>("client_id", clientId));
            paraList.Add(new KeyValuePair<string, string>("client_secret", clientSecret));

            HttpResponseMessage response = client2.PostAsync(authHost, new FormUrlEncodedContent(paraList)).Result;
            String result = response.Content.ReadAsStringAsync().Result;
            Console.WriteLine(result);
            return result;
        }



        public void IdcardDemo()
        {
            var image = File.ReadAllBytes(@"G:\我自己\身份证\2s.jpg");
            var idCardSide = "back";

            // 调用身份证识别,可能会抛出网络等异常,请使用try/catch捕获
            var result = client.Idcard(image, idCardSide);
            Console.WriteLine(result);
            // 如果有可选参数
            var options = new Dictionary<string, object>{
            {"detect_direction", "true"},
            {"detect_risk", "false"}
            };

            // 带参数调用身份证识别
            result = client.Idcard(image, idCardSide, options);
            Console.WriteLine(result);
        }
    }
}

识别成功:




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赵之章

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值