timestamp_tools.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import torch
  2. import copy
  3. import logging
  4. import numpy as np
  5. from typing import Any, List, Tuple, Union
  6. def ts_prediction_lfr6_standard(us_alphas,
  7. us_peaks,
  8. char_list,
  9. vad_offset=0.0,
  10. force_time_shift=-1.5
  11. ):
  12. if not len(char_list):
  13. return []
  14. START_END_THRESHOLD = 5
  15. MAX_TOKEN_DURATION = 12
  16. TIME_RATE = 10.0 * 6 / 1000 / 3 # 3 times upsampled
  17. if len(us_alphas.shape) == 2:
  18. _, peaks = us_alphas[0], us_peaks[0] # support inference batch_size=1 only
  19. else:
  20. _, peaks = us_alphas, us_peaks
  21. num_frames = peaks.shape[0]
  22. if char_list[-1] == '</s>':
  23. char_list = char_list[:-1]
  24. timestamp_list = []
  25. new_char_list = []
  26. # for bicif model trained with large data, cif2 actually fires when a character starts
  27. # so treat the frames between two peaks as the duration of the former token
  28. fire_place = torch.where(peaks>1.0-1e-4)[0].cpu().numpy() + force_time_shift # total offset
  29. num_peak = len(fire_place)
  30. assert num_peak == len(char_list) + 1 # number of peaks is supposed to be number of tokens + 1
  31. # begin silence
  32. if fire_place[0] > START_END_THRESHOLD:
  33. # char_list.insert(0, '<sil>')
  34. timestamp_list.append([0.0, fire_place[0]*TIME_RATE])
  35. new_char_list.append('<sil>')
  36. # tokens timestamp
  37. for i in range(len(fire_place)-1):
  38. new_char_list.append(char_list[i])
  39. if MAX_TOKEN_DURATION < 0 or fire_place[i+1] - fire_place[i] <= MAX_TOKEN_DURATION:
  40. timestamp_list.append([fire_place[i]*TIME_RATE, fire_place[i+1]*TIME_RATE])
  41. else:
  42. # cut the duration to token and sil of the 0-weight frames last long
  43. _split = fire_place[i] + MAX_TOKEN_DURATION
  44. timestamp_list.append([fire_place[i]*TIME_RATE, _split*TIME_RATE])
  45. timestamp_list.append([_split*TIME_RATE, fire_place[i+1]*TIME_RATE])
  46. new_char_list.append('<sil>')
  47. # tail token and end silence
  48. # new_char_list.append(char_list[-1])
  49. if num_frames - fire_place[-1] > START_END_THRESHOLD:
  50. _end = (num_frames + fire_place[-1]) * 0.5
  51. # _end = fire_place[-1]
  52. timestamp_list[-1][1] = _end*TIME_RATE
  53. timestamp_list.append([_end*TIME_RATE, num_frames*TIME_RATE])
  54. new_char_list.append("<sil>")
  55. else:
  56. timestamp_list[-1][1] = num_frames*TIME_RATE
  57. if vad_offset: # add offset time in model with vad
  58. for i in range(len(timestamp_list)):
  59. timestamp_list[i][0] = timestamp_list[i][0] + vad_offset / 1000.0
  60. timestamp_list[i][1] = timestamp_list[i][1] + vad_offset / 1000.0
  61. res_txt = ""
  62. for char, timestamp in zip(new_char_list, timestamp_list):
  63. res_txt += "{} {} {};".format(char, str(timestamp[0]+0.0005)[:5], str(timestamp[1]+0.0005)[:5])
  64. res = []
  65. for char, timestamp in zip(new_char_list, timestamp_list):
  66. if char != '<sil>':
  67. res.append([int(timestamp[0] * 1000), int(timestamp[1] * 1000)])
  68. return res_txt, res
  69. def time_stamp_sentence(punc_id_list, time_stamp_postprocessed, text_postprocessed):
  70. res = []
  71. if text_postprocessed is None:
  72. return res
  73. if time_stamp_postprocessed is None:
  74. return res
  75. if len(time_stamp_postprocessed) == 0:
  76. return res
  77. if len(text_postprocessed) == 0:
  78. return res
  79. if punc_id_list is None or len(punc_id_list) == 0:
  80. res.append({
  81. 'text': text_postprocessed.split(),
  82. "start": time_stamp_postprocessed[0][0],
  83. "end": time_stamp_postprocessed[-1][1]
  84. })
  85. return res
  86. if len(punc_id_list) != len(time_stamp_postprocessed):
  87. res.append({
  88. 'text': text_postprocessed.split(),
  89. "start": time_stamp_postprocessed[0][0],
  90. "end": time_stamp_postprocessed[-1][1]
  91. })
  92. return res
  93. sentence_text = ''
  94. sentence_start = time_stamp_postprocessed[0][0]
  95. texts = text_postprocessed.split()
  96. for i in range(len(punc_id_list)):
  97. sentence_text += texts[i]
  98. if punc_id_list[i] == 2:
  99. sentence_text += ','
  100. res.append({
  101. 'text': sentence_text,
  102. "start": sentence_start,
  103. "end": time_stamp_postprocessed[i][1]
  104. })
  105. sentence_text = ''
  106. sentence_start = time_stamp_postprocessed[i][1]
  107. elif punc_id_list[i] == 3:
  108. sentence_text += '.'
  109. res.append({
  110. 'text': sentence_text,
  111. "start": sentence_start,
  112. "end": time_stamp_postprocessed[i][1]
  113. })
  114. sentence_text = ''
  115. sentence_start = time_stamp_postprocessed[i][1]
  116. return res