{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "source": [ "!pip install SpeechRecognition pydub" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Bb_GmLxLYPSl", "outputId": "542a41c4-063d-4766-b2bc-9cab97080256" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Collecting SpeechRecognition\n", " Downloading speechrecognition-3.14.5-py3-none-any.whl.metadata (30 kB)\n", "Requirement already satisfied: pydub in /usr/local/lib/python3.12/dist-packages (0.25.1)\n", "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.12/dist-packages (from SpeechRecognition) (4.15.0)\n", "Downloading speechrecognition-3.14.5-py3-none-any.whl (32.9 MB)\n", "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m32.9/32.9 MB\u001b[0m \u001b[31m22.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", "\u001b[?25hInstalling collected packages: SpeechRecognition\n", "Successfully installed SpeechRecognition-3.14.5\n" ] } ] }, { "cell_type": "code", "source": [ "from IPython.display import HTML, display\n", "import base64\n", "import json\n", "import numpy as np\n", "import soundfile as sf\n", "from google.colab import output\n", "\n", "RECORD = \"\"\"\n", "\n", "\n", "\n", "\"\"\"\n", "\n", "display(HTML(RECORD))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 40 }, "id": "PhN27gCwYYxs", "outputId": "c30a588f-c16d-4d31-dd9e-383ba4c20697" }, "execution_count": null, "outputs": [ { "output_type": "display_data", "data": { "text/plain": [ "" ], "text/html": [ "\n", "\n", "\n", "\n" ] }, "metadata": {} } ] }, { "cell_type": "code", "source": [ "def save_audio(base64data):\n", " audio_bytes = base64.b64decode(base64data)\n", " with open(\"audio.wav\", \"wb\") as f:\n", " f.write(audio_bytes)\n", "\n", "output.register_callback('notebook.save_audio', save_audio)" ], "metadata": { "id": "hkVcUaEWYY1R" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "import speech_recognition as sr\n", "from pydub import AudioSegment\n", "\n", "# Convert the audio file to a standard WAV format\n", "# The MediaRecorder might save in a format that speech_recognition doesn't natively handle\n", "try:\n", " audio_file_path = \"audio.wav\"\n", " sound = AudioSegment.from_file(audio_file_path)\n", " sound.export(audio_file_path, format=\"wav\")\n", "except Exception as e:\n", " print(f\"Error converting audio file: {e}\")\n", "\n", "r = sr.Recognizer()\n", "with sr.AudioFile(\"audio.wav\") as source:\n", " audio = r.record(source)\n", "\n", "try:\n", " text = r.recognize_google(audio, language=\"id-ID\")\n", " print(\"Hasil Pengenalan:\", text)\n", "except sr.UnknownValueError:\n", " print(\"Suara tidak dikenali\")\n", "except sr.RequestError as e:\n", " print(f\"Could not request results from Google Speech Recognition service; {e}\")\n", "except Exception as e:\n", " print(f\"An unexpected error occurred: {e}\")" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_L5FGiYPYj9i", "outputId": "dea16927-be11-457d-bc44-f8baf5e02e3c" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Hasil Pengenalan: data komputer komputer komputer\n" ] } ] } ] }