-
Notifications
You must be signed in to change notification settings - Fork 0
/
pic_word.py
347 lines (310 loc) · 9.36 KB
/
pic_word.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# coding=utf-8
import sys
import json
import base64
# 保证兼容python2以及python3
import time
IS_PY3 = 3
from urllib.request import urlopen
from urllib.request import Request
from urllib.error import URLError
from urllib.parse import urlencode
# 防止https证书校验不正确
import ssl
import os
import operator
ssl._create_default_https_context = ssl._create_unverified_context
API_KEY = 'ZOYegMvX3fVYGcn1zf13suG2'
SECRET_KEY = '6HjW4xdUWw9MXoaVT2jqTrfZGmOPWpZw'
OCR_URL = 'https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic'
""" TOKEN start """
TOKEN_URL = 'https://aip.baidubce.com/oauth/2.0/token'
"""
获取token
"""
# 定义垃圾词
rubbish_word = ['己utho', 'bnmou', 'pyho如', '知中国大学MOOC', '中国大学MOOC', 'python', '2', 'bdryou', 'pythoni', 'baryoul',
'barpou', 'bdrpou', "python'", '和中国大学MOOC', 'pythom', 'pyhQ心', 'pyho或', 'bAmou', 'pyhQ吸', 'bao则',
'bpo网', 'bpo画', 'pythor如', '-Python', 'bAgou', 'bAgpou', 'bAmpou','bampou','bAmpou','pyhg吸', 'pyhQ或']
# 试下 正则的匹配方式
rubbish_words = ['bpo', '中国大', '已', '己', 'ou', 'po画', 'po网', 'bo画']
# 填入 课程名称
className = "python语言程序设计"
def fetch_token():
params = {'grant_type': 'client_credentials',
'client_id': API_KEY,
'client_secret': SECRET_KEY}
post_data = urlencode(params)
if (IS_PY3):
post_data = post_data.encode('utf-8')
req = Request(TOKEN_URL, post_data)
try:
f = urlopen(req, timeout=5)
result_str = f.read()
except URLError as err:
print(err)
if (IS_PY3):
result_str = result_str.decode()
result = json.loads(result_str)
if ('access_token' in result.keys() and 'scope' in result.keys()):
if not 'brain_all_scope' in result['scope'].split(' '):
print('please ensure has check the ability')
exit()
return result['access_token']
else:
print('please overwrite the correct API_KEY and SECRET_KEY')
exit()
"""
读取文件
"""
def read_file(image_path):
f = None
try:
f = open(image_path, 'rb')
return f.read()
except:
print('read image file fail')
return None
finally:
if f:
f.close()
"""
调用远程服务
"""
def request(url, data):
req = Request(url, data.encode('utf-8'))
has_error = False
try:
f = urlopen(req)
result_str = f.read()
if (IS_PY3):
result_str = result_str.decode()
return result_str
except URLError as err:
print(err)
def file_video_name(file_dir):
File_Name = []
for files in os.listdir(file_dir):
if os.path.splitext(files)[1] == '.mp4':
File_Name.append(files)
return File_Name
def file_pic_name(file_dir):
File_Name = []
for files in os.listdir(file_dir):
if os.path.splitext(files)[1] == '.png':
File_Name.append(files)
return File_Name
def sort_string_list(string_list):
new = []
for i in string_list:
new.append(float(i[:-4]))
new.sort()
return new
def rubbish_word_fun(result_json, rubbish_word):
restart = True
while restart:
restart = False
word_num = len(result_json['words_result'])
for k in range(word_num):
try:
if result_json['words_result'][k]['words'] in rubbish_word:
del result_json['words_result'][k]
restart = True
elif len(result_json['words_result'][k]['words']) <= 3:
del result_json['words_result'][k]
restart = True
elif k == (word_num - 1):
restart = False
except Exception as e:
print(e)
return result
def topic_rechange(topic):
illagle = ['.', ',', '"', '…', '?', '/', '\\', '(', ')', '<', '>', ':', '*']
for word in illagle:
topic = topic.replace(word, '')
return topic
pic_root = os.path.join(os.getcwd(), 'pic')
word_root = os.path.join(os.getcwd(), 'text')
classPicRoot = os.path.join(pic_root, className)
classWordRoot = os.path.join(word_root, className)
if __name__ == '__main__':
# 获取access token
token = fetch_token()
# 拼接通用文字识别高精度url
image_url = OCR_URL + "?access_token=" + token
text = ""
topic = ""
# main = '[3.2.6]--单元小结.mp4'
# pic_file_name = file_pic_name(now_address)
# sort_pic_file = sort_string_list(pic_file_name)
# print(pic_file_name)
# 记得到时候激活
# os.mkdir(os.path.join(word_root, '[9.7.1]--练习与作业.mp4'))
# text_address = os.path.join(classWordRoot, main)
# try:
# os.mkdir(text_address)
# except Exception as e:
# print(e)
# print(os.path.join(word_root, '[9.7.1]--练习与作业.mp4'))
# 文件遍历
# 记录图片开始的记录
# num = 0
# 对于整理好的文件进行一个处理
# main = 65165
for filepath, dirnames, filenames in os.walk(os.path.join(pic_root, className)):
main = os.path.split(filepath)[-1]
# 单纯判断一下 第一个情况
if main == className:
continue
now_address = os.path.join(classPicRoot, main)
pic_file_name = file_pic_name(now_address)
sort_pic_file = sort_string_list(pic_file_name)
text_address = os.path.join(classWordRoot, main)
# 写个判断 是不是已经提取了
'''
1. 对比提取的文件夹是不是存在
'''
if main in os.listdir(classWordRoot):
print("文字已经提取 跳过")
continue
try:
os.mkdir(text_address)
except Exception as e:
print(e)
num = 0
text = ""
topic = ""
content = ""
for i in range(len(sort_pic_file)):
# 前后顺序很重要
name = str(sort_pic_file[i]) + '.png'
print(name)
# print(i)
request_json = True
while request_json:
pic_add = os.path.join(now_address, name)
# print(pic_add)
pic_content = read_file(pic_add)
result = request(image_url, urlencode({'image': base64.b64encode(pic_content)}))
result_json = json.loads(result)
# 错误请求
try:
if result_json['error_code'] == 18:
print('qps请求炸了')
print(result_json)
time.sleep(0.5)
request_json = True
except:
request_json = False
pass
new_dict = {
}
# 垃圾词清理
try:
if result_json['words_result_num'] <= 3:
num = num + 1
print('初始内容太少 删除')
continue
except Exception as e:
print(e)
# else:
# result_json = rubbish_word_fun(result_json, rubbish_word)
else:
restart = True
while restart:
restart = False
exam = len(result_json['words_result'])
for k in range(exam):
# print(i)
try:
# if result_json['words_result'][k]['words'] in rubbish_word:
# del result_json['words_result'][k]
# restart = True
for rubbish_word_item in rubbish_words:
if operator.contains(result_json['words_result'][k]['words'], rubbish_word_item):
del result_json['words_result'][k]
restart = True
if k == (exam - 1):
restart = False
except:
pass
# 被删光管理
if len(result_json['words_result']) == 0:
num = num + 1
print('被我删光了 你来打我')
continue
# 改成函数 不晓得 为什么不行
# result_json = rubbish_word_fun(result_json, rubbish_word)
# for i in range(1, len(result_json['words_result'])):
# text += result_json['words_result'][i]['words']
# 第一个数据必不可能是topic相同
# 这里topic改变 应该留到限免去处理
# try:
# if topic != result_json['words_result'][0]['words']:
# text = ''
# except Exception as e:
# continue
try:
for lala in range(1, len(result_json['words_result'])):
# 这个是最后的text
if result_json['words_result'][lala]['words'] in topic_context:
continue
text += result_json['words_result'][lala]['words']
except Exception as e:
print(e)
# 这句话 只能走一次 的确直走一次
if i == num:
startTime = name[:-4]
topic = result_json['words_result'][0]['words']
# topic 处理
topic = topic_rechange(topic)
for lala in range(1, len(result_json['words_result'])):
text += result_json['words_result'][lala]['words']
print(startTime)
# 完成了改变
elif topic != result_json['words_result'][0]['words']:
new_dict['className'] = className
new_dict['main'] = main
new_dict['topic'] = topic
new_dict['content'] = text
new_dict['startTime'] = startTime
new_dict['endTime'] = name[:-4]
json_dit = topic + '.json'
json_add = os.path.join(text_address, json_dit)
print(new_dict)
# 成功啦
with open(json_add, 'w', encoding='utf8') as f:
json.dump(new_dict, f, ensure_ascii=False)
print('处理好了')
startTime = name[:-4]
new_dict = {}
text = ""
# 关注尾部
# 最后一个是抓住了
elif i == (len(pic_file_name) - 1):
new_dict['className'] = className
new_dict['main'] = main
new_dict['topic'] = topic
new_dict['content'] = text
new_dict['startTime'] = startTime
new_dict['endTime'] = name[:-4]
topic = topic.replace(':', '')
json_dit = topic + '.json'
json_add = os.path.join(text_address, json_dit)
print(json_add)
print(new_dict)
with open(json_add, 'w', encoding='utf8') as f:
json.dump(new_dict, f, ensure_ascii=False)
print('最后一个')
startTime = name[:-4]
new_dict = {}
text = ""
# new_dict['topic'] = result_json['words_result'][0]['words']
# print(len(result_json['words_result']))
topic = result_json['words_result'][0]['words']
# topic 处理
topic = topic_rechange(topic)
topic_context = []
for content in range(1, len(result_json['words_result'])):
topic_context.append(result_json['words_result'][content]['words'])
print(topic_context)