How to use google speech recognition api in c#? - c#

I want to get the audio file from c# and send to google speech recognition API for get the "speech to text" answer.
My code is like this:
try
{
byte[] BA_AudioFile = GetFile(filename);
HttpWebRequest _HWR_SpeechToText = null;
_HWR_SpeechToText =
(HttpWebRequest)HttpWebRequest.Create(
"https://www.google.com/speech-api/v2/recognize?output=json&lang=" + DEFAULT_LANGUAGE + "&key=" + key);
_HWR_SpeechToText.Credentials = CredentialCache.DefaultCredentials;
_HWR_SpeechToText.Method = "POST";
_HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
_HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
Stream stream = _HWR_SpeechToText.GetRequestStream();
stream.Write(BA_AudioFile, 0, BA_AudioFile.Length);
stream.Close();
HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
if (HWR_Response.StatusCode == HttpStatusCode.OK)
{
StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
Console.WriteLine(SR_Response.ToString());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
This part is for upload the file.wav and get the response for the google API, which I find from Internet.
But my code always catches the exceptions:
you must write content length bytes to the request stream before calling at _HWR_SpeechToText.GetResponse(); But I already wroteh the ContextLength.
So my question is why my program failed? It's because the google link or the HTTPWebRequest I used inappropriately?
Is this the right place I got the API key?

Just tested this myself, below is a working solution if you have a valid API key.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.IO;
namespace GoogleRequest
{
class Program
{
static void Main(string[] args)
{
try
{
FileStream fileStream = File.OpenRead("good-morning-google.flac");
MemoryStream memoryStream = new MemoryStream();
memoryStream.SetLength(fileStream.Length);
fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);
byte[] BA_AudioFile = memoryStream.GetBuffer();
HttpWebRequest _HWR_SpeechToText = null;
_HWR_SpeechToText =
(HttpWebRequest)HttpWebRequest.Create(
"https://www.google.com/speech-api/v2/recognize?output=json&lang=en-us&key=YOUR_API_KEY_HERE");
_HWR_SpeechToText.Credentials = CredentialCache.DefaultCredentials;
_HWR_SpeechToText.Method = "POST";
_HWR_SpeechToText.ContentType = "audio/x-flac; rate=44100";
_HWR_SpeechToText.ContentLength = BA_AudioFile.Length;
Stream stream = _HWR_SpeechToText.GetRequestStream();
stream.Write(BA_AudioFile, 0, BA_AudioFile.Length);
stream.Close();
HttpWebResponse HWR_Response = (HttpWebResponse)_HWR_SpeechToText.GetResponse();
if (HWR_Response.StatusCode == HttpStatusCode.OK)
{
StreamReader SR_Response = new StreamReader(HWR_Response.GetResponseStream());
Console.WriteLine(SR_Response.ReadToEnd());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
}
}

Related

Bot Framework - Using Custom Speech Service Error 400 C#

I created a bot with bot framework and now i'm trying to use the CustomSpeech service instead of the bing SpeechToText Service that works fine. I have tried various way to resolve the problem but i get the error 400 and i don't know how to solve this.
The method where i would like to get the text from a Stream of a wav pcm audio:
public static async Task<string> CustomSpeechToTextStream(Stream audioStream)
{
audioStream.Seek(0, SeekOrigin.Begin);
var customSpeechUrl = "https://westus.stt.speech.microsoft.com/speech/recognition/interactive/cognitiveservices/v1?cid=<MyEndPointId>";
string token;
token = GetToken();
HttpWebRequest request = null;
request = (HttpWebRequest)HttpWebRequest.Create(customSpeechUrl);
request.SendChunked = true;
//request.Accept = #"application/json;text/xml";
request.Method = "POST";
request.ProtocolVersion = HttpVersion.Version11;
request.ContentType = "audio/wav; codec=\"audio/pcm\"; samplerate=16000";
request.Headers["Authorization"] = "Bearer " + token;
byte[] buffer = null;
int bytesRead = 0;
using (Stream requestStream = request.GetRequestStream())
{
// Read 1024 raw bytes from the input audio file.
buffer = new Byte[checked((uint)Math.Min(1024, (int)audioStream.Length))];
while ((bytesRead = audioStream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
requestStream.Flush();
}
string responseString = string.Empty;
// Get the response from the service.
using (WebResponse response = request.GetResponse()) // Here i get the error
{
using (StreamReader sr = new StreamReader(response.GetResponseStream()))
{
responseString = sr.ReadToEnd();
}
}
dynamic deserializedResponse = Newtonsoft.Json.JsonConvert.DeserializeObject(responseString);
if (deserializedResponse.RecognitionStatus == "Success")
{
return deserializedResponse.DisplayText;
}
else
{
return null;
}
}
At using (WebResponse response = request.GetResponse()){} i get an exception (Error 400).
Am I doing the HttpWebRequest in the right way?
I read in internet that maybe the problem is the file audio... but then why with the same Stream bing speech service doesn't return this error?
In my case the problem was that i had a wav stream audio that doesn't had the file header that Cris (Custom Speech Service) needs. The sulution is creating a temporary file wav, read the file wav and copy it in a Stream to send it as array to Cris
byte[] buffer = null;
int bytesRead = 0;
using (Stream requestStream = request.GetRequestStream())
{
buffer = new Byte[checked((uint)Math.Min(1024, (int)audioStream.Length))];
while ((bytesRead = audioStream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
requestStream.Flush();
}
or copy it in a MemoryStream and send it as array
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(audioStream.ToArray(), 0, audioStream.ToArray().Length);
requestStream.Flush();
}

Need a simple Google Speech API example for .NET

I need a simple Google Speech API example for .NET in which I upload a voice file and receive the text.
Thanks.
Just worked for me, you can create a class as follow:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
namespace GoogleAPI{
public class GoogleSpeech
{
public string URL { get; set; }
public void GetSpeechTranscript(string filePath)
{
try
{
FileStream fileStream = File.OpenRead(filePath);
MemoryStream memoryStream = new MemoryStream();
memoryStream.SetLength(fileStream.Length);
fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length);
byte[] resBytes = memoryStream.GetBuffer();
HttpWebRequest request = null;
request = (HttpWebRequest)HttpWebRequest.Create(this.URL + "&key=YOUR_API_KEY");
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "POST";
request.ContentType = "audio/x-flac; rate=44100";
request.ContentLength = resBytes.Length;
Stream stream = request.GetRequestStream();
stream.Write(resBytes, 0, resBytes.Length);
stream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
StreamReader reqStream = new StreamReader(response.GetResponseStream());
var result = reqStream.ReadToEnd();
Console.WriteLine(result);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.ReadLine();
}
}
}
Hope this help!

C# Httpwebrequest Log In System

The following code should log user in, but it does not work. The code uses 9gag as an example, but the general idea should work elsewhere too. The user does not get access his/hers profile.
What is wrong with the code?
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace ttp_B
{
internal class Program
{
private static void Main(string[] args)
{
bool flag = false;
//string word_to_stop = "profile";
string urltosite = "https://9gag.com/login"; // site that i'm trying to log in
string emailaddress = "";
string password = "";
var coo = new System.Net.CookieContainer();
try
{
var request = System.Net.WebRequest.Create(urltosite) as System.Net.HttpWebRequest;
request.CookieContainer = coo;
request.Method = "POST";
request.Proxy = new WebProxy("127.0.0.1", 8888); // fiddler
//some extra headers
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0";
using (var stream = request.GetRequestStream())
{
byte[] buffer =
Encoding.UTF8.GetBytes(
string.Format(
"csrftoken=&next=http%3A%2F%2F9gag.com%2F&location=1&username={0}&password={1}", emailaddress, password));
//this text of request is correct I guess. Got it from fiddler btw.
stream.Write(buffer, 0, buffer.Length);
stream.Close();
}
using (var response = request.GetResponse() as HttpWebResponse)
{
coo.Add(response.Cookies); // adding cookies, just to make this working properly
using (var sr = new System.IO.StreamReader(response.GetResponseStream()))
{
string http_code = sr.ReadToEnd(); // gettin' that new html document
//flag = (http_code.Contains(word_to_stop)); // looking for word that I'm sure exist after succesfull loggin' in
//if(flag == true)
//{
// console.writeline("Works");
//}
}
}
}
catch (WebException e)
{
Console.Write(e.ToString());
}
}
}
}
As far as I can see, the request stream isn't closed before you go for the response.
simplified it should look like this:
var stream = request.GetRequestStream();
tStream(buffer, 0, buffer.Length);
//close the stream
tStream.Close();
//go for the response
request.GetResponse();

How to post video properties with youtube-data-api on c#

I try to upload videos to my youtube account with youtube api v3 on asp.net. I searched a lot but didn't find any code sample to do this. Actually now i can upload videos somehow but i can't give name, description etc. to my videos. Here's my code which i use to upload my videos.
Uri uri = new Uri("https://www.googleapis.com/upload/youtube/v3/videos?part=snippet");
WebClient wc = new WebClient();
wc.Headers.Add("Authorization", "Bearer {access_token}");
byte[] file = File.ReadAllBytes(Server.MapPath("/videos/test.mp4"));
byte[] response = wc.UploadData(uri, file);
string jSonResult = String.Format("\nResult received was {0}",
Encoding.ASCII.GetString(response));
return jSonResult;
Don't know if you already found a solution.
But this code works on my machine! ;)
byte[] jsonBytes = Encoding.UTF8.GetBytes(json);
//byte[] file = File.ReadAllBytes(videoFilePath);
using (var fileStream = new FileStream(videoFilePath, FileMode.Open))
{
Uri uri = new Uri("https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status");
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri);
request.Method = "POST";
request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + authToken);
request.ContentLength = jsonBytes.Length;
request.ContentType = "application/json; charset=utf-8";
request.Headers.Add("X-Upload-Content-Length", fileStream.Length.ToString());
request.Headers.Add("X-Upload-Content-Type", "video/*");
string location = string.Empty;
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(jsonBytes, 0, jsonBytes.Length);
}
try
{
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
location = response.Headers["Location"];
}
}
catch (WebException ex)
{
Response.Write(ex.ToString());
}
request = (HttpWebRequest) WebRequest.Create(location);
request.Method = "PUT";
request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + authToken);
request.ContentLength = fileStream.Length;
request.ContentType = "video/*";
using (Stream dataStream = request.GetRequestStream())
{
byte[] buffer = new byte[fileStream.Length];
var data = fileStream.Read(buffer, 0, buffer.Length);
dataStream.Write(buffer, 0, data);
//dataStream.Write(file, 0, file.Length);
}
try
{
using (HttpWebResponse response = (HttpWebResponse) request.GetResponse())
{
}
}
catch (WebException ex)
{
Response.Write(ex.ToString());
}

How to Call a WebService without using WebReference?

I want to know if there is someone who had use a Class to Call a WebService, this WS receives an integer after the organizational references and responds into a json file,
Actually my issue is to call the webservice without using a webreference, and read the json file and parse it into a dictionary ,
I appreciate your help
Best Regards, i let you my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Net;
using Dimex.ChangeSAP.Core.Utilities;
namespace Dimex.ChangeSAP.Core.Utilities
{
class ConsumirWebService
{
public void ConsumirWS()
{
Dimex.ChangeSAP.Core.Entities.Seguridad.Usuario users = new Dimex.ChangeSAP.Core.Entities.Seguridad.Usuario();
int idUsuaro = users.IdUsuario;
try
{
System.Net.WebRequest req = System.Net.WebRequest.Create("http://192.168.8.97/PassportPruebas/api/partners?enterprise_system_id=1&organizational_reference=" + idUsuaro);
//req.Proxy = new System.Net.WebProxy(ProxyString, true);
//Add these, as we're doing a POST
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
//We need to count how many bytes we're sending.
//Post'ed Faked Forms should be name=value&
string postData = "OPERATION_NAME=ADD_REQUEST&TECHNICIAN_KEY=90BA&INPUT_DATA=" + sendXML;
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(postData);
req.ContentLength = bytes.Length;
System.IO.Stream os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length); //Push it out there
os.Close();
System.Net.WebResponse resp = req.GetResponse();
if (resp == null)
{
return null;
}
System.IO.StreamReader sr =
new System.IO.StreamReader(resp.GetResponseStream());
string respuesta = sr.ReadToEnd().Trim();
return respuesta;
}
catch (Exception ex)
{
return "";
//throw or return an appropriate response/exception
}
}
}
}
Well Actually here is my code for anyone with this kind of issue too,
public static string LlamarWebService(string url)
{
try
{
System.Net.WebRequest req = System.Net.WebRequest.Create(url);
req.ContentType = "application/json";
req.Method = "GET";
System.Net.WebResponse resp = req.GetResponse();
if (resp == null) return null;
System.IO.StreamReader sr =
new System.IO.StreamReader(resp.GetResponseStream());
string respuesta = sr.ReadToEnd().Trim();
return respuesta;
}
catch (Exception ex)
{
throw ex;
// return "";
//throw or return an appropriate response/exception
}
}
you can create a proxy class using wsdl utility or svcutil in visualstudiocommand prompt enter command wsdl.exe /out:[path and name of the file] /language:CS

Categories