File size: 1,316 Bytes
23590ba 17fd256 23590ba 17fd256 981b713 62ca4e5 981b713 17fd256 6432060 17fd256 fe652f1 17fd256 ceda7cb 17fd256 9225a67 794fd6d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import os
import json
import numpy as np
from vosk import Model, KaldiRecognizer
from flask import Flask
from flask_socketio import SocketIO, emit
# Cấu hình Flask & WebSocket
app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins="*")
# Kiểm tra và load model Vosk
MODEL_PATH = "model/vosk-model"
if not os.path.exists(MODEL_PATH):
raise Exception("❌ Model Vosk không tìm thấy!")
print("✅ Đang tải model Vosk...")
model = Model(MODEL_PATH)
# Tạo bộ nhận diện giọng nói
rec = KaldiRecognizer(model, 16000)
# Xử lý dữ liệu âm thanh nhận từ client
@socketio.on("audio_chunk")
def handle_audio(audio_data):
print(f"📩 Nhận dữ liệu âm thanh: {len(audio_data)} bytes")
# Chuyển đổi dữ liệu âm thanh thành numpy array
audio_np = np.frombuffer(audio_data, dtype=np.int16)
# Nhận diện giọng nói
if rec.AcceptWaveform(audio_np.tobytes()):
result = json.loads(rec.Result())
text = result.get("text", "")
print(f"📤 Kết quả nhận diện: {text}")
emit("stt_result", {"text": text}) # Gửi kết quả về client theo thời gian thực
# Chạy Flask server
if __name__ == "__main__":
socketio.run(app, host="0.0.0.0", port=5000, debug=True, allow_unsafe_werkzeug=True)
|