[color=]特点:
[color=]不需要安装第三库
,调用的是Umi-OCR 的api
搜索来源 是https://www.52pojie.cn/thread-1931388-1-1.html
蓝奏云 https://hiroi-sora.lanzoul.com/s/umi-ocr (国内推荐,免注册/无限速)
GitHub https://github.com/hiroi-sora/Umi-OCR/releases/latest
打开软件 全局设置——高级——运行HTPP服务(勾选)
软件Umi-OCR实现思路:
1.把图片转换成txt(base64)
2.通过Umi-OCR 的api 发送 127.0.0.1
3.返回数据
r'C:\Users\rap\Desktop\52pj.png' 这个地址改成你们想实现转换的的图片
[Python] 纯文本查看 复制代码import requests
import json
import base64
class OCRClient:
def __init__(self, url):
self.url = url
def get_ocr_result(self, file_path):
# 读取文件内容并编码为base64字符串
with open(file_path, "rb") as image_file:
encoded_string = base64.b64encode(image_file.read()).decode('utf-8') # 确保转换为utf-8字符串
# 准备请求数据
data = {
"base64": encoded_string,
"options": {
"data.format": "text",
}
}
headers = {"Content-Type": "application/json"}
data_str = json.dumps(data)
# 发送POST请求
try:
response = requests.post(self.url, data=data_str, headers=headers)
response.raise_for_status() # 检查请求是否成功
res_dict = json.loads(response.text)
return res_dict['data']
except requests.RequestException as e:
return f"An error occurred: {e}"
# 使用OCRClient类
ocr_client = OCRClient("http://127.0.0.1:1224/api/ocr")
file_path = r'C:\Users\rap\Desktop\52pj.png'
data_value = ocr_client.get_ocr_result(file_path)
print(data_value)