汉王OCR接口调用——python
生活随笔
收集整理的這篇文章主要介紹了
汉王OCR接口调用——python
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
相較于前邊兩個,這個識別率比較低,而且免費次數也少(白嫖黨表示很難受)
# -*- coding: utf-8 -*- import os, sys, requests, base64, json # from PIL import Image # img=Image.open('pic/1.jpg') # img.save('pic/1.jpg') # reload(sys) # sys.setdefaultencoding('utf-8')key = 'xxxxxxxx'#自己新建的項目密鑰 url = 'http://api.hanvon.com/rt/ws/v1/ocr/generaltext/recg?key=%s&code=9c56015f-af3c-4b82-a4eb-dc34efa54683' % key print(url)def get_json(path):base64img = ""#base64img需要是utf-8格式的字符串base64img = base64.b64encode(open(path, 'rb').read())# base64img = str(base64img, 'utf-8')base64img = base64img.encode("UTF-8")data = {"uid": '', "isText": "1", "image": base64img}data = json.dumps(data)headers = {"Content-Type": "application/octet-stream"}resp = requests.post(url, data=data, headers=headers)# print(resp.text)return respif __name__ == '__main__':import globimport ospath_pic='pic'path_save='hanwang_OCR'if not os.path.exists(path_save):os.mkdir(path_save)extensions=['jpg', 'JPG', 'jpeg', 'JPEG']for extension in extensions:for path in glob.glob(os.path.join(path_pic, '*.'+extension)):path_txt = path_save + '/' + path.split('/')[-1].split('.')[0] + '.txt'resp = get_json(path)results=resp.textlines=results.split('"chars":')[1:]with open(path_txt,'w') as file:for line in lines:words=line.split('"code":"')[1:]for word in words:# get_char = word[0]# print(get_char)get_char=word.split('","coords"')[0]file.write(get_char)file.write('\n')總結
以上是生活随笔為你收集整理的汉王OCR接口调用——python的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 智能制造新模式, 柔性制造正当时
- 下一篇: 微软ERP的制造业解决方案(转)