c# - 使用 Vision 框架 NSInvalidArgumentException 的 xamarin iOS 人脸检测
问题描述
我目前正在开发一个使用 iOS Vision 框架进行实时摄像头面部检测的应用程序,但我遇到了一个间歇性的 Objective-C 错误,奇怪的是每次都不会发生。
Objective-C exception thrown. Name: NSInvalidArgumentException Reason: *** -[__NSPlaceholderArray initWithObjects:count:]: attempt to insert nil object from objects[0]
这个错误会停止人脸检测过程,这非常令人沮丧。
这就是我到目前为止所拥有的,如果有人能指出导致这个错误的原因以及我能做些什么来纠正它,那将是一个很大的帮助。谢谢 !
public class DataOutputDelegate : AVCaptureVideoDataOutputSampleBufferDelegate
{
public UIImage CapturedImage { get; set; }
VNDetectFaceRectanglesRequest faceDetection = new VNDetectFaceRectanglesRequest(null);
VNSequenceRequestHandler faceDetectionRequest = new VNSequenceRequestHandler();
[Export("captureOutput:didOutputSampleBuffer:fromConnection:")]
public override void DidOutputSampleBuffer(AVCaptureOutput captureOutput, CMSampleBuffer sampleBuffer, AVCaptureConnection connection)
{
if (connection.SupportsVideoOrientation)
connection.VideoOrientation = AVCaptureVideoOrientation.Portrait;
try
{
using (var pixelBuffer = sampleBuffer.GetImageBuffer())
using (var attachments = sampleBuffer.GetAttachments<NSString, NSObject>(CMAttachmentMode.ShouldPropagate))
using (var ciimage = new CIImage(pixelBuffer, attachments))
using (var ciImageWithOrientation = ciimage.CreateWithOrientation(CIImageOrientation.RightTop))
{
if(faceDetection != null)
{
faceDetectionRequest.Perform(new VNRequest[] { faceDetection }, ciImageWithOrientation, out var performError);
if(performError != null)
{
throw new Exception(performError.LocalizedDescription);
}
CheckForFace();
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
sampleBuffer.Dispose();
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
void CheckForFace()
{
var observations = faceDetection.GetResults<VNFaceObservation>() ?? Array.Empty<VNFaceObservation>();
if (observations.Length > 0)
{
Device.BeginInvokeOnMainThread(() =>
{
MessagingCenter.Send<object>(new object(), Constants.CameraPreview.MessageKeys.FaceDetected);
Console.WriteLine("\nFace detected . . .");
});
}
else
{
Device.BeginInvokeOnMainThread(() =>
{
MessagingCenter.Send<object>(new object(), Constants.CameraPreview.MessageKeys.FaceLost);
Console.WriteLine("\nFace is lost . . .");
});
}
return;
}
}
解决方案
推荐阅读
- c++ - 即使安装了 4 个软件包,我也无法在 Raspbian Buster 上使用 curl/curl.h
- rust - 有没有办法检查字段的可变性?
- pyshark - 带有错误 display_filter 参数的 pyshark 实时捕获
- python - 需要在数据帧中逐行划分两列并添加显示结果的新列的函数(使用'def')
- android - Multiple Reference , Multiple View Holder , Recycler 视图适配器
- node.js - 填充不填充,仅显示 ID
- r - R异常值指数错误
- python - 把一句话中的一个词倒过来
- javascript - 如何在反应组件中以不同的形式显示数组的每个部分
- mongodb - 如何将 GeoQuery 与 MongoDB 中的另一个单独查询结合起来并对结果进行分页?