Quick Start: spraak herkennen die zijn opgeslagen in Blob StorageQuickstart: Recognize speech stored in blob storage

In deze Quick start gaat u een REST API gebruiken om spraak te herkennen van bestanden in een batch-proces.In this quickstart, you will use a REST API to recognize speech from files in a batch process. Een batch-proces voert de spraak transcriptie uit zonder gebruikers interacties.A batch process executes the speech transcription without any user interactions. Het biedt u een eenvoudig programmeer model, zonder de nood zaak om gelijktijdigheid, aangepaste spraak modellen of andere gegevens te beheren.It gives you a simple programming model, without the need to manage concurrency, custom speech models, or other details. Dit omvat geavanceerde beheer opties, terwijl er efficiënt gebruik wordt gemaakt van Azure speech-service bronnen.It entails advanced control options, while making efficient use of Azure speech service resources.

Zie batch transcriptievoor meer informatie over de beschik bare opties en configuratie details.For more information on the available options and configuration details, see batch transcription.

In de volgende snelstartgids vindt u een voor beeld van het gebruik.The following quickstart will walk you through a usage sample.

U kunt alle C#-voorbeelden van de Speech SDK op GitHub bekijken of downloaden.You can view or download all Speech SDK C# Samples on GitHub.

VereistenPrerequisites

Voordat u aan de slag gaat, moet u het volgende doen:Before you get started, make sure to:

Uw project openen in Visual StudioOpen your project in Visual Studio

De eerste stap is om ervoor te zorgen dat uw project in Visual Studio is geopend.The first step is to make sure that you have your project open in Visual Studio.

  1. Start Visual Studio 2019.Launch Visual Studio 2019.
  2. Laad uw project en open Program.cshet.Load your project and open Program.cs.

Een verwijzing naar Newton soft. json toevoegenAdd a reference to Newtonsoft.Json

  1. Klik in de Solution Explorer met de rechter muisknop op het project HelloWorld en selecteer vervolgens NuGet-pakketten beheren om de NuGet-pakket manager weer te geven.In the Solution Explorer, right-click the helloworld project, and then select Manage NuGet Packages to show the NuGet Package Manager.
  2. Zoek in de rechter bovenhoek de vervolg keuzelijst pakket bron en zorg ervoor dat deze nuget.org optie is geselecteerd.In the upper-right corner, find the Package Source drop-down box, and make sure that nuget.org is selected.
  3. Selecteer in de linkerbovenhoek de optie Bladeren.In the upper-left corner, select Browse.
  4. Typ Newton soft. json in het zoekvak en selecteer Enter.In the search box, type newtonsoft.json and select Enter.
  5. Selecteer in de zoek resultaten het pakket Newton soft. json en selecteer vervolgens installeren om de nieuwste stabiele versie te installeren.From the search results, select the Newtonsoft.Json package, and then select Install to install the latest stable version.
  6. Accepteer alle overeenkomsten en licenties om de installatie te starten.Accept all agreements and licenses to start the installation. Nadat het pakket is geïnstalleerd, wordt er een bevestiging weer gegeven in het console venster van Package Manager .After the package is installed, a confirmation appears in the Package Manager Console window.

Begin met een van de standaard codeStart with some boilerplate code

Laten we een code toevoegen die als een skelet voor het project werkt.Let's add some code that works as a skeleton for our project.

class Program
{
    // Replace with your subscription key
    const string SubscriptionKey = "YourSubscriptionKey";

    // Update with your service region
    const string Region = "YourServiceRegion";
    const int Port = 443;
 
    // Recordings and locale
    const string Locale = "en-US";
    const string RecordingsBlobUri = "YourFileUrl";
 
    // Name and description
    const string Name = "Simple transcription";
    const string Description = "Simple transcription description";
 
    const string SpeechToTextBasePath = "api/speechtotext/v2.0/";
 
    static async Task Main()
    {
        // Cognitive Services follows security best practices.
        // If you experience connectivity issues, see:
        // https://docs.microsoft.com/en-us/dotnet/framework/network-programming/tls
 
        await TranscribeAsync();
    }
 
    static async Task TranscribeAsync()
    {
        Console.WriteLine("Starting transcriptions client...");
    }
}

U moet de volgende waarden vervangen:You'll need to replace the following values:

  • YourSubscriptionKey: gevonden op de pagina sleutels van de Azure portal voor de spraak resourceYourSubscriptionKey: found in the the Keys page of the Azure portal for the Speech resource
  • YourServiceRegion: gevonden op de pagina overzicht van de Azure portal voor de spraak resourceYourServiceRegion: found in the the Overview page of the Azure portal for the Speech resource
  • YourFileUrl: gevonden op op de pagina BLOB service/containers van de Azure portal voor de bron van het opslag accountYourFileUrl: found in under the Blob service / Containers page of the Azure portal for the Storage account resource
    • Selecteer de juiste containerSelect the appropriate container
    • Selecteer de gewenste BLOBSelect the desired blob
    • Kopieer de URL onder de pagina EigenschappenCopy the URL under the Properties page

JSON-wrappersJSON Wrappers

Als de aanvragen van de REST API in JSON-indeling worden uitgevoerd en de resultaten worden geretourneerd in JSON, kunnen ze met alleen teken reeksen communiceren, maar dit wordt niet aanbevolen.As the REST API's take requests in JSON format and also return results in JSON we could interact with them using only strings, but that's not recommended. Om de aanvragen en reacties gemakkelijker te kunnen beheren, declareren we een aantal klassen die moeten worden gebruikt voor het serialiseren/deserialiseren van de JSON.In order to make the requests and responses easier to manage, we'll declare a few classes to use for serializing / deserializing the JSON.

Ga verder en plaats de declaraties TranscribeAsyncna.Go ahead and put their declarations after TranscribeAsync.

public class ModelIdentity
{
    ModelIdentity(Guid id) => Id = id;

    public Guid Id { get; private set; }

    public static ModelIdentity Create(Guid Id) => new ModelIdentity(Id);
}

public class Transcription
{
    [JsonConstructor]
    Transcription(
        Guid id,
        string name,
        string description,
        string locale,
        DateTime createdDateTime,
        DateTime lastActionDateTime,
        string status,
        Uri recordingsUrl,
        IReadOnlyDictionary<string, string> resultsUrls)
    {
        Id = id;
        Name = name;
        Description = description;
        CreatedDateTime = createdDateTime;
        LastActionDateTime = lastActionDateTime;
        Status = status;
        Locale = locale;
        RecordingsUrl = recordingsUrl;
        ResultsUrls = resultsUrls;
    }

    public string Name { get; set; }

    public string Description { get; set; }

    public string Locale { get; set; }

    public Uri RecordingsUrl { get; set; }

    public IReadOnlyDictionary<string, string> ResultsUrls { get; set; }

    public Guid Id { get; set; }

    public DateTime CreatedDateTime { get; set; }

    public DateTime LastActionDateTime { get; set; }

    public string Status { get; set; }

    public string StatusMessage { get; set; }
}

public class TranscriptionDefinition
{
    TranscriptionDefinition(
        string name,
        string description,
        string locale,
        Uri recordingsUrl,
        IEnumerable<ModelIdentity> models)
    {
        Name = name;
        Description = description;
        RecordingsUrl = recordingsUrl;
        Locale = locale;
        Models = models;
        Properties = new Dictionary<string, string>
        {
            ["PunctuationMode"] = "DictatedAndAutomatic",
            ["ProfanityFilterMode"] = "Masked",
            ["AddWordLevelTimestamps"] = "True"
        };
    }

    public string Name { get; set; }

    public string Description { get; set; }

    public Uri RecordingsUrl { get; set; }

    public string Locale { get; set; }

    public IEnumerable<ModelIdentity> Models { get; set; }

    public IDictionary<string, string> Properties { get; set; }

    public static TranscriptionDefinition Create(
        string name,
        string description,
        string locale,
        Uri recordingsUrl)
        => new TranscriptionDefinition(name, description, locale, recordingsUrl, new ModelIdentity[0]);
}

Een HTTP-client maken en configurerenCreate and configure an Http Client

Het eerste wat u nodig hebt, is een HTTP-client waarvoor een juiste basis-URL en verificatieset is ingesteld.The first thing we'll need is an Http Client that has a correct base URL and authentication set. Voeg deze code in TranscribeAsyncin.Insert this code in TranscribeAsync.

var client = new HttpClient
{
    Timeout = TimeSpan.FromMinutes(25),
    BaseAddress = new UriBuilder(Uri.UriSchemeHttps, $"{Region}.cris.ai", Port).Uri,
    DefaultRequestHeaders =
    {
        { "Ocp-Apim-Subscription-Key", SubscriptionKey }
    }
};

Een transcriptie-aanvraag genererenGenerate a transcription request

Vervolgens genereren we de transcriptie-aanvraag.Next, we'll generate the transcription request. Voeg deze code toe TranscribeAsyncaan.Add this code to TranscribeAsync.

var transcriptionDefinition =
    TranscriptionDefinition.Create(
        Name,
        Description,
        Locale,
        new Uri(RecordingsBlobUri));

var res = JsonConvert.SerializeObject(transcriptionDefinition);
var sc = new StringContent(res);
sc.Headers.ContentType = JsonMediaTypeFormatter.DefaultMediaType;

De aanvraag verzenden en de status controlerenSend the request and check its status

Nu gaan we de aanvraag verzenden naar de speech-service en de eerste respons code controleren.Now we post the request to the Speech service and check the initial response code. Deze antwoord code geeft eenvoudigweg aan of de service de aanvraag heeft ontvangen.This response code will simply indicate if the service has received the request. De service retourneert een URL in de antwoord headers die de locatie is waar de transcriptie-status wordt opgeslagen.The service will return a Url in the response headers that's the location where it will store the transcription status.

Uri transcriptionLocation = null;
using (var response = await client.PostAsync($"{SpeechToTextBasePath}Transcriptions/", sc))
{
    if (!response.IsSuccessStatusCode)
    {
        Console.WriteLine("Error {0} starting transcription.", response.StatusCode);
        return;
    }

    transcriptionLocation = response.Headers.Location;
}

Wacht tot de transcriptie is voltooidWait for the transcription to complete

Omdat de transcriptie asynchroon wordt verwerkt, moeten we elke regel vaak de status van de service controleren.Since the service processes the transcription asynchronously, we need to poll for its status every so often. We controleren elke vijf seconden.We'll check every 5 seconds.

We kunnen de status controleren door de inhoud op te halen bij de URL die we hebben ontvangen toen de aanvraag werd verzonden.We can check the status by retrieving the content at the Url we got when the posted the request. Wanneer de inhoud weer wordt opgehaald, deserialiseren we deze in een van onze helperklasse om gemakkelijker te kunnen communiceren met.When we get the content back, we deserialize it into one of our helper class to make it easier to interact with.

Dit is de polling code met status weergave voor alles, behalve een geslaagde voltooiing, we doen dat nu.Here's the polling code with status display for everything except a successful completion, we'll do that next.

Console.WriteLine($"Created transcription at location {transcriptionLocation}.");
Console.WriteLine("Checking status.");

var completed = false;

// Check for the status of our transcriptions periodically
while (!completed)
{
    Transcription transcription = null;
    using (var response = await client.GetAsync(transcriptionLocation.AbsolutePath))
    {
        var contentType = response.Content.Headers.ContentType;
        if (response.IsSuccessStatusCode &&
            string.Equals(contentType.MediaType, "application/json", StringComparison.OrdinalIgnoreCase))
        {
            transcription = await response.Content.ReadAsAsync<Transcription>();
        }
        else
        {
            Console.WriteLine("Error with status {0} getting transcription result", response.StatusCode);
            continue;
        }
    }

    switch (transcription.Status)
    {
        case "Failed":
            completed = true;
            Console.WriteLine("Transcription failed. Status: {0}", transcription.StatusMessage);
            break;

        case "Succeeded":
            break;

        case "Running":
            Console.WriteLine("Transcription is still running.");
            break;

        case "NotStarted":
            Console.WriteLine("Transcription has not started.");
            break;
    }

    await Task.Delay(TimeSpan.FromSeconds(5));
}

Console.WriteLine("Press any key...");
Console.ReadKey();

De transcriptie-resultaten weer gevenDisplay the transcription results

Zodra de service de transcriptie heeft voltooid, worden de resultaten opgeslagen in een andere URL die we vanaf het status antwoord kunnen ontvangen.Once the service has successfully completed the transcription the results will be stored in another Url that we can get from the status response. Hier maken we een aanvraag om deze resultaten te downloaden naar een tijdelijk bestand voordat ze worden gelezen en gedeserialiseerd.Here we make a request to download those results in to a temporary file before reading and deserializing them. Zodra de resultaten zijn geladen, kunnen ze worden afgedrukt op de-console.Once the results are loaded we can print them to the console. Voeg de volgende code toe aan case "Succeeded": het label.Add the following code to the case "Succeeded": label.

completed = true;
var webClient = new WebClient();
var filename = Path.GetTempFileName();
webClient.DownloadFile(transcription.ResultsUrls["channel_0"], filename);
var results = File.ReadAllText(filename);
Console.WriteLine($"Transcription succeeded. Results: {Environment.NewLine}{results}");
File.Delete(filename);

Controleer uw codeCheck your code

Op dit moment moet uw code er als volgt uitzien: (er zijn enkele opmerkingen aan deze versie toegevoegd)At this point, your code should look like this: (We've added some comments to this version)

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Formatting;

namespace BatchClient
{
    class Program
    {
        // Replace with your subscription key
        const string SubscriptionKey = "YourSubscriptionKey";

        // Update with your service region
        const string Region = "YourServiceRegion";
        const int Port = 443;

        // Recordings and locale
        const string Locale = "en-US";
        const string RecordingsBlobUri = "YourFileUrl";

        // Name and description
        const string Name = "Simple transcription";
        const string Description = "Simple transcription description";

        const string SpeechToTextBasePath = "api/speechtotext/v2.0/";

        static async Task Main()
        {
            // For non-Windows 10 users.
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            await TranscribeAsync();
        }

        static async Task TranscribeAsync()
        {
            Console.WriteLine("Starting transcriptions client...");

            // Create the client object and authenticate
            var client = new HttpClient
            {
                Timeout = TimeSpan.FromMinutes(25),
                BaseAddress = new UriBuilder(Uri.UriSchemeHttps, $"{Region}.cris.ai", Port).Uri,
                DefaultRequestHeaders =
                {
                    { "Ocp-Apim-Subscription-Key", SubscriptionKey }
                }
            };

            var transcriptionDefinition =
                TranscriptionDefinition.Create(
                    Name,
                    Description,
                    Locale,
                    new Uri(RecordingsBlobUri));

            var res = JsonConvert.SerializeObject(transcriptionDefinition);
            var sc = new StringContent(res);
            sc.Headers.ContentType = JsonMediaTypeFormatter.DefaultMediaType;

            Uri transcriptionLocation = null;

            using (var response = await client.PostAsync($"{SpeechToTextBasePath}Transcriptions/", sc))
            {
                if (!response.IsSuccessStatusCode)
                {
                    Console.WriteLine("Error {0} starting transcription.", response.StatusCode);
                    return;
                }

                transcriptionLocation = response.Headers.Location;
            }

            Console.WriteLine($"Created transcription at location {transcriptionLocation}.");
            Console.WriteLine("Checking status.");

            var completed = false;

            // Check for the status of our transcriptions periodically
            while (!completed)
            {
                Transcription transcription = null;

                // Get all transcriptions for the user
                using (var response = await client.GetAsync(transcriptionLocation.AbsolutePath))
                {
                    var contentType = response.Content.Headers.ContentType;
                    if (response.IsSuccessStatusCode &&
                        string.Equals(contentType.MediaType, "application/json", StringComparison.OrdinalIgnoreCase))
                    {
                        transcription = await response.Content.ReadAsAsync<Transcription>();
                    }
                    else
                    {
                        Console.WriteLine("Error with status {0} getting transcription result", response.StatusCode);
                        continue;
                    }
                }

                // For each transcription in the list we check the status
                switch (transcription.Status)
                {
                    case "Failed":
                        completed = true;
                        Console.WriteLine("Transcription failed. Status: {0}", transcription.StatusMessage);
                        break;

                    case "Succeeded":
                        completed = true;
                        var webClient = new WebClient();
                        var filename = Path.GetTempFileName();
                        webClient.DownloadFile(transcription.ResultsUrls["channel_0"], filename);
                        var results = File.ReadAllText(filename);
                        Console.WriteLine($"Transcription succeeded. Results: {Environment.NewLine}{results}");
                        File.Delete(filename);
                        break;

                    case "Running":
                        Console.WriteLine("Transcription is still running.");
                        break;

                    case "NotStarted":
                        Console.WriteLine("Transcription has not started.");
                        break;
                }

                await Task.Delay(TimeSpan.FromSeconds(5));
            }

            Console.WriteLine("Press any key...");
            Console.ReadKey();
        }
    }

    public class ModelIdentity
    {
        ModelIdentity(Guid id) => Id = id;

        public Guid Id { get; private set; }

        public static ModelIdentity Create(Guid Id) => new ModelIdentity(Id);
    }

    public class Transcription
    {
        [JsonConstructor]
        Transcription(
            Guid id,
            string name,
            string description,
            string locale,
            DateTime createdDateTime,
            DateTime lastActionDateTime,
            string status,
            Uri recordingsUrl,
            IReadOnlyDictionary<string, string> resultsUrls)
        {
            Id = id;
            Name = name;
            Description = description;
            CreatedDateTime = createdDateTime;
            LastActionDateTime = lastActionDateTime;
            Status = status;
            Locale = locale;
            RecordingsUrl = recordingsUrl;
            ResultsUrls = resultsUrls;
        }

        public string Name { get; set; }

        public string Description { get; set; }

        public string Locale { get; set; }

        public Uri RecordingsUrl { get; set; }

        public IReadOnlyDictionary<string, string> ResultsUrls { get; set; }

        public Guid Id { get; set; }

        public DateTime CreatedDateTime { get; set; }

        public DateTime LastActionDateTime { get; set; }

        public string Status { get; set; }

        public string StatusMessage { get; set; }
    }

    public class TranscriptionDefinition
    {
        TranscriptionDefinition(
            string name,
            string description,
            string locale,
            Uri recordingsUrl,
            IEnumerable<ModelIdentity> models)
        {
            Name = name;
            Description = description;
            RecordingsUrl = recordingsUrl;
            Locale = locale;
            Models = models;
            Properties = new Dictionary<string, string>
            {
                ["PunctuationMode"] = "DictatedAndAutomatic",
                ["ProfanityFilterMode"] = "Masked",
                ["AddWordLevelTimestamps"] = "True"
            };
        }

        public string Name { get; set; }

        public string Description { get; set; }

        public Uri RecordingsUrl { get; set; }

        public string Locale { get; set; }

        public IEnumerable<ModelIdentity> Models { get; set; }

        public IDictionary<string, string> Properties { get; set; }

        public static TranscriptionDefinition Create(
            string name,
            string description,
            string locale,
            Uri recordingsUrl)
            => new TranscriptionDefinition(name, description, locale, recordingsUrl, new ModelIdentity[0]);
    }
}

Uw app bouwen en uitvoerenBuild and run your app

Nu bent u klaar om uw app te bouwen en de spraak herkenning te testen met behulp van de speech-service.Now you're ready to build your app and test our speech recognition using the Speech service.

  1. De code compileren : Kies > buildBuild Solutionin de menu balk van Visual Studio.Compile the code - From the menu bar of Visual Studio, choose Build > Build Solution.
  2. Start uw app -vanuit de menu balk, kies fout > opsporing starten of druk op F5.Start your app - From the menu bar, choose Debug > Start Debugging or press F5.
  3. Herkenning starten : u wordt gevraagd om een woord groep in het Engels te spreken.Start recognition - It'll prompt you to speak a phrase in English. Uw spraak wordt verzonden naar de spraak service, getranscribeerd als tekst en weer gegeven in de-console.Your speech is sent to the Speech service, transcribed as text, and rendered in the console.

Volgende stappenNext steps

In deze Quick start gaat u een REST API gebruiken om spraak te herkennen van bestanden in een batch-proces.In this quickstart, you will use a REST API to recognize speech from files in a batch process. Een batch-proces voert de spraak transcriptie uit zonder gebruikers interacties.A batch process executes the speech transcription without any user interactions. Het biedt u een eenvoudig programmeer model, zonder de nood zaak om gelijktijdigheid, aangepaste spraak modellen of andere gegevens te beheren.It gives you a simple programming model, without the need to manage concurrency, custom speech models, or other details. Dit omvat geavanceerde beheer opties, terwijl er efficiënt gebruik wordt gemaakt van Azure speech-service bronnen.It entails advanced control options, while making efficient use of Azure speech service resources.

Zie batch transcriptievoor meer informatie over de beschik bare opties en configuratie details.For more information on the available options and configuration details, see batch transcription.

In de volgende snelstartgids vindt u een voor beeld van het gebruik.The following quickstart will walk you through a usage sample.

U kunt alle C++-voorbeelden van de Speech SDK op GitHub bekijken of downloaden.You can view or download all Speech SDK C++ Samples on GitHub.

VereistenPrerequisites

Voordat u aan de slag gaat, moet u het volgende doen:Before you get started, make sure to:

Uw project openen in Visual StudioOpen your project in Visual Studio

De eerste stap is om ervoor te zorgen dat uw project in Visual Studio is geopend.The first step is to make sure that you have your project open in Visual Studio.

  1. Start Visual Studio 2019.Launch Visual Studio 2019.
  2. Laad uw project en open helloworld.cpphet.Load your project and open helloworld.cpp.

Een verwijzing toevoegenAdd a references

Om onze code ontwikkeling te versnellen, gebruiken we een aantal externe onderdelen:To speed up our code development we'll be using a couple of external components:

  • Cpp rest SDK Een client bibliotheek voor het aanbrengen van REST-aanroepen naar een REST-service.CPP Rest SDK A client library for making REST calls to a REST service.
  • nlohmann/JSON Handige JSON-parsering/serialisatie/deserialisatie bibliotheek.nlohmann/json Handy JSON Parsing / Serialization / Deserialization library.

Beide kunnen worden geïnstalleerd met behulp van vcpkg.Both can be installed using vcpkg.

vcpkg install cpprestsdk cpprestsdk:x64-windows
vcpkg install nlohmann-json

Begin met een van de standaard codeStart with some boilerplate code

Laten we een code toevoegen die als een skelet voor het project werkt.Let's add some code that works as a skeleton for our project.

#include <iostream>
#include <strstream>
#include <Windows.h>
#include <locale>
#include <codecvt>
#include <string>

#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <nlohmann/json.hpp>

using namespace std;
using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace concurrency::streams;       // Asynchronous streams
using json = nlohmann::json;

const string_t region = U("YourServiceRegion");
const string_t subscriptionKey = U("YourSubscriptionKey");
const string name = "Simple transcription";
const string description = "Simple transcription description";
const string myLocale = "en-US";
const string recordingsBlobUri = "YourFileUrl";

void recognizeSpeech()
{
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t >> converter;

}

int wmain()
{
    recognizeSpeech();
    cout << "Please press a key to continue.\n";
    cin.get();
    return 0;
}

U moet de volgende waarden vervangen:You'll need to replace the following values:

  • YourSubscriptionKey: gevonden op de pagina sleutels van de Azure portal voor de spraak resourceYourSubscriptionKey: found in the the Keys page of the Azure portal for the Speech resource
  • YourServiceRegion: gevonden op de pagina overzicht van de Azure portal voor de spraak resourceYourServiceRegion: found in the the Overview page of the Azure portal for the Speech resource
  • YourFileUrl: gevonden op op de pagina BLOB service/containers van de Azure portal voor de bron van het opslag accountYourFileUrl: found in under the Blob service / Containers page of the Azure portal for the Storage account resource
    • Selecteer de juiste containerSelect the appropriate container
    • Selecteer de gewenste BLOBSelect the desired blob
    • Kopieer de URL onder de pagina EigenschappenCopy the URL under the Properties page

JSON-wrappersJSON Wrappers

Als de aanvragen van de REST API in JSON-indeling worden uitgevoerd en de resultaten worden geretourneerd in JSON, kunnen ze met alleen teken reeksen communiceren, maar dit wordt niet aanbevolen.As the REST API's take requests in JSON format and also return results in JSON we could interact with them using only strings, but that's not recommended. Om ervoor te zorgen dat de aanvragen en antwoorden gemakkelijker te beheren zijn, declareren we een aantal klassen die moeten worden gebruikt voor het serialiseren/deserialiseren van de JSON en een aantal methoden om nlohmann/JSON te helpen.In order to make the requests and responses easier to manage, we'll declare a few classes to use for serializing / deserializing the JSON and some methods to assist nlohmann/json.

Plaats de declaraties vooraf recognizeSpeech .Go ahead and put their declarations before recognizeSpeech .

class TranscriptionDefinition {
private:
    TranscriptionDefinition(string name,
        string description,
        string locale,
        string recordingsUrl,
        std::list<string> models) {

        Name = name;
        Description = description;
        RecordingsUrl = recordingsUrl;
        Locale = locale;
        Models = models;
    }

public:
    string Name;
    string Description;
    string RecordingsUrl;
    string Locale;
    std::list<string> Models;
    std::map<string, string> properties;

    static TranscriptionDefinition Create(string name, string description, string locale, string recordingsUrl) {
        return TranscriptionDefinition(name, description, locale, recordingsUrl, std::list<string>());
    }
    static TranscriptionDefinition Create(string name, string description, string locale, string recordingsUrl,
        std::list<string> models) {
        return TranscriptionDefinition(name, description, locale, recordingsUrl, models);
    }
};

void to_json(nlohmann::json& j, const TranscriptionDefinition& t) {
    j = nlohmann::json{
            { "description", t.Description },
            { "locale", t.Locale },
            { "models", t.Models },
            { "name", t.Name },
            { "properties", t.properties },
            { "recordingsurl",t.RecordingsUrl }
    };
};

void from_json(const nlohmann::json& j, TranscriptionDefinition& t) {
    j.at("locale").get_to(t.Locale);
    j.at("models").get_to(t.Models);
    j.at("name").get_to(t.Name);
    j.at("properties").get_to(t.properties);
    j.at("recordingsurl").get_to(t.RecordingsUrl);
}

class Transcription {
public:
    string name;
    string description;
    string locale;
    string recordingsUrl;
    map<string, string> resultsUrls;
    string id;
    string createdDateTime;
    string lastActionDateTime;
    string status;
    string statusMessage;
};

void to_json(nlohmann::json& j, const Transcription& t) {
    j = nlohmann::json{
            { "description", t.description },
            { "locale", t.locale },
            { "createddatetime", t.createdDateTime },
            { "name", t.name },
            { "id", t.id },
            { "recordingsurl",t.recordingsUrl },
            { "resultUrls", t.resultsUrls},
            { "status", t.status},
            { "statusmessage", t.statusMessage}
    };
};

void from_json(const nlohmann::json& j, Transcription& t) {
    j.at("description").get_to(t.description);
    j.at("locale").get_to(t.locale);
    j.at("createdDateTime").get_to(t.createdDateTime);
    j.at("name").get_to(t.name);
    j.at("recordingsUrl").get_to(t.recordingsUrl);
    t.resultsUrls = j.at("resultsUrls").get<map<string, string>>();
    j.at("status").get_to(t.status);
    j.at("statusMessage").get_to(t.statusMessage);
}
class Result
{
public:
    string Lexical;
    string ITN;
    string MaskedITN;
    string Display;
};
void from_json(const nlohmann::json& j, Result& r) {
    j.at("Lexical").get_to(r.Lexical);
    j.at("ITN").get_to(r.ITN);
    j.at("MaskedITN").get_to(r.MaskedITN);
    j.at("Display").get_to(r.Display);
}

class NBest : public Result
{
public:
    double Confidence;	
};
void from_json(const nlohmann::json& j, NBest& nb) {
    j.at("Confidence").get_to(nb.Confidence);
    j.at("Lexical").get_to(nb.Lexical);
    j.at("ITN").get_to(nb.ITN);
    j.at("MaskedITN").get_to(nb.MaskedITN);
    j.at("Display").get_to(nb.Display);
}

class SegmentResult
{
public:
    string RecognitionStatus;
    ULONG Offset;
    ULONG Duration;
    std::list<NBest> NBest;
};
void from_json(const nlohmann::json& j, SegmentResult& sr) {
    j.at("RecognitionStatus").get_to(sr.RecognitionStatus);
    j.at("Offset").get_to(sr.Offset);
    j.at("Duration").get_to(sr.Duration);
    sr.NBest = j.at("NBest").get<list<NBest>>();
}

class AudioFileResult
{
public:
    string AudioFileName;
    std::list<SegmentResult> SegmentResults;
    std::list<Result> CombinedResults;
};
void from_json(const nlohmann::json& j, AudioFileResult& arf) {
    j.at("AudioFileName").get_to(arf.AudioFileName);
    arf.SegmentResults = j.at("SegmentResults").get<list<SegmentResult>>();
    arf.CombinedResults = j.at("CombinedResults").get<list<Result>>();
}

class RootObject {
public:
    std::list<AudioFileResult> AudioFileResults;
};
void from_json(const nlohmann::json& j, RootObject& r) {
    r.AudioFileResults = j.at("AudioFileResults").get<list<AudioFileResult>>();
}

Een HTTP-client maken en configurerenCreate and configure an Http Client

Het eerste wat u nodig hebt, is een HTTP-client waarvoor een juiste basis-URL en verificatieset is ingesteld.The first thing we'll need is an Http Client that has a correct base URL and authentication set. Deze code invoegen inrecognizeSpeechInsert this code in recognizeSpeech

utility::string_t service_url = U("https://") + region + U(".cris.ai/api/speechtotext/v2.0/Transcriptions/");
uri u(service_url);

http_client c(u);
http_request msg(methods::POST);
msg.headers().add(U("Content-Type"), U("application/json"));
msg.headers().add(U("Ocp-Apim-Subscription-Key"), subscriptionKey);

Een transcriptie-aanvraag genererenGenerate a transcription request

Vervolgens genereren we de transcriptie-aanvraag.Next, we'll generate the transcription request. Voeg deze code toe aanrecognizeSpeechAdd this code to recognizeSpeech

auto transportdef = TranscriptionDefinition::Create(name, description, myLocale, recordingsBlobUri);

nlohmann::json transportdefJSON = transportdef;

msg.set_body(transportdefJSON.dump());

De aanvraag verzenden en de status controlerenSend the request and check its status

Nu gaan we de aanvraag verzenden naar de speech-service en de eerste respons code controleren.Now we post the request to the Speech service and check the initial response code. Deze antwoord code geeft eenvoudigweg aan of de service de aanvraag heeft ontvangen.This response code will simply indicate if the service has received the request. De service retourneert een URL in de antwoord headers die de locatie is waar de transcriptie-status wordt opgeslagen.The service will return a Url in the response headers that's the location where it will store the transcription status.


auto response = c.request(msg).get();
auto statusCode = response.status_code();

if (statusCode != status_codes::Accepted)
{
    cout << "Unexpected status code " << statusCode << endl;
    return;
}

string_t transcriptionLocation = response.headers()[U("location")];

cout << "Transcription status is located at " << converter.to_bytes(transcriptionLocation) << endl;

Wacht tot de transcriptie is voltooidWait for the transcription to complete

Omdat de transcriptie asynchroon wordt verwerkt, moeten we elke regel vaak de status van de service controleren.Since the service processes the transcription asynchronously, we need to poll for its status every so often. We controleren elke vijf seconden.We'll check every 5 seconds.

We kunnen de status controleren door de inhoud op te halen bij de URL die we hebben ontvangen toen de aanvraag werd verzonden.We can check the status by retrieving the content at the Url we got when the posted the request. Wanneer de inhoud weer wordt opgehaald, deserialiseren we deze in een van onze helperklasse om gemakkelijker te kunnen communiceren met.When we get the content back, we deserialize it into one of our helper class to make it easier to interact with.

Dit is de polling code met status weergave voor alles, behalve een geslaagde voltooiing, we doen dat nu.Here's the polling code with status display for everything except a successful completion, we'll do that next.

bool completed = false;

while (!completed)
{
    auto statusResponse = statusCheckClient.request(statusCheckMessage).get();
    auto statusResponseCode = statusResponse.status_code();

    if (statusResponseCode != status_codes::OK)
    {
        cout << "Fetching the transcription returned unexpected http code " << statusResponseCode << endl;
        return;
    }

    auto body = statusResponse.extract_string().get();
    nlohmann::json statusJSON = nlohmann::json::parse(body);
    Transcription transcriptonStatus = statusJSON;

    if (!_stricmp(transcriptonStatus.status.c_str(), "Failed"))
    {
        completed = true;
        cout << "Transcription has failed " << transcriptonStatus.statusMessage << endl;
    }
    else if (!_stricmp(transcriptonStatus.status.c_str(), "Succeeded"))
    {
    }
    else if (!_stricmp(transcriptonStatus.status.c_str(), "Running"))
    {
        cout << "Transcription is running." << endl;
    }
    else if (!_stricmp(transcriptonStatus.status.c_str(), "NotStarted"))
    {
        cout << "Transcription has not started." << endl;
    }

    if (!completed) {
        Sleep(5000);
    }

}

De transcriptie-resultaten weer gevenDisplay the transcription results

Zodra de service de transcriptie heeft voltooid, worden de resultaten opgeslagen in een andere URL die we vanaf het status antwoord kunnen ontvangen.Once the service has successfully completed the transcription the results will be stored in another Url that we can get from the status response.

De inhoud van de URL wordt gedownload, de JSON gedeserialiseerd en de resultaten door lopen die worden afgedrukt op de weergave tekst.We'll download the contents of that URL, deserialize the JSON, and loop through the results printing out the display text as we go.

completed = true;
cout << "Success!" << endl;
string result = transcriptonStatus.resultsUrls["channel_0"];
cout << "Transcription has completed. Results are at " << result << endl;
cout << "Fetching results" << endl;

http_client result_client(converter.from_bytes(result));
http_request resultMessage(methods::GET);
resultMessage.headers().add(U("Ocp-Apim-Subscription-Key"), subscriptionKey);

auto resultResponse = result_client.request(resultMessage).get();

auto responseCode = resultResponse.status_code();

if (responseCode != status_codes::OK)
{
    cout << "Fetching the transcription returned unexpected http code " << responseCode << endl;
    return;
}

auto resultBody = resultResponse.extract_string().get();

nlohmann::json resultJSON = nlohmann::json::parse(resultBody);
RootObject root = resultJSON;

for (AudioFileResult af : root.AudioFileResults)
{
    cout << "There were " << af.SegmentResults.size() << " results in " << af.AudioFileName << endl;
    
    for (SegmentResult segResult : af.SegmentResults)
    {
        cout << "Status: " << segResult.RecognitionStatus << endl;

        if (!_stricmp(segResult.RecognitionStatus.c_str(), "success") && segResult.NBest.size() > 0)
        {
            cout << "Best text result was: '" << segResult.NBest.front().Display << "'" << endl;
        }
    }
}

Controleer uw codeCheck your code

Op dit moment moet uw code er als volgt uitzien: (er zijn enkele opmerkingen aan deze versie toegevoegd)At this point, your code should look like this: (We've added some comments to this version)

#include <iostream>
#include <strstream>
#include <Windows.h>
#include <locale>
#include <codecvt>
#include <string>

#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <nlohmann/json.hpp>

using namespace std;
using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace concurrency::streams;       // Asynchronous streams
using json = nlohmann::json;

const string_t region = U("YourServiceRegion");
const string_t subscriptionKey = U("YourSubscriptionKey");
const string name = "Simple transcription";
const string description = "Simple transcription description";
const string myLocale = "en-US";
const string recordingsBlobUri = "YourFileUrl";

class TranscriptionDefinition {
private:
    TranscriptionDefinition(string name,
        string description,
        string locale,
        string recordingsUrl,
        std::list<string> models) {

        Name = name;
        Description = description;
        RecordingsUrl = recordingsUrl;
        Locale = locale;
        Models = models;
    }

public:
    string Name;
    string Description;
    string RecordingsUrl;
    string Locale;
    std::list<string> Models;
    std::map<string, string> properties;

    static TranscriptionDefinition Create(string name, string description, string locale, string recordingsUrl) {
        return TranscriptionDefinition(name, description, locale, recordingsUrl, std::list<string>());
    }
    static TranscriptionDefinition Create(string name, string description, string locale, string recordingsUrl,
        std::list<string> models) {
        return TranscriptionDefinition(name, description, locale, recordingsUrl, models);
    }
};

void to_json(nlohmann::json& j, const TranscriptionDefinition& t) {
    j = nlohmann::json{
            { "description", t.Description },
            { "locale", t.Locale },
            { "models", t.Models },
            { "name", t.Name },
            { "properties", t.properties },
            { "recordingsurl",t.RecordingsUrl }
    };
};

void from_json(const nlohmann::json& j, TranscriptionDefinition& t) {
    j.at("locale").get_to(t.Locale);
    j.at("models").get_to(t.Models);
    j.at("name").get_to(t.Name);
    j.at("properties").get_to(t.properties);
    j.at("recordingsurl").get_to(t.RecordingsUrl);
}

class Transcription {
public:
    string name;
    string description;
    string locale;
    string recordingsUrl;
    map<string, string> resultsUrls;
    string id;
    string createdDateTime;
    string lastActionDateTime;
    string status;
    string statusMessage;
};

void to_json(nlohmann::json& j, const Transcription& t) {
    j = nlohmann::json{
            { "description", t.description },
            { "locale", t.locale },
            { "createddatetime", t.createdDateTime },
            { "name", t.name },
            { "id", t.id },
            { "recordingsurl",t.recordingsUrl },
            { "resultUrls", t.resultsUrls},
            { "status", t.status},
            { "statusmessage", t.statusMessage}
    };
};

void from_json(const nlohmann::json& j, Transcription& t) {
    j.at("description").get_to(t.description);
    j.at("locale").get_to(t.locale);
    j.at("createdDateTime").get_to(t.createdDateTime);
    j.at("name").get_to(t.name);
    j.at("recordingsUrl").get_to(t.recordingsUrl);
    t.resultsUrls = j.at("resultsUrls").get<map<string, string>>();
    j.at("status").get_to(t.status);
    j.at("statusMessage").get_to(t.statusMessage);
}
class Result
{
public:
    string Lexical;
    string ITN;
    string MaskedITN;
    string Display;
};
void from_json(const nlohmann::json& j, Result& r) {
    j.at("Lexical").get_to(r.Lexical);
    j.at("ITN").get_to(r.ITN);
    j.at("MaskedITN").get_to(r.MaskedITN);
    j.at("Display").get_to(r.Display);
}

class NBest : public Result
{
public:
    double Confidence;	
};
void from_json(const nlohmann::json& j, NBest& nb) {
    j.at("Confidence").get_to(nb.Confidence);
    j.at("Lexical").get_to(nb.Lexical);
    j.at("ITN").get_to(nb.ITN);
    j.at("MaskedITN").get_to(nb.MaskedITN);
    j.at("Display").get_to(nb.Display);
}

class SegmentResult
{
public:
    string RecognitionStatus;
    ULONG Offset;
    ULONG Duration;
    std::list<NBest> NBest;
};
void from_json(const nlohmann::json& j, SegmentResult& sr) {
    j.at("RecognitionStatus").get_to(sr.RecognitionStatus);
    j.at("Offset").get_to(sr.Offset);
    j.at("Duration").get_to(sr.Duration);
    sr.NBest = j.at("NBest").get<list<NBest>>();
}

class AudioFileResult
{
public:
    string AudioFileName;
    std::list<SegmentResult> SegmentResults;
    std::list<Result> CombinedResults;
};
void from_json(const nlohmann::json& j, AudioFileResult& arf) {
    j.at("AudioFileName").get_to(arf.AudioFileName);
    arf.SegmentResults = j.at("SegmentResults").get<list<SegmentResult>>();
    arf.CombinedResults = j.at("CombinedResults").get<list<Result>>();
}

class RootObject {
public:
    std::list<AudioFileResult> AudioFileResults;
};
void from_json(const nlohmann::json& j, RootObject& r) {
    r.AudioFileResults = j.at("AudioFileResults").get<list<AudioFileResult>>();
}


void recognizeSpeech()
{
    std::wstring_convert<std::codecvt_utf8_utf16<wchar_t >> converter;

    utility::string_t service_url = U("https://") + region + U(".cris.ai/api/speechtotext/v2.0/Transcriptions/");
    uri u(service_url);

    http_client c(u);
    http_request msg(methods::POST);
    msg.headers().add(U("Content-Type"), U("application/json"));
    msg.headers().add(U("Ocp-Apim-Subscription-Key"), subscriptionKey);

    auto transportdef = TranscriptionDefinition::Create(name, description, myLocale, recordingsBlobUri);

    nlohmann::json transportdefJSON = transportdef;

    msg.set_body(transportdefJSON.dump());

    auto response = c.request(msg).get();
    auto statusCode = response.status_code();

    if (statusCode != status_codes::Accepted)
    {
        cout << "Unexpected status code " << statusCode << endl;
        return;
    }

    string_t transcriptionLocation = response.headers()[U("location")];

    cout << "Transcription status is located at " << converter.to_bytes(transcriptionLocation) << endl;

    http_client statusCheckClient(transcriptionLocation);
    http_request statusCheckMessage(methods::GET);
    statusCheckMessage.headers().add(U("Ocp-Apim-Subscription-Key"), subscriptionKey);

    bool completed = false;

    while (!completed)
    {
        auto statusResponse = statusCheckClient.request(statusCheckMessage).get();
        auto statusResponseCode = statusResponse.status_code();

        if (statusResponseCode != status_codes::OK)
        {
            cout << "Fetching the transcription returned unexpected http code " << statusResponseCode << endl;
            return;
        }

        auto body = statusResponse.extract_string().get();
        nlohmann::json statusJSON = nlohmann::json::parse(body);
        Transcription transcriptonStatus = statusJSON;

        if (!_stricmp(transcriptonStatus.status.c_str(), "Failed"))
        {
            completed = true;
            cout << "Transcription has failed " << transcriptonStatus.statusMessage << endl;
        }
        else if (!_stricmp(transcriptonStatus.status.c_str(), "Succeeded"))
        {
            completed = true;
            cout << "Success!" << endl;
            string result = transcriptonStatus.resultsUrls["channel_0"];
            cout << "Transcription has completed. Results are at " << result << endl;
            cout << "Fetching results" << endl;

            http_client result_client(converter.from_bytes(result));
            http_request resultMessage(methods::GET);
            resultMessage.headers().add(U("Ocp-Apim-Subscription-Key"), subscriptionKey);

            auto resultResponse = result_client.request(resultMessage).get();

            auto responseCode = resultResponse.status_code();

            if (responseCode != status_codes::OK)
            {
                cout << "Fetching the transcription returned unexpected http code " << responseCode << endl;
                return;
            }

            auto resultBody = resultResponse.extract_string().get();
            
            nlohmann::json resultJSON = nlohmann::json::parse(resultBody);
            RootObject root = resultJSON;

            for (AudioFileResult af : root.AudioFileResults)
            {
                cout << "There were " << af.SegmentResults.size() << " results in " << af.AudioFileName << endl;
                
                for (SegmentResult segResult : af.SegmentResults)
                {
                    cout << "Status: " << segResult.RecognitionStatus << endl;

                    if (!_stricmp(segResult.RecognitionStatus.c_str(), "success") && segResult.NBest.size() > 0)
                    {
                        cout << "Best text result was: '" << segResult.NBest.front().Display << "'" << endl;
                    }
                }
            }
        }
        else if (!_stricmp(transcriptonStatus.status.c_str(), "Running"))
        {
            cout << "Transcription is running." << endl;
        }
        else if (!_stricmp(transcriptonStatus.status.c_str(), "NotStarted"))
        {
            cout << "Transcription has not started." << endl;
        }

        if (!completed) {
            Sleep(5000);
        }

    }
}

int wmain()
{
    recognizeSpeech();
    cout << "Please press a key to continue.\n";
    cin.get();
    return 0;
}

Uw app bouwen en uitvoerenBuild and run your app

Nu bent u klaar om uw app te bouwen en de spraak herkenning te testen met behulp van de speech-service.Now you're ready to build your app and test our speech recognition using the Speech service.

Volgende stappenNext steps


In deze Quick start gaat u een REST API gebruiken om spraak te herkennen van bestanden in een batch-proces.In this quickstart, you will use a REST API to recognize speech from files in a batch process. Een batch-proces voert de spraak transcriptie uit zonder gebruikers interacties.A batch process executes the speech transcription without any user interactions. Het biedt u een eenvoudig programmeer model, zonder de nood zaak om gelijktijdigheid, aangepaste spraak modellen of andere gegevens te beheren.It gives you a simple programming model, without the need to manage concurrency, custom speech models, or other details. Dit omvat geavanceerde beheer opties, terwijl er efficiënt gebruik wordt gemaakt van Azure speech-service bronnen.It entails advanced control options, while making efficient use of Azure speech service resources.

Zie batch transcriptievoor meer informatie over de beschik bare opties en configuratie details.For more information on the available options and configuration details, see batch transcription.

In de volgende snelstartgids vindt u een voor beeld van het gebruik.The following quickstart will walk you through a usage sample.

U kunt alle Java-voorbeelden van de Speech SDK op GitHub bekijken of downloaden.You can view or download all Speech SDK Java Samples on GitHub.

VereistenPrerequisites

Voordat u aan de slag gaat, moet u het volgende doen:Before you get started, make sure to:

Open uw project in eclipsOpen your project in Eclipse

De eerste stap is om ervoor te zorgen dat uw project in eclips is geopend.The first step is to make sure that you have your project open in Eclipse.

  1. Eclipse startenLaunch Eclipse
  2. Laad uw project en open Main.javahet.Load your project and open Main.java.

Een verwijzing naar Gson toevoegenAdd a reference to Gson

In deze Quick Start maakt u gebruik van een externe JSON-serialisatiefunctie/deserialisatie.We'll be using an external JSON serializer / deserializer in this quickstart. Voor Java hebben we gekozen voor Gson.For Java we've chosen Gson.

Open pom. XML en voeg de volgende verwijzing toe.Open your pom.xml and add the following reference.

<dependencies>
  <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.6</version>
  </dependency>
</dependencies>

Begin met een van de standaard codeStart with some boilerplate code

Laten we een code toevoegen die als een skelet voor het project werkt.Let's add some code that works as a skeleton for our project.

package quickstart;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.UUID;

import com.google.gson.Gson;
public class Main {

    private static String region = "YourServiceRegion";
    private static String subscriptionKey = "YourSubscriptionKey";
    private static String Locale = "en-US";
    private static String RecordingsBlobUri = "YourFileUrl";
    private static String Name = "Simple transcription";
    private static String Description = "Simple transcription description";

    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("Starting transcriptions client...");
    }
}

U moet de volgende waarden vervangen:You'll need to replace the following values:

  • YourSubscriptionKey: gevonden op de pagina sleutels van de Azure portal voor de spraak resourceYourSubscriptionKey: found in the the Keys page of the Azure portal for the Speech resource
  • YourServiceRegion: gevonden op de pagina overzicht van de Azure portal voor de spraak resourceYourServiceRegion: found in the the Overview page of the Azure portal for the Speech resource
  • YourFileUrl: gevonden op op de pagina BLOB service/containers van de Azure portal voor de bron van het opslag accountYourFileUrl: found in under the Blob service / Containers page of the Azure portal for the Storage account resource
    • Selecteer de juiste containerSelect the appropriate container
    • Selecteer de gewenste BLOBSelect the desired blob
    • Kopieer de URL onder de pagina EigenschappenCopy the URL under the Properties page

JSON-wrappersJSON Wrappers

Als de aanvragen van de REST API in JSON-indeling worden uitgevoerd en de resultaten worden geretourneerd in JSON, kunnen ze met alleen teken reeksen communiceren, maar dit wordt niet aanbevolen.As the REST API's take requests in JSON format and also return results in JSON we could interact with them using only strings, but that's not recommended. Om de aanvragen en reacties gemakkelijker te kunnen beheren, declareren we een aantal klassen die moeten worden gebruikt voor het serialiseren/deserialiseren van de JSON.In order to make the requests and responses easier to manage, we'll declare a few classes to use for serializing / deserializing the JSON.

Plaats de declaraties vooraf Main.Go ahead and put their declarations before Main.

final class Transcription {
    public String name;
    public String description;
    public String locale;
    public URL recordingsUrl;
    public Hashtable<String, String> resultsUrls;
    public UUID id;
    public Date createdDateTime;
    public Date lastActionDateTime;
    public String status;
    public String statusMessage;
}

final class TranscriptionDefinition {
    private TranscriptionDefinition(String name, String description, String locale, URL recordingsUrl,
            ModelIdentity[] models) {
        this.Name = name;
        this.Description = description;
        this.RecordingsUrl = recordingsUrl;
        this.Locale = locale;
        this.Models = models;
        this.properties = new Hashtable<String, String>();
        this.properties.put("PunctuationMode", "DictatedAndAutomatic");
        this.properties.put("ProfanityFilterMode", "Masked");
        this.properties.put("AddWordLevelTimestamps", "True");
    }

    public String Name;
    public String Description;
    public URL RecordingsUrl;
    public String Locale;
    public ModelIdentity[] Models;
    public Dictionary<String, String> properties;

    public static TranscriptionDefinition Create(String name, String description, String locale, URL recordingsUrl) {
        return TranscriptionDefinition.Create(name, description, locale, recordingsUrl, new ModelIdentity[0]);
    }

    public static TranscriptionDefinition Create(String name, String description, String locale, URL recordingsUrl,
            ModelIdentity[] models) {
        return new TranscriptionDefinition(name, description, locale, recordingsUrl, models);
    }
}

final class ModelIdentity {
    private ModelIdentity(UUID id) {
        this.Id = id;
    }

    public UUID Id;

    public static ModelIdentity Create(UUID Id) {
        return new ModelIdentity(Id);
    }
}

class AudioFileResult {
    public String AudioFileName;
    public SegmentResult[] SegmentResults;
}

class RootObject {
    public AudioFileResult[] AudioFileResults;
}

class NBest {
    public double Confidence;
    public String Lexical;
    public String ITN;
    public String MaskedITN;
    public String Display;
}

class SegmentResult {
    public String RecognitionStatus;
    public String Offset;
    public String Duration;
    public NBest[] NBest;
}

Een HTTP-client maken en configurerenCreate and configure an Http Client

Het eerste wat u nodig hebt, is een HTTP-client waarvoor een juiste basis-URL en verificatieset is ingesteld.The first thing we'll need is an Http Client that has a correct base URL and authentication set. Deze code invoegen in Main[!code-java]Insert this code in Main [!code-java]

Een transcriptie-aanvraag genererenGenerate a transcription request

Vervolgens genereren we de transcriptie-aanvraag.Next, we'll generate the transcription request. Voeg deze code toe Main aan[!code-java]Add this code to Main [!code-java]

De aanvraag verzenden en de status controlerenSend the request and check its status

Nu gaan we de aanvraag verzenden naar de speech-service en de eerste respons code controleren.Now we post the request to the Speech service and check the initial response code. Deze antwoord code geeft eenvoudigweg aan of de service de aanvraag heeft ontvangen.This response code will simply indicate if the service has received the request. De service retourneert een URL in de antwoord headers die de locatie is waar de transcriptie-status wordt opgeslagen.The service will return a Url in the response headers that's the location where it will store the transcription status.

Gson gson = new Gson();

OutputStream stream = postConnection.getOutputStream();
stream.write(gson.toJson(definition).getBytes());
stream.flush();

int statusCode = postConnection.getResponseCode();

if (statusCode != HttpURLConnection.HTTP_ACCEPTED) {
    System.out.println("Unexpected status code " + statusCode);
    return;
}

Wacht tot de transcriptie is voltooidWait for the transcription to complete

Omdat de transcriptie asynchroon wordt verwerkt, moeten we elke regel vaak de status van de service controleren.Since the service processes the transcription asynchronously, we need to poll for its status every so often. We controleren elke vijf seconden.We'll check every 5 seconds.

We kunnen de status controleren door de inhoud op te halen bij de URL die we hebben ontvangen toen de aanvraag werd verzonden.We can check the status by retrieving the content at the Url we got when the posted the request. Wanneer de inhoud weer wordt opgehaald, deserialiseren we deze in een van onze helperklasse om gemakkelijker te kunnen communiceren met.When we get the content back, we deserialize it into one of our helper class to make it easier to interact with.

Dit is de polling code met status weergave voor alles, behalve een geslaagde voltooiing, we doen dat nu.Here's the polling code with status display for everything except a successful completion, we'll do that next.

String transcriptionLocation = postConnection.getHeaderField("location");

System.out.println("Transcription is located at " + transcriptionLocation);

URL transcriptionUrl = new URL(transcriptionLocation);

boolean completed = false;
while (!completed) {
    {
        HttpURLConnection getConnection = (HttpURLConnection) transcriptionUrl.openConnection();
        getConnection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
        getConnection.setRequestMethod("GET");

        int responseCode = getConnection.getResponseCode();

        if (responseCode != HttpURLConnection.HTTP_OK) {
            System.out.println("Fetching the transcription returned unexpected http code " + responseCode);
            return;
        }

        Transcription t = gson.fromJson(new InputStreamReader(getConnection.getInputStream()),
                Transcription.class);

        switch (t.status) {
        case "Failed":
            completed = true;
            System.out.println("Transcription has failed " + t.statusMessage);
            break;
        case "Succeeded":
            break;
        case "Running":
            System.out.println("Transcription is running.");
            break;
        case "NotStarted":
            System.out.println("Transcription has not started.");
            break;
        }

        if (!completed) {
            Thread.sleep(5000);
        }
    }

De transcriptie-resultaten weer gevenDisplay the transcription results

Zodra de service de transcriptie heeft voltooid, worden de resultaten opgeslagen in een andere URL die we vanaf het status antwoord kunnen ontvangen.Once the service has successfully completed the transcription the results will be stored in another Url that we can get from the status response.

De inhoud van de URL wordt gedownload, de JSON gedeserialiseerd en de resultaten door lopen die worden afgedrukt op de weergave tekst.We'll download the contents of that URL, deserialize the JSON, and loop through the results printing out the display text as we go.

package quickstart;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.UUID;

import com.google.gson.Gson;

final class Transcription {
    public String name;
    public String description;
    public String locale;
    public URL recordingsUrl;
    public Hashtable<String, String> resultsUrls;
    public UUID id;
    public Date createdDateTime;
    public Date lastActionDateTime;
    public String status;
    public String statusMessage;
}

final class TranscriptionDefinition {
    private TranscriptionDefinition(String name, String description, String locale, URL recordingsUrl,
            ModelIdentity[] models) {
        this.Name = name;
        this.Description = description;
        this.RecordingsUrl = recordingsUrl;
        this.Locale = locale;
        this.Models = models;
        this.properties = new Hashtable<String, String>();
        this.properties.put("PunctuationMode", "DictatedAndAutomatic");
        this.properties.put("ProfanityFilterMode", "Masked");
        this.properties.put("AddWordLevelTimestamps", "True");
    }

    public String Name;
    public String Description;
    public URL RecordingsUrl;
    public String Locale;
    public ModelIdentity[] Models;
    public Dictionary<String, String> properties;

    public static TranscriptionDefinition Create(String name, String description, String locale, URL recordingsUrl) {
        return TranscriptionDefinition.Create(name, description, locale, recordingsUrl, new ModelIdentity[0]);
    }

    public static TranscriptionDefinition Create(String name, String description, String locale, URL recordingsUrl,
            ModelIdentity[] models) {
        return new TranscriptionDefinition(name, description, locale, recordingsUrl, models);
    }
}

final class ModelIdentity {
    private ModelIdentity(UUID id) {
        this.Id = id;
    }

    public UUID Id;

    public static ModelIdentity Create(UUID Id) {
        return new ModelIdentity(Id);
    }
}

class AudioFileResult {
    public String AudioFileName;
    public SegmentResult[] SegmentResults;
}

class RootObject {
    public AudioFileResult[] AudioFileResults;
}

class NBest {
    public double Confidence;
    public String Lexical;
    public String ITN;
    public String MaskedITN;
    public String Display;
}

class SegmentResult {
    public String RecognitionStatus;
    public String Offset;
    public String Duration;
    public NBest[] NBest;
}

public class Main {

    private static String region = "YourServiceRegion";
    private static String subscriptionKey = "YourSubscriptionKey";
    private static String Locale = "en-US";
    private static String RecordingsBlobUri = "YourFileUrl";
    private static String Name = "Simple transcription";
    private static String Description = "Simple transcription description";

    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("Starting transcriptions client...");
        String url = "https://" + region + ".cris.ai/api/speechtotext/v2.0/Transcriptions/";
        URL serviceUrl = new URL(url);

        HttpURLConnection postConnection = (HttpURLConnection) serviceUrl.openConnection();
        postConnection.setDoOutput(true);
        postConnection.setRequestMethod("POST");
        postConnection.setRequestProperty("Content-Type", "application/json");
        postConnection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);

        TranscriptionDefinition definition = TranscriptionDefinition.Create(Name, Description, Locale,
                new URL(RecordingsBlobUri));

        Gson gson = new Gson();

        OutputStream stream = postConnection.getOutputStream();
        stream.write(gson.toJson(definition).getBytes());
        stream.flush();

        int statusCode = postConnection.getResponseCode();

        if (statusCode != HttpURLConnection.HTTP_ACCEPTED) {
            System.out.println("Unexpected status code " + statusCode);
            return;
        }

        String transcriptionLocation = postConnection.getHeaderField("location");

        System.out.println("Transcription is located at " + transcriptionLocation);

        URL transcriptionUrl = new URL(transcriptionLocation);

        boolean completed = false;
        while (!completed) {
            {
                HttpURLConnection getConnection = (HttpURLConnection) transcriptionUrl.openConnection();
                getConnection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
                getConnection.setRequestMethod("GET");

                int responseCode = getConnection.getResponseCode();

                if (responseCode != HttpURLConnection.HTTP_OK) {
                    System.out.println("Fetching the transcription returned unexpected http code " + responseCode);
                    return;
                }

                Transcription t = gson.fromJson(new InputStreamReader(getConnection.getInputStream()),
                        Transcription.class);

                switch (t.status) {
                case "Failed":
                    completed = true;
                    System.out.println("Transcription has failed " + t.statusMessage);
                    break;
                case "Succeeded":
                    completed = true;
                    String result = t.resultsUrls.get("channel_0");
                    System.out.println("Transcription has completed. Results are at " + result);

                    System.out.println("Fetching results");
                    URL resultUrl = new URL(result);

                    HttpURLConnection resultConnection = (HttpURLConnection) resultUrl.openConnection();
                    resultConnection.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey);
                    resultConnection.setRequestMethod("GET");

                    responseCode = resultConnection.getResponseCode();

                    if (responseCode != HttpURLConnection.HTTP_OK) {
                        System.out.println("Fetching the transcription returned unexpected http code " + responseCode);
                        return;
                    }

                    RootObject root = gson.fromJson(new InputStreamReader(resultConnection.getInputStream()),
                            RootObject.class);

                    for (AudioFileResult af : root.AudioFileResults) {
                        System.out
                                .println("There were " + af.SegmentResults.length + " results in " + af.AudioFileName);
                        for (SegmentResult segResult : af.SegmentResults) {
                            System.out.println("Status: " + segResult.RecognitionStatus);
                            if (segResult.RecognitionStatus.equalsIgnoreCase("success") && segResult.NBest.length > 0) {
                                System.out.println("Best text result was: '" + segResult.NBest[0].Display + "'");
                            }
                        }
                    }

                    break;
                case "Running":
                    System.out.println("Transcription is running.");
                    break;
                case "NotStarted":
                    System.out.println("Transcription has not started.");
                    break;
                }

                if (!completed) {
                    Thread.sleep(5000);
                }
            }
        }
    }
}

Controleer uw codeCheck your code

Op dit moment moet uw code er als volgt uitzien: (er zijn enkele opmerkingen aan deze versie toegevoegd) [! code-Java [] (~/samples-cognitive-services-speech-sdk/quickstart/java/jre/from-blob/src/quickstart/Main.java]At this point, your code should look like this: (We've added some comments to this version) [!code-java[](~/samples-cognitive-services-speech-sdk/quickstart/java/jre/from-blob/src/quickstart/Main.java]

Uw app bouwen en uitvoerenBuild and run your app

Nu bent u klaar om uw app te bouwen en de spraak herkenning te testen met behulp van de speech-service.Now you're ready to build your app and test our speech recognition using the Speech service.

Volgende stappenNext steps

In deze Quick start gaat u een REST API gebruiken om spraak te herkennen van bestanden in een batch-proces.In this quickstart, you will use a REST API to recognize speech from files in a batch process. Een batch-proces voert de spraak transcriptie uit zonder gebruikers interacties.A batch process executes the speech transcription without any user interactions. Het biedt u een eenvoudig programmeer model, zonder de nood zaak om gelijktijdigheid, aangepaste spraak modellen of andere gegevens te beheren.It gives you a simple programming model, without the need to manage concurrency, custom speech models, or other details. Dit omvat geavanceerde beheer opties, terwijl er efficiënt gebruik wordt gemaakt van Azure speech-service bronnen.It entails advanced control options, while making efficient use of Azure speech service resources.

Zie batch transcriptievoor meer informatie over de beschik bare opties en configuratie details.For more information on the available options and configuration details, see batch transcription.

In de volgende snelstartgids vindt u een voor beeld van het gebruik.The following quickstart will walk you through a usage sample.

U kunt alle Python-voorbeelden van de Speech SDK op GitHub bekijken of downloaden.You can view or download all Speech SDK Python Samples on GitHub.

VereistenPrerequisites

Voordat u aan de slag gaat, moet u het volgende doen:Before you get started, make sure to:

Down load en installeer de API-client bibliotheekDownload and install the API client library

Als u het voor beeld wilt uitvoeren, moet u de python-bibliotheek genereren voor de REST API die wordt gegenereerd via Swagger.To execute the sample you need to generate the Python library for the REST API which is generated through Swagger.

Voer de volgende stappen uit om de installatie uit te voeren:Follow these steps for the installation:

  1. Ga naar https://editor.swagger.io.Go to https://editor.swagger.io.
  2. Klik op bestanden klik vervolgens op URL importeren.Click File, then click Import URL.
  3. Voer de URL voor Swagger in, inclusief de regio voor uw speech https://<your-region>.cris.ai/docs/v2.0/swaggerservice-abonnement:.Enter the Swagger URL including the region for your Speech service subscription: https://<your-region>.cris.ai/docs/v2.0/swagger.
  4. Klik op client genereren en selecteer python.Click Generate Client and select Python.
  5. Sla de client bibliotheek op.Save the client library.
  6. Pak de gedownloade python-client-generated. zip ergens in het bestands systeem uit.Extract the downloaded python-client-generated.zip somewhere in your file system.
  7. Installeer de uitgepakte python-client module in uw python-omgeving met pip install path/to/package/python-clientPIP:.Install the extracted python-client module in your Python environment using pip: pip install path/to/package/python-client.
  8. Het geïnstalleerde pakket heeft de naam swagger_client.The installed package has the name swagger_client. U kunt controleren of de installatie is uitgevoerd met behulp van de opdracht python -c "import swagger_client".You can check that the installation worked using the command python -c "import swagger_client".

Notitie

Als gevolg van een bekende fout in de Swagger automatisch genereren, kunnen er fouten optreden bij het swagger_client importeren van het pakket.Due to a known bug in the Swagger autogeneration, you might encounter errors on importing the swagger_client package. Deze kunnen worden opgelost door de regel met de inhoud te verwijderenThese can be fixed by deleting the line with the content

from swagger_client.models.model import Model  # noqa: F401,E501

uit het bestand swagger_client/models/model.py en de regel met de inhoudfrom the file swagger_client/models/model.py and the line with the content

from swagger_client.models.inner_error import InnerError  # noqa: F401,E501

van het bestand swagger_client/models/inner_error.py in het geïnstalleerde pakket.from the file swagger_client/models/inner_error.py inside the installed package. In het fout bericht wordt aangegeven waar deze bestanden zich bevinden voor uw installatie.The error message will tell you where these files are located for your installation.

Andere afhankelijkheden installerenInstall other dependencies

Het voor beeld maakt requests gebruik van de-bibliotheek.The sample uses the requests library. U kunt deze installeren met behulp van de opdrachtYou can install it with the command

pip install requests

Begin met een van de standaard codeStart with some boilerplate code

Laten we een code toevoegen die als een skelet voor het project werkt.Let's add some code that works as a skeleton for our project.

#!/usr/bin/env python
# coding: utf-8
from typing import List

import logging
import sys
import requests
import time
import swagger_client as cris_client


logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format="%(message)s")

# Your subscription key and region for the speech service
SUBSCRIPTION_KEY = "YourSubscriptionKey"
SERVICE_REGION = "YourServiceRegion"

NAME = "Simple transcription"
DESCRIPTION = "Simple transcription description"

LOCALE = "en-US"
RECORDINGS_BLOB_URI = "<Your SAS Uri to the recording>"

# Set subscription information when doing transcription with custom models
ADAPTED_ACOUSTIC_ID = None  # guid of a custom acoustic model
ADAPTED_LANGUAGE_ID = None  # guid of a custom language model


def transcribe():
    logging.info("Starting transcription client...")


if __name__ == "__main__":
    transcribe()

U moet de volgende waarden vervangen:You'll need to replace the following values:

  • YourSubscriptionKey: gevonden op de pagina sleutels van de Azure portal voor de spraak resourceYourSubscriptionKey: found in the the Keys page of the Azure portal for the Speech resource
  • YourServiceRegion: gevonden op de pagina overzicht van de Azure portal voor de spraak resourceYourServiceRegion: found in the the Overview page of the Azure portal for the Speech resource
  • YourFileUrl: gevonden op op de pagina BLOB service/containers van de Azure portal voor de bron van het opslag accountYourFileUrl: found in under the Blob service / Containers page of the Azure portal for the Storage account resource
    • Selecteer de juiste containerSelect the appropriate container
    • Selecteer de gewenste BLOBSelect the desired blob
    • Kopieer de URL onder de pagina EigenschappenCopy the URL under the Properties page

Een HTTP-client maken en configurerenCreate and configure an Http Client

Het eerste wat u nodig hebt, is een HTTP-client waarvoor een juiste basis-URL en verificatieset is ingesteld.The first thing we'll need is an Http Client that has a correct base URL and authentication set. Deze code invoegen in transcribe[!code-python]Insert this code in transcribe [!code-python]

Een transcriptie-aanvraag genererenGenerate a transcription request

Vervolgens genereren we de transcriptie-aanvraag.Next, we'll generate the transcription request. Voeg deze code toe transcribe aan[!code-python]Add this code to transcribe [!code-python]

De aanvraag verzenden en de status controlerenSend the request and check its status

Nu gaan we de aanvraag verzenden naar de speech-service en de eerste respons code controleren.Now we post the request to the Speech service and check the initial response code. Deze antwoord code geeft eenvoudigweg aan of de service de aanvraag heeft ontvangen.This response code will simply indicate if the service has received the request. De service retourneert een URL in de antwoord headers die de locatie is waar de transcriptie-status wordt opgeslagen.The service will return a Url in the response headers that's the location where it will store the transcription status.

data, status, headers = transcription_api.create_transcription_with_http_info(transcription_definition)

# extract transcription location from the headers
transcription_location: str = headers["location"]

# get the transcription Id from the location URI
created_transcription: str = transcription_location.split('/')[-1]

logging.info("Created new transcription with id {}".format(created_transcription))

Wacht tot de transcriptie is voltooidWait for the transcription to complete

Omdat de transcriptie asynchroon wordt verwerkt, moeten we elke regel vaak de status van de service controleren.Since the service processes the transcription asynchronously, we need to poll for its status every so often. We controleren elke vijf seconden.We'll check every 5 seconds.

Alle transcripties die door deze speech service-resource worden verwerkt, worden geïnventariseerd en er wordt gezocht naar het account dat we hebben gemaakt.We'll enumerate all the transcriptions that this Speech service resource is processing and look for the one we created.

Dit is de polling code met status weergave voor alles, behalve een geslaagde voltooiing, we doen dat nu.Here's the polling code with status display for everything except a successful completion, we'll do that next.

logging.info("Checking status.")

completed = False

while not completed:
    running, not_started = 0, 0

    # get all transcriptions for the user
    transcriptions: List[cris_client.Transcription] = transcription_api.get_transcriptions()

    # for each transcription in the list we check the status
    for transcription in transcriptions:
        if transcription.status in ("Failed", "Succeeded"):
            # we check to see if it was the transcription we created from this client
            if created_transcription != transcription.id:
                continue

            completed = True

            if transcription.status == "Succeeded":
            else:
                logging.info("Transcription failed :{}.".format(transcription.status_message))
                break
        elif transcription.status == "Running":
            running += 1
        elif transcription.status == "NotStarted":
            not_started += 1

    logging.info("Transcriptions status: "
            "completed (this transcription): {}, {} running, {} not started yet".format(
                completed, running, not_started))

    # wait for 5 seconds
    time.sleep(5)

De transcriptie-resultaten weer gevenDisplay the transcription results

Zodra de service de transcriptie heeft voltooid, worden de resultaten opgeslagen in een andere URL die we vanaf het status antwoord kunnen ontvangen.Once the service has successfully completed the transcription the results will be stored in another Url that we can get from the status response.

Hier krijgen we dat resultaat JSON en weer gegeven.Here we get that result JSON and display it.

results_uri = transcription.results_urls["channel_0"]
results = requests.get(results_uri)
logging.info("Transcription succeeded. Results: ")
logging.info(results.content.decode("utf-8"))

Controleer uw codeCheck your code

Op dit moment moet uw code er als volgt uitzien: (er zijn enkele opmerkingen aan deze versie toegevoegd)[!code-python]At this point, your code should look like this: (We've added some comments to this version) [!code-python]

Uw app bouwen en uitvoerenBuild and run your app

Nu bent u klaar om uw app te bouwen en de spraak herkenning te testen met behulp van de speech-service.Now you're ready to build your app and test our speech recognition using the Speech service.

Volgende stappenNext steps

In deze Quick start gaat u een REST API gebruiken om spraak te herkennen van bestanden in een batch-proces.In this quickstart, you will use a REST API to recognize speech from files in a batch process. Een batch-proces voert de spraak transcriptie uit zonder gebruikers interacties.A batch process executes the speech transcription without any user interactions. Het biedt u een eenvoudig programmeer model, zonder de nood zaak om gelijktijdigheid, aangepaste spraak modellen of andere gegevens te beheren.It gives you a simple programming model, without the need to manage concurrency, custom speech models, or other details. Dit omvat geavanceerde beheer opties, terwijl er efficiënt gebruik wordt gemaakt van Azure speech-service bronnen.It entails advanced control options, while making efficient use of Azure speech service resources.

Zie batch transcriptievoor meer informatie over de beschik bare opties en configuratie details.For more information on the available options and configuration details, see batch transcription.

In de volgende snelstartgids vindt u een voor beeld van het gebruik.The following quickstart will walk you through a usage sample.

U kunt alle JavaScript-voorbeelden van de Speech SDK bekijken of downloaden op GitHub.You can view or download all Speech SDK JavaScript Samples on GitHub.

VereistenPrerequisites

Voordat u aan de slag gaat, moet u het volgende doen:Before you get started, make sure to:

Een nieuw JS-bestand makenCreate a new JS file

De eerste stap is het openen van uw project in uw favoriete editor.The first step is to make sure that you have your project open in your favorite editor.

Noem het bestand index.js.Call your file index.js.

Beginnen met standaardcodeStart with some boilerplate code

We gaan wat code toevoegen die als basis voor het project gaat dienen.Let's add some code that works as a skeleton for our project.

const https = require("https");

// Replace with your subscription key
SubscriptionKey = "YourSubscriptionKey";

// Update with your service region
Region = "YourServiceRegion";
Port = 443;

// Recordings and locale
Locale = "en-US";
RecordingsBlobUri = "YourFileUrl";

// Name and description
Name = "Simple transcription";
Description = "Simple transcription description";

SpeechToTextBasePath = "/api/speechtotext/v2.0/";

U moet de volgende waarden vervangen:You'll need to replace the following values:

  • YourSubscriptionKey: gevonden op de pagina sleutels van de Azure portal voor de spraak resourceYourSubscriptionKey: found in the the Keys page of the Azure portal for the Speech resource
  • YourServiceRegion: gevonden op de pagina overzicht van de Azure portal voor de spraak resourceYourServiceRegion: found in the the Overview page of the Azure portal for the Speech resource
  • YourFileUrl: gevonden op op de pagina BLOB service/containers van de Azure portal voor de bron van het opslag accountYourFileUrl: found in under the Blob service / Containers page of the Azure portal for the Storage account resource
    • Selecteer de juiste containerSelect the appropriate container
    • Selecteer de gewenste BLOBSelect the desired blob
    • Kopieer de URL onder de pagina EigenschappenCopy the URL under the Properties page

JSON-wrappersJSON Wrappers

De aanvragen van de REST API worden in JSON-indeling uitgevoerd en de resultaten worden ook in JSON geretourneerd.As the REST API's take requests in JSON format and also return results in JSON. Om ervoor te zorgen dat de aanvragen en antwoorden gemakkelijker te begrijpen zijn, declareren we een aantal klassen die moeten worden gebruikt voor het serialiseren/deserialiseren van de JSON.In order to make the requests and responses easier to understand, we'll declare a few classes to use for serializing / deserializing the JSON.

class ModelIdentity {
    id;
}

class Transcription {
    Name;
    Description;
    Locale;
    RecordingsUrl;
    ResultsUrls;
    Id;
    CreatedDateTime;
    LastActionDateTime;
    Status;
    StatusMessage;
}

class TranscriptionDefinition {
    Name;
    Description;
    RecordingsUrl;
    Locale;
    Models;
    Properties;
}

Maak een eerste transcriptie-aanvraag.Create an initial transcription request.

Genereeer vervolgens de transcriptie-aanvraag.Next, we'll generate the transcription request.

const ts = {
    Name: Name,
    Description: Description,
    Locale: Locale,
    RecordingsUrl: RecordingsBlobUri,
    Properties: {
        "PunctuationMode": "DictatedAndAutomatic",
        "ProfanityFilterMode": "Masked",
        "AddWordLevelTimestamps": "True"
    },
    Models: []
}

const postPayload = JSON.stringify(ts);

const startOptions = {
    hostname: Region + ".cris.ai",
    port: Port,
    path: SpeechToTextBasePath + "Transcriptions/",
    method: "POST",
    headers: {
        "Content-Type": "application/json",
        'Content-Length': postPayload.length,
        "Ocp-Apim-Subscription-Key": SubscriptionKey
    }
}

Verzend de transcriptie-aanvraag.Send the transcription request.

Nu gaan we de aanvraag naar de Speech-service verzenden en de eerste responscode controleren.Now we post the request to the Speech service and check the initial response code. Deze responscode geeft simpelweg aan of de service de aanvraag heeft ontvangen.This response code will simply indicate if the service has received the request. De service retourneert in de antwoordheaders een URL met de locatie waar de transcriptiestatus wordt opgeslagen.The service will return a Url in the response headers that's the location where it will store the transcription status.

Vervolgens roept u een CheckTranscriptionStatus-methode op om de status te controleren en uiteindelijk de resultaten af te drukken.Then we'll call a method CheckTranscriptionStatus to check on the status and eventually print the results. Vervolgens implementeren we CheckTranscriptionStatus.We'll implement CheckTranscriptionStatus next.

const request = https.request(startOptions, (response) => {
    if (response.statusCode != 202) {
        console.error("Error, status code " + response.statusCode);
    } else {

        const transcriptionLocation = response.headers.location;

        console.info("Created transcription at location " + transcriptionLocation);
        console.info("Checking status.");

        CheckTranscriptionStatus(transcriptionLocation);
    }
});

request.on("error", error => {
    console.error(error);
});

request.write(postPayload);
request.end();

De status van aanvragen controlerenCheck the requests status

Omdat de transcriptie asynchroon wordt verwerkt, moeten we regelmatig de status van de service controleren.Since the service processes the transcription asynchronously, we need to poll for its status every so often. We controleren deze elke vijf seconden.We'll check every 5 seconds.

We kunnen de status controleren door de inhoud op te halen via de URL die we hebben ontvangen toen de aanvraag werd verzonden.We can check the status by retrieving the content at the Url we got when the posted the request. Wanneer de inhoud is opgehaald, deserialiseren we deze in een van onze helperklassen om gemakkelijker te kunnen communiceren.When we get the content back, we deserialize it into one of our helper class to make it easier to interact with.

Dit is de polling-code met statusweergave voor alles, behalve een geslaagde voltooiing, dat gaan we nu doen.Here's the polling code with status display for everything except a successful completion, we'll do that next.

CheckTranscriptionStatus neemt de status-URL van de transcriptie-aanvraag en pollt deze elke vijf seconden totdat deze is geslaagd of mislukt.CheckTranscriptionStatus takes the status URL from the transcription request and polls it every 5 seconds until it indicates success or and error. Vervolgens roept het PrintResults aan om de resultaten van de transcriptie af te drukken.It then calls PrintResults to print the results of the transcription. Nu gaan we PrintResults implementeren.We'll implement PrintResults next.

function CheckTranscriptionStatus(statusUrl) {
    transcription = null;
    const fetchOptions = {
        headers: {
            "Ocp-Apim-Subscription-Key": SubscriptionKey
        }
    }

    const fetchRequest = https.get(new URL(statusUrl), fetchOptions, (response) => {
        if (response.statusCode !== 200) {
            console.info("Error retrieving status: " + response.statusCode);
        } else {
            let responseText = '';
            response.setEncoding('utf8');
            response.on("data", (chunk) => {
                responseText += chunk;
            });

            response.on("end", () => {
                const statusObject = JSON.parse(responseText);

                var done = false;
                switch (statusObject.status) {
                    case "Failed":
                        console.info("Transcription failed. Status: " + transcription.StatusMessage);
                        done = true;
                        break;
                    case "Succeeded":
                        done = true;
                        PrintResults(statusObject.resultsUrls["channel_0"]);
                        break;
                    case "Running":
                        console.info("Transcription is still running.");
                        break;
                    case "NotStarted":
                        console.info("Transcription has not started.");
                        break;
                }

                if (!done) {
                    setTimeout(() => {
                        CheckTranscriptionStatus(statusUrl);
                    }, (5000));
                }
            });
        }
    });

    fetchRequest.on("error", error => {
        console.error(error);
    });
}

De transcriptieresultaten weergevenDisplay the transcription results

Zodra de service de transcriptie heeft voltooid, worden de resultaten opgeslagen in een andere URL die we via het statusantwoord ontvangen.Once the service has successfully completed the transcription the results will be stored in another Url that we can get from the status response. Vervolgens maken we een aanvraag om de resultaten te downloaden naar een tijdelijk bestand voordat ze worden gelezen en gedeserialiseerd.Here we make a request to download those results in to a temporary file before reading and deserializing them. Zodra de resultaten zijn geladen, kunnen ze worden afgedrukt naar de console.Once the results are loaded we can print them to the console.

function PrintResults(resultUrl)
{
    const fetchOptions = {
        headers: {
            "Ocp-Apim-Subscription-Key": SubscriptionKey
        }
    }

    const fetchRequest = https.get(new URL(resultUrl), fetchOptions, (response) => {
        if (response.statusCode !== 200) {
            console.info("Error retrieving status: " + response.statusCode);
        } else {
            let responseText = '';
            response.setEncoding('utf8');
            response.on("data", (chunk) => {
                responseText += chunk;
            });

            response.on("end", () => {
                console.info("Transcription Results:");
                console.info(responseText);
            });
        }
    });
}

Uw code controlerenCheck your code

Op dit punt moet uw code er als volgt uitzien:At this point, your code should look like this:

const https = require("https");

// Replace with your subscription key
SubscriptionKey = "YourSubscriptionKey";

// Update with your service region
Region = "YourServiceRegion";
Port = 443;

// Recordings and locale
Locale = "en-US";
RecordingsBlobUri = "YourFileUrl";

// Name and description
Name = "Simple transcription";
Description = "Simple transcription description";

SpeechToTextBasePath = "/api/speechtotext/v2.0/";

class ModelIdentity {
    id;
}

class Transcription {
    Name;
    Description;
    Locale;
    RecordingsUrl;
    ResultsUrls;
    Id;
    CreatedDateTime;
    LastActionDateTime;
    Status;
    StatusMessage;
}

class TranscriptionDefinition {
    Name;
    Description;
    RecordingsUrl;
    Locale;
    Models;
    Properties;
}

const ts = {
    Name: Name,
    Description: Description,
    Locale: Locale,
    RecordingsUrl: RecordingsBlobUri,
    Properties: {
        "PunctuationMode": "DictatedAndAutomatic",
        "ProfanityFilterMode": "Masked",
        "AddWordLevelTimestamps": "True"
    },
    Models: []
}

const postPayload = JSON.stringify(ts);

const startOptions = {
    hostname: Region + ".cris.ai",
    port: Port,
    path: SpeechToTextBasePath + "Transcriptions/",
    method: "POST",
    headers: {
        "Content-Type": "application/json",
        'Content-Length': postPayload.length,
        "Ocp-Apim-Subscription-Key": SubscriptionKey
    }
}

function PrintResults(resultUrl)
{
    const fetchOptions = {
        headers: {
            "Ocp-Apim-Subscription-Key": SubscriptionKey
        }
    }

    const fetchRequest = https.get(new URL(resultUrl), fetchOptions, (response) => {
        if (response.statusCode !== 200) {
            console.info("Error retrieving status: " + response.statusCode);
        } else {
            let responseText = '';
            response.setEncoding('utf8');
            response.on("data", (chunk) => {
                responseText += chunk;
            });

            response.on("end", () => {
                console.info("Transcription Results:");
                console.info(responseText);
            });
        }
    });
}

function CheckTranscriptionStatus(statusUrl) {
    transcription = null;
    const fetchOptions = {
        headers: {
            "Ocp-Apim-Subscription-Key": SubscriptionKey
        }
    }

    const fetchRequest = https.get(new URL(statusUrl), fetchOptions, (response) => {
        if (response.statusCode !== 200) {
            console.info("Error retrieving status: " + response.statusCode);
        } else {
            let responseText = '';
            response.setEncoding('utf8');
            response.on("data", (chunk) => {
                responseText += chunk;
            });

            response.on("end", () => {
                const statusObject = JSON.parse(responseText);

                var done = false;
                switch (statusObject.status) {
                    case "Failed":
                        console.info("Transcription failed. Status: " + transcription.StatusMessage);
                        done = true;
                        break;
                    case "Succeeded":
                        done = true;
                        PrintResults(statusObject.resultsUrls["channel_0"]);
                        break;
                    case "Running":
                        console.info("Transcription is still running.");
                        break;
                    case "NotStarted":
                        console.info("Transcription has not started.");
                        break;
                }

                if (!done) {
                    setTimeout(() => {
                        CheckTranscriptionStatus(statusUrl);
                    }, (5000));
                }
            });
        }
    });

    fetchRequest.on("error", error => {
        console.error(error);
    });
}

const request = https.request(startOptions, (response) => {
    if (response.statusCode != 202) {
        console.error("Error, status code " + response.statusCode);
    } else {

        const transcriptionLocation = response.headers.location;

        console.info("Created transcription at location " + transcriptionLocation);
        console.info("Checking status.");

        CheckTranscriptionStatus(transcriptionLocation);
    }
});

request.on("error", error => {
    console.error(error);
});

request.write(postPayload);
request.end();

Uw app uitvoerenRun your app

U bent nu klaar om uw app te bouwen en de spraakherkenning te testen met behulp van de Speech-service.Now you're ready to build your app and test our speech recognition using the Speech service.

Start uw app- Voer knooppunt index.js uit.Start your app - Run node index.js.

Volgende stappenNext steps

Bekijk of down load alle Speech SDK -voor beelden op github.View or download all Speech SDK Samples on GitHub.

Aanvullende taal-en platform ondersteuningAdditional language and platform support

Als u op dit tabblad hebt geklikt, ziet u waarschijnlijk geen Snelstartgids in uw favoriete programmeer taal.If you've clicked this tab, you probably didn't see a quickstart in your favorite programming language. Geen zorgen: er zijn extra Quick Start-materialen en code voorbeelden beschikbaar op GitHub.Don't worry, we have additional quickstart materials and code samples available on GitHub. Gebruik de tabel om het juiste voor beeld te vinden voor uw programmeer taal en combi natie van platform/OS.Use the table to find the right sample for your programming language and platform/OS combination.

TaalLanguage Extra Quick startsAdditional Quickstarts CodevoorbeeldenCode samples
C#C# Van Mic, uit bestandFrom mic, From file .NET Framework, .net core, UWP, Unity, Xamarin.NET Framework, .NET Core, UWP, Unity, Xamarin
C++C++ Van Mic, uit bestandFrom mic, From file Windows, Linux, macOSWindows, Linux, macOS
JavaJava Van Mic, uit bestandFrom mic, From file Android, jreAndroid, JRE
JavascriptJavaScript Browser van Mic, node. js vanuit bestandBrowser from mic, Node.js from file Windows, Linux, macOSWindows, Linux, macOS
Objective-CObjective-C IOS van Mic, macOS van MiciOS from mic, macOS from mic IOS, macOSiOS, macOS
PythonPython Van Mic, uit bestandFrom mic, From file Windows, Linux, macOSWindows, Linux, macOS
SwiftSwift IOS van Mic, macOS van MiciOS from mic, macOS from mic IOS, macOSiOS, macOS