Update app.py
Browse files
app.py
CHANGED
@@ -88,32 +88,27 @@ class AudioProcessor:
|
|
88 |
def get_best_match(self, target_time, diarization_output):
|
89 |
target_start_time = target_time['start_time']
|
90 |
target_end_time = target_time['end_time']
|
91 |
-
|
92 |
-
# 确保 diarization_output 是 Annotation 类型
|
93 |
-
if not isinstance(diarization_output, Annotation):
|
94 |
-
print(f"Error: Expected an Annotation object, but got {type(diarization_output)}")
|
95 |
-
return None # 或者返回一个空的默认值
|
96 |
-
|
97 |
# 用于存储每个说话人时间段的重叠比例
|
98 |
speaker_segments = []
|
99 |
for segment, label in diarization_output.itertracks(yield_label=True):
|
100 |
try:
|
101 |
start_seconds = segment.start
|
102 |
end_seconds = segment.end
|
103 |
-
|
104 |
# 计算目标音频时间段和说话人时间段的重叠时间
|
105 |
overlap = self.calculate_overlap(target_start_time, target_end_time, start_seconds, end_seconds)
|
106 |
overlap_ratio = overlap / (end_seconds - start_seconds)
|
107 |
-
|
108 |
# 记录说话人标签和重叠比例
|
109 |
speaker_segments.append((label, overlap_ratio, start_seconds, end_seconds))
|
110 |
-
|
111 |
except Exception as e:
|
112 |
print(f"处理行时出错: '{segment}'. 错误: {e}")
|
113 |
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
|
118 |
# 获取该说话人除了目标语音时间段外的所有时间段
|
119 |
def get_speaker_time_segments(self, diarization_output, target_time, speaker_label):
|
@@ -148,7 +143,7 @@ class AudioProcessor:
|
|
148 |
# 执行说话人分离
|
149 |
diarization_result = self.diarize_audio("final_output.wav")
|
150 |
|
151 |
-
if isinstance(diarization_result,
|
152 |
return diarization_result, None # 出错时返回错误信息
|
153 |
else:
|
154 |
# 获取最佳匹配的说话人标签和时间段
|
|
|
88 |
def get_best_match(self, target_time, diarization_output):
|
89 |
target_start_time = target_time['start_time']
|
90 |
target_end_time = target_time['end_time']
|
91 |
+
|
|
|
|
|
|
|
|
|
|
|
92 |
# 用于存储每个说话人时间段的重叠比例
|
93 |
speaker_segments = []
|
94 |
for segment, label in diarization_output.itertracks(yield_label=True):
|
95 |
try:
|
96 |
start_seconds = segment.start
|
97 |
end_seconds = segment.end
|
98 |
+
|
99 |
# 计算目标音频时间段和说话人时间段的重叠时间
|
100 |
overlap = self.calculate_overlap(target_start_time, target_end_time, start_seconds, end_seconds)
|
101 |
overlap_ratio = overlap / (end_seconds - start_seconds)
|
102 |
+
|
103 |
# 记录说话人标签和重叠比例
|
104 |
speaker_segments.append((label, overlap_ratio, start_seconds, end_seconds))
|
105 |
+
|
106 |
except Exception as e:
|
107 |
print(f"处理行时出错: '{segment}'. 错误: {e}")
|
108 |
|
109 |
+
# 按照重叠比例排序,返回重叠比例最大的一段
|
110 |
+
best_match = max(speaker_segments, key=lambda x: x[1], default=None)
|
111 |
+
return best_match
|
112 |
|
113 |
# 获取该说话人除了目标语音时间段外的所有时间段
|
114 |
def get_speaker_time_segments(self, diarization_output, target_time, speaker_label):
|
|
|
143 |
# 执行说话人分离
|
144 |
diarization_result = self.diarize_audio("final_output.wav")
|
145 |
|
146 |
+
if isinstance(diarization_result, Annotation) and diarization_result.startswith("错误"):
|
147 |
return diarization_result, None # 出错时返回错误信息
|
148 |
else:
|
149 |
# 获取最佳匹配的说话人标签和时间段
|