一.开通移动云OCR服务
在下面这个网址开通免费服务,,每个账号可免费使用500次,先要实名认证。
通用文字识别 (10086.cn)https://ecloud.10086.cn/home/product-introduction/Generalverify
有两种方式:
这里选择第二种 。
二.下载python_sdk测试代码
Python_SDK下载 (10086.cn)https://ecloud.10086.cn/op-help-center/doc/article/40776然后在pycharm中打开上面下载好的文件。
三.测试OCR功能
1.打开文件
打开OCRTest.py文件:
里面的每个函数都是开通的OCR服务类型,你开通哪项服务,就配置哪个函数,对照表:
产品名称 |
服务名称 |
接口名称 |
请求路径 |
通用文字识别 | 通用类识别 |
网络图片文字识别 |
/api/ocr/v1/webimage |
通用印刷体识别 |
/api/ocr/v1/general |
||
通用手写体识别 |
/api/ocr/v1/handwriting |
||
视频文字识别(视频上传) |
/api/ocr/v1/videoupload |
||
视频文字识别(视频文字识别结果查询) |
/api/ocr/v1/videoresult |
||
防疫核酸报告识别 |
/api/ocr/v1/nat |
||
文档类识别 |
文档识别 |
/api/ocr/v1/generic |
|
表格识别 |
/api/ocr/v1/form |
||
卡证识别 | 卡证识别 |
身份证识别 |
/api/ocr/v1/idcard |
营业执照识别(标准版) |
/api/ocr/v1/businesslicense |
||
营业执照识别(特殊版) |
/api/ocr/v1/enterpriselicense |
||
银行卡识别 |
/api/ocr/v1/bankcard |
||
名片识别 |
/api/ocr/v1/businesscard |
||
票据识别 | 票据识别 |
火车票识别 |
/api/ocr/v1/trainticket |
增值税发票识别 |
/api/ocr/v1/invoice |
||
出租车发票识别 |
/api/ocr/v1/taxiinvoice |
||
定额发票识别 |
/api/ocr/v1/fixedinvoice |
||
机票行程单识别 |
/api/ocr/v1/flights |
||
过路费发票识别 |
/api/ocr/v1/toll |
||
混贴票据识别 |
/api/ocr/v1/mixedbills |
||
行业文字识别 | 资产类识别 |
印章检测 |
/api/ocr/v1/seal |
印章识别 |
/api/ocr/v1/sealrecognition |
||
汽车类识别 |
车牌识别 |
/api/ocr/v1/licenseplate |
|
行驶证识别 |
/api/ocr/v1/driving |
||
驾驶证识别 |
/api/ocr/v1/drive |
||
VIN码识别 |
/api/ocr/v1/vincode |
||
仪器仪表类识别 |
电表识别 |
/api/ocr/v1/ammeter |
|
教育类识别 |
公式识别 |
/api/ocr/v1/formula |
|
自定义模板识别 |
自定义模板识别 |
自定义模板识别 |
/api/ocr/v1/selfdefinition |
智能结构化识别 |
智能结构化识别 |
/api/ocr/v1/smartstructure |
2.配置函数
比如我开通的服务是网络图片文字识别,配置的函数是:
def request_webimage_file():
print("请求File参数")
requesturl = '/api/ocr/v1/webimage' #填写对照表中的api地址
#填写要识别图片的路径,代码包里提供了两张图片
imagepath = 'C:\\Users\\zhuli\\Desktop\\smartlib_python_sdk\\test_code\\sfz.jpg'
try:
ocr_client = CMSSEcloudOcrClient(accesskey, secretkey, url)
response = ocr_client.request_ocr_service_file(requestpath=requesturl, imagepath= imagepath)
print(response.text)
except ValueError as e:
print(e)
- 第1行,引入我们的CMSSEcloudOcrClient类
- 第3和第4行为用户的AK、SK信息
- 第7行为requesturl参数为接口请求地址
- 第8行为请求图片的路径
- 第11行发起https请求,示例中request_ocr_service_file接收的图片的路径,同时还支持request_ocr_service_url(接收URL参数)和request_ocr_service_base64(接收图片base64编码)
- 第12行打印响应内容
3.配置移动云API调用所需的“AccessKey”、“SecretKey”
进入该网址:https://ecloud.10086.cn/api/page/op-usercenter-static/#/aksk?productType=accesskeyhttps://ecloud.10086.cn/api/page/op-usercenter-static/#/aksk?productType=accesskey
点击左上角的创建可创建密钥,会生成一个excel文件,里面有“AccessKey”、“SecretKey”。
找到OCRTest.py的这两行:
将密钥填入对应的位置。
然后在终端输入:
python setup.py install
4.图片识别
运行OCRTest.py文件,终端输出:
"C:\Users\ysy2001 0615\Desktop\PythonSDK\Python_SDK\venv\Scripts\python.exe" "C:\Users\ysy2001 0615\Desktop\PythonSDK\Python_SDK\test_code\OCRTest.py"
请求File参数
{"requestId":"reqId-db2d0b5745d0a3dbf1689-9e8075e5-2","state":"OK","body":{"content":{"prism_wnum":11,"prism_wordsInfo":[{"prob":0.6822017431259155,"position":[{"x":64,"y":37},{"x":21,"y":39},{"x":21,"y":27},{"x":63,"y":25}],"word":"姓名","chars":[{"pos":[{"x":24,"y":25},{"x":36,"y":25},{"x":36,"y":39},{"x":24,"y":39}],"confidence":0.0956088437898568,"char":"姓"},{"pos":[{"x":39,"y":25},{"x":51,"y":25},{"x":51,"y":39},{"x":39,"y":39}],"confidence":0.08598547386547697,"char":"名"}]},{"prob":0.7948694229125977,"position":[{"x":99,"y":38},{"x":57,"y":38},{"x":57,"y":23},{"x":100,"y":23}],"word":"韦小宝","chars":[{"pos":[{"x":60,"y":23},{"x":72,"y":23},{"x":72,"y":38},{"x":60,"y":38}],"confidence":0.08316039122068702,"char":"韦"},{"pos":[{"x":70,"y":23},{"x":83,"y":23},{"x":83,"y":38},{"x":70,"y":38}],"confidence":0.09125237194941925,"char":"小"},{"pos":[{"x":81,"y":23},{"x":93,"y":23},{"x":93,"y":38},{"x":81,"y":38}],"confidence":0.09230028952018327,"char":"宝"}]},{"prob":0.6656411290168762,"position":[{"x":75,"y":65},{"x":20,"y":65},{"x":20,"y":47},{"x":75,"y":47}],"word":"性别男","chars":[{"pos":[{"x":21,"y":47},{"x":37,"y":47},{"x":37,"y":65},{"x":21,"y":65}],"confidence":0.0872939909963341,"char":"性"},{"pos":[{"x":31,"y":47},{"x":47,"y":47},{"x":47,"y":65},{"x":31,"y":65}],"confidence":0.07940399219819497,"char":"别"},{"pos":[{"x":49,"y":47},{"x":65,"y":47},{"x":65,"y":65},{"x":49,"y":65}],"confidence":0.08107724526057385,"char":"男"}]},{"prob":0.7900686860084534,"position":[{"x":131,"y":63},{"x":88,"y":61},{"x":89,"y":47},{"x":132,"y":49}],"word":"民族汉","chars":[{"pos":[{"x":89,"y":47},{"x":102,"y":47},{"x":102,"y":63},{"x":89,"y":63}],"confidence":0.09217217634938077,"char":"民"},{"pos":[{"x":102,"y":47},{"x":114,"y":47},{"x":114,"y":63},{"x":102,"y":63}],"confidence":0.09340092631165517,"char":"族"},{"pos":[{"x":114,"y":47},{"x":127,"y":47},{"x":127,"y":63},{"x":114,"y":63}],"confidence":0.09088020316953409,"char":"汉"}]},{"prob":0.8024003505706787,"position":[{"x":61,"y":84},{"x":22,"y":84},{"x":22,"y":71},{"x":61,"y":72}],"word":"出生","chars":[{"pos":[{"x":23,"y":71},{"x":34,"y":71},{"x":34,"y":84},{"x":23,"y":84}],"confidence":0.07454915327918127,"char":"出"},{"pos":[{"x":35,"y":71},{"x":47,"y":71},{"x":47,"y":84},{"x":35,"y":84}],"confidence":0.0838299437269847,"char":"生"}]},{"prob":0.8024003505706787,"position":[{"x":159,"y":85},{"x":56,"y":86},{"x":56,"y":72},{"x":159,"y":71}],"word":"1654年12月20日","chars":[{"pos":[{"x":57,"y":71},{"x":63,"y":71},{"x":63,"y":86},{"x":57,"y":86}],"confidence":0.08048306484940677,"char":"1"},{"pos":[{"x":60,"y":71},{"x":66,"y":71},{"x":66,"y":86},{"x":60,"y":86}],"confidence":0.07077925460296725,"char":"6"},{"pos":[{"x":62,"y":71},{"x":68,"y":71},{"x":68,"y":86},{"x":62,"y":86}],"confidence":0.07927936587835137,"char":"5"},{"pos":[{"x":65,"y":71},{"x":71,"y":71},{"x":71,"y":86},{"x":65,"y":86}],"confidence":0.08434467112840616,"char":"4"},{"pos":[{"x":70,"y":71},{"x":76,"y":71},{"x":76,"y":86},{"x":70,"y":86}],"confidence":0.08105277973402224,"char":"年"},{"pos":[{"x":75,"y":71},{"x":81,"y":71},{"x":81,"y":86},{"x":75,"y":86}],"confidence":0.08495977211713442,"char":"1"},{"pos":[{"x":78,"y":71},{"x":84,"y":71},{"x":84,"y":86},{"x":78,"y":86}],"confidence":0.09189763192040779,"char":"2"},{"pos":[{"x":82,"y":71},{"x":88,"y":71},{"x":88,"y":86},{"x":82,"y":86}],"confidence":0.07305568947744293,"char":"月"},{"pos":[{"x":87,"y":71},{"x":93,"y":71},{"x":93,"y":86},{"x":87,"y":86}],"confidence":0.07719586748106957,"char":"2"},{"pos":[{"x":89,"y":71},{"x":95,"y":71},{"x":95,"y":86},{"x":89,"y":86}],"confidence":0.08392865783456832,"char":"0"},{"pos":[{"x":94,"y":71},{"x":100,"y":71},{"x":100,"y":86},{"x":94,"y":86}],"confidence":0.08628613377026872,"char":"日"}]},{"prob":0.7320006489753723,"position":[{"x":58,"y":108},{"x":22,"y":108},{"x":22,"y":97},{"x":58,"y":97}],"word":"住址","chars":[{"pos":[{"x":24,"y":97},{"x":34,"y":97},{"x":34,"y":108},{"x":24,"y":108}],"confidence":0.09009406955958693,"char":"住"},{"pos":[{"x":37,"y":97},{"x":47,"y":97},{"x":47,"y":108},{"x":37,"y":108}],"confidence":0.08240888515657947,"char":"址"}]},{"prob":0.7900686860084534,"position":[{"x":190,"y":108},{"x":55,"y":107},{"x":55,"y":97},{"x":190,"y":97}],"word":"北京市东城区景山前街4号","chars":[{"pos":[{"x":58,"y":97},{"x":66,"y":97},{"x":66,"y":108},{"x":58,"y":108}],"confidence":0.08713626978853328,"char":"北"},{"pos":[{"x":66,"y":97},{"x":75,"y":97},{"x":75,"y":108},{"x":66,"y":108}],"confidence":0.09658446442664059,"char":"京"},{"pos":[{"x":75,"y":97},{"x":82,"y":97},{"x":82,"y":108},{"x":75,"y":108}],"confidence":0.09711321959702555,"char":"市"},{"pos":[{"x":82,"y":97},{"x":89,"y":97},{"x":89,"y":108},{"x":82,"y":108}],"confidence":0.09856503984905221,"char":"东"},{"pos":[{"x":89,"y":97},{"x":96,"y":97},{"x":96,"y":108},{"x":89,"y":108}],"confidence":0.08821399269578022,"char":"城"},{"pos":[{"x":98,"y":97},{"x":106,"y":97},{"x":106,"y":108},{"x":98,"y":108}],"confidence":0.08318493837647765,"char":"区"},{"pos":[{"x":104,"y":97},{"x":112,"y":97},{"x":112,"y":108},{"x":104,"y":108}],"confidence":0.08305766109568175,"char":"景"},{"pos":[{"x":112,"y":97},{"x":119,"y":97},{"x":119,"y":108},{"x":112,"y":108}],"confidence":0.07607177167586324,"char":"山"},{"pos":[{"x":118,"y":97},{"x":126,"y":97},{"x":126,"y":108},{"x":118,"y":108}],"confidence":0.08421159482850636,"char":"前"},{"pos":[{"x":126,"y":97},{"x":133,"y":97},{"x":133,"y":108},{"x":126,"y":108}],"confidence":0.09308668400515782,"char":"街"},{"pos":[{"x":133,"y":97},{"x":141,"y":97},{"x":141,"y":108},{"x":133,"y":108}],"confidence":0.0723923305743872,"char":"4"},{"pos":[{"x":141,"y":97},{"x":148,"y":97},{"x":148,"y":108},{"x":141,"y":108}],"confidence":0.07152081204563109,"char":"号"}]},{"prob":0.6656411290168762,"position":[{"x":123,"y":128},{"x":56,"y":128},{"x":56,"y":116},{"x":123,"y":116}],"word":"紫禁城敬事房","chars":[{"pos":[{"x":56,"y":116},{"x":60,"y":116},{"x":60,"y":128},{"x":56,"y":128}],"confidence":0.09226816954953951,"char":"紫"},{"pos":[{"x":59,"y":116},{"x":63,"y":116},{"x":63,"y":128},{"x":59,"y":128}],"confidence":0.07426896576246664,"char":"禁"},{"pos":[{"x":63,"y":116},{"x":67,"y":116},{"x":67,"y":128},{"x":63,"y":128}],"confidence":0.09067550017265255,"char":"城"},{"pos":[{"x":66,"y":116},{"x":70,"y":116},{"x":70,"y":128},{"x":66,"y":128}],"confidence":0.0765029225688767,"char":"敬"},{"pos":[{"x":70,"y":116},{"x":74,"y":116},{"x":74,"y":128},{"x":70,"y":128}],"confidence":0.09292123645870534,"char":"事"},{"pos":[{"x":73,"y":116},{"x":77,"y":116},{"x":77,"y":128},{"x":73,"y":128}],"confidence":0.08314556446170798,"char":"房"}]},{"prob":0.7948694229125977,"position":[{"x":94,"y":167},{"x":24,"y":167},{"x":24,"y":157},{"x":94,"y":157}],"word":"公民身份证号码","chars":[{"pos":[{"x":25,"y":157},{"x":29,"y":157},{"x":29,"y":167},{"x":25,"y":167}],"confidence":0.08861113638800246,"char":"公"},{"pos":[{"x":28,"y":157},{"x":32,"y":157},{"x":32,"y":167},{"x":28,"y":167}],"confidence":0.08254439412470047,"char":"民"},{"pos":[{"x":32,"y":157},{"x":36,"y":157},{"x":36,"y":167},{"x":32,"y":167}],"confidence":0.08884088398096258,"char":"身"},{"pos":[{"x":35,"y":157},{"x":40,"y":157},{"x":40,"y":167},{"x":35,"y":167}],"confidence":0.07859658124232792,"char":"份"},{"pos":[{"x":39,"y":157},{"x":43,"y":157},{"x":43,"y":167},{"x":39,"y":167}],"confidence":0.08619067298610167,"char":"证"},{"pos":[{"x":43,"y":157},{"x":47,"y":157},{"x":47,"y":167},{"x":43,"y":167}],"confidence":0.08511877072698217,"char":"号"},{"pos":[{"x":46,"y":157},{"x":50,"y":157},{"x":50,"y":167},{"x":46,"y":167}],"confidence":0.09074754695507889,"char":"码"}]},{"prob":0.7320006489753723,"position":[{"x":254,"y":166},{"x":106,"y":166},{"x":106,"y":158},{"x":254,"y":158}],"word":"11204416541220243X","chars":[{"pos":[{"x":108,"y":158},{"x":116,"y":158},{"x":116,"y":166},{"x":108,"y":166}],"confidence":0.0711357259043661,"char":"1"},{"pos":[{"x":115,"y":158},{"x":124,"y":158},{"x":124,"y":166},{"x":115,"y":166}],"confidence":0.09493274829684896,"char":"1"},{"pos":[{"x":124,"y":158},{"x":133,"y":158},{"x":133,"y":166},{"x":124,"y":166}],"confidence":0.0901567424680299,"char":"2"},{"pos":[{"x":133,"y":158},{"x":142,"y":158},{"x":142,"y":166},{"x":133,"y":166}],"confidence":0.07465050410196451,"char":"0"},{"pos":[{"x":141,"y":158},{"x":149,"y":158},{"x":149,"y":166},{"x":141,"y":166}],"confidence":0.08667510833307837,"char":"4"},{"pos":[{"x":148,"y":158},{"x":157,"y":158},{"x":157,"y":166},{"x":148,"y":166}],"confidence":0.0864850438550479,"char":"4"},{"pos":[{"x":156,"y":158},{"x":165,"y":158},{"x":165,"y":166},{"x":156,"y":166}],"confidence":0.07474846232751661,"char":"1"},{"pos":[{"x":165,"y":158},{"x":173,"y":158},{"x":173,"y":166},{"x":165,"y":166}],"confidence":0.0851283759530122,"char":"6"},{"pos":[{"x":172,"y":158},{"x":181,"y":158},{"x":181,"y":166},{"x":172,"y":166}],"confidence":0.07180779276748162,"char":"5"},{"pos":[{"x":181,"y":158},{"x":190,"y":158},{"x":190,"y":166},{"x":181,"y":166}],"confidence":0.08309512465379258,"char":"4"},{"pos":[{"x":189,"y":158},{"x":198,"y":158},{"x":198,"y":166},{"x":189,"y":166}],"confidence":0.08760201548613727,"char":"1"},{"pos":[{"x":196,"y":158},{"x":205,"y":158},{"x":205,"y":166},{"x":196,"y":166}],"confidence":0.08620213212777092,"char":"2"},{"pos":[{"x":205,"y":158},{"x":214,"y":158},{"x":214,"y":166},{"x":205,"y":166}],"confidence":0.07708197697260562,"char":"2"},{"pos":[{"x":213,"y":158},{"x":222,"y":158},{"x":222,"y":166},{"x":213,"y":166}],"confidence":0.09455778959674814,"char":"0"},{"pos":[{"x":221,"y":158},{"x":229,"y":158},{"x":229,"y":166},{"x":221,"y":166}],"confidence":0.07772310130275195,"char":"2"},{"pos":[{"x":229,"y":158},{"x":238,"y":158},{"x":238,"y":166},{"x":229,"y":166}],"confidence":0.0836733998033232,"char":"4"},{"pos":[{"x":237,"y":158},{"x":246,"y":158},{"x":246,"y":166},{"x":237,"y":166}],"confidence":0.0832613556488638,"char":"3"},{"pos":[{"x":245,"y":158},{"x":254,"y":158},{"x":254,"y":166},{"x":245,"y":166}],"confidence":0.08408428869218577,"char":"X"}]}]}}}Process finished with exit code 0
识别成功,但是数据是混乱的,要清洗数据。
参考文献:
[1]使用SDK调用OCR服务 (10086.cn)文章来源:https://www.toymoban.com/news/detail-428224.html
[2]移动云官网_云主机_云空间_CDN_云硬盘_云数据库 (10086.cn) 文章来源地址https://www.toymoban.com/news/detail-428224.html
到了这里,关于调用移动云OCR识别身份证的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!