Microsoft. Azure: Speech Recognition/image recognition/facial recognition/emotion recognition,

Source: Internet
Author: User

Microsoft. Azure: Speech Recognition/image recognition/facial recognition/emotion recognition,

 

I mentioned the bottom layer of Image Recognition in my first blog. the most precise image recognition requires massive data refining. The bottom layer I write is not supported by hundreds of millions of data. It is actually a waste product.

Here we will introduce several cloud services of Microsoft. They are all paid. Individuals can apply for a 30-day free trial.

Public class FaceHelper
{
Private const string uriBase = "https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect ";
Private static string subscriptionKey = string. Empty;
Public FaceHelper (string Key, string imageFilePath)
{
If (! String. IsNullOrWhiteSpace (Key ))
{
SubscriptionKey = Key;
MakeAnalysisRequest (imageFilePath );
}
}

Static async void MakeAnalysisRequest (string imageFilePath)
{
HttpClient client = new HttpClient ();

Client. DefaultRequestHeaders. Add ("Ocp-Apim-subscribe-Key", subscriptionKey );

String requestParameters = "returnFaceId = true & parameters = false & parameters = age, gender, headPose, smile, facialHair, glasses, emotion, hair, makeup, occlusion, accessories, blur, exposure, noise ";

String uri = uriBase + "? "+ RequestParameters;

HttpResponseMessage response;

Byte [] byteData = GetImageAsByteArray (imageFilePath );

Using (ByteArrayContent content = new ByteArrayContent (byteData ))
{
Content. Headers. ContentType = new MediaTypeHeaderValue ("application/octet-stream ");

Response = await client. PostAsync (uri, content );

String contentString = await response. Content. ReadAsStringAsync ();

Console. WriteLine ("\ nResponse: \ n ");
Console. WriteLine (JsonPrettyPrint (contentString ));
}
}

Static byte [] GetImageAsByteArray (string imageFilePath)
{
FileStream fileStream = new FileStream (imageFilePath, FileMode. Open, FileAccess. Read );
BinaryReader binaryReader = new BinaryReader (fileStream );
Return binaryReader. ReadBytes (int) fileStream. Length );
}

Static string JsonPrettyPrint (string json)
{
If (string. IsNullOrEmpty (json ))
Return string. Empty;

Json = json. Replace (Environment. NewLine, ""). Replace ("\ t ","");

StringBuilder sb = new StringBuilder ();
Bool quote = false;
Bool ignore = false;
Int offset = 0;
Int indentLength = 3;

Foreach (char ch in json)
{
Switch (ch)
{
Case '"':
If (! Ignore) quote =! Quote;
Break;
Case '\'':
If (quote) ignore =! Ignore;
Break;
}

If (quote)
Sb. Append (ch );
Else
{
Switch (ch)
{
Case '{':
Case '[':
Sb. Append (ch );
Sb. Append (Environment. NewLine );
Sb. Append (new string ('', ++ offset * indentLength ));
Break;
Case '}':
Case ']':
Sb. Append (Environment. NewLine );
Sb. Append (new string ('', -- offset * indentLength ));
Sb. Append (ch );
Break;
Case ',':
Sb. Append (ch );
Sb. Append (Environment. NewLine );
Sb. Append (new string ('', offset * indentLength ));
Break;
Case ':':
Sb. Append (ch );
Sb. Append ('');
Break;
Default:
If (ch! = '') Sb. Append (ch );
Break;
}
}
}

Return sb. ToString (). Trim ();
}

}

Face recognition API: detects, recognizes, analyzes, organizes, and marks faces in photos

FaceHelper face = new FaceHelper ("your key", ConfigurationManager. deleettings ["Face"]);

 

 

The returned values are very detailed. The face is in the area of the image. Gender. No hair. There is no beard. Are there any glasses clearly written? I will not list them here

The following is the speech recognition. The REST and SOCKET speech recognition are also divided into Chinese and English us. The transmitted audio is also divided into the length. The following configuration is English recognition. The REST.15 seconds or less audio

Public class VoiceHelper
{
/// <Summary>
/// Recognition Mode
/// Three modes are supported: interactive, conversation, and dictation. The recognition mode adjusts Speech Recognition Based on how users speak. Select an appropriate recognition mode for your application.
/// </Summary>
Public VoiceHelper (string file, string key)
{
String url = "https://speech.platform.bing.com/speech/recognition/dictation/cognitiveservices/v1? Language = en-US & format = simple ";

String responseString = string. Empty;
HttpWebRequest request = null;
Request = (HttpWebRequest) HttpWebRequest. Create (url );
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 ["Ocp-Apim-subkeys-Key"] = key;

Using (FileStream fs = new FileStream (file, FileMode. Open, FileAccess. Read ))
{

Byte [] buffer = null;
Int bytesRead = 0;
Using (Stream requestStream = request. GetRequestStream ())
{

Buffer = new Byte [checked (uint) Math. Min (1024, (int) fs. Length)];
While (bytesRead = fs. Read (buffer, 0, buffer. Length ))! = 0)
{
RequestStream. Write (buffer, 0, bytesRead );
}

RequestStream. Flush ();
}
}

Using (WebResponse response = request. GetResponse ())
{
Console. WriteLine (HttpWebResponse) response). StatusCode );

Using (StreamReader sr = new StreamReader (response. GetResponseStream ()))
{
ResponseString = sr. ReadToEnd ();
}

Console. WriteLine (responseString );
}


}
}

 

 

 

 

VoiceHelper voice = new VoiceHelper (@ ConfigurationManager. etettings ["Voice"], "your key ");

 

 

 

 

This speech recognition is still acceptable. Displaytext is what I said in the audio. I repeat the TEST three times. The sound is very dumb and also very low. The recognition rate is very good.

However, it must be noted that only 15 seconds with PCM single channel (single channel), 16 KHz WAV files are supported

The following is image recognition. This is fun. I put a large plane. In the returned data, the blue sky of the plane was recognized.

 

Public class OCRHelper
{
Const string subscriptionKey = "your key ";

Const string uriBase = "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/analyze ";

Public OCRHelper (string file)
{
// Get the path and filename to process from the user.
Console. WriteLine ("Analyze an image :");
Console. Write ("Enter the path to an image you wish to analzye :");

// Execute the rest api call.
MakeAnalysisRequest (file );

Console. WriteLine ("\ nPlease wait a moment for the results to appear. Then, press Enter to exit... \ n ");

}
/// <Summary>
/// Gets the analysis of the specified image file by using the Computer Vision rest api.
/// </Summary>
/// <Param name = "imageFilePath"> The image file. </param>
Static async void MakeAnalysisRequest (string imageFilePath)
{
HttpClient client = new HttpClient ();

// Request headers.
Client. DefaultRequestHeaders. Add ("Ocp-Apim-subscribe-Key", subscriptionKey );

// Request parameters. A third optional parameter is "details ".
String requestParameters = "visualFeatures = Categories, Description, Color & language = en ";

// Assemble the URI for the rest api Call.
String uri = uriBase + "? "+ RequestParameters;

HttpResponseMessage response;

// Request body. Posts a locally stored JPEG image.
Byte [] byteData = GetImageAsByteArray (imageFilePath );

Using (ByteArrayContent content = new ByteArrayContent (byteData ))
{
// This example uses content type "application/octet-stream ".
// The other content types you can use are "application/json" and "multipart/form-data ".
Content. Headers. ContentType = new MediaTypeHeaderValue ("application/octet-stream ");

// Execute the rest api call.
Response = await client. PostAsync (uri, content );

// Get the JSON response.
String contentString = await response. Content. ReadAsStringAsync ();

// Display the JSON response.
Console. WriteLine ("\ nResponse: \ n ");
Console. WriteLine (JsonPrettyPrint (contentString ));
// Description. captions. text: English description of the image
}
}


/// <Summary>
/// Returns the contents of the specified file as a byte array.
/// </Summary>
/// <Param name = "imageFilePath"> The image file to read. </param>
/// <Returns> The byte array of the image data. </returns>
Static byte [] GetImageAsByteArray (string imageFilePath)
{
FileStream fileStream = new FileStream (imageFilePath, FileMode. Open, FileAccess. Read );
BinaryReader binaryReader = new BinaryReader (fileStream );
Return binaryReader. ReadBytes (int) fileStream. Length );
}


/// <Summary>
/// Formats the given JSON string by adding line breaks and indents.
/// </Summary>
/// <Param name = "json"> The raw JSON string to format. </param>
/// <Returns> The formatted JSON string. </returns>
Static string JsonPrettyPrint (string json)
{
If (string. IsNullOrEmpty (json ))
Return string. Empty;

Json = json. Replace (Environment. NewLine, ""). Replace ("\ t ","");

StringBuilder sb = new StringBuilder ();
Bool quote = false;
Bool ignore = false;
Int offset = 0;
Int indentLength = 3;

Foreach (char ch in json)
{
Switch (ch)
{
Case '"':
If (! Ignore) quote =! Quote;
Break;
Case '\'':
If (quote) ignore =! Ignore;
Break;
}

If (quote)
Sb. Append (ch );
Else
{
Switch (ch)
{
Case '{':
Case '[':
Sb. Append (ch );
Sb. Append (Environment. NewLine );
Sb. Append (new string ('', ++ offset * indentLength ));
Break;
Case '}':
Case ']':
Sb. Append (Environment. NewLine );
Sb. Append (new string ('', -- offset * indentLength ));
Sb. Append (ch );
Break;
Case ',':
Sb. Append (ch );
Sb. Append (Environment. NewLine );
Sb. Append (new string ('', offset * indentLength ));
Break;
Case ':':
Sb. Append (ch );
Sb. Append ('');
Break;
Default:
If (ch! = '') Sb. Append (ch );
Break;
}
}
}

Return sb. ToString (). Trim ();
}
}

 

OCRHelper ocr = new OCRHelper (@ "C: \ Users \ Administrator \ Desktop \ test2.png ");

Yesinput Parameters

 

 

Below are output parameters

 

A big bird floated in the sky

The emotion recognition interface is not explained. Face Recognition is more detailed than emotion recognition.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.