记录名片识别和图片转文字。
腾讯AI开放平台提供的名片OCR很强大, 而且可以免费使用, 所以本文使用的是腾讯AI开放平台。
腾讯AI开放平台API调用需要注意签名方法, 请查看之前文章传送门
名片OCR API
import logging
logger = logging.getLogger(__name__)
import base64
import time
import requests
import json
def bc_ocr(_path = None, _byte = None):
''' 腾讯AI名片识别
_path和_byte必须存在一个, 优先级 _byte > _path
:param _path:
:param _bytes:
:return:
'''
try:
if _path is None and _byte is None: return
if _byte is None:
with open(_path, 'rb') as f:
_byte = f.read()
if _byte is None: return
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
b_str = base64.b64encode(_byte)
params = {'app_id': APP_ID, 'time_stamp':int(time.time()), 'nonce_str': 'fa577ce340859f9fe', 'image': b_str}
sign = get_sign_code(params, API_KEY)
params['sign'] = sign
url = 'https://api.ai.qq.com/fcgi-bin/ocr/ocr_bcocr'
rt = requests.post(url, data=params)
jsonData = json.loads(rt.text)
logger.info('腾讯名片OCR结果:[{}]'.format(jsonData))
if jsonData.get('ret') == 0:
return jsonData.get('data')
except Exception as e:
logger.error('tencen business_card_ocr error [{}]'.format(e))
通用图片识别 API
import logging
logger = logging.getLogger(__name__)
import base64
import time
import requests
import json
def ocr(_path = None, _byte = None):
'''腾讯 图片识别
_path和_byte必须存在一个, 优先级 _byte > _path
:param _path:
:param _byte:
:return:
'''
try:
if _path is None and _byte is None: return
if _byte is None:
with open(_path, 'rb') as f:
_byte = f.read()
if _byte is None: return
APP_ID = '你的 App ID'
API_KEY = '你的 Api Key'
image_str = base64.b64encode(_byte)
url = 'https://api.ai.qq.com/fcgi-bin/ocr/ocr_generalocr'
params = {'app_id': APP_ID, 'time_stamp':int(time.time()), 'nonce_str': 'fa577ce340859f9fe', 'image': image_str}
sign = get_sign_code(params, API_KEY)
params['sign'] = sign
rt = requests.post(url, data=params)
jsonData = json.loads(rt.text)
if jsonData.get('ret') == 0:
item_list = jsonData.get('data').get('item_list')
return '\r\n'.join([item.get('itemstring') for item in item_list])
except Exception as e:
logger.error('tencen ocr error [{}]'.format(e))