相较于前边两个,这个识别率比较低,而且免费次数也少(白嫖党表示很难受)
# -*- 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 resp
if __name__ == '__main__':
import glob
import os
path_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.text
lines=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')