圖片文字識別


 
文檔網址: https://ai.baidu.com/docs#/OCR-API/top

    百度ai平臺, 文字識別,人臉識別, 圖片識別 很多, 還是很牛的,具體請看文檔吧


 
# coding=utf-8
import requests, base64
# from urllib import parse


class Image_Rec:
    def __init__(self, filepath):
        self.filepath = filepath
        # ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        self.client_id = ''
        self.client_secret = ''
        self.at_header = {
            'Content-type': 'application/json; charset=UTF-8',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
        }
        self.access_token_url = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + self.client_id + '&client_secret=' + self.client_secret
        # ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        self.ia_header = {
            'Content-type': 'application/x-www-form-urlencoded',
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
        }
        self.ia_data ={
            'image': ''
        }
        self.api_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic?access_token='


    def get_access_token(self):
        '''
        1.  鑑權使用的 Access_token
        :return: 
        '''
        acc = requests.post(url=self.access_token_url, headers=self.at_header)
        access_token_result = eval(acc.content.decode())    # eval  字符串轉成字典
        # print(access_token_result)
        # print(type(access_token_result))
        return access_token_result['access_token']

    def image_option(self):
        '''
        圖片地址base64編碼
        :return: 
        '''
        with open(self.filepath, 'rb') as fp:
            picture = fp.read()
        print(picture)
        base_picture = base64.b64encode(picture)
        str_base_picture = str(base_picture, encoding='utf-8')
        # print(type(str(base_picture, encoding='utf-8')))    # <class 'str'>
        return str_base_picture



    def get_image_data(self):
        self.api_url = self.api_url + self.get_access_token()
        self.ia_data['image'] = self.image_option()
        response = requests.post(url=self.api_url, headers=self.ia_header, data=self.ia_data)
        image_rec_result = eval(response.content.decode())   # 字符串轉成字典
        print('--> ', image_rec_result)







if __name__ == '__main__':
    filepath = 'chitu.jpg'
    im = Image_Rec(filepath)
    # im.get_access_token()
    im.get_image_data()

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章