在我們的全球化世界中,跨越語言界線的溝通比以往任何時候都更加重要。在這篇文章中,我們將探討如何實施這項技術,讓溝通變得更包容、方便每個人。
程式碼可以在這裡取得
在我的 github 上
首先要做的是安裝相依性
blinker==1.8.2 cachetools==5.5.0 certifi==2024.8.30 chardet==3.0.4 charset-normalizer==3.4.0 click==8.1.7 colorama==0.4.6 Flask==3.0.3 google-api-core==2.22.0 google-auth==2.36.0 google-cloud-texttospeech==2.21.0 googleapis-common-protos==1.65.0 googletrans==4.0.0rc1 grpcio==1.67.1 grpcio-status==1.67.1 gTTS==2.5.3 h11==0.9.0 h2==3.2.0 hpack==3.0.0 hstspreload==2024.11.1 httpcore==0.9.1 httpx==0.13.3 hyperframe==5.2.0 idna==2.10 itsdangerous==2.2.0 Jinja2==3.1.4 Levenshtein==0.26.1 MarkupSafe==3.0.2 playsound==1.2.2 prompt_toolkit==3.0.48 proto-plus==1.25.0 protobuf==5.28.3 pyasn1==0.6.1 pyasn1_modules==0.4.1 PyAudio==0.2.14 python-Levenshtein==0.26.1 RapidFuzz==3.10.1 requests==2.32.3 rfc3986==1.5.0 rsa==4.9 sniffio==1.3.1 SpeechRecognition==3.11.0 typing_extensions==4.12.2 urllib3==2.2.3 wcwidth==0.2.13 Werkzeug==3.1.2 wit==6.0.1
音訊到文字轉換
from gtts import gTTS import playsound import os def speak_translation(text, lang): tts = gTTS(text=text, lang=lang) filename = "translation.mp3" tts.save(filename) playsound.playsound(filename) os.remove(filename)
Google雲端文字語音
from google.cloud import texttospeech def synthesize_speech(text, language_code="wo-WO", voice_name="wo-WO-Standard-A", output_file="output.mp3"): client = texttospeech.TextToSpeechClient() input_text = texttospeech.SynthesisInput(text=text) # Configurez la voix pour le Wolof voice = texttospeech.VoiceSelectionParams( language_code=language_code, name=voice_name, ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL, ) # Paramètres audio audio_config = texttospeech.AudioConfig( audio_encoding=texttospeech.AudioEncoding.MP3 ) # Synthèse vocale response = client.synthesize_speech( input=input_text, voice=voice, audio_config=audio_config ) # Sauvegarder le fichier audio with open(output_file, "wb") as out: out.write(response.audio_content) print(f"Audio content written to file {output_file}") # Utilisez cette fonction avec votre texte synthesize_speech("Bonjour, je teste la traduction en Wolof.", "wo-WO")
翻譯
from googletrans import Translator def translate_text(text, target_lang): try: translator = Translator() translation = translator.translate(text, dest=target_lang) print(f"Traduction : {translation.text}") return translation.text except Exception as e: print(f"Erreur lors de la traduction : {e}") return "Traduction non disponible"
語音偵測
import speech_recognition as sr def record_audio(): recognizer = sr.Recognizer() with sr.Microphone() as source: print("Parlez maintenant...") audio = recognizer.listen(source) try: text = recognizer.recognize_google(audio, language="fr-FR") print(f"Vous avez dit : {text}") return text except sr.UnknownValueError: print("Désolé, je n'ai pas compris.") except sr.RequestError as e: print(f"Erreur de service : {e}")
Witai 參數:
您必須前往 Meta API (Facebook) 來建立您的代幣
import requests WIT_AI_TOKEN = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' def send_to_wit(text): headers = {'Authorization': f'Bearer {WIT_AI_TOKEN}'} response = requests.get(f'https://api.wit.ai/message?v=20230414&q={text}', headers=headers) return response.json()
主檔
from flask import Flask, request, jsonify from convertion_audio_to_text import speak_translation from translation import translate_text from voice_detection import record_audio from witai_params import send_to_wit import Levenshtein app = Flask(__name__) # Langues disponibles AVAILABLE_LANGUAGES = { "sw": "Swahili", "wo": "Wolof", "fon": "Fon", "en": "Anglais", "fr": "Français" } def calculate_score(reference_text, user_text): similarity = Levenshtein.ratio(reference_text.lower(), user_text.lower()) * 100 return round(similarity, 2) @app.route('/available_languages', methods=['GET']) def available_languages(): """Retourne les langues disponibles pour la traduction.""" return jsonify(AVAILABLE_LANGUAGES) @app.route('/process_audio', methods=['POST']) def process_audio(): """Traite l'audio, traduit le texte et évalue la prononciation.""" try: # Étape 1 : Récupérer la langue cible depuis la requête target_lang = request.json.get('target_lang') if not target_lang: return jsonify({"error": "Paramètre 'target_lang' manquant"}), 400 if target_lang not in AVAILABLE_LANGUAGES: return jsonify({ "error": f"Langue cible '{target_lang}' non supportée.", "available_languages": AVAILABLE_LANGUAGES # Retourner la liste des langues disponibles }), 400 # Étape 2 : Traduire le texte initial text = record_audio() if not text: return jsonify({"error": "No audio detected or transcription failed"}), 400 wit_response = send_to_wit(text) print("Wit.ai Response:", wit_response) translation = translate_text(text, target_lang) speak_translation(translation, lang=target_lang) # Étape 3 : Boucle de répétition pour évaluer la prononciation score = 0 while score = 80: message = "Bravo! Félicitations, vous êtes un génie!" return jsonify({ "original_text": text, "wit_response": wit_response, "translated_text": translation, "repeated_text": repeat_text, "score": score, "message": message }), 200 elif score <p>如今,設計機器人來解決我們日常生活中的複雜問題變得越來越容易。不過,這並不排除自學語言的重要性。使用 BotAI 等技術進行即時語音翻譯應該主要是為了豐富我們在複雜環境中的互動。透過將這些工具與個人語言學習結合,我們可以促進更有效的溝通,同時促進個人語言財富。 </p> <p>程式碼可以在這裡取得<br> 在我的 github 上</p>
以上是如何使用 witai 創建語音翻譯機器人的詳細內容。更多資訊請關注PHP中文網其他相關文章!

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Python3.6環境下加載Pickle文件報錯:ModuleNotFoundError:Nomodulenamed...

如何解決jieba分詞在景區評論分析中的問題?當我們在進行景區評論分析時,往往會使用jieba分詞工具來處理文�...

如何使用正則表達式匹配到第一個閉合標籤就停止?在處理HTML或其他標記語言時,常常需要使用正則表達式來�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。