Распознать объект подачи камеры моего телефона с помощью Unity и SDK Watson - PullRequest
0 голосов
/ 22 октября 2018

Я хотел бы распознать объект подачи камеры моего телефона с помощью Unity и SDK Watson.Знаете ли вы какую-нибудь демонстрацию или учебник для изучения?

1 Ответ

0 голосов
/ 29 октября 2018

Вы можете использовать Unity's WebCamTexture, чтобы получить доступ к камере вашего устройства.Вы можете использовать эту текстуру для подачи в DetectFaces метод в Watson Unity SDK .Пожалуйста, посмотрите эту суть .

void Start()
{
    LogSystem.InstallDefaultReactors();

    //  Init WebCamTexture
    webcamTexture = new WebCamTexture();
    webcamTexture.requestedWidth = 640;
    webcamTexture.requestedHeight = 480;
    webcamTexture.Play();

    //  Set raw image texture
    rawImage.material.mainTexture = webcamTexture;

    //  Create service in coroutine
    Runnable.Run(CreateService());
}

private void TakePhoto()
{
    //  Create and set texture
    texture2d = new Texture2D(webcamTexture.width, webcamTexture.height);
    texture2d.SetPixels(webcamTexture.GetPixels());
    texture2d.Apply();

    //  Set imageData
    imageData = texture2d.EncodeToPNG();

    //  Save image
    File.WriteAllBytes(Application.dataPath + "/myImage.png", texture2d.EncodeToPNG());
}

private IEnumerator CreateService()
{
    yield return new WaitForSeconds(0.5f);
    TakePhoto();

    //  Create tokenOptions
    TokenOptions visualRecognitionTokenOptions = new TokenOptions()
    {
        IamApiKey = visualRecognitionApiKey
    };

    //  Create credentials
    Credentials visualRecognitionCredentials = new Credentials(visualRecognitionTokenOptions, visualRecognitionServiceUrl);

    //  Wait for tokendata
    while (!visualRecognitionCredentials.HasIamTokenData())
        yield return null;

    //  Instantiate service
    visualRecognition = new VisualRecognition(visualRecognitionCredentials);

    //  Set version date
    visualRecognition.VersionDate = versionDate;

    //  Classify
    visualRecognition.DetectFaces(OnDetectFaces, OnFail, imageData);
}

private void OnDetectFaces(DetectedFaces response, Dictionary<string, object> customData)
{
    //  Print response json to console
    Log.Debug("DetectFacesExample", "{0}", customData["json"].ToString());

    //  Print gender, age and confidence
    Log.Debug("DetectFacesExample", "gender: {0}, score: {1}, age: {2} - {3}, score: {4}", response.images[0].faces[0].gender.gender, response.images[0].faces[0].gender.score, response.images[0].faces[0].age.min, response.images[0].faces[0].age.max, response.images[0].faces[0].age.score);
}

//  Fail callback
private void OnFail(RESTConnector.Error error, Dictionary<string, object> customData)
{
    Log.Debug("DetectFacesExample", "Failed to classify");
}
...