Z
Z
Zupand2022-03-09 15:12:47
C++ / C#
Zupand, 2022-03-09 15:12:47

Error with Vosk c# library, what should I do?

I am trying to create a speech recognition program using the Vosk-api library. I have a problem with a model. When I specify the path to the speech model, it gives an error. I tried to all folders nothing helps

error: ERROR (VoskAPI:Model():model.cc:122) Folder 'model' does not contain model files. Make sure you specified the model path properly in Model constructor. If you are not sure about relative path, use absolute path specification.
unhandled exception. System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
at Vosk.VoskPINVOKE.new_Model(String jarg1)
at Vosk.Model..ctor(String model_path)
at VoskDemo.Main() in F:\programsf\ConsoleApp1\ConsoleApp1\Program.cs:line 91

github - https://github.com/alphacep/vosk-api

model link https://alphacephei.com/vosk/models

code

using System;
using System.IO;
using Vosk;

public class VoskDemo
{
public static void DemoBytes(Model model)
{
// Demo byte buffer
VoskRecognizer rec = new VoskRecognizer(model, 16000.0f);
rec.SetMaxAlternatives(0);
rec.SetWords(true);
using (Stream source = File.OpenRead(@"F:\programsf\ConsoleApp1\ConsoleApp1\bin\x86\model\decoder-test.wav"))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
{
if (rec.AcceptWaveform(buffer, bytesRead))
{
Console.WriteLine(rec.Result());
}
else
{
Console.WriteLine(rec.PartialResult());
}
}
}
Console.WriteLine(rec.FinalResult());
}

public static void DemoFloats(Model model)
{
// Demo float array
VoskRecognizer rec = new VoskRecognizer(model, 16000.0f);
using (Stream source = File.OpenRead(@"F:\programsf\ConsoleApp1\ConsoleApp1\bin\x86\model\decoder-test.wav"))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
{
float[] fbuffer = new float[bytesRead / 2];
for (int i = 0, n = 0; i < fbuffer.Length; i++, n += 2)
{
fbuffer[i] = (short)(buffer[n] | buffer[n + 1] << 8);
}
if (rec.AcceptWaveform(fbuffer, fbuffer.Length))
{
Console.WriteLine(rec.Result());
}
else
{
Console.WriteLine(rec.PartialResult());
}
}
}
Console.WriteLine(rec.FinalResult());
}

public static void DemoSpeaker(Model model)
{
// Output speakers
SpkModel spkModel = new SpkModel(@"F:\programsf\ConsoleApp1\ConsoleApp1\bin\x86\model");
VoskRecognizer rec = new VoskRecognizer(model, 16000.0f);
rec.SetSpkModel(spkModel);

using (Stream source = File.OpenRead(@"F:\programsf\ConsoleApp1\ConsoleApp1\bin\x86\model\decoder-test.wav"))
{
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
{
if (rec.AcceptWaveform(buffer, bytesRead))
{
Console.WriteLine(rec.Result());
}
else
{
Console.WriteLine(rec.PartialResult());
}
}
}
Console.WriteLine(rec.FinalResult());
}

public static void Main()
{
// You can set to -1 to disable logging messages
Vosk.Vosk.SetLogLevel(0);

Model model = new Model(System.IO.Path.GetFullPath(@"F:\programsf\ConsoleApp1\ConsoleApp1\bin\x86\model"));
DemoBytes(model);
DemoFloats(model);
DemoSpeaker(model);
}
}



error in this line line Model model = new Model(System.IO.Path.GetFullPath(@"F:\programsf\ConsoleApp1\ConsoleApp1\bin\x86\model"));

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question