当前位置: 首页 > news >正文

百度OCR初探-python

百度OCR使用🚀


        在项目中需要对一些资料首先进行OCR识别,然后对OCR之后得到的结果进行结构化分析,各家的都打算简单尝试一下,首先尝试一下百度的OCR,首先找到百度的OCR的官方,开始自己搜索然后尝试。

OCR(Optical Character Recognition,光学字符识别)技术是一种将图片中的文字内容转换成机器可读的文本的技术。这种技术可以识别和转换各种来源的文本数据,如扫描文档、照片中的文字和PDF文件中的打印文字。OCR技术广泛应用于数据录入、自动化文档处理、车牌识别以及智能文档搜索等领域。


文章目录

  • 百度OCR使用🚀
  • 1.服务开通
  • 2.免费试用领取
  • 3.API使用
    • 3.1 access_token 获取
    • 3.2 OCR 识别python代码部分
  • 4.结束
  • 附加—付费开通(如果没有免费试用的话)

1.服务开通


https://cloud.baidu.com/product/ocr/general
在这里插入图片描述
点击立即使用,下面会跳出安全协议,划到下面然后,点我已阅读并同意即可。
在这里插入图片描述

2.免费试用领取


第一部分的协议点击确认同意之后,进入到当前页面,然后点这个免费尝鲜的去领取。
在这里插入图片描述
然后选择待领取接口点上全部,然后先下面的零元领取
在这里插入图片描述
然后出现领取成功的字样。
在这里插入图片描述
然后点击前往应用列表,点击创建应用,然后把上卖弄的应用名称填了。

在这里插入图片描述
然后再把应用归属选了自己测试的话选个个人就行,然后再随便填一下应用描述,点击立即创建。
在这里插入图片描述
然后点击返回应用列表。
在这里插入图片描述
然后就能看见自己的API Key 和 Serect Key了,把这两个key给保存下来,代码中会用到
在这里插入图片描述

3.API使用


3.1 access_token 获取

        在获得了API-Key和Serect-Key之后,接下来要使用这两个Key通过网络请求获得一个AccessToken,API使用的操作流程,参考通用文字识别的API文档,文档地址如下。
https://ai.baidu.com/ai-doc/OCR/1k3h7y3db
在这里插入图片描述
        然后下滑找到获取Access_token部分,然后选择自己获取access_token的方式,这里选择python,然后我又对python获取Access_token的代码简单的进行了一些改造,然后输出。
在这里插入图片描述

改造之后的代码如下:

import requests
import jsonapi_key = "自己的api_key"
secret_key = "自己的secret_key"def main():url = f"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={api_key}&client_secret={secret_key}"headers = {'Content-Type': 'application/json','Accept': 'application/json'}response = requests.post(url, headers=headers)  # 简化请求方式# 解析JSON响应体response_data = response.json()# 打印格式化后的JSONprint(json.dumps(response_data, indent=4))  # 添加indent参数来美化输出 indent=4 表示现实的时候每个键值对前缩进几个空格if __name__ == '__main__':main()

运行之后的结果如下,可以从打印输出部分找到access_token用于之后的APi调用
在这里插入图片描述

3.2 OCR 识别python代码部分

回到通用文字识别(高精度版)https://cloud.baidu.com/doc/OCR/s/1k3h7y3db
在这里插入图片描述
下划到请求代码示例的部分,然后找到这个python示例的代码
在这里插入图片描述


# encoding:utf-8import requests
import base64'''
通用文字识别(高精度版,高精度含位置版,标准版,标准含位置版)
切换不同的request_url就能使用不同版本的百度OCR识别
'''# 高精度版
# request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic"# 高精度含位置版
request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate"# 标准版
# request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic"# 标准含位置版
# request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general"# 二进制方式打开图片文件
f = open('长垣县人民医院体检报告单_00.png', 'rb')
img = base64.b64encode(f.read())params = {"image":img}
access_token = '*****************************************'  #换成自己的access_token
request_url = request_url + "?access_token=" + access_token
headers = {'content-type': 'application/x-www-form-urlencoded'}
response = requests.post(request_url, data=params, headers=headers)if response:print(response.json())

这里我找了一个病例文档进行测试,测试图片如下:
在这里插入图片描述

高精度版运行结果如下:

{'words_result': [{'words': '长垣县人民医院体检报告单'}, {'words': '体检日期:'}, {'words': '体检号:'}, {'words': '姓名'}, {'words': '性别'}, {'words': '年龄'}, {'words': '婚否'}, {'words': '电话或住址'}, {'words': '单位'}, {'words': '既往病史'}, {'words': '身高'}, {'words': '体重'}, {'words': '左'}, {'words': '裸眼'}, {'words': '矫正'}, {'words': '单色识别'}, {'words': '眼科'}, {'words': '右'}, {'words': '视力'}, {'words': '视力'}, {'words': '彩色及编号'}, {'words': '医生签字:'}, {'words': '其他眼疾'}, {'words': '血压:'}, {'words': 'mmhg'}, {'words': '心率:'}, {'words': '次/分'}, {'words': '内科'}, {'words': '脾:听诊'}, {'words': '性'}, {'words': '心:听诊'}, {'words': '性'}, {'words': '医生签字:'}, {'words': '肝:肋下'}, {'words': '及'}, {'words': '脾:肋下及'}, {'words': '其他:'}, {'words': '化验结果'}, {'words': '项目名称'}, {'words': '检查结果'}, {'words': '单位'}, {'words': '参考值'}, {'words': '项目名称'}, {'words': '检查结果'}, {'words': '单位'}, {'words': '参考值'}, {'words': '谷丙转氨酶'}, {'words': 'U/L'}, {'words': '0-40'}, {'words': '肺部【光检查:两肺】'}, {'words': '(未见可见)活动性病变。'}, {'words': '医生签字:'}, {'words': '以上体检结论:'}, {'words': '体检单位签章'}, {'words': '体检医师:'}], 'words_result_num': 55, 'log_id': 1801536176318950955}

高精度含位置版结果如下:
含位置版本,除了字符结果之外,还会有位置参数,类似于目标检测一类算法的输出结果,会给左上角的坐标点和宽高,默认识别页面的左上角坐标是(0,0)

{'words_result': [{'words': '长垣县人民医院体检报告单', 'location': {'top': 243, 'left': 673, 'width': 638, 'height': 62}}, {'words': '体检日期:', 'location': {'top': 352, 'left': 299, 'width': 194, 'height': 53}}, {'words': '体检号:', 'location': {'top': 352, 'left': 1116, 'width': 148, 'height': 53}}, {'words': '姓名', 'location': {'top': 468, 'left': 350, 'width': 88, 'height': 48}}, {'words': '性别', 'location': {'top': 467, 'left': 777, 'width': 90, 'height': 48}}, {'words': '年龄', 'location': {'top': 468, 'left': 1176, 'width': 92, 'height': 47}}, {'words': '婚否', 'location': {'top': 572, 'left': 350, 'width': 91, 'height': 48}}, {'words': '电话或住址', 'location': {'top': 569, 'left': 714, 'width': 225, 'height': 54}}, {'words': '单位', 'location': {'top': 678, 'left': 352, 'width': 89, 'height': 48}}, {'words': '既往病史', 'location': {'top': 781, 'left': 302, 'width': 183, 'height': 53}}, {'words': '身高', 'location': {'top': 890, 'left': 303, 'width': 88, 'height': 48}}, {'words': '体重', 'location': {'top': 890, 'left': 856, 'width': 91, 'height': 48}}, {'words': '左', 'location': {'top': 992, 'left': 529, 'width': 49, 'height': 53}}, {'words': '裸眼', 'location': {'top': 997, 'left': 623, 'width': 92, 'height': 46}}, {'words': '矫正', 'location': {'top': 994, 'left': 931, 'width': 92, 'height': 48}}, {'words': '单色识别', 'location': {'top': 992, 'left': 1195, 'width': 180, 'height': 54}}, {'words': '眼科', 'location': {'top': 1099, 'left': 303, 'width': 91, 'height': 47}}, {'words': '右', 'location': {'top': 1099, 'left': 529, 'width': 48, 'height': 51}}, {'words': '视力', 'location': {'top': 1101, 'left': 624, 'width': 91, 'height': 48}}, {'words': '视力', 'location': {'top': 1101, 'left': 930, 'width': 92, 'height': 48}}, {'words': '彩色及编号', 'location': {'top': 1099, 'left': 1191, 'width': 213, 'height': 50}}, {'words': '医生签字:', 'location': {'top': 1100, 'left': 1409, 'width': 185, 'height': 50}}, {'words': '其他眼疾', 'location': {'top': 1204, 'left': 531, 'width': 184, 'height': 53}}, {'words': '血压:', 'location': {'top': 1310, 'left': 532, 'width': 101, 'height': 50}}, {'words': 'mmhg', 'location': {'top': 1317, 'left': 787, 'width': 119, 'height': 49}}, {'words': '心率:', 'location': {'top': 1310, 'left': 1074, 'width': 102, 'height': 52}}, {'words': '次/分', 'location': {'top': 1312, 'left': 1289, 'width': 102, 'height': 50}}, {'words': '内科', 'location': {'top': 1415, 'left': 308, 'width': 88, 'height': 49}}, {'words': '脾:听诊', 'location': {'top': 1417, 'left': 530, 'width': 150, 'height': 53}}, {'words': '性', 'location': {'top': 1414, 'left': 796, 'width': 51, 'height': 54}}, {'words': '心:听诊', 'location': {'top': 1416, 'left': 1074, 'width': 176, 'height': 50}}, {'words': '性', 'location': {'top': 1414, 'left': 1313, 'width': 51, 'height': 54}}, {'words': '医生签字:', 'location': {'top': 1415, 'left': 1408, 'width': 187, 'height': 51}}, {'words': '肝:肋下', 'location': {'top': 1520, 'left': 531, 'width': 181, 'height': 53}}, {'words': '及', 'location': {'top': 1522, 'left': 832, 'width': 49, 'height': 52}}, {'words': '脾:肋下及', 'location': {'top': 1519, 'left': 1074, 'width': 319, 'height': 56}}, {'words': '其他:', 'location': {'top': 1626, 'left': 530, 'width': 103, 'height': 53}}, {'words': '化验结果', 'location': {'top': 1726, 'left': 898, 'width': 182, 'height': 53}}, {'words': '项目名称', 'location': {'top': 1860, 'left': 300, 'width': 158, 'height': 47}}, {'words': '检查结果', 'location': {'top': 1859, 'left': 496, 'width': 161, 'height': 48}}, {'words': '单位', 'location': {'top': 1860, 'left': 697, 'width': 80, 'height': 45}}, {'words': '参考值', 'location': {'top': 1859, 'left': 818, 'width': 118, 'height': 49}}, {'words': '项目名称', 'location': {'top': 1858, 'left': 977, 'width': 160, 'height': 49}}, {'words': '检查结果', 'location': {'top': 1858, 'left': 1178, 'width': 160, 'height': 49}}, {'words': '单位', 'location': {'top': 1860, 'left': 1377, 'width': 80, 'height': 46}}, {'words': '参考值', 'location': {'top': 1859, 'left': 1496, 'width': 119, 'height': 49}}, {'words': '谷丙转氨酶', 'location': {'top': 1970, 'left': 299, 'width': 235, 'height': 54}}, {'words': 'U/L', 'location': {'top': 1976, 'left': 629, 'width': 68, 'height': 46}}, {'words': '0-40', 'location': {'top': 1976, 'left': 783, 'width': 88, 'height': 45}}, {'words': '肺部【光检查:两肺】', 'location': {'top': 2074, 'left': 297, 'width': 444, 'height': 56}}, {'words': '(未见可见)活动性病变。', 'location': {'top': 2076, 'left': 862, 'width': 497, 'height': 54}}, {'words': '医生签字:', 'location': {'top': 2076, 'left': 1429, 'width': 192, 'height': 51}}, {'words': '以上体检结论:', 'location': {'top': 2180, 'left': 308, 'width': 279, 'height': 53}}, {'words': '体检单位签章', 'location': {'top': 2213, 'left': 1427, 'width': 236, 'height': 48}}, {'words': '体检医师:', 'location': {'top': 2380, 'left': 299, 'width': 194, 'height': 54}}], 'words_result_num': 55, 'log_id': 1801539915093494463}

标准版识别结果,时间上会快一些:

{'words_result': [{'words': '长垣县人民医院体检报告单'}, {'words': '体检日期:'}, {'words': '体检号:'}, {'words': '姓名'}, {'words': '性别'}, {'words': '年龄'}, {'words': '婚否'}, {'words': '电话或住址'}, {'words': '单位'}, {'words': '既往病史'}, {'words': '身高'}, {'words': '体重'}, {'words': '左'}, {'words': '裸眼'}, {'words': '矫正'}, {'words': '单色识别'}, {'words': '眼科'}, {'words': '右'}, {'words': '视力'}, {'words': '视力'}, {'words': '彩色及编号医生签字:'}, {'words': '其他眼疾'}, {'words': '血压:'}, {'words': 'mmhg'}, {'words': '心率:'}, {'words': '次/分'}, {'words': '内科'}, {'words': '脾:听诊'}, {'words': '性'}, {'words': '心:听诊'}, {'words': '性'}, {'words': '医生签字:'}, {'words': '肝:肋下'}, {'words': '及'}, {'words': '脾:肋下'}, {'words': '及'}, {'words': '其他:'}, {'words': '化验结果'}, {'words': '项目名称检查结果'}, {'words': '单位参考值项目名称'}, {'words': '检查结果单位参考值'}, {'words': '谷丙转氨酶'}, {'words': 'U/L'}, {'words': '0-40'}, {'words': '肺部【光检查:两肺】'}, {'words': '(未见可见)活动性病变。'}, {'words': '医生签字:'}, {'words': '以上体检结论:'}, {'words': '体检单位签章'}, {'words': '体检医师:'}], 'words_result_num': 50, 'log_id': 1801541714667838725}

标准含位置版识别结果:

{'words_result': [{'words': '长垣县人民医院体检报告单', 'location': {'top': 247, 'left': 675, 'width': 632, 'height': 52}}, {'words': '体检日期:', 'location': {'top': 348, 'left': 297, 'width': 201, 'height': 58}}, {'words': '体检号:', 'location': {'top': 350, 'left': 1115, 'width': 157, 'height': 54}}, {'words': '姓名', 'location': {'top': 464, 'left': 345, 'width': 101, 'height': 54}}, {'words': '性别', 'location': {'top': 464, 'left': 771, 'width': 101, 'height': 54}}, {'words': '年龄', 'location': {'top': 464, 'left': 1174, 'width': 97, 'height': 54}}, {'words': '婚否', 'location': {'top': 569, 'left': 345, 'width': 101, 'height': 54}}, {'words': '电话或住址', 'location': {'top': 569, 'left': 709, 'width': 232, 'height': 52}}, {'words': '单位', 'location': {'top': 674, 'left': 345, 'width': 101, 'height': 52}}, {'words': '既往病史', 'location': {'top': 779, 'left': 301, 'width': 187, 'height': 52}}, {'words': '身高', 'location': {'top': 887, 'left': 298, 'width': 101, 'height': 52}}, {'words': '体重', 'location': {'top': 885, 'left': 853, 'width': 101, 'height': 54}}, {'words': '左', 'location': {'top': 990, 'left': 526, 'width': 56, 'height': 52}}, {'words': '裸眼', 'location': {'top': 993, 'left': 619, 'width': 104, 'height': 52}}, {'words': '矫正', 'location': {'top': 990, 'left': 926, 'width': 104, 'height': 54}}, {'words': '单色识别', 'location': {'top': 993, 'left': 1192, 'width': 187, 'height': 52}}, {'words': '眼科', 'location': {'top': 1096, 'left': 298, 'width': 101, 'height': 54}}, {'words': '右', 'location': {'top': 1098, 'left': 526, 'width': 53, 'height': 52}}, {'words': '视力', 'location': {'top': 1098, 'left': 622, 'width': 97, 'height': 54}}, {'words': '视力', 'location': {'top': 1098, 'left': 926, 'width': 104, 'height': 52}}, {'words': '彩色及编号医生签字:', 'location': {'top': 1098, 'left': 1187, 'width': 413, 'height': 52}}, {'words': '其他眼疾', 'location': {'top': 1202, 'left': 526, 'width': 193, 'height': 54}}, {'words': '血压:', 'location': {'top': 1315, 'left': 528, 'width': 109, 'height': 43}}, {'words': 'mmhg', 'location': {'top': 1323, 'left': 787, 'width': 123, 'height': 39}}, {'words': '心率:', 'location': {'top': 1308, 'left': 1070, 'width': 115, 'height': 54}}, {'words': '次/分', 'location': {'top': 1308, 'left': 1283, 'width': 106, 'height': 54}}, {'words': '内科', 'location': {'top': 1411, 'left': 301, 'width': 101, 'height': 54}}, {'words': '脾:听诊', 'location': {'top': 1416, 'left': 528, 'width': 152, 'height': 52}}, {'words': '性', 'location': {'top': 1411, 'left': 794, 'width': 58, 'height': 57}}, {'words': '心:听诊', 'location': {'top': 1412, 'left': 1072, 'width': 237, 'height': 52}}, {'words': '性', 'location': {'top': 1413, 'left': 1312, 'width': 53, 'height': 52}}, {'words': '医生签字:', 'location': {'top': 1413, 'left': 1404, 'width': 197, 'height': 56}}, {'words': '肝:肋下', 'location': {'top': 1516, 'left': 527, 'width': 190, 'height': 60}}, {'words': '及', 'location': {'top': 1519, 'left': 827, 'width': 56, 'height': 54}}, {'words': '脾:肋下', 'location': {'top': 1522, 'left': 1070, 'width': 200, 'height': 52}}, {'words': '及', 'location': {'top': 1522, 'left': 1346, 'width': 50, 'height': 52}}, {'words': '其他:', 'location': {'top': 1624, 'left': 528, 'width': 109, 'height': 54}}, {'words': '化验结果', 'location': {'top': 1720, 'left': 897, 'width': 186, 'height': 57}}, {'words': '项目名称检查结果', 'location': {'top': 1860, 'left': 301, 'width': 400, 'height': 41}}, {'words': '单位参考值项目名称', 'location': {'top': 1860, 'left': 696, 'width': 480, 'height': 43}}, {'words': '检查结果单位参考值', 'location': {'top': 1860, 'left': 1176, 'width': 438, 'height': 43}}, {'words': '谷丙转氨酶', 'location': {'top': 1968, 'left': 301, 'width': 235, 'height': 52}}, {'words': 'U/L', 'location': {'top': 1972, 'left': 624, 'width': 75, 'height': 52}}, {'words': '0-40', 'location': {'top': 1978, 'left': 782, 'width': 89, 'height': 39}}, {'words': '肺部【光检查:两肺】', 'location': {'top': 2074, 'left': 298, 'width': 448, 'height': 52}}, {'words': '(未见可见)活动性病变。', 'location': {'top': 2074, 'left': 861, 'width': 498, 'height': 53}}, {'words': '医生签字:', 'location': {'top': 2077, 'left': 1427, 'width': 200, 'height': 52}}, {'words': '以上体检结论:', 'location': {'top': 2179, 'left': 306, 'width': 283, 'height': 52}}, {'words': '体检单位签章', 'location': {'top': 2215, 'left': 1427, 'width': 238, 'height': 43}}, {'words': '体检医师:', 'location': {'top': 2378, 'left': 301, 'width': 195, 'height': 54}}], 'words_result_num': 50, 'log_id': 1801542280884898583}

4.结束


总体用下来感觉还挺简单的,缺点是需要网络环境而且需要花钱,但是对于学习基本的OCR操作流程来说应该是一个不错的选择。

附加—付费开通(如果没有免费试用的话)

然后进入到如下界面,点一下通用文字识别的开通。
在这里插入图片描述
进入之后接某名称这里点一下全选这样之后就都能用了,之后底部还有一个协议需要勾选,然后点确认开通。
在这里插入图片描述
然后就提示你需要充钱了
在这里插入图片描述
然后选微信支付重10块钱意思一下。
在这里插入图片描述
好现在已经有10块钱了。
在这里插入图片描述

相关文章:

  • 怎么提升机器人外呼的转化效率
  • linux网络编程poll笔记
  • 实验12 路由重分布
  • STL-常用容器
  • Elastic 索引结构-倒排索引
  • 【AI绘画】Stable Diffusion 3开源
  • Linux3(进程 编辑文件 用户管理 网络)
  • C#A类调用B类的方法,在方法中更新B类的控件
  • c#中上传超过30mb的文件,接口一直报404,小于30mb的却可以上传成功
  • Java多线程-StampedLock(原子读写锁)
  • 小学生期刊知网//《小学生》评职称认可吗?
  • 笔记-前端
  • Android 列表视频滑动自动播放(实现思路)
  • 如何舒适的使用VScode
  • win10 双显卡,双显示器,VGA那个经常出现息屏(待机后无法唤醒),必须重启才能解决,(图文)手把手教你如何处理简单愉快的解决。
  • 【翻译】babel对TC39装饰器草案的实现
  • 【跃迁之路】【444天】程序员高效学习方法论探索系列(实验阶段201-2018.04.25)...
  • 2019年如何成为全栈工程师?
  • android高仿小视频、应用锁、3种存储库、QQ小红点动画、仿支付宝图表等源码...
  • CentOS7简单部署NFS
  • CSS 提示工具(Tooltip)
  • Docker下部署自己的LNMP工作环境
  • ES6之路之模块详解
  • HomeBrew常规使用教程
  • JavaScript 无符号位移运算符 三个大于号 的使用方法
  • JS基础之数据类型、对象、原型、原型链、继承
  • Mithril.js 入门介绍
  • mysql常用命令汇总
  • 从重复到重用
  • 发布国内首个无服务器容器服务,运维效率从未如此高效
  • 浮动相关
  • 深入浅出webpack学习(1)--核心概念
  • 使用Tinker来调试Laravel应用程序的数据以及使用Tinker一些总结
  • 携程小程序初体验
  • 一、python与pycharm的安装
  • 06-01 点餐小程序前台界面搭建
  • 大数据全解:定义、价值及挑战
  • ​埃文科技受邀出席2024 “数据要素×”生态大会​
  • #android不同版本废弃api,新api。
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • (003)SlickEdit Unity的补全
  • (1)(1.11) SiK Radio v2(一)
  • (4) PIVOT 和 UPIVOT 的使用
  • (C语言)fread与fwrite详解
  • (day6) 319. 灯泡开关
  • (poj1.2.1)1970(筛选法模拟)
  • (python)数据结构---字典
  • (第8天)保姆级 PL/SQL Developer 安装与配置
  • (附源码)ssm高校实验室 毕业设计 800008
  • (附源码)流浪动物保护平台的设计与实现 毕业设计 161154
  • (官网安装) 基于CentOS 7安装MangoDB和MangoDB Shell
  • (规划)24届春招和25届暑假实习路线准备规划
  • .net core开源商城系统源码,支持可视化布局小程序
  • .NET 分布式技术比较
  • .NET开源的一个小而快并且功能强大的 Windows 动态桌面软件 - DreamScene2