-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_recognition.py
41 lines (32 loc) · 1.32 KB
/
text_recognition.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
import whisper
from datetime import timedelta
model = whisper.load_model('small')
def sec_to_hour_min_sec(seconds):
dt = timedelta(seconds=seconds)
hours, remainder = divmod(dt.seconds, 3600)
minutes, seconds = divmod(remainder, 60)
return '{}:{:02d}:{:02d}'.format(hours, minutes, seconds)
async def async_text_recognition(path):
res = model.transcribe(path)
txt = []
for seg in res['segments']:
basic_txt = seg['text']
# txt_to_trans = seg['text']
# if trans.detect(txt_to_trans).lang == 'ko':
# print('[{0} ~ ] {1}'.format(sec_to_hour_min_sec(seg['start']), txt_to_trans))
# continue
# trans_text = trans.translate(seg['text'], dest='ko').text
try:
# 중복 확인
if basic_txt == basic_txt2:
continue
else:
print('[{0} ~ ] {1}'.format(sec_to_hour_min_sec(seg['start']),
basic_txt))
except:
print('[{0} ~ ] {1}'.format(sec_to_hour_min_sec(seg['start']),
basic_txt))
basic_txt2 = basic_txt
txt.append(str('[{0} ~ ] {1}'.format(sec_to_hour_min_sec(seg['start']),
basic_txt)))
return txt