wit-ai / wit-unity

Wit-Unity is a Unity C# wrapper around the the Wit.ai rest APIs and is a core component of Voice SDK.
Other
80 stars 20 forks source link

I need help on wit.ai setup up. #21

Closed nzmajid closed 3 years ago

nzmajid commented 3 years ago

Hi,

I'm doing a project based on a course in Skillshare.com. In this course, I will send a sound file (speech) to wit.ai and will get reply by wit.ait. The entities values can be seen in the console of unity.

witprob

However I fail to get the entities from wit.ai.

If I'm using the original script from the lecturer of the course (using original bearer and string url value from the lecturer), I will get the entities and everything is fine. Please refer the photo below.

wiitt

Someone told me to use my app ID instead. I don't understand where should I use the app ID because the script that I got from the lecturer only have URL and bearer line to be edited. I hope some one can help me on this problem.

Here are the scripts for this project:

The first Script

` public partial class Wit3D : MonoBehaviour {

// Class Variables

// Audio variables
public AudioClip commandClip;
int samplerate;

// API access parameters
string url = "https://api.wit.ai/speech?v=20180206"; //This line is originally taken from the lecturer
string token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // To put the bearer based on the http API from wit.ai setting

//Custom 1
// GameObject to use as a default spawn point
private bool isRecording = false;
private bool pressedButton = false;
public Text myResultBox;
public VideoPlayer vidScreen;
public GameObject vidCanvas;

// Use this for initialization
void Start () {

    // If you are a Windows user and receiving a Tlserror
    // See: https://github.com/afauch/wit3d/issues/2
    // Uncomment the line below to bypass SSL
    // System.Net.ServicePointManager.ServerCertificateValidationCallback = (a, b, c, d) => { return true; };

    // set samplerate to 16000 for wit.ai
    samplerate = 16000;
    vidScreen.GetComponent<VideoPlayer> ();
}

//Custom 2
public void startStopRecord(){
    if (isRecording == true) {
        pressedButton = true;
        isRecording = false;
    } else if (isRecording == false) {
        isRecording = true;
        pressedButton = true;
    }

}
//Custom 3
public void playVideo(){
    vidScreen.Play ();
    vidCanvas.SetActive (false);
}
//Custom 4
public void stopVideo(){
    vidScreen.Stop ();
    vidCanvas.SetActive (true);
}

// Update is called once per frame
void Update () {
    if (pressedButton == true) {
        pressedButton = false;
        if (isRecording) {
            myResultBox.text = "Listening for command";
            commandClip = Microphone.Start (null, false, 5, samplerate);  //Start recording (rewriting older recordings)
        }

        //Custom 5
        if (!isRecording) {
            myResultBox.text = null;
            myResultBox.text = "Saving Voice Request";
            // Save the audio file
            Microphone.End (null);
            if (SavWav.Save ("sample", commandClip)) {
                myResultBox.text = "Sending audio to AI...";
            } else {
                myResultBox.text = "FAILED";
            }

            // At this point, we can delete the existing audio clip
            commandClip = null;

            //Start a coroutine called "WaitForRequest" with that WWW variable passed in as an argument
            StartCoroutine(SendRequestToWitAi());

        }
    }

}

public IEnumerator SendRequestToWitAi(){
    //Custom 6
    string file = Application.persistentDataPath + "/sample.wav";
    string API_KEY = token;

    FileStream filestream = new FileStream (file, FileMode.Open, FileAccess.Read);
    BinaryReader filereader = new BinaryReader (filestream);
    byte[] postData = filereader.ReadBytes ((Int32)filestream.Length);
    filestream.Close ();
    filereader.Close ();

    //Custom 7
    Dictionary<string, string> headers = new Dictionary<string, string>();
    headers["Content-Type"] = "audio/wav";
    headers["Authorization"] = "Bearer " + API_KEY;

    float timeSent = Time.time;
    WWW www = new WWW(url, postData, headers);
    yield return www;

    while (!www.isDone) {
        myResultBox.text = "Thinking and deciding ...";
        yield return null;
    }
    float duration = Time.time - timeSent;

    if (www.error != null && www.error.Length > 0) {
        UnityEngine.Debug.Log("Error: " + www.error + " (" + duration + " secs)");
        yield break;
    }
    UnityEngine.Debug.Log("Success (" + duration + " secs)");
    UnityEngine.Debug.Log("Result: " + www.text);
    Handle (www.text);

}

} `

The second script:

` //Custom 8 public partial class Wit3D : MonoBehaviour {

public Text myHandleTextBox;
private bool actionFound = false;

void Handle (string jsonString) {

    if (jsonString != null) {

        RootObject theAction = new RootObject ();
        Newtonsoft.Json.JsonConvert.PopulateObject (jsonString, theAction);

        if (theAction.entities.open != null) {
            foreach (Open aPart in theAction.entities.open) {
                Debug.Log (aPart.value);
                myHandleTextBox.text = aPart.value;
                actionFound = true;
            }
        }
        if (theAction.entities.close != null) {
            foreach (Close aPart in theAction.entities.close) {
                Debug.Log (aPart.value);
                myHandleTextBox.text = aPart.value;
                actionFound = true;
            }
        }

        if (!actionFound) {
            myHandleTextBox.text = "Request unknown, please ask a different way.";
        } else {
            actionFound = false;
        }

    }//END OF IF

}//END OF HANDLE VOID

}//END OF CLASS

//Custom 9 public class Open { public bool suggested { get; set; } public double confidence { get; set; } public string value { get; set; } public string type { get; set; } }

public class Close { public bool suggested { get; set; } public double confidence { get; set; } public string value { get; set; } public string type { get; set; } }

public class Stop { public bool suggested { get; set; } public double confidence { get; set; } public string value { get; set; } public string type { get; set; } }

public class Entities { public List open { get; set; } public List close { get; set; } public List stop { get; set; } }

public class RootObject { public string _text { get; set; } public Entities entities { get; set; } public string msg_id { get; set; } } `

This are my setup in wit.ai:

utterences

intent

entities

trait

yolanother commented 3 years ago

Do you have a link to the skillshare course? The scripts you are using here are really old and unsupported. I would recommend upgrading to the sdk provided by this repo.

yolanother commented 3 years ago

https://github.com/wit-ai/wit-unity/blob/main/Tutorials/ShapesTutorial.md

nzmajid commented 3 years ago

Hi,

Thanks for your willingness to help. This is the link of the course https://www.skillshare.com/classes/Unity-Ttorial-Part-Discover-Voice-Controlled-AR-With-A-Clod-AI-%E2%9C%85/242803432?via=search-layout-grid. . I believe the video was made in 2018. Thanks for sharing the repo link. Actually I'm an IoT AR project from this course https://www.udemy.com/course/internet-of-things-using-augmented-reality-and-unity-iotar/. I want to use wit.ai to command the AR image to appear by voice command. That's why I need to understand the course from Skillshare. I got the IoT project done successfully even though it was really old. The unity version that the lecturer used was 2015, but I am still able to achieve the same result in unity 2017.4.4 version.

I wonder why when I used the same script and using the lecturer settings (the date and his token) I got the result as it was intended to be (Skillshare course). But when I use my own settings, the date and token ((line 48,49 of the script)), I get the reply from wit.ai, but I don't get the value from the data that was sent by wit.ai. And I found in the video course, the lecturer's wit.ai setting was so simple and quite different from today's GUI. Is it because I put the wrong values for intent, value, entities in the settings, so the data structure that I received cannot be parsed correctly? Or is it my wit.ai BOT does not get enough training and samples?

yolanother commented 3 years ago

The new sdk should be quite a bit easier to use. It has a tool built into it that you can use to test your utterances locally. You can find it in Window->Wit->Utterance Viewer

That tool also provides a way to get the data without having to guess at the json structure. After testing an utterance you can right click on a value you want to receive and read and there is an option to add it to a game object.