Skip to content

Commit

Permalink
Add new azure speech api support
Browse files Browse the repository at this point in the history
  • Loading branch information
laalaguer committed Mar 25, 2019
1 parent 780969a commit 787e8be
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -81,3 +81,6 @@ proxies.txt
# speech-to-text module folders
deepspeech
pocketsphinx

# visual studio code editor configs
.vscode
4 changes: 4 additions & 0 deletions examples/nonocaptcha.example.yaml
Expand Up @@ -15,6 +15,10 @@ speech:
model_dir: pocketsphinx/model
azure:
api_subkey:
azurespeech:
region:
subscription_key:
language_type:
amazon:
secret_key_id:
secret_access_key:
Expand Down
15 changes: 11 additions & 4 deletions nonocaptcha/audio.py
Expand Up @@ -11,7 +11,7 @@
from aiohttp.client_exceptions import ClientError

from nonocaptcha import util
from nonocaptcha.speech import Amazon, Azure, Sphinx, DeepSpeech
from nonocaptcha.speech import Amazon, Azure, Sphinx, DeepSpeech, AzureSpeech
from nonocaptcha.base import Base
from nonocaptcha.exceptions import DownloadError, ReloadError, TryAgain

Expand All @@ -27,7 +27,7 @@ def __init__(self, page, loop, proxy, proxy_auth, proc_id):
async def solve_by_audio(self):
"""Go through procedures to solve audio"""
await self.get_frames()
for i in range(10):
for _ in range(10):
try:
answer = await self.loop.create_task(self.get_audio_response())
except DownloadError:
Expand Down Expand Up @@ -73,8 +73,15 @@ async def get_audio_response(self):
else:
answer = None
service = self.speech_service.lower()
if service in ["azure", "pocketsphinx", "deepspeech"]:
if service == "azure":
if service in [
"azure",
"pocketsphinx",
"deepspeech",
"azurespeech"
]:
if service == "azurespeech":
speech = AzureSpeech()
elif service == "azure":
speech = Azure()
elif service == "pocketsphinx":
speech = Sphinx()
Expand Down
4 changes: 4 additions & 0 deletions nonocaptcha/nonocaptcha.example.yaml
Expand Up @@ -17,6 +17,10 @@ speech:
model_dir: pocketsphinx/model
azure:
api_subkey:
azurespeech:
region:
subscription_key:
language_type:
amazon:
secret_key_id:
secret_access_key:
Expand Down
58 changes: 58 additions & 0 deletions nonocaptcha/speech.py
Expand Up @@ -13,6 +13,7 @@
import sys
import time
import websockets
import requests

from datetime import datetime
from uuid import uuid4
Expand Down Expand Up @@ -178,6 +179,63 @@ async def get_text(self, audio_data):
await transcribe._endpoint._aio_session.close()


class AzureSpeech(object):
API_REGION = settings["speech"]["azurespeech"]["region"]
SUB_KEY = settings["speech"]["azurespeech"]["subscription_key"]
language_type = settings["speech"]["azurespeech"]['language_type']

@util.threaded
def extract_json_body(self, response):
return json.loads(response)

async def bytes_from_file(self, filename):
async with aiofiles.open(filename, "rb") as f:
chunk = await f.read()
return chunk

async def get_text(self, mp3_filename):
''' return text result or None '''
# convert mp3 file to WAV
wav_filename = await mp3_to_wav(mp3_filename)
# read bytes from WAV file.
wav_bytes = await self.bytes_from_file(wav_filename)
# get result of speech
headers = {
'Ocp-Apim-Subscription-Key': self.SUB_KEY,
'Accept': 'application/json;text/xml',
'Content-Type': 'audio/wav; codecs=audio/pcm; samplerate=16000',
}

speech_to_text_url = (
f"https://{self.API_REGION}.stt.speech.microsoft.com/speech/"
f"recognition/conversation/cognitiveservices/v1?"
f"language={self.language_type}&format=detailed"
)

response = requests.post(
speech_to_text_url,
headers=headers,
data=wav_bytes
)
if response.status_code == 200:
print(response.content)
content = await self.extract_json_body(response.content)
if (
"RecognitionStatus" in content
and content["RecognitionStatus"] == "Success"
):
answer = content["NBest"][0]["Lexical"]
return answer
if (
"RecognitionStatus" in content
and content["RecognitionStatus"] == "EndOfDictation"
):
return
else:
print(response.status_code)
return None


class Azure(object):
SUB_KEY = settings["speech"]["azure"]["api_subkey"]

Expand Down

0 comments on commit 787e8be

Please sign in to comment.