阿里云调用ocr文本识别接口
。
很高兴为您写代码! 以下是一个简单的阿里云OCR API调用代码示例:
```
import requests
import base64
def ocr_api_call(image_path):
# 阿里云OCR API接口地址
api_url = "https://ocrcp.market.alicloudapi.com/rest/160601/ocr/ocr_idcard.json"
# 您的阿里云API Key
appcode = "您的阿里云API Key"
headers = {
'Authorization': 'APPCODE ' + appcode,
'Content-Type': 'application/json; charset=UTF-8'
}
# 将图片文件读入内存并转换为base64编码
with open(image_path, "rb") as image_file:
image_data = base64.b64encode(image_file.read()).decode("utf-8")
# 将图片数据作为请求参数发送请求
data = {
"inputs": [
{
"image": {
"dataType": 50,
"dataValue": image_data
},
"configure": {
"dataType": 50,
"dataValue": "{\"side\":\"face\"}"
}
}
]
}
response = requests.post(api_url, headers=headers, json=data)
# 解析响应结果
result = response.json()
return result
# 调用OCR API
result = ocr_api_call("image.jpg")
print(result)
```
请注意,您需要在代码中替换为您的阿里云API Key。希望这个代码示例对您有所帮助!