使用 Vision Framework 进行实时矩形检测

问题描述 投票:0回答:3

我正在尝试从实时预览图层中检测矩形,但无法检测到所有矩形。

我在做什么

设置视觉请求

 func setupVision() {
    let rectanglesDetectionRequest = VNDetectRectanglesRequest(completionHandler: self.handleRectangles)
    rectanglesDetectionRequest.maximumObservations = 0
    rectanglesDetectionRequest.quadratureTolerance = 45.0
    rectanglesDetectionRequest.minimumAspectRatio = 0.64
    self.requests = [rectanglesDetectionRequest]
}

    
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {
        return
    }
    
    let exifOrientation = self.exifOrientationFromDeviceOrientation()
    
    
    DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
        var requestOptions: [VNImageOption : Any] = [:]
        
        if let cameraIntrinsicData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil) {
            requestOptions = [.cameraIntrinsics: cameraIntrinsicData]
        }
        
        DispatchQueue.global(qos: .background).async {
            let imageRequestHandler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, orientation:exifOrientation, options: requestOptions)
            do {
                try imageRequestHandler.perform(self.requests)
            } catch {
                print(error)
            }
        }
        

    }
    

    var arr = Array<VNTrackRectangleRequest>()
    for obs in self.rectanglesss{
            let trackRequest = VNTrackRectangleRequest(rectangleObservation: obs, completionHandler: self.handleSequenceRequestUpdate)
            trackRequest.trackingLevel = .accurate
            arr.append(trackRequest)
    }
    
    
    do {
        try self.sequenceHandler.perform(arr, on: pixelBuffer, orientation: exifOrientation)
    } catch {
        print(error)
    }
}

有人可以帮助我找出我做错了什么吗? 当我尝试使用直角时,有时它会检测到很少的矩形,而使用锐角时,它只能检测到 2-3 个矩形附近。在这里,我尝试使用 SET 卡,我添加了 2 张我得到的图像。

结果

enter image description here enter image description here

ios swift camera rectangles apple-vision
3个回答
0
投票

尝试用 iPhone 通过鸟瞰图查看它们?并获得一张非白色的不同桌子


0
投票

建议您将手机平放拍摄,然后设置最小置信度


0
投票

用这个...

https://developer.apple.com/documentation/vision/vndetectrectanglesrequest/2875373-maximumobservations

let request = VNDetectRectanglesRequest { (request, error) in
    // Your completion handler code
}

request.maximumObservations = 2
© www.soinside.com 2019 - 2024. All rights reserved.