[Python] 纯文本查看 复制代码 import os
import time
# pip install baidu-aip 来进行安装
from aip import AipOcr
# 新建一个AipOcr对象
# appId apiKey secretKey 需要在[url]https://cloud.baidu.com/product/ocr.html[/url]免费开通文字识别服务后获取
config = {
'appId': 'XXXXXXXX',
'apiKey': 'XXXXXXXX',
'secretKey': 'XXXXXXXX'
}
client = AipOcr(**config)
pic_dir = r"D:/test/4/"
# 读取图片
def get_file_content(file_path):
with open(file_path, 'rb') as fp:
return fp.read()
# 识别图片里的文字
def img_to_str(image_path):
image = get_file_content(image_path)
# 调用通用文字识别, 图片参数为本地图片
result = client.basicAccurate(image)
# 结果拼接返回
words_list = []
if 'words_result' in result:
if len(result['words_result']) > 0:
for w in result['words_result']:
words_list.append(w['words'])
print(words_list)
#取施工区域和施工内容后面的字符后进行组合
l1 = str([s for s in words_list if '施工区域' in s])
l2 = str([s for s in words_list if '施工内容' in s])
l3 = l1[7:-2]
l4 = l2[7:-2]
l5 = l3 + l4
file_name = get_code(words_list, 4)
if file_name == None:
file_name = 'None' + '-' + str(time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime()))
print(file_name)
try:
os.rename(image_path, pic_dir + l5.replace("/", "") + '.jpg')
except:os.rename(image_path, pic_dir + l5.replace("/", "") +"1"+ '.jpg')
# 获取字符串列表中最长的字符串
def get_longest_str(str_list):
return max(str_list, key=len)
# 获取字符串列表中7位服务编号、11位快速服务代码
def get_code(str_list, length):
code_num = ''
for str in str_list:
if len(str.strip()) == length:
code_num = str
return code_num
return None
# 遍历某个文件夹下所有图片
def query_picture(dir_path):
pic_path_list = []
for filename in os.listdir(dir_path):
pic_path_list.append(dir_path + filename)
return pic_path_list
if __name__ == '__main__':
pic_list = query_picture(pic_dir)
if len(pic_list) > 0:
for i in pic_list:
img_to_str(i)
怎么才能实现能看到我处理了多少张图片呢?还有这个百度的OCR有次数限制,如果我换成本地识别 代码会不会大范围修改呢?求大佬解答 |