Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift AVCaptureVideoPreviewLayer不检测屏幕两个范围内的对象_Swift_Xcode_Avcapturesession_Coreml_Apple Vision - Fatal编程技术网

Swift AVCaptureVideoPreviewLayer不检测屏幕两个范围内的对象

Swift AVCaptureVideoPreviewLayer不检测屏幕两个范围内的对象,swift,xcode,avcapturesession,coreml,apple-vision,Swift,Xcode,Avcapturesession,Coreml,Apple Vision,我下载了苹果公司关于在实时捕获中识别物体的项目。 当我尝试该应用程序时,我发现如果我将要识别的对象放在相机视图的顶部或底部,则该应用程序无法识别该对象: 在第一张图片中,香蕉位于摄像头视图的中心,应用程序能够识别它 在这两幅图像中,香蕉靠近摄影机视图的边界,无法识别对象 以下是会话和预览层的设置方式: func setupAVCapture() { var deviceInput: AVCaptureDeviceInput! // Select a video devi

我下载了苹果公司关于在实时捕获中识别物体的项目。 当我尝试该应用程序时,我发现如果我将要识别的对象放在相机视图的顶部或底部,则该应用程序无法识别该对象:

在第一张图片中,香蕉位于摄像头视图的中心,应用程序能够识别它

在这两幅图像中,香蕉靠近摄影机视图的边界,无法识别对象

以下是会话和预览层的设置方式:

 func setupAVCapture() {
    var deviceInput: AVCaptureDeviceInput!

    // Select a video device, make an input
    let videoDevice = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: .video, position: .back).devices.first
    do {
        deviceInput = try AVCaptureDeviceInput(device: videoDevice!)
    } catch {
        print("Could not create video device input: \(error)")
        return
    }

    session.beginConfiguration()
    session.sessionPreset = .vga640x480 // Model image size is smaller.

    // Add a video input
    guard session.canAddInput(deviceInput) else {
        print("Could not add video device input to the session")
        session.commitConfiguration()
        return
    }
    session.addInput(deviceInput)
    if session.canAddOutput(videoDataOutput) {
        session.addOutput(videoDataOutput)
        // Add a video data output
        videoDataOutput.alwaysDiscardsLateVideoFrames = true
        videoDataOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_420YpCbCr8BiPlanarFullRange)]
        videoDataOutput.setSampleBufferDelegate(self, queue: videoDataOutputQueue)
    } else {
        print("Could not add video data output to the session")
        session.commitConfiguration()
        return
    }
    let captureConnection = videoDataOutput.connection(with: .video)
    // Always process the frames
    captureConnection?.isEnabled = true
    do {
        try  videoDevice!.lockForConfiguration()
        let dimensions = CMVideoFormatDescriptionGetDimensions((videoDevice?.activeFormat.formatDescription)!)
        bufferSize.width = CGFloat(dimensions.width)
        bufferSize.height = CGFloat(dimensions.height)
        videoDevice!.unlockForConfiguration()
    } catch {
        print(error)
    }
    session.commitConfiguration()
    previewLayer = AVCaptureVideoPreviewLayer(session: session)
    previewLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill
    rootLayer = previewView.layer
    previewLayer.frame = rootLayer.bounds
    rootLayer.addSublayer(previewLayer)
}
你可以下载这个项目, 我想知道这是否正常

有什么解决办法吗? 是否使用coreml拍摄方形照片,但不包括这两个范围?
有什么提示吗?谢谢

这可能是因为
imageCropAndScaleOption
设置为
centerCrop

Core ML模型需要方形图像,但视频帧不是方形的。这可以通过在
VNCoreMLRequest
上设置
imageCropAndScaleOption
选项来解决。但是,结果可能不如中心裁剪的结果好(这取决于模型最初的训练方式)


另请参见Apple文档中的
VNIGECROPANDSCALEOPTION

这可能是因为
imageCropAndScaleOption
设置为
centerCrop

Core ML模型需要方形图像,但视频帧不是方形的。这可以通过在
VNCoreMLRequest
上设置
imageCropAndScaleOption
选项来解决。但是,结果可能不如中心裁剪的结果好(这取决于模型最初的训练方式)

另请参见Apple文档中的
vImageCropandScaleOption