diff options
Diffstat (limited to 'asr.py')
| -rw-r--r-- | asr.py | 20 |
1 files changed, 17 insertions, 3 deletions
| @@ -8,10 +8,19 @@ from tqdm import tqdm | |||
| 8 | 8 | ||
| 9 | # converts TimestampedResult to JSON-serializable dictionary | 9 | # converts TimestampedResult to JSON-serializable dictionary |
| 10 | def result_to_dict(result, offset_seconds): | 10 | def result_to_dict(result, offset_seconds): |
| 11 | token_map = [ | ||
| 12 | [token, round(ts + offset_seconds, 2)] | ||
| 13 | for token, ts in zip(result.tokens, result.timestamps) | ||
| 14 | ] | ||
| 15 | |||
| 16 | start_time = token_map[0][1] if token_map else offset_seconds | ||
| 17 | end_time = token_map[-1][1] if token_map else offset_seconds | ||
| 18 | |||
| 11 | return { | 19 | return { |
| 12 | "text": result.text, | 20 | "text": result.text, |
| 13 | "tokens": result.tokens, | 21 | "start_time": start_time, |
| 14 | "timestamps": [ts + offset_seconds for ts in result.timestamps] | 22 | "end_time": end_time, |
| 23 | "token_map": token_map | ||
| 15 | } | 24 | } |
| 16 | 25 | ||
| 17 | def main(input_file, output_file): | 26 | def main(input_file, output_file): |
| @@ -56,7 +65,12 @@ def main(input_file, output_file): | |||
| 56 | transcript_results = model.recognize(np.copy(chunk)) | 65 | transcript_results = model.recognize(np.copy(chunk)) |
| 57 | 66 | ||
| 58 | if transcript_results: | 67 | if transcript_results: |
| 59 | for result in transcript_results: | 68 | if not isinstance(transcript_results, list): |
| 69 | results_list = [transcript_results] | ||
| 70 | else: | ||
| 71 | results_list = transcript_results | ||
| 72 | |||
| 73 | for result in results_list: | ||
| 60 | full_transcript_data.append(result_to_dict(result, chunk_offset_seconds)) | 74 | full_transcript_data.append(result_to_dict(result, chunk_offset_seconds)) |
| 61 | except Exception as e: | 75 | except Exception as e: |
| 62 | print(f" Error processing chunk {i + 1}: {e}") | 76 | print(f" Error processing chunk {i + 1}: {e}") |
