使用IBM Watson提供的Visual Recognition API,面部检测程序出现问题。我根据找到的教程编写了一个程序,但是遇到一个错误,该错误阻止了它的运行。错误如下: https://i.stack.imgur.com/CBN0p.png
这是我正在运行的用于面部检测的代码:
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());
}
}
这是使网络摄像头能够跟踪我的脸的脚本:
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();
}
}
}
我希望这有助于找出问题所在。 FacesTopLevelMultiple用于FaceDetector脚本底部附近。
答案 0 :(得分:0)
detectFaces回调似乎已过时(可能来自需要更新的教程)。方法签名应该是
OnDetectFaces(DetectedFaces multipleImages, Dictionary<string, object> customData)
您应该能够使用
找到面孔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);
}