# coding=utf-8
#
# Copyright (c) 2020 The UESTC-Signin Authors. All rights reserved.
# Use of this source code is governed by a MIT-style license that can be
# found in the LICENSE file. See the AUTHORS file for names of contributors.
#
# Authors: ehds(<EMAIL>)

import requests
import base64

'''
手写文字识别
'''


def baidu_ocr(file_path):
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=bl7DR1WXUaINikVf2zmQmoXz&client_secret=<KEY>'
    response = requests.get(host)
    # get access_token
    access = None
    if response:
        access = response.json()["access_token"]

    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting"
    # 二进制方式打开图片文件
    f = open(file_path, 'rb')
    img = base64.b64encode(f.read())

    params = {"image": img}
    request_url = request_url + "?access_token=" + access
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        return response.json()["words_result"][0]["words"]
    return ""


if __name__ == "__main__":

    print(baidu_ocr("captcha.jpg"))

# coding=utf-8
#
# Copyright (c) 2020 The UESTC-Signin Authors. All rights reserved.
# Use of this source code is governed by a MIT-style license that can be
# found in the LICENSE file. See the AUTHORS file for names of contributors.
#
# Authors: ehds(<EMAIL>)

import requests
import base64

'''
手写文字识别
'''


def baidu_ocr(file_path):
    host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=bl7DR1WXUaINikVf2zmQmoXz&client_secret=<KEY>'
    response = requests.get(host)
    # get access_token
    access = None
    if response:
        access = response.json()["access_token"]

    request_url = "https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting"
    # 二进制方式打开图片文件
    f = open(file_path, 'rb')
    img = base64.b64encode(f.read())

    params = {"image": img}
    request_url = request_url + "?access_token=" + access
    headers = {'content-type': 'application/x-www-form-urlencoded'}
    response = requests.post(request_url, data=params, headers=headers)
    if response:
        return response.json()["words_result"][0]["words"]
    return ""


if __name__ == "__main__":

    print(baidu_ocr("captcha.jpg"))

# coding=utf-8 # # Copyright (c) 2020 The UESTC-Signin Authors. All rights reserved. # Use of this source code is governed by a MIT-style license that can be # found in the LICENSE file. See the AUTHORS file for names of contributors. # # Authors: ehds(<EMAIL>) 手写文字识别 # get access_token # 二进制方式打开图片文件