python调用百度图片文字识别接口

# 登录百度api应用页面获取下面三相内容
APP_ID = 'xxxxx'
API_KEY = 'xxxxxxx'
SECRET_KEY = 'xxxxxxx'

class BaiduImg():
    def __init__(self, img_path):
        self.img_path = img_path # 传入图片地址

    """ 读取图片 """

    def get_file_content(self, filePath):
        with open(filePath, 'rb') as fp:
            return fp.read()

    def img_ocr(self):
        client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
        image = self.get_file_content(self.img_path)

        """ 调用通用文字识别, 图片参数为本地图片 """
        client.basicGeneral(image)
        """ 如果有可选参数 """
        options = {}
        options["language_type"] = "CHN_ENG"
        options["detect_direction"] = "true"
        options["detect_language"] = "true"
        options["probability"] = "true"
        """ 带参数调用通用文字识别, 图片参数为本地图片 """
        bendi = client.basicGeneral(image, options)
        # print(bendi)
        return bendi  # 返回字典数据

baidu = BaiduImg('xx.png')
rst = baidu.img_ocr()
print(rst)
# {
	'log_id': 2610378325233689652,
	'direction': 0,
	'words_result_num': 4,
	'words_result': [{
		'words': '百度识图,"鉴"你所见官网',
		'probability': {
			'variance': 0.018005,
			'average': 0.936804,
			'min': 0.486269
		}
	}, {
		'words': '百度图片使用世界前沿的人工智能技术为用户頭选海量的高清美图',
		'probability': {
			'variance': 0.016179,
			'average': 0.959505,
			'min': 0.407384
		}
	}, {
		'words': '用更流畅、更快捷、更精住的搜素体验带你去',
		'probability': {
			'variance': 0.012763,
			'average': 0.945982,
			'min': 0.541851
		}
	}, {
		'words': 'hitu.baidu.comr-百度快照-7519条评价',
		'probability': {
			'variance': 0.021318,
			'average': 0.892922,
			'min': 0.43175
		}
	}],
	'language': 3
}

测试图片

猜你喜欢

转载自blog.csdn.net/qq_39224439/article/details/84305867