· 6 years ago · Jul 19, 2019, 05:42 AM
1# -*-coding:utf-8-*-
2
3# 安装使用Python SDK: pip install baidu-aip
4from aip import AipOcr
5# 安装 pip install pillow
6from PIL import Image
7import os
8
9
10# 百度云API
11APP_ID = '167234254'
12API_KEY = '88ZfjQ455rgfgTLTihkjcsOl'
13SECRET_KEY = 'Dr0kHGzc2gfgs778jbBfclwfrQk0'
14aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
15
16def get_file_content(filePath):
17 with open(filePath, 'rb') as fp:
18 return fp.read()
19
20
21# 创建文件夹
22def folderNot(Path):
23 # 判断文件夹是否存在
24 folder = os.path.exists(photoPath + "/Ocr")
25 if not folder:
26 os.makedirs(photoPath + "/Ocr")
27 print("Ocr 文件夹创建成功")
28 else:
29 print("已存在,可以存入图片")
30
31
32
33# 文字识别
34def Ocr(Path, OcrModel):
35 for root, dirs, files in os.walk(Path, topdown=False): # 该迭代类型每单元返回三个部分,我们需要的文件名在第三部分,具体参数自行了解
36 files.sort() # 对文件名进行排序,确保Ocr时按照时间顺序来
37 for name in files: # name 即为文件夹下每个文件的文件名
38 if '.jpg' in name: # 判断是否为图片格式,笔者这里设置png因为常用QQ截图,可自行添加其他格式
39 filePath = os.path.join(root, name) # 路径与文件名拼接,方面接下来通过 Image.open()打开。
40 options = {
41 'detect_direction': 'true',
42 'language_type': 'CHN_ENG',
43 }
44 im = Image.open(filePath)
45 left, upper, right, lower = 0, 0 + 420, 840, 480 # 图片裁剪的位子,只保留有文字的区域
46 region = im.crop((left, upper, right, lower)) # 裁剪图片
47 namePath = region.save(Path + '/Ocr/' + name) # 裁剪后按原文件名命名,保存在同目录Ocr文件夹下
48 if OcrModel == 1: # 网络图片文字识别,每天 500 次免费
49 result = aipOcr.webImage(get_file_content(Path + '/Ocr/' + name), options)
50 if OcrModel == 2: # 通用文字高精度识别,每天 800 次免费
51 result = aipOcr.basicAccurate(get_file_content(Path + '/Ocr/' + name), options)
52 if OcrModel == 3: # 通用文字识别,每天 50 000 次免费
53 result = aipOcr.basicGeneral(get_file_content(Path + '/Ocr/' + name), options)
54 ocrWords = open(Path + '/Ocr.txt', 'a') # 刚开始用的是 'w' 模式,不管怎么搞后面的总是覆盖前面的,选用'a'模式后就可以了
55 for i in result['words_result']: # result为字典类型,识别出的文字信息存放在'words_result'对应的字典中
56 print(i['words']) # 列表中接着嵌套字典,每一部分信息存储在'words'键中
57 ocrWords.write(name[0:3] + ' ' + i['words'].encode('utf-8') + '\n') # 发现Unicode字符集,无法写入到Txt文件里面,转 utf-8后可以写入
58
59 ocrWords.close()
60
61
62
63# 调用 FFmpeg 对视频,每2秒截一次图
64def ffmpegS(Path,ffModel):
65 for root, dirs, files in os.walk(Path, topdown=False):
66 for name in files: # name 即为文件夹下每个文件的文件名
67 if 'mp4' in name: # 判断是否为 mp4 视频
68 filePath = os.path.join(root, name) # 路径与文件名拼接
69 if ffModel == 1:
70 print(os.system('/usr/local/Cellar/ffmpeg/4.1.3_1/bin/ffmpeg -ss 00:00 -i ' \
71 + filePath + ' -f image2 -r 0.4 -t 10:01 ' + Path + '/%3d.jpg'))
72 if ffModel == 2:
73 print('你选择了,不截图。')
74
75 # /Users/Shuk/Desktop/2
76
77
78# Ocrfile 去重
79def fileW(Path):
80 PathOcr = Path + '/Ocr.txt'
81 PathNewOcr = Path + '/new_Ocr.txt'
82 outfile = open(PathNewOcr, "w")
83 OcrFile = open(PathOcr, "r")
84
85 lines_seen = set() # Build an unordered collection of unique elements.
86
87 for line in OcrFile:
88 line = line.strip('\n')
89 if line[3:] not in lines_seen: # 切掉前3位编号,与元组里的数据对比
90 line1 = line[3:] # 将切掉前3位编号的,纯文本重新赋值
91 lines_seen.add(line1) # 将纯文本存入元组
92 outfile.write(line + '\n') # 再把带编号的数据写入TXT
93
94 OcrFile.close()
95 outfile.close()
96 print('Ocr去重完成')
97
98
99photoPath = raw_input('请输入图片所在路径: ')
100ffmpegModel = int(raw_input('是否需要进行截图? 1.开始截图 | 2.跳过: '))
101OcrModel = int(raw_input('请输入Ocr模式: 1.图片文字 | 2.高精度 | 3.通用识别: '))
102
103
104ffmpegS(photoPath, ffmpegModel)
105folderNot(photoPath) # 创建文件夹
106Ocr(photoPath, OcrModel) # 文字识别
107fileW(photoPath) # TXT 去重
108
109#os._exit(0)