first commit
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
|
||||
# encoding:utf-8
|
||||
|
||||
import requests
|
||||
import base64
|
||||
import json
|
||||
from orc_t import get_tocken
|
||||
|
||||
|
||||
def orc(img, find=False, string=''):
|
||||
if find:
|
||||
url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general'
|
||||
else:
|
||||
url = "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic" # 只识别没有位置
|
||||
token = get_tocken()['access_token']
|
||||
request_url = url
|
||||
# 二进制方式打开图片文件
|
||||
f = open(img, 'rb')
|
||||
img = base64.b64encode(f.read())
|
||||
params = {"image": img}
|
||||
access_token = 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)
|
||||
text = ''
|
||||
tem = response.json()['words_result']
|
||||
result = []
|
||||
if find:
|
||||
for i in tem:
|
||||
if i["words"].find(string) > -1:
|
||||
text = i["words"]
|
||||
x = int((i['location']['top']+i['location']['height'])/2)
|
||||
y = int((i["location"]["left"]+i['location']['width'])/2)
|
||||
result.append([text, (x, y)])
|
||||
else:
|
||||
continue
|
||||
return result
|
||||
else:
|
||||
for i in tem:
|
||||
text += i["words"]
|
||||
return text
|
||||
# return response['words_result'][0]['words']
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
print(orc('2.png'))
|
||||
Reference in New Issue
Block a user