timestamp_utils.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # -*- encoding: utf-8 -*-
  2. # Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
  3. # MIT License (https://opensource.org/licenses/MIT)
  4. import numpy as np
  5. def time_stamp_lfr6_onnx(us_cif_peak, char_list, begin_time=0.0, total_offset=-1.5):
  6. if not len(char_list):
  7. return []
  8. START_END_THRESHOLD = 5
  9. MAX_TOKEN_DURATION = 30
  10. TIME_RATE = 10.0 * 6 / 1000 / 3 # 3 times upsampled
  11. cif_peak = us_cif_peak.reshape(-1)
  12. num_frames = cif_peak.shape[-1]
  13. if char_list[-1] == '</s>':
  14. char_list = char_list[:-1]
  15. # char_list = [i for i in text]
  16. timestamp_list = []
  17. new_char_list = []
  18. # for bicif model trained with large data, cif2 actually fires when a character starts
  19. # so treat the frames between two peaks as the duration of the former token
  20. fire_place = np.where(cif_peak>1.0-1e-4)[0] + total_offset # np format
  21. num_peak = len(fire_place)
  22. assert num_peak == len(char_list) + 1 # number of peaks is supposed to be number of tokens + 1
  23. # begin silence
  24. if fire_place[0] > START_END_THRESHOLD:
  25. # char_list.insert(0, '<sil>')
  26. timestamp_list.append([0.0, fire_place[0]*TIME_RATE])
  27. new_char_list.append('<sil>')
  28. # tokens timestamp
  29. for i in range(len(fire_place)-1):
  30. new_char_list.append(char_list[i])
  31. if i == len(fire_place)-2 or MAX_TOKEN_DURATION < 0 or fire_place[i+1] - fire_place[i] < MAX_TOKEN_DURATION:
  32. timestamp_list.append([fire_place[i]*TIME_RATE, fire_place[i+1]*TIME_RATE])
  33. else:
  34. # cut the duration to token and sil of the 0-weight frames last long
  35. _split = fire_place[i] + MAX_TOKEN_DURATION
  36. timestamp_list.append([fire_place[i]*TIME_RATE, _split*TIME_RATE])
  37. timestamp_list.append([_split*TIME_RATE, fire_place[i+1]*TIME_RATE])
  38. new_char_list.append('<sil>')
  39. # tail token and end silence
  40. if num_frames - fire_place[-1] > START_END_THRESHOLD:
  41. _end = (num_frames + fire_place[-1]) / 2
  42. timestamp_list[-1][1] = _end*TIME_RATE
  43. timestamp_list.append([_end*TIME_RATE, num_frames*TIME_RATE])
  44. new_char_list.append("<sil>")
  45. else:
  46. timestamp_list[-1][1] = num_frames*TIME_RATE
  47. if begin_time: # add offset time in model with vad
  48. for i in range(len(timestamp_list)):
  49. timestamp_list[i][0] = timestamp_list[i][0] + begin_time / 1000.0
  50. timestamp_list[i][1] = timestamp_list[i][1] + begin_time / 1000.0
  51. assert len(new_char_list) == len(timestamp_list)
  52. res_str = ""
  53. for char, timestamp in zip(new_char_list, timestamp_list):
  54. res_str += "{} {} {};".format(char, timestamp[0], timestamp[1])
  55. res = []
  56. for char, timestamp in zip(new_char_list, timestamp_list):
  57. if char != '<sil>':
  58. res.append([int(timestamp[0] * 1000), int(timestamp[1] * 1000)])
  59. return res_str, res