我想使用Unity和SDK Watson在计算机的摄像头中进行面部识别。我在互联网上寻找了一些教程和演示,然后我终于明白了。这是我在Unity中的第一个项目,因此,如果有人可以帮助我纠正两个我无法修复的错误,我将非常感激。
我将以下代码用于相机渲染和捕获图像:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CameraRender : MonoBehaviour {
public Image overlay;
public FaceDetector fd;
// Use this for initialization
void Start () {
WebCamTexture backCam = new WebCamTexture();
backCam.Play();
overlay.material.mainTexture = backCam;
}
public void CaptureImage()
{
ScreenCapture.CaptureScreenshot(Application.persistentDataPath + "/screenshot.png");
fd.DetectFaces(Application.persistentDataPath + "/screenshot.png");
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
CaptureImage();
}
}
}
另外一个用于面部检测:
using IBM.Watson.DeveloperCloud.Connection;
using IBM.Watson.DeveloperCloud.Logging;
using IBM.Watson.DeveloperCloud.Services.VisualRecognition.v3;
using IBM.Watson.DeveloperCloud.Utilities;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FaceDetector : MonoBehaviour {
public Text dataOutput;
private VisualRecognition _visualRecognition;
// Use this for initialization
void Start ()
{
Credentials credentials = new Credentials(apiKey: "key", url: "url");
_visualRecognition = new ExampleVisualRecognition(credentials)
{
VersionDate = "2016-05-20"
};
}
public void DetectFaces(string path)
{
// Classify using image url
//if (!_visualRecognition.DetectFaces("<image-url>", OnDetectFaces, OnFail))
// Log.Debug("ExampleVisualRecognition.DetectFaces()", "Detect faces failed!");
// Classify using image path
if (!_visualRecognition.DetectFaces(OnDetectFaces, OnFail, path)) {
Log.Debug("ExampleVisualRecognition.DetectFaces()", "Detect faces failed!");
} else
{
Debug.Log("Calling Watson");
dataOutput.text = "";
}
}
private void OnDetectFaces(DetectedFaces multipleImages, Dictionary<string, object> customData)
{
var data = multipleImages.images[0].faces[0]; //assume 1
dataOutput.text = "Age : " + data.age.min + "-" + data.age.max + " PROBABILITY: " + data.age.score + "\n" + "Gender" + data.gender.gender + " PROBABILITY: " + data.age.score + "\n";
Log.Debug("ExampleVisualRecognition.OnDetectFaces(): Detect faces result: {0}", customData["json"].ToString());
}
private void OnFail(RESTConnector.Error error, Dictionary<string,object> customData)
{
Debug.LogError("ExampleVisualRecognition.OnFail(): Error received: " + error.ToString());
}
// Update is called once per frame
void Update () {
}
}
但是我无法解决这两个错误:
NotImplementedException: The requested feature is not implemented. IBM.Watson.DeveloperCloud.Services.VisualRecognition.v3.VisualRecognition.op_Implicit (.ExampleVisualRecognition v) (at Assets/Watson/Scripts/Services/VisualRecognition/v3/VisualRecognition.cs:1444)
FaceDetector.Start () (at Assets/FaceDetector.cs:21)
NullReferenceException: Object reference not set to an instance of an object
FaceDetector.DetectFaces (System.String path) (at Assets/FaceDetector.cs:35)
CameraRender.CaptureImage () (at Assets/CameraRender.cs:20)
CameraRender.Update () (at Assets/CameraRender.cs:27)
有人可以帮忙吗?谢谢大家。
答案 0 :(得分:0)
您应在实例化ExampleVisualRecognition
时实例化VisualRecognition
。请参阅this gist。
private IEnumerator CreateService()
{
// 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, imagePath);
}
private void OnDetectFaces(DetectedFaces response, Dictionary<string, object> customData)
{
// Print response json to console
Log.Debug("ClassifyExample", "{0}", customData["json"].ToString());
// Print gender, age and confidence
Log.Debug("ClassifyExample", "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("ClassifyExample", "Failed to classify");
}
}