unity3d - 找不到 FacesTopLevelMultiple
问题描述
使用 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 脚本底部附近使用。
解决方案
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);
}
推荐阅读
- c++ - 用于读取复数的 c++ 正则表达式
- android - Websocket 在“101 Switching Protocols”响应中关闭与 EOF 异常的连接
- c - 函数参数接收一个 int "b" 数字但打印一个随机数(函数调用函数)
- ruby-on-rails - Rails 5.2 Auth - create_new_auth_token 后返回的令牌
- google-cloud-platform - 是否有用于 google 语音到文本的语言支持的 API?
- java - 如何使用毕加索从 .mp3 文件中检索专辑封面
- android - React-Native - Firebase 云消息传递 - 如果从托盘打开则没有事件
- c# - 将 2 个 datagridview 分组列减去第三列
- python - 为什么在编写二叉搜索树时会出错?
- sql-server - 如何使用 T-SQL 解码 base64 unicode 字符串