首页 > 解决方案 > 使用 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#objective-cxamarin.iosface-detection

解决方案


推荐阅读