{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Speechrecognition on Dutch data\n", "\n", "* We follow the tutorial on " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Solving environment: \\ ^C\n", "failed\n", "\n", "CondaError: KeyboardInterrupt\n", "\n" ] } ], "source": [ "# Install a conda package in the current Jupyter kernel\n", "import sys\n", "!{sys.executable} -m pip install SpeechRecognition" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " % Total % Received % Xferd Average Speed Time Time Time Current\n", " Dload Upload Total Spent Left Speed\n", "100 3173k 100 3173k 0 0 3173k 0 0:00:01 0:00:01 --:--:-- 2397k\n" ] } ], "source": [ "# get an example file\n", "\n", "!curl -O https://raw.githubusercontent.com/realpython/python-speech-recognition/master/audio_files/harvard.wav" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.8.1'" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import speech_recognition as sr\n", ">>> sr.__version__" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "r = sr.Recognizer()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ ">>> harvard = sr.AudioFile('harvard.wav')\n", ">>> with harvard as source:\n", "... audio = r.record(source)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'the still smell of old beer lingers it takes heat to bring out the order a cold dip restores health and Zest a salt pickled tastes fine with him tacos Al pastor are my favourite exist for food is the hot cross bun'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ ">>> r.recognize_google(audio)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Nu voor nederlandse data" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " % Total % Received % Xferd Average Speed Time Time Time Current\n", " Dload Upload Total Spent Left Speed\n", "100 713k 100 713k 0 0 713k 0 0:00:01 --:--:-- 0:00:01 7285k\n" ] } ], "source": [ "! curl -O http://lands.let.ru.nl/cgn/sound/fn000040.wav" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "dh= sr.AudioFile('fn000040.wav')\n", "with dh as source:\n", " audio=r.record(source)" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'telefoon ziekenhuis in Amsterdam is Danny Blind vanmiddag aan zijn rechterknie onderzocht er is gebleken dat er geen verder verlies van het kraakbeen is opgetreden wel Is er wat irritatie aan de binnenmeniscus geconstateerd Er zal per wedstrijd dan ook worden gekeken of Danny Blind vanaf nu inzetbaar is maar er werd eerste feest voor helemaal nooit meer kunnen spelen Nou dat valt dus gelukkig mee ben ik erg blij mee persoonlijk af en zullen we maar zeggen'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "r.recognize_google(audio,language='nl-NL')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dit is heel erg goed!\n", "\n", "Transcript van \n", ">in het VU-ziekenhuis in Amsterdam || is Danny Blind vanmiddag aan zijn rechter knie onderzocht. || er is gebleken dat er geen verder verlies || van het kraakbeen is opgetreden. || wel is er wat irritatie aan de binnenmeniscus geconstateerd. || er zal per wedstrijd dan ook worden gekeken || of Danny Blind vanaf nu inzetbaar is || maar er werd eerst gevreesd voor helemaal nooit meer kunnen spelen.|| nou dat valt dus gelukkig mee. || ben 'k erg blij mee persoonlijk als fan || zullen we maar zeggen. ||" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-rw-r--r-- 1 admin staff 65M Mar 27 15:30 track2.wav\r\n" ] } ], "source": [ "# Convert an mp3 or mp4 video file to a wav file. \n", "# Easy on the mac \n", "# \n", "!afconvert /Users/admin/Downloads/2015-10-12-19-17-58-Gemeenteraad_sd.mp4 track2.wav -d LEI32\n", "!ls -lh track2.wav" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "collapsed": true }, "outputs": [], "source": [ "dh= sr.AudioFile('/Users/admin/Downloads/track2.wav')\n", "with dh as source:\n", " audio1=r.record(source,duration=30) # met duration kan je de tijd van het fragment instellen\n", " audio2=r.record(source,duration=30)\n", " audio3=r.record(source,duration=30)\n", " audio4=r.record(source,duration=60)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Dutch gemeenteraad data\n", "\n", "* Voorbeeld: \n", "* Ik heb de hele file gedoqwnload en ook een stukje video (De opening). (track2.wav)\n", "* Op de hele track2.wav kreeg ik een broken pipe error\n", "* Op een stukje van minder dan een minuut gaat het \"perfect\".\n", "* Op een stuk van 1 minuten keerg ik ook een broken pipe" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "dan zijn we bij agenda punt 3 Dat is spreekrecht van de burgers mevrouw Stienstra en de heer De Geus die hebben beide gevraagd om op dat punt 12 dat gaat over Huisduinen naar de stelling te mogen inspreken en dat kan natuurlijk en we hebben ook wat documentatie gekregen Als ik het goed heb was dat van de heer De Geus eerst mevrouw Stienstra\n", "CPU times: user 25.9 ms, sys: 27.3 ms, total: 53.2 ms\n", "Wall time: 5.78 s\n", "als in spreekster Ja als je daar plaats van nemen mevrouw en je kent de spelregels paar minuten per minuut maximaal 5 Ja ik zou het gewoon heel erg jammer vinden als de plek voor het pand\n", "CPU times: user 26 ms, sys: 26.7 ms, total: 52.8 ms\n", "Wall time: 5.27 s\n", "7 oké als het helemaal wordt volgebouwd met name omdat het gewoon een historisch pand is en ik zou graag een plaatje rond willen laten gaan dit is het pand waar het om gaat zijn zoals de tekeningen nu uitziet wordt het dan de voorkant wil je eigenlijk compleet volgebouwd B12 nw13 ja als die misschien weggelaten ze ook\n", "CPU times: user 22 ms, sys: 23.2 ms, total: 45.2 ms\n", "Wall time: 5.68 s\n" ] }, { "ename": "RequestError", "evalue": "recognition request failed: Bad Gateway", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mHTTPError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m~/anaconda/envs/python3.6/lib/python3.6/site-packages/speech_recognition/__init__.py\u001b[0m in \u001b[0;36mrecognize_google\u001b[0;34m(self, audio_data, key, language, show_all)\u001b[0m\n\u001b[1;32m 839\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 840\u001b[0;31m \u001b[0mresponse\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0murlopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrequest\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moperation_timeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 841\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mHTTPError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/anaconda/envs/python3.6/lib/python3.6/urllib/request.py\u001b[0m in \u001b[0;36murlopen\u001b[0;34m(url, data, timeout, cafile, capath, cadefault, context)\u001b[0m\n\u001b[1;32m 222\u001b[0m \u001b[0mopener\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0m_opener\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 223\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mopener\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 224\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/anaconda/envs/python3.6/lib/python3.6/urllib/request.py\u001b[0m in \u001b[0;36mopen\u001b[0;34m(self, fullurl, data, timeout)\u001b[0m\n\u001b[1;32m 531\u001b[0m \u001b[0mmeth\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgetattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mprocessor\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmeth_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 532\u001b[0;31m \u001b[0mresponse\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmeth\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreq\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresponse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 533\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/anaconda/envs/python3.6/lib/python3.6/urllib/request.py\u001b[0m in \u001b[0;36mhttp_response\u001b[0;34m(self, request, response)\u001b[0m\n\u001b[1;32m 641\u001b[0m response = self.parent.error(\n\u001b[0;32m--> 642\u001b[0;31m 'http', request, response, code, msg, hdrs)\n\u001b[0m\u001b[1;32m 643\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/anaconda/envs/python3.6/lib/python3.6/urllib/request.py\u001b[0m in \u001b[0;36merror\u001b[0;34m(self, proto, *args)\u001b[0m\n\u001b[1;32m 569\u001b[0m \u001b[0margs\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mdict\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'default'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'http_error_default'\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0morig_args\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 570\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_call_chain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 571\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/anaconda/envs/python3.6/lib/python3.6/urllib/request.py\u001b[0m in \u001b[0;36m_call_chain\u001b[0;34m(self, chain, kind, meth_name, *args)\u001b[0m\n\u001b[1;32m 503\u001b[0m \u001b[0mfunc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mgetattr\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhandler\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmeth_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 504\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0margs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 505\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mresult\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m~/anaconda/envs/python3.6/lib/python3.6/urllib/request.py\u001b[0m in \u001b[0;36mhttp_error_default\u001b[0;34m(self, req, fp, code, msg, hdrs)\u001b[0m\n\u001b[1;32m 649\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mhttp_error_default\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mreq\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfp\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhdrs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 650\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mHTTPError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mreq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfull_url\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcode\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmsg\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mhdrs\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mfp\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 651\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mHTTPError\u001b[0m: HTTP Error 502: Bad Gateway", "\nDuring handling of the above exception, another exception occurred:\n", "\u001b[0;31mRequestError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n", "\u001b[0;32m~/anaconda/envs/python3.6/lib/python3.6/site-packages/speech_recognition/__init__.py\u001b[0m in \u001b[0;36mrecognize_google\u001b[0;34m(self, audio_data, key, language, show_all)\u001b[0m\n\u001b[1;32m 840\u001b[0m \u001b[0mresponse\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0murlopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrequest\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0moperation_timeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 841\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mHTTPError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 842\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mRequestError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"recognition request failed: {}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreason\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 843\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mURLError\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 844\u001b[0m \u001b[0;32mraise\u001b[0m \u001b[0mRequestError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"recognition connection failed: {}\"\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0me\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreason\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mRequestError\u001b[0m: recognition request failed: Bad Gateway" ] } ], "source": [ "for f in [audio1,audio2,audio3,audio4]:\n", " %time print(r.recognize_google(f,language='nl-NL'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Conclusie\n", "\n", "* Google api werkt niet goed want max 50 requests per dag, dus erg weinig, en alleen voor korte stukjes\n", "* CMU Sphinx heeft een offline SR die ook een nederlands taal model heeft...." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }