1. To register for Baidu AI, you need to obtain 3 things in this step. After these three things are obtained, there is a limit on the number of times and time. If you do academic research, you can ignore:
https://blog.csdn.net/qq_40484582/article/details/82054009 (
2) (1) AK: API_KEY;
(2) SK: SECRET_KEY;
(3) AT: access_token
If you get the first two, you can directly fill in this link to the browser (or cn.bing.com/?toHttps=1&redig=A4DDA4194C544F0B98D53B777EAB03A5), and you can find the access_token:
https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=【官网获取的AK】&client_secret=【官网获取的SK】
2. Call Baidu API (my environment is visual studio)
https://cloud.baidu.com/doc/OCR/s/vk3h7y58v
2.1 Download Baidu's SDK
http://ai.baidu.com/sdk#search
Because I am using C#, I downloaded several folders:
Baidu.Aip
├── net35(这个指的是.Net版本)
│ ├── AipSdk.dll // 百度AI服务 windows 动态库
│ ├── AipSdk.xml // 注释文件
│ └── Newtonsoft.Json.dll // 第三方依赖
├── net40
├── net45
└── netstandard2.0
├── AipSdk.deps.json
└── AipSdk.dll
2.2 VS call
Put .AipSdk.dll and Newtonsoft.Json.dll in the Debug folder of the current project.
Then add a reference in VS
2.3 Program call:
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.Net.Http;
using System.Net;
using System.Web;
// 通用文字识别(高精度含位置版)
public static string accurate()
{
string token = "24.08c4557c1c6ca46c71eacde1200fb911.2592000.1588499297.282335-19243980";
//string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate?access_token=" + token;
string host = "https://aip.baidubce.com/rest/2.0/ocr/v1/general?access_token=" + token;
Encoding encoding = Encoding.Default;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(host);
request.Method = "post";
request.KeepAlive = true;
// 图片的base64编码
string base64 = getFileBase64((System.IO.Directory.GetCurrentDirectory() + "test.jpg"));
String str = "image=" + HttpUtility.UrlEncode(base64);
byte[] buffer = encoding.GetBytes(str);
request.ContentLength = buffer.Length;
request.GetRequestStream().Write(buffer, 0, buffer.Length);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string result = reader.ReadToEnd();
Console.WriteLine("通用文字识别(高精度含位置版):");
Console.WriteLine(result);
return result;
}
public static String getFileBase64(String fileName)
{
FileStream filestream = new FileStream(fileName, FileMode.Open);
byte[] arr = new byte[filestream.Length];
filestream.Read(arr, 0, (int)filestream.Length);
string baser64 = Convert.ToBase64String(arr);
filestream.Close();
return baser64;
}