vosk-api icon indicating copy to clipboard operation
vosk-api copied to clipboard

How to catch the json format result in C#?

Open zydjohnHotmail opened this issue 2 years ago • 3 comments

Hello: I want to use C# to save all the voice recognized text in a file, so I can edit it later. I found to use Console.WriteLine to show the result, for all NON-English text, the words look like garbage, but using Debug.WriteLine the shown text is correct. So I write some code trying to show NON-English text in debug output window. The following is my code:

    public static void DemoBytes(Model model)
    {
        // Demo byte buffer
        VoskRecognizer rec = new(model, 16000.0f);
        rec.SetMaxAlternatives(0);
        rec.SetWords(true);
        using (Stream source = File.OpenRead(UA_Wav_File1))
        {
            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());
                    string json_data = rec.Result().ToString();
                    if (json_data.Contains("["))
                    {
                        Debug.WriteLine(json_data);
                        Console.Beep();
                    }
                }
                else
                {
                    Console.WriteLine(rec.PartialResult());
                }
            }
        }
        Console.WriteLine(rec.FinalResult());
    }

But my code didn’t catch any Json format result. However, from console window, I can see quite a lot of text output, like the following: { "partial" : "????? ???? ?????? ????? ?? ?? ??? ??? ?????? ?????? ?? ??????? ????? ????? ?? ????? ?? ?????????? ???? ???????????" } { "result" : [{ "conf" : 0.953055, "end" : 287.400000, "start" : 287.071523, "word" : "?????" }, { "conf" : 0.936494, "end" : 287.820000, "start" : 287.400000, "word" : "????" }, { "conf" : 0.399469, "end" : 288.329795, "start" : 287.878682, "word" : "??????" }, { "conf" : 0.545751, "end" : 288.780000, "start" : 288.329795, "word" : "?????" }, { "conf" : 0.982125, "end" : 289.203750, "start" : 288.810000, "word" : "??" }, { "conf" : 0.633553, "end" : 289.410117, "start" : 289.230000, "word" : "??" }, { "conf" : 0.240568, "end" : 289.768037, "start" : 289.533633, "word" : "???" }, { "conf" : 0.177813, "end" : 290.100000, "start" : 289.770000, "word" : "???" }, { "conf" : 0.939531, "end" : 290.730293, "start" : 290.490000, "word" : "???" }, { "conf" : 0.681968, "end" : 291.180000, "start" : 290.939971, "word" : "???" }, { "conf" : 0.992080, "end" : 291.600000, "start" : 291.180117, "word" : "??????" }, { "conf" : 1.000000, "end" : 292.200000, "start" : 291.900000, "word" : "??" }, { "conf" : 1.000000, "end" : 292.710000, "start" : 292.200000, "word" : "???????" }, { "conf" : 0.868925, "end" : 293.187158, "start" : 292.770293, "word" : "?????" }, { "conf" : 0.280664, "end" : 293.519912, "start" : 293.190000, "word" : "?????" }, { "conf" : 0.253797, "end" : 293.845635, "start" : 293.558174, "word" : "??" }, { "conf" : 0.510191, "end" : 294.779912, "start" : 294.565898, "word" : "?" }, { "conf" : 0.502767, "end" : 294.870000, "start" : 294.779912, "word" : "??" }, { "conf" : 0.430731, "end" : 295.518984, "start" : 294.870000, "word" : "??????????" }, { "conf" : 0.758802, "end" : 295.770000, "start" : 295.519980, "word" : "????" }, { "conf" : 0.526706, "end" : 296.730000, "start" : 296.248740, "word" : "???????????" }], "text" : "????? ???? ?????? ????? ?? ?? ??? ??? ??? ??? ?????? ?? ??????? ????? ????? ?? ? ?? ?????????? ???? ???????????" } { "partial" : "" }

Let me know if I made any mistake, I simply want to catch those Json format rec.Result() in C#. Please advise, Thanks,

zydjohnHotmail avatar Jun 10 '22 09:06 zydjohnHotmail

What do you mean by "catch". Please explain. Also please format your post properly. Also please read about console codepage.

nshmyrev avatar Jun 12 '22 15:06 nshmyrev

Catch in my C# code, it means, my code will run and show the string in Debug window. Unfortunately, my code did not show any Result() in debug window, even they showed in Console window. If I made any mistake in my code, then please advise on how I can change my code, so it will show the Result() in debug window. The issue is: for Console window, all non-English language looks garbage. But for Debug window, they look normal. By the way, my code can show rec.PartialResult() in Debug window, but not rec.Result(). Please advise,

zydjohnHotmail avatar Jun 12 '22 15:06 zydjohnHotmail

Change Console.WriteLine to Debug.WriteLine everywhere

nshmyrev avatar Jun 12 '22 22:06 nshmyrev