Python基于百度AI实现OCR文字识别

  

Python基于百度AI实现OCR文字识别攻略

一、前置条件

  1. 注册百度AI,获取API Key和Secret Key

  2. 安装 Python3,并安装所需第三方库 requests

bash
pip install requests

二、百度AI接口调用

  1. 导入requests库

python
import requests

  1. 设置请求url和headers信息

python
# 设置请求url和headers信息
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
params = {"access_token": access_token}
headers = {"Content-Type": "application/x-www-form-urlencoded"}

其中,access_token为前置条件1获取到的API Key和Secret Key生成的token。

  1. 调用接口,传入需要识别的图片

```python
# 传入需要识别的图片
with open(image_file_path, "rb") as f:
img = base64.b64encode(f.read())

# 调用接口
response = requests.post(request_url, params=params, headers=headers, data={"image": img})
```

其中,image_file_path为需要识别的图片文件路径。

  1. 处理识别结果

python
# 处理识别结果
if response:
result = response.json()
if "words_result" in result:
for words_result in result["words_result"]:
print(words_result["words"])
else:
print("识别失败")

将识别结果打印输出即可。

三、示例

以下是两个包含示例的完整代码:

示例1:识别本地图片

import requests
import base64

# 设置API Key和Secret Key
client_id = "your_api_key"
client_secret = "your_secret_key"

# 获取access_token
token_url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + client_id + "&client_secret=" + client_secret
response = requests.post(token_url)
access_token = response.json()["access_token"]

# 设置请求url和headers信息
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
params = {"access_token": access_token}
headers = {"Content-Type": "application/x-www-form-urlencoded"}

# 传入需要识别的图片
image_file_path = "test.jpg"
with open(image_file_path, "rb") as f:
    img = base64.b64encode(f.read())

# 调用接口
response = requests.post(request_url, params=params, headers=headers, data={"image": img})

# 处理识别结果
if response:
    result = response.json()
    if "words_result" in result:
        for words_result in result["words_result"]:
            print(words_result["words"])
else:
    print("识别失败")

示例2:识别远程网络图片

import requests
import base64

# 设置API Key和Secret Key
client_id = "your_api_key"
client_secret = "your_secret_key"

# 获取access_token
token_url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=" + client_id + "&client_secret=" + client_secret
response = requests.post(token_url)
access_token = response.json()["access_token"]

# 设置请求url和headers信息
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"
params = {"access_token": access_token}
headers = {"Content-Type": "application/x-www-form-urlencoded"}

# 传入需要识别的图片
image_url = "http://www.example.com/test.jpg"
response = requests.get(image_url)
img = base64.b64encode(response.content)

# 调用接口
response = requests.post(request_url, params=params, headers=headers, data={"image": img})

# 处理识别结果
if response:
    result = response.json()
    if "words_result" in result:
        for words_result in result["words_result"]:
            print(words_result["words"])
else:
    print("识别失败")

以上就是Python基于百度AI实现OCR文字识别的完整攻略,希望能对大家有所帮助。

相关文章