Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
# app.py
|
2 |
-
# Phiên bản hoàn chỉnh, đã
|
3 |
-
# Đã thêm các dòng print để chẩn đoán dữ liệu.
|
4 |
|
5 |
import os
|
6 |
import joblib
|
@@ -10,8 +9,9 @@ from flask import Flask, request, jsonify, render_template
|
|
10 |
from werkzeug.utils import secure_filename
|
11 |
import traceback
|
12 |
|
13 |
-
# --- Thư viện mới để đọc audio
|
14 |
from pydub import AudioSegment
|
|
|
15 |
|
16 |
# --- Cấu hình TensorFlow và các thư viện AI ---
|
17 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
@@ -106,19 +106,17 @@ def _create_spectrogram_image(y, sr):
|
|
106 |
print(f"Lỗi tạo ảnh spectrogram: {e}")
|
107 |
return np.zeros(SPECTROGRAM_SHAPE)
|
108 |
|
109 |
-
# --- HÀM XỬ LÝ AUDIO ĐÃ ĐƯỢC CẬP NHẬT ---
|
110 |
def process_audio_file(file_path):
|
111 |
"""
|
112 |
-
Hàm tổng hợp phiên bản mới: Dùng pydub
|
113 |
sau đó chuyển đổi sang định dạng mà librosa có thể xử lý an toàn.
|
114 |
"""
|
115 |
try:
|
116 |
-
# 1. Dùng pydub để mở file audio
|
117 |
audio = AudioSegment.from_file(file_path)
|
118 |
|
119 |
-
# 2.
|
120 |
-
# Chuẩn hóa âm lượng về một mức tiêu chuẩn (-20 dBFS).
|
121 |
-
# Điều này giúp giảm sự khác biệt về âm lượng giữa các bản ghi.
|
122 |
target_dbfs = -20.0
|
123 |
change_in_dbfs = target_dbfs - audio.dBFS
|
124 |
audio = audio.apply_gain(change_in_dbfs)
|
@@ -128,20 +126,25 @@ def process_audio_file(file_path):
|
|
128 |
audio = audio.set_frame_rate(SAMPLE_RATE)
|
129 |
|
130 |
# 4. Chuyển đổi audio của pydub thành mảng NumPy cho librosa
|
131 |
-
# Chuẩn hóa về khoảng [-1, 1]
|
132 |
samples = np.array(audio.get_array_of_samples()).astype(np.float32)
|
133 |
y = samples / (2**(audio.sample_width * 8 - 1))
|
134 |
|
135 |
-
# 5.
|
136 |
-
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
else:
|
139 |
-
|
140 |
|
141 |
-
#
|
142 |
-
traditional_features = _extract_traditional_features(
|
143 |
-
wav2vec_features = _extract_wav2vec_features(
|
144 |
-
spectrogram = _create_spectrogram_image(
|
145 |
|
146 |
return traditional_features, wav2vec_features, spectrogram
|
147 |
|
@@ -178,7 +181,7 @@ def predict():
|
|
178 |
return jsonify({'error': 'Không thể xử lý file audio.'}), 500
|
179 |
|
180 |
# ========== BẮT ĐẦU PHẦN GHI LOG CHẨN ĐOÁN ==========
|
181 |
-
print("\n--- BẮT ĐẦU CHẨN ĐOÁN DỮ LIỆU ĐẦU VÀO ---")
|
182 |
print(f"DEBUG | trad_feats stats: mean={np.mean(trad_feats):.2f}, std={np.std(trad_feats):.2f}, min={np.min(trad_feats):.2f}, max={np.max(trad_feats):.2f}")
|
183 |
print(f"DEBUG | w2v_feats stats: mean={np.mean(w2v_feats):.2f}, std={np.std(w2v_feats):.2f}, min={np.min(w2v_feats):.2f}, max={np.max(w2v_feats):.2f}")
|
184 |
print(f"DEBUG | spec_img stats: mean={np.mean(spec_img):.2f}, std={np.std(spec_img):.2f}, min={np.min(spec_img):.2f}, max={np.max(spec_img):.2f}")
|
|
|
1 |
# app.py
|
2 |
+
# Phiên bản hoàn chỉnh, đã thêm bước giảm nhiễu để cải thiện độ chính xác.
|
|
|
3 |
|
4 |
import os
|
5 |
import joblib
|
|
|
9 |
from werkzeug.utils import secure_filename
|
10 |
import traceback
|
11 |
|
12 |
+
# --- Thư viện mới để đọc audio và giảm nhiễu ---
|
13 |
from pydub import AudioSegment
|
14 |
+
import noisereduce as nr
|
15 |
|
16 |
# --- Cấu hình TensorFlow và các thư viện AI ---
|
17 |
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
|
|
|
106 |
print(f"Lỗi tạo ảnh spectrogram: {e}")
|
107 |
return np.zeros(SPECTROGRAM_SHAPE)
|
108 |
|
109 |
+
# --- HÀM XỬ LÝ AUDIO ĐÃ ĐƯỢC CẬP NHẬT VỚI BƯỚC GIẢM NHIỄU ---
|
110 |
def process_audio_file(file_path):
|
111 |
"""
|
112 |
+
Hàm tổng hợp phiên bản mới: Dùng pydub, chuẩn hóa âm lượng, giảm nhiễu,
|
113 |
sau đó chuyển đổi sang định dạng mà librosa có thể xử lý an toàn.
|
114 |
"""
|
115 |
try:
|
116 |
+
# 1. Dùng pydub để mở file audio
|
117 |
audio = AudioSegment.from_file(file_path)
|
118 |
|
119 |
+
# 2. Chuẩn hóa âm lượng về một mức tiêu chuẩn
|
|
|
|
|
120 |
target_dbfs = -20.0
|
121 |
change_in_dbfs = target_dbfs - audio.dBFS
|
122 |
audio = audio.apply_gain(change_in_dbfs)
|
|
|
126 |
audio = audio.set_frame_rate(SAMPLE_RATE)
|
127 |
|
128 |
# 4. Chuyển đổi audio của pydub thành mảng NumPy cho librosa
|
|
|
129 |
samples = np.array(audio.get_array_of_samples()).astype(np.float32)
|
130 |
y = samples / (2**(audio.sample_width * 8 - 1))
|
131 |
|
132 |
+
# 5. **BƯỚC MỚI: GIẢM NHIỄU (NOISE REDUCTION)**
|
133 |
+
# Thực hiện giảm nhiễu trên tín hiệu âm thanh
|
134 |
+
print("DEBUG | Bắt đầu giảm nhiễu...")
|
135 |
+
y_reduced_noise = nr.reduce_noise(y=y, sr=SAMPLE_RATE, prop_decrease=0.8)
|
136 |
+
print("DEBUG | Giảm nhiễu hoàn tất.")
|
137 |
+
|
138 |
+
# 6. Chuẩn hóa độ dài audio về MAX_SAMPLES (sử dụng tín hiệu đã giảm nhiễu)
|
139 |
+
if len(y_reduced_noise) > MAX_SAMPLES:
|
140 |
+
y_final = y_reduced_noise[:MAX_SAMPLES]
|
141 |
else:
|
142 |
+
y_final = np.pad(y_reduced_noise, (0, MAX_SAMPLES - len(y_reduced_noise)), mode='constant')
|
143 |
|
144 |
+
# 7. Trích xuất đồng thời các bộ đặc trưng (dùng tín hiệu cuối cùng)
|
145 |
+
traditional_features = _extract_traditional_features(y_final, SAMPLE_RATE)
|
146 |
+
wav2vec_features = _extract_wav2vec_features(y_final, SAMPLE_RATE)
|
147 |
+
spectrogram = _create_spectrogram_image(y_final, SAMPLE_RATE)
|
148 |
|
149 |
return traditional_features, wav2vec_features, spectrogram
|
150 |
|
|
|
181 |
return jsonify({'error': 'Không thể xử lý file audio.'}), 500
|
182 |
|
183 |
# ========== BẮT ĐẦU PHẦN GHI LOG CHẨN ĐOÁN ==========
|
184 |
+
print("\n--- BẮT ĐẦU CHẨN ĐOÁN DỮ LIỆU ĐẦU VÀO (SAU KHI GIẢM NHIỄU) ---")
|
185 |
print(f"DEBUG | trad_feats stats: mean={np.mean(trad_feats):.2f}, std={np.std(trad_feats):.2f}, min={np.min(trad_feats):.2f}, max={np.max(trad_feats):.2f}")
|
186 |
print(f"DEBUG | w2v_feats stats: mean={np.mean(w2v_feats):.2f}, std={np.std(w2v_feats):.2f}, min={np.min(w2v_feats):.2f}, max={np.max(w2v_feats):.2f}")
|
187 |
print(f"DEBUG | spec_img stats: mean={np.mean(spec_img):.2f}, std={np.std(spec_img):.2f}, min={np.min(spec_img):.2f}, max={np.max(spec_img):.2f}")
|