University of Toledo logo from the UT website. Create and run the sample application To create the sample in Visual Studio, do the following steps: 1. Create a new Visual Studio solution in Visual Studio, using the Visual C# Console App template. 2. 1. Install the Newtonsoft.Json NuGet package. On the menu, click Tools, select NuGet Package Manager, then Manage NuGet Packages for Solution. 2. 3. Click the Browse tab, and in the Search box type "Newtonsoft.Json". Select Newtonsoft.Json when it displays, then click the checkbox next to your project name, and Install. 3. 4. 5. Paste in the code below, making sure you have inserted the key and endpoint info. Run the program. At the prompt, enter the path to your image (you must include the full path). 6. The program should return JSON that locates and converts your text. using Newtonsoft.Json.Linq; using System; using System.IO; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; namespace CSHttpClientSample { static class Program { // Add your Computer Vision subscription key and endpoint static string subscriptionKey ""; static string endpoint ""; // the Batch Read method endpoint static string uriBase endpoint + "vision/v2.1/read/core/asyncBatchAnalyze"; static async Task Main() { // Get the path and filename to process from the user. Console.WriteLine("Text Recognition: "); Console.Write( "Enter the path to an image with text you wish to read: "); string imageFilePath Console.ReadLine(); } // Get the path and filename to process from the user. Console.WriteLine("Text Recognition: "); Console.Write( "Enter the path to an image with text you wish to read: "); string imageFilePath Console.ReadLine(); if (File.Exists (imageFilePath)) { // Call the REST API method. Console.WriteLine("\nWait a moment for the results to appear. \n"); await ReadText (imageFilePath); } else { } Console.WriteLine("\nInvalid file path"); Console.WriteLine("\nPress Enter to exit..."); Console.ReadLine(); /// /// Gets the text from the specified image file by using /// the Computer Vision REST API. /// /// The image file with text. static async Task ReadText(string imageFilePath) { try { HttpClient client new HttpClient(); // Request headers. client.DefaultRequestHeaders.Add( "Ocp-Apim-Subscription-Key", subscriptionKey); // Assemble the URI for the REST API method.. string uri uriBase; HttpResponseMessage response; // Two REST API methods are required to extract text. // One method to submit the image for processing, the other method // to retrieve the text found in the image. // operationLocation stores the URI of the second REST API method, // returned by the first REST API method. string operationLocation; // Reads the contents of the specified local image // into a byte array. byte[] byteData Get ImageAsByteArray (imageFilePath);

COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
1st Edition
ISBN:9780357392676
Author:FREUND, Steven
Publisher:FREUND, Steven
Chapter10: Data Analysis With Power Tools And Creating Macros
Section: Chapter Questions
Problem 6EYW
icon
Related questions
Question
// Reads the contents of the specified local image // into a byte array. byte[] byteData = GetImageAsByteArray(imageFilePath); // Adds the byte array as an octet stream to the request body. using (ByteArrayContent content = new ByteArraycontent (byteData)) content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"'); / The asynt proe a to thody e the eaten text in the image. response = await client.PostAsync(uri, content); } /I The response header for the Batch Read method contains the URI // The Batch Read operation does not return anything in the response body. if (response.IsSuccessStatusCode) operationLocation = response.Headers.GetValues("Operation-Location").FirstOrDefault(); else // Display the JSON error data. string errorstring = await response.Content.ReadAsStringAsync(); Console WriteLine("\n\nResponse: \n{0}\n", Token. Parse(errorString).ToString()); // If the first REST API method completes successfully, the second // REST API method retrieves the text written in the image. // Note: The response may not be immediately available. Text // recognition is an asynchronous operation that can take a variable /I amount of time depending on the length of the text. /I You may need to wait or retry this operation. // This example checks once per second for ten seconds. string contentString; int i = 0; do System. Threading. Thread. Sleep (1000); response = await client. GetAsync(operationLocation); contentString = await response.Content.ReadAsStringAsync(); ++i; while (1 < 10 && contentstring. IndexOf(" "status!": "Succeeded = - 1); if (i == 10 && contentString. Indexof("|"status\":\"Succeeded\"") == -1) Console.WriteLine("\nTimeout error.\n"); recurn. // Display the JSON response. Console.WriteLine("\nResponse:\n\n{0}\n", Token. Parse(contentString).ToString()); catch (Exception e) Console.WriteLine("\n" + e.Message); / /// Returns the contents of the specified file as a byte array. /// /// ‹param name="imageFilePath" ›The image file to read. ‹/param> /// < returns>The byte array of the image data.‹/returns › static byte[] GetImageAsByteArray(string imageFilePath) // Open a read-only file stream for the specified file. using (FileStream fileStream = new FileStream(imageFilePath, FileMode.Open, FileAccess.Read)) // Read the file's contents into a byte array. BinaryReader binaryReader = new BinaryReader(fileStream); return binaryReader.ReadBytes((int)fileStream.Length); Follow the procedure and solve this question
University of Toledo logo from the UT website.
Create and run the sample application
To create the sample in Visual Studio, do the following steps:
1.
Create a new Visual Studio solution in Visual Studio, using the Visual C# Console App
template.
2.
1.
Install the Newtonsoft.Json NuGet package.
On the menu, click Tools, select NuGet Package Manager, then Manage
NuGet Packages for Solution.
2.
3.
Click the Browse tab, and in the Search box type "Newtonsoft.Json".
Select Newtonsoft.Json when it displays, then click the checkbox next to your
project name, and Install.
3.
4.
5.
Paste in the code below, making sure you have inserted the key and endpoint info.
Run the program.
At the prompt, enter the path to your image (you must include the full path).
6. The program should return JSON that locates and converts your text.
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace CSHttpClientSample
{
static class Program
{
// Add your Computer Vision subscription key and endpoint
static string subscriptionKey "<your key>";
static string endpoint "<your endpoint>";
// the Batch Read method endpoint
static string uriBase endpoint + "vision/v2.1/read/core/asyncBatchAnalyze";
static async Task Main()
{
// Get the path and filename to process from the user.
Console.WriteLine("Text Recognition: ");
Console.Write(
"Enter the path to an image with text you wish to read: ");
string imageFilePath Console.ReadLine();
Transcribed Image Text:University of Toledo logo from the UT website. Create and run the sample application To create the sample in Visual Studio, do the following steps: 1. Create a new Visual Studio solution in Visual Studio, using the Visual C# Console App template. 2. 1. Install the Newtonsoft.Json NuGet package. On the menu, click Tools, select NuGet Package Manager, then Manage NuGet Packages for Solution. 2. 3. Click the Browse tab, and in the Search box type "Newtonsoft.Json". Select Newtonsoft.Json when it displays, then click the checkbox next to your project name, and Install. 3. 4. 5. Paste in the code below, making sure you have inserted the key and endpoint info. Run the program. At the prompt, enter the path to your image (you must include the full path). 6. The program should return JSON that locates and converts your text. using Newtonsoft.Json.Linq; using System; using System.IO; using System.Linq; using System.Net.Http; using System.Net.Http.Headers; using System.Threading.Tasks; namespace CSHttpClientSample { static class Program { // Add your Computer Vision subscription key and endpoint static string subscriptionKey "<your key>"; static string endpoint "<your endpoint>"; // the Batch Read method endpoint static string uriBase endpoint + "vision/v2.1/read/core/asyncBatchAnalyze"; static async Task Main() { // Get the path and filename to process from the user. Console.WriteLine("Text Recognition: "); Console.Write( "Enter the path to an image with text you wish to read: "); string imageFilePath Console.ReadLine();
}
// Get the path and filename to process from the user.
Console.WriteLine("Text Recognition: ");
Console.Write(
"Enter the path to an image with text you wish to read: ");
string imageFilePath
Console.ReadLine();
if (File.Exists (imageFilePath))
{
// Call the REST API method.
Console.WriteLine("\nWait a moment for the results to appear. \n");
await ReadText (imageFilePath);
}
else
{
}
Console.WriteLine("\nInvalid file path");
Console.WriteLine("\nPress Enter to exit...");
Console.ReadLine();
/// <summary>
/// Gets the text from the specified image file by using
/// the Computer Vision REST API.
/// </summary>
/// <param name="imageFilePath">The image file with text.</param>
static async Task ReadText(string imageFilePath)
{
try
{
HttpClient client new HttpClient();
// Request headers.
client.DefaultRequestHeaders.Add(
"Ocp-Apim-Subscription-Key", subscriptionKey);
// Assemble the URI for the REST API method..
string uri uriBase;
HttpResponseMessage response;
// Two REST API methods are required to extract text.
// One method to submit the image for processing, the other method
// to retrieve the text found in the image.
// operationLocation stores the URI of the second REST API method,
// returned by the first REST API method.
string operationLocation;
// Reads the contents of the specified local image
// into a byte array.
byte[] byteData Get ImageAsByteArray (imageFilePath);
Transcribed Image Text:} // Get the path and filename to process from the user. Console.WriteLine("Text Recognition: "); Console.Write( "Enter the path to an image with text you wish to read: "); string imageFilePath Console.ReadLine(); if (File.Exists (imageFilePath)) { // Call the REST API method. Console.WriteLine("\nWait a moment for the results to appear. \n"); await ReadText (imageFilePath); } else { } Console.WriteLine("\nInvalid file path"); Console.WriteLine("\nPress Enter to exit..."); Console.ReadLine(); /// <summary> /// Gets the text from the specified image file by using /// the Computer Vision REST API. /// </summary> /// <param name="imageFilePath">The image file with text.</param> static async Task ReadText(string imageFilePath) { try { HttpClient client new HttpClient(); // Request headers. client.DefaultRequestHeaders.Add( "Ocp-Apim-Subscription-Key", subscriptionKey); // Assemble the URI for the REST API method.. string uri uriBase; HttpResponseMessage response; // Two REST API methods are required to extract text. // One method to submit the image for processing, the other method // to retrieve the text found in the image. // operationLocation stores the URI of the second REST API method, // returned by the first REST API method. string operationLocation; // Reads the contents of the specified local image // into a byte array. byte[] byteData Get ImageAsByteArray (imageFilePath);
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:
9780357392676
Author:
FREUND, Steven
Publisher:
CENGAGE L
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage