unity-game-engineibm-watsonvisual-recognition

FacesTopLevelMultiple could not be found


Having an issue with a face detection program, using the Visual Recognition API that IBM Watson provides. I wrote a program based on a tutorial I found, but I'm getting an error that's preventing it from running. The error is as follows: https://i.sstatic.net/CBN0p.png

Here is the code that I'm running for face detection:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using IBM.Watson.DeveloperCloud.Services.VisualRecognition.v3;
using IBM.Watson.DeveloperCloud.Logging;
using IBM.Watson.DeveloperCloud.Utilities;
using IBM.Watson.DeveloperCloud.Connection;


public class FaceDetector : MonoBehaviour
{
    public Text dataOutput;
    private VisualRecognition _visualRecognition;
    private string path = "C:\\Users\\Alberto\\Desktop\\Alberto.jpg";

    public string _serviceUrl;
    public string _iamApikey;

    void Start() {
        LogSystem.InstallDefaultReactors();
        Runnable.Run(CreateService());
    }

    private IEnumerator CreateService() {
        if (string.IsNullOrEmpty(_iamApikey)) {
            throw new WatsonException("Please provide IAM ApiKey for the service.");
        }

        Credentials credentials = null;

        TokenOptions tokenOptions = new TokenOptions() {
            IamApiKey = _iamApikey
        };

        credentials = new Credentials(tokenOptions, _serviceUrl);

        //wait for token
        while (!credentials.HasIamTokenData())
            yield return null;

        //create credentials
        _visualRecognition = new VisualRecognition(credentials);
        _visualRecognition.VersionDate = "2019-02-26";
    }

    public void DetectFaces(string path) {
        //classify using image url
        // if (!_visualRecognition.DetectFaces(picURL, OnDetectFaces, OnFail))
        //     Log.Debug("ExampleVisualRecognition.DetectFaces()", "Detect faces failed!");

        //classify using image path
        if(!_visualRecognition.DetectFaces(OnDetectFaces, OnFail, path)) {
            Debug.Log("ExampleVisualRecognition.DetectFaces()", "Detect faces failed!");
        } else {
            Debug.Log("Calling Watson");
            dataOutput.text = "";
        }
    }

    private void OnDetectFaces(FacesTopLevelMultiple multipleImages, Dictionary<string, object> customData) {
        var data = multipleImages.images[0].faces[0];
        dataOutput.text = "Age : " + data.age.min + "-" + data.age.max + " PROBABILITY: " + data.age.score + "\n";
        "Gender: " + data.gender.gender + " PROBABILITY: " + data.gender.score + "\n";
        Debug.Log("ExampleVisualRecognition.OnDetectFaces(): Detect faces result: " + customData["json"].ToString());
    }

    private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData) {
        Debug.LogError("ExampleVisualRecognition.OnFail(): Error received: " + error.ToString());
    }
}

This is the script that enables the webcam to track my face:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using IBM.Watson.DeveloperCloud.Services.VisualRecognition.v3;
using IBM.Watson.DeveloperCloud.Logging;
using IBM.Watson.DeveloperCloud.Utilities;

public class CameraRender : MonoBehaviour
{
    public Image overlay;
    public FaceDetector fd;
    // Start is called before the first frame update
    void Start()
    {
        WebCamTexture backCam = new WebCamTexture();
        backCam.Play();
        overlay.material.mainTexture = backCam;
    }

    public void CaptureImage() {
        ScreenCapture.CaptureScreenshot("screenshot.png");
        fd.DetectFaces(application.persistentDataPath + "/screenshot.png");

    }
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0)) {
            CaptureImage();
        }
    }

}

I hope this helps figure out what the issue is. FacesTopLevelMultiple is used near the bottom of the FaceDetector script.


Solution

  • The detectFaces callback seems to be outdated (probably from a tutorial that needs to be updated). The method signature should be

    OnDetectFaces(DetectedFaces multipleImages, Dictionary<string, object> customData)
    

    You should be able to find the faces using

    private void OnDetectFaces(DetectedFaces multipleImages, Dictionary<string, object> customData)
    {
        Log.Debug("ExampleFaceDetection", "First face age: {0}", multipleImages.images[0].faces[0].age);
        Log.Debug("ExampleFaceDetection", "First face gender: {0}", multipleImages.images[0].faces[0].gender);
    }