{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test de reconocimiento de voz para instrucciones de piloto:\n",
    "\n",
    "Se usa Google Cloud Platform, servicio que es de pago y habrá que evaluar sus costes, así como mejorar las comunicaciones para un correcto procesado vía internet."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Microfono activo...\n",
      "Esperando instrucciones de piloto.\n",
      "\n",
      ".\n",
      ".\n",
      ".\n",
      "..\n",
      "Audio o instrucción incorrecta\n",
      ".\n",
      "..\n",
      "Instrucción: [Huecco ]\n",
      ".\n",
      "..\n",
      "Instrucción: [simple ]\n",
      ".\n",
      "..\n",
      "Instrucción: [multicapa ]\n",
      ".\n",
      "..\n",
      "Instrucción: [incidencia ]\n",
      ".\n",
      ".\n",
      ".\n",
      "..\n",
      "Instrucción: [salir ]\n",
      "\n",
      "Ha solicitado terminar trabajo...\n",
      "['Huecco', 'simple', 'multicapa', 'incidencia', 'salir']\n"
     ]
    }
   ],
   "source": [
    "import speech_recognition as sr\n",
    "\n",
    "# obtain audio from the microphone\n",
    "#r = sr.Recognizer()\n",
    "#with sr.Microphone() as source:\n",
    "    #print(\"Microfono activo...\\nEsperando instrucciones de piloto.\\n\")\n",
    "    #audio = r.listen(source)\n",
    "\n",
    "# recognize speech using Google Cloud Speech\n",
    "GOOGLE_CLOUD_SPEECH_CREDENTIALS = r\"\"\"{\n",
    "  \"type\": \"service_account\",\n",
    "  \"project_id\": \"airvant-experimentacion\",\n",
    "  \"private_key_id\": \"221ddc0fe8af899abc52777956d60c60d2376897\",\n",
    "  \"private_key\": \"-----BEGIN PRIVATE KEY-----\\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQDD3dAotpzG5srm\\ne/71UW1+moIXJdIDdmWXuDNBDy+/OFMQQrTJiitdyFfoJ+hC2cN65Hn3JeoT3v31\\nBwfJZRRwWKE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xAmP+wmsJNFDGvHj86EVK+93Wb6+cY+KEtGCUr8VzfAUw/Mk\\nFD2YufY0djHg25lg8suoVzwyY6znJBqGxz/Cz6i+CbIFv03sWIKTG8A1AoGARmGa\\nRNw8hSdVP84bck8qP5MNnP5+uUAifUazJysnYxw0dIDGMIGRVInfwWLngSWCUEAJ\\n4C5dowrf63abJFaXH9SldOhClQpCvorTIXMLYE6sEa3lOTfGkgWen5E5DeiPadyR\\n1JiIYqtxiHjEsAtk8T/0MtMgXqWceFMWYcUbP3sCgYAjBgGS2tqYTLYbUaGwQaMs\\nk1qO8mNsmeMtNLI+TujMjBy7JIgfbm7AksouBlaTZ14mwxgDkXkwzF5DJGyMfWHI\\nsrJk/rKoZ7BKbC2gEF2uuBsNgCCEH1qEDGCIdVTlePnO1xULoQr2jNxbyVhsQq9b\\nqyLumJELL/J0CJeiFs1FEw==\\n-----END PRIVATE KEY-----\\n\",\n",
    "  \"client_email\": \"definir-ubicaci-n@airvant-experimentacion.iam.gserviceaccount.com\",\n",
    "  \"client_id\": \"114996435871567233179\",\n",
    "  \"auth_uri\": \"https://accounts.google.com/o/oauth2/auth\",\n",
    "  \"token_uri\": \"https://accounts.google.com/o/oauth2/token\",\n",
    "  \"auth_provider_x509_cert_url\": \"https://www.googleapis.com/oauth2/v1/certs\",\n",
    "  \"client_x509_cert_url\": \"https://www.googleapis.com/robot/v1/metadata/x509/definir-ubicaci-n%40airvant-experimentacion.iam.gserviceaccount.com\"\n",
    "}\"\"\"\n",
    "\n",
    "\n",
    "print(\"Microfono activo...\\nEsperando instrucciones de piloto.\\n\")\n",
    "instrucciones = list()\n",
    "instruccion = str()\n",
    "while(instruccion != \"salir \"):\n",
    "    r = sr.Recognizer()\n",
    "    try:\n",
    "        with sr.Microphone() as source:\n",
    "            print(\".\")\n",
    "            audio = r.listen(source, timeout=2)\n",
    "            print(\"..\")\n",
    "        instruccion = r.recognize_google_cloud(audio, language = \"es-ES\", credentials_json=GOOGLE_CLOUD_SPEECH_CREDENTIALS)   \n",
    "        print(\"Instrucción: [\" + str(instruccion) + \"]\")\n",
    "        instrucciones.append(instruccion[:-1])\n",
    "    \n",
    "    except sr.UnknownValueError:\n",
    "        print(\"Audio o instrucción incorrecta\")\n",
    "    \n",
    "    except sr.RequestError as e:\n",
    "        print(\"Error de conexión; {0}\".format(e))    \n",
    "    \n",
    "    except sr.WaitTimeoutError:\n",
    "        continue\n",
    "    \n",
    "    except:\n",
    "        continue\n",
    "        \n",
    "print(\"\\nHa solicitado terminar trabajo...\")\n",
    "print(instrucciones)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
