本文整理汇总了Python中aip.AipOcr方法的典型用法代码示例。如果您正苦于以下问题:Python aip.AipOcr方法的具体用法?Python aip.AipOcr怎么用?Python aip.AipOcr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aip
的用法示例。
在下文中一共展示了aip.AipOcr方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: verify
# 需要导入模块: import aip [as 别名]
# 或者: from aip import AipOcr [as 别名]
def verify(self, image, ocr=None):
try:
from aip import AipOcr
except Exception as e:
print(e, '\n请在命令行下执行: pip install baidu-aip')
conf = ocr or {
'appId': '11645803',
'apiKey': 'RUcxdYj0mnvrohEz6MrEERqz',
'secretKey': '4zRiYambxQPD1Z5HFh9VOoPXPK9AgBtZ'
}
ocr = AipOcr(**conf)
try:
r = ocr.basicGeneral(image).get('words_result')[0]['words']
except Exception as e:
print(e, '\n验证码图片无法识别!')
r = False
return r
示例2: baidu_ocr
# 需要导入模块: import aip [as 别名]
# 或者: from aip import AipOcr [as 别名]
def baidu_ocr(img, options, line=0):
"""
调用百度api进行图片识别
:param options: 百度ocr选项
:param img: 获取图片
:param line: 选择行数,暂时没有用途,以防万一留下这个变量,默认为第一行
:return: 返回识别结果
"""
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
image = img
""" 调用通用文字识别, 图片参数为本地图片 """
result = client.basicGeneral(image, _options(options))
if result["words_result_num"] > 1 or line >= 1:
# TODO
raise NotImplementedError
elif result["words_result_num"] == 1:
return result["words_result"][line]["words"]
else:
return ""
示例3: get_verifycode
# 需要导入模块: import aip [as 别名]
# 或者: from aip import AipOcr [as 别名]
def get_verifycode(self, client=object, imageUrl=''):
# 填写自己的 baidu-ocr api信息
APP_ID = 'xxxxxxxxxx'
API_KEY = 'xxxxxxxxxxxx'
SECRET_KEY = 'xxxxxxxxxxxxxxx'
options = {}
options["recognize_granularity"] = "big"
options["detect_direction"] = "true"
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
retry_times = 3
i = 0
while i < retry_times:
i += 1
try:
# image = get_file_content(tmpImageName)
image = self.sess.get(imageUrl, proxies=utils.get_proxy(),
headers=self.a_task.M_HEADERS,
cookies=self.a_task.M_COOKIES,
verify=False)
# open('vc____.jpg', 'wb').write(image.content)
response = client.numbers(image.content, options)
debug_p('[get_verifycode] vc_code response=', response)
# dict: {'log_id': 3705378724129786481, 'direction': 0, 'words_result_num': 1, 'words_result': [{'location': {'width': 78, 'top': 1, 'left': 13, 'height': 37}, 'words': '4217'}]}
words_result = response['words_result']
verifycode = words_result[0].get('words', '')
if not verifycode or len(verifycode) < 4:
continue
if len(verifycode) > 4:
verifycode = verifycode[:4]
debug_p('[get_verifycode] verifycode=', verifycode, 'i=', i)
return verifycode
except Exception as e:
# speed up
i += 1
debug_p('[get_verifycode] Exception', 'i=', i, 'traceback=', traceback.format_exc())
pass
return '0000'
示例4: __init__
# 需要导入模块: import aip [as 别名]
# 或者: from aip import AipOcr [as 别名]
def __init__(self, d, app_id, api_key, secrect_key):
self._d = d
self._APP_ID = app_id
self._API_KEY = api_key
self._SECRECT_KEY = secrect_key
self._client = AipOcr(self._APP_ID, self._API_KEY, self._SECRECT_KEY)
示例5: image_to_string
# 需要导入模块: import aip [as 别名]
# 或者: from aip import AipOcr [as 别名]
def image_to_string(image, token={
'appId': '11645803',
'apiKey': 'RUcxdYj0mnvrohEz6MrEERqz',
'secretKey': '4zRiYambxQPD1Z5HFh9VOoPXPK9AgBtZ'}):
if not isinstance(image, bytes):
buf = import_module('io').BytesIO()
image.save(buf, 'png')
image = buf.getvalue()
return import_module('aip').AipOcr(**token).basicGeneral(image).get(
'words_result')[0]['words']
示例6: get_text_from_image
# 需要导入模块: import aip [as 别名]
# 或者: from aip import AipOcr [as 别名]
def get_text_from_image(image_data, app_id, app_key, app_secret, api_version=0, timeout=3):
"""
Get image text use baidu ocr
:param image_data:
:param app_id:
:param app_key:
:param app_secret:
:param api_version:
:param timeout:
:return:
"""
client = AipOcr(appId=app_id, apiKey=app_key, secretKey=app_secret)
client.setConnectionTimeoutInMillis(timeout * 1000)
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
if api_version == 1:
result = client.basicAccurate(image_data, options)
else:
result = client.basicGeneral(image_data, options)
if "error_code" in result:
print("baidu api error: ", result["error_msg"])
return ""
return [words["words"] for words in result["words_result"]]
示例7: create_bae
# 需要导入模块: import aip [as 别名]
# 或者: from aip import AipOcr [as 别名]
def create_bae():
# 百度AI配置
APP_ID = config['bae_ai']['APP_ID']
API_KEY = config['bae_ai']['API_KEY']
SECRET_KEY = config['bae_ai']['SECRET_KEY']
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
return client
# 百度通用orc
示例8: __init__
# 需要导入模块: import aip [as 别名]
# 或者: from aip import AipOcr [as 别名]
def __init__(self,app_id,api_key,secret_key):
self.image_client = AipImageClassify(app_id,api_key,secret_key)
self.words_client = AipOcr(app_id,api_key,secret_key)
示例9: init_baidu_ocr
# 需要导入模块: import aip [as 别名]
# 或者: from aip import AipOcr [as 别名]
def init_baidu_ocr(baidu_ocr_config):
app_id, api_key, secret_key = baidu_ocr_config
client = AipOcr(app_id, api_key, secret_key)
return client
# {'words_result': [{'words': '11.代表作之一是《蒙娜丽莎的眼'},
# {'words': '泪》的歌手是?'}, {'words': '林志颖'},
# {'words': '林志炫'}, {'words': '林志玲'}],
# 'log_id': 916087026228727188, 'words_result_num': 5}
示例10: main
# 需要导入模块: import aip [as 别名]
# 或者: from aip import AipOcr [as 别名]
def main():
start = time.time()
os.system("adb shell /system/bin/screencap -p /sdcard/screenshot.png")
os.system("adb pull /sdcard/screenshot.png ")
im = Image.open(r"./screenshot.png")
img_size = im.size
w = im.size[0]
h = im.size[1]
print("xx:{}".format(img_size))
region = im.crop((70,200, w-70,1200)) #裁剪的区域
region.save("./crop_test1.png")
f=open('./crop_test1.png','rb')
image_data=f.read()
client = AipOcr(app_id, api_key, secret_key) #识别过程
result = client.basicGeneral(image_data)
if "error_code" in result:
print("baidu api error: ", result["error_msg"])
else:
words_results = result['words_result'] #得到识别结果的问题+选项
#print (result['words_result'])
question = ''
choice = ['','','','','','']
num = 0
choice_num = 0
for words_result in words_results:
num+=1
if(num >=len(words_results)-2):
choice[choice_num] = words_result['words']
choice_num+=1
else:
question = question + words_result['words']
print(question) #打印问题
print(' A:'+choice[0]+' B:'+choice[1]+' C:'+choice[2]) #打印选项
s = Search(question,choice)
s.search()
end = time.time()
print('程序用时:'+str(end-start)+'秒')