【问题标题】:AVCaptureVideoPreviewLayer does not detect objects in two ranges of the screenAVCaptureVideoPreviewLayer 不检测屏幕两个范围内的对象
【发布时间】:2023-04-11 04:49:01
【问题描述】:

我下载了 Apple 的关于在 Live Capture 中识别对象的项目。 当我尝试应用程序时,我发现如果我将要识别的对象放在相机视图的顶部或底部,应用程序将无法识别该对象:

在第一张图片中,香蕉位于相机视图的中心,应用能够识别它。

image object in center

在这两个图像中,香蕉靠近相机视图的边界,它无法识别对象。

image object on top

image object on bottom

这就是 session 和 previewLayer 的设置方式:

 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)
}

您可以下载项目here, 我想知道这是否正常。

有解决办法吗? 用coreml详细说明是否需要方形照片并且不包括两个范围? 有什么提示吗?谢谢

【问题讨论】:

    标签: swift xcode avcapturesession coreml apple-vision


    【解决方案1】:

    这可能是因为imageCropAndScaleOption 设置为centerCrop

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

    另请参阅 Apple 文档中的 VNImageCropAndScaleOption

    【讨论】:

      猜你喜欢
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多