用Python实现最简单的文字识别:基于百度云文字识别API

Python版本:3.6.5

百度云提供的文字识别技术,准确率还是非常高的,而且每天还有5w次免费的调用量,对于用来学习或者偶尔拿来用用,已经完全足够了。文章提供一个模板,稍加修改就可以直接套用。

# -*- coding: utf-8 -*-
# author: Qlly
# date: 2018-08-24

import requests
import base64

class Orc_main():
    def orc_look(self, path):
        access_token = ""  # 自行注册百度云账号,即可获取自己专属的access_token,必须输入!
        with open(path, 'rb') as f:
            image_data = f.read()
            base64_ima = base64.b64encode(image_data)
            data = {
                'image': base64_ima
            }
            headers = {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
            url = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token=" + str(access_token)
            r = requests.post(url, params=headers, data=data).json()
            for word in r['words_result']:
                yield word['words']
            # 返回一个生成器,可自行修改

if __name__ == '__main__':
    om = Orc_main()
    path = ""  # 图片文件路径,必须输入!
    words = om.orc_look(path) 
    # 输出文字(返回结果)
    for word in words:
        print(word)

猜你喜欢

转载自blog.csdn.net/qq_29750277/article/details/82014800