import json
import base64
import requests
import urllib
import os
import glob

#图片转文字 程序 主要调用百度  AI 项目接口实现
#1、读取图片文件
file_path = r'g:\imgs'
pic_list =[]
#把图片文件组装进图片文件列表
for i in range(18):
    pic_list.append('images_'+str(i)+'.png')

#把图片文件组装进图片文件列表
#for imgfile in sorted(glob.glob(file_path + "\*.jpg")):
#    pic_list.append(imgfile)

print(pic_list)
#结果输出文件
result_file = os.path.join(file_path, 'temp.txt')

for file in pic_list:
    file_name = os.path.join(file_path, file)
    #读取图片文件并进行转码
    with open(file_name, 'rb') as f:
        pic1 = f.read()
    data = dict()
    data['languagetype'] = "CHN_ENG"  # 识别文字
    data['image'] = str(base64.b64encode(pic1), 'utf-8')
    decoded_data = urllib.parse.urlencode(data)
    #2、获取access_token，拼接API接口
    get_token = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&' \
                'client_id=L8DSGvCxigKvQxYGGR5b6OUn&client_secret=PnLBBvgV71A12NMSEG2PECbhTFxOpmNl'
    #通用文字识别
    API_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic/?access_token='
    #手写文字识别
    #API_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting/?access_token='
    #高精度文字识别
    #API_url= 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic/?access_token='
    headers={"Content-Type": "application/x-www-form-urlencoded"}
    text = requests.get(get_token).text
    access_token = json.loads(text)['access_token']
    #print(access_token)
    url = API_url + access_token
    #3、请求API接口传入数据，返回文字
    response = requests.post(url, data= decoded_data, headers=headers)
    #print(response.text)
    words_result = json.loads(response.text)['words_result']
    temp_file = open(result_file, 'a')
    for words in words_result:
        temp_file.writelines(words['words']+'\n')
        #print(words['words'])
    temp_file.close()

25925fc345d002b741a1bbeb96eb5ba94ac717e3
<mask token>

<mask token>
for i in range(18):
    pic_list.append('images_' + str(i) + '.png')
print(pic_list)
<mask token>
for file in pic_list:
    file_name = os.path.join(file_path, file)
    with open(file_name, 'rb') as f:
        pic1 = f.read()
    data = dict()
    data['languagetype'] = 'CHN_ENG'
    data['image'] = str(base64.b64encode(pic1), 'utf-8')
    decoded_data = urllib.parse.urlencode(data)
    get_token = (
        'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=L8DSGvCxigKvQxYGGR5b6OUn&client_secret=PnLBBvgV71A12NMSEG2PECbhTFxOpmNl'
        )
    API_url = (
        'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic/?access_token='
        )
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    text = requests.get(get_token).text
    access_token = json.loads(text)['access_token']
    url = API_url + access_token
    response = requests.post(url, data=decoded_data, headers=headers)
    words_result = json.loads(response.text)['words_result']
    temp_file = open(result_file, 'a')
    for words in words_result:
        temp_file.writelines(words['words'] + '\n')
    temp_file.close()

<mask token>
file_path = 'g:\\imgs'
pic_list = []
for i in range(18):
    pic_list.append('images_' + str(i) + '.png')
print(pic_list)
result_file = os.path.join(file_path, 'temp.txt')
for file in pic_list:
    file_name = os.path.join(file_path, file)
    with open(file_name, 'rb') as f:
        pic1 = f.read()
    data = dict()
    data['languagetype'] = 'CHN_ENG'
    data['image'] = str(base64.b64encode(pic1), 'utf-8')
    decoded_data = urllib.parse.urlencode(data)
    get_token = (
        'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=L8DSGvCxigKvQxYGGR5b6OUn&client_secret=PnLBBvgV71A12NMSEG2PECbhTFxOpmNl'
        )
    API_url = (
        'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic/?access_token='
        )
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    text = requests.get(get_token).text
    access_token = json.loads(text)['access_token']
    url = API_url + access_token
    response = requests.post(url, data=decoded_data, headers=headers)
    words_result = json.loads(response.text)['words_result']
    temp_file = open(result_file, 'a')
    for words in words_result:
        temp_file.writelines(words['words'] + '\n')
    temp_file.close()

import json
import base64
import requests
import urllib
import os
import glob
file_path = 'g:\\imgs'
pic_list = []
for i in range(18):
    pic_list.append('images_' + str(i) + '.png')
print(pic_list)
result_file = os.path.join(file_path, 'temp.txt')
for file in pic_list:
    file_name = os.path.join(file_path, file)
    with open(file_name, 'rb') as f:
        pic1 = f.read()
    data = dict()
    data['languagetype'] = 'CHN_ENG'
    data['image'] = str(base64.b64encode(pic1), 'utf-8')
    decoded_data = urllib.parse.urlencode(data)
    get_token = (
        'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=L8DSGvCxigKvQxYGGR5b6OUn&client_secret=PnLBBvgV71A12NMSEG2PECbhTFxOpmNl'
        )
    API_url = (
        'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic/?access_token='
        )
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    text = requests.get(get_token).text
    access_token = json.loads(text)['access_token']
    url = API_url + access_token
    response = requests.post(url, data=decoded_data, headers=headers)
    words_result = json.loads(response.text)['words_result']
    temp_file = open(result_file, 'a')
    for words in words_result:
        temp_file.writelines(words['words'] + '\n')
    temp_file.close()

import json
import base64
import requests
import urllib
import os
import glob

#图片转文字 程序 主要调用百度  AI 项目接口实现
#1、读取图片文件
file_path = r'g:\imgs'
pic_list =[]
#把图片文件组装进图片文件列表
for i in range(18):
    pic_list.append('images_'+str(i)+'.png')

#把图片文件组装进图片文件列表
#for imgfile in sorted(glob.glob(file_path + "\*.jpg")):
#    pic_list.append(imgfile)

print(pic_list)
#结果输出文件
result_file = os.path.join(file_path, 'temp.txt')

for file in pic_list:
    file_name = os.path.join(file_path, file)
    #读取图片文件并进行转码
    with open(file_name, 'rb') as f:
        pic1 = f.read()
    data = dict()
    data['languagetype'] = "CHN_ENG"  # 识别文字
    data['image'] = str(base64.b64encode(pic1), 'utf-8')
    decoded_data = urllib.parse.urlencode(data)
    #2、获取access_token，拼接API接口
    get_token = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&' \
                'client_id=L8DSGvCxigKvQxYGGR5b6OUn&client_secret=PnLBBvgV71A12NMSEG2PECbhTFxOpmNl'
    #通用文字识别
    API_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic/?access_token='
    #手写文字识别
    #API_url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting/?access_token='
    #高精度文字识别
    #API_url= 'https://aip.baidubce.com/rest/2.0/ocr/v1/accurate_basic/?access_token='
    headers={"Content-Type": "application/x-www-form-urlencoded"}
    text = requests.get(get_token).text
    access_token = json.loads(text)['access_token']
    #print(access_token)
    url = API_url + access_token
    #3、请求API接口传入数据，返回文字
    response = requests.post(url, data= decoded_data, headers=headers)
    #print(response.text)
    words_result = json.loads(response.text)['words_result']
    temp_file = open(result_file, 'a')
    for words in words_result:
        temp_file.writelines(words['words']+'\n')
        #print(words['words'])
    temp_file.close()
