Batch-wise Speech Transcription
238 for b in range(begin_idx,end_idx):
239
240 timestamp_xls_filename = os.path.join(output_dir,'transcribe_timestamp_{}_{:02d}.xlsx'.format('_'.join(suffix),b+1))
241 timestamp_writer = pd.ExcelWriter(timestamp_xls_filename)
242
243 ''' check batch xlsx status 1. complete task 2. proccessing yet finished 3. new '''
244 titles = []
245 if os.path.isfile(timestamp_xls_filename) and not(args.reset):
246 print('load {} ...'.format(timestamp_xls_filename))
247
248 book = load_workbook(timestamp_xls_filename)
249
250 for ws in book.worksheets:
251 mmm = re.match(r'(.+)\.wav_(.+)', ws.title)
252 titles.append(os.path.join(input_dir,mmm.group(1)+ '.wav'))
253 titles = sorted(list(set(titles)))
254 print('number of sheets: {}'.format(len(titles)))
255 if len(titles) == batch_size or (b == int(np.ceil(len(entireSpeechFileList)/batch_size)) and len(titles) == len(entireSpeechFileList) % batch_size):
256 continue
257 else:
258 book = load_workbook(timestamp_xls_filename)
259 timestamp_writer.book = book
260 timestamp_writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
261 speechFileQueue = sorted(list(set(entireSpeechFileList[b*batch_size:(b+1)*batch_size]) - set(titles)))
262
263 batch_transcribe_speechFile(speechFileQueue,existSpeechFileList,choices_list,timestamp_writer,args.verbose,args.nr,beta=args.beta)
264 else:
265 print('create new {} !!!'.format(timestamp_xls_filename))
266 speechFileQueue = entireSpeechFileList[b*batch_size:(b+1)*batch_size]
267
268 batch_transcribe_speechFile(speechFileQueue,existSpeechFileList,choices_list,timestamp_writer,args.verbose,args.nr,beta=args.beta)
行238-269是一個for loop,範圍range(begin_idx,end_idx)
是從begin_idx
到end_idx-1
。
行240-241是產生每個batch有時間資訊的辨識xlsx檔名timestamp_xls_filename
跟檔案物件timestamp_writer
。
行244呈現建立一個空的列表titles
,用來儲存xlsx裡面已辨識音頻檔案的名稱,來核對每個batch xlsx檔的執行情況。
每個batch xlsx檔有三種執行情況
- batch裡面每個音頻都辨識完成 (行255-256)
- batch裡面有些音頻辨識完成,有些還沒 (行257-263)
- batch還沒開始辨識 (行264-268)
行245檢查timestamp_xls_filename
檔是否存在跟是否要覆寫,如果為False,代表batch要重頭開始辨識(情況3),如果檔案存在,再檢查檔案裡面已辨識音頻檔的數量,如果跟batch_size
相同或者最後一個batch且數量與最後一個batch的音頻數量相同(行255),代表該batch為情況1,不然為情況2。
情況1,程式會跳過這個batch,執行下一個batch。
情況2,程式會找出還沒辨識的音頻檔(行261),呼叫函數batch_transcribe_speechFile()
辨識還沒辨識的音頻檔。
情況3,程式會從頭開始辨識batch裡的每一個音頻檔。