完成矩形请求,iOS,Swift

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

我试图检测具有视觉和快速的矩形。

我运行代码,但发现下一个print语句显示在矩形请求函数完成之前,这导致问题,我正在尝试处理一个尚未存在的矩形。

我可以编写一个完成程序,允许直接检测在它向前移动之前完成吗?

func getRectArray(completion: @escaping (_ finished: Bool) -> ()) {
let imageRequestHandler = VNImageRequestHandler(cgImage: tempFICG!, orientation: CGImagePropertyOrientation(rawValue: 0)!, options: requestOptions)

    do {
        print("Tony got to check rect part")
        try imageRequestHandler.perform([self.rectanglesRequest])
        print("Tony got part after rect request")
    } catch {
        print(error)
    }

// These print statements show before the print statements at the end of the rect detect.
    print("Tony Finished checking crops and text")
    print("Tony recCrop amount is is \(recCrops.count)")

// I want to add a completion here that completes the function this is in but it completes too early
completion(true)
 }
}

调用矩形的调用。

lazy var rectanglesRequest: VNDetectRectanglesRequest = { [unowned self] in
    print("Tony 2 Requested....")
    return VNDetectRectanglesRequest(completionHandler: self.handleRectangles)

}()

函数.. func handleRectangles(请求:VNRequest,错误:错误?){

    guard let observations = request.results as? [VNRectangleObservation]
        else { return }

    guard let detectedRectangle = observations.first else {

        print("Tony Text Detecting 2")
        return

    }
    noRect = false
    print("Tony: Handle rectangles the second time")
    let imageSize = changedImage.extent.size

    let boundingBox = detectedRectangle.boundingBox.scaled(to: imageSize)
    guard changedImage.extent.contains(boundingBox)
        else {
            noRect = true

            print("invalid detected rectangle"); return }

    print("Tony Rectangle confidence is: \(detectedRectangle.confidence)")
    conf = "\(detectedRectangle.confidence)"
    let topLeft = detectedRectangle.topLeft.scaled(to: imageSize)
    let topRight = detectedRectangle.topRight.scaled(to: imageSize)
    let bottomLeft = detectedRectangle.bottomLeft.scaled(to: imageSize)
    let bottomRight = detectedRectangle.bottomRight.scaled(to: imageSize)
    let newBoundingBox = boundingBox.insetBy(dx: imageSize.width * -0.2, dy: imageSize.height * -0.2)

    print("Tony Rect size is: \(Int(newBoundingBox.width))")

    let correctedImage = changedImage
        .cropped(to: newBoundingBox)

        .applyingFilter("CIPerspectiveCorrection", parameters: [
            "inputTopLeft": CIVector(cgPoint: topLeft),
            "inputTopRight": CIVector(cgPoint: topRight),
            "inputBottomLeft": CIVector(cgPoint: bottomLeft),
            "inputBottomRight": CIVector(cgPoint: bottomRight)
            ])

    inputImage = correctedImage

    DispatchQueue.main.async {
        if self.conf == nil {
            self.conf = "none"

        self.XMergeNumberLabel.text = ("XM: \(self.swiftPage.xMergeRadius) - Conf: \(self.conf!)")
        let cgImage = self.context.createCGImage(inputImage, from: inputImage.extent)
        self.finalImageView.image =  UIImage(cgImage: cgImage!)

        if self.finalImageView.image != nil {

            self.finalImage = self.finalImageView.image
            recCrops.append(self.finalImage)
            print("Tony got to end or rects going to return")
            print("Tony recCrop second amount is \(recCrops.count)")
        }else {
            return
        }
        }
    }

这是输出。正如你所看到的那样,“托尼必须结束或将要返回”是在“托尼完成检查庄稼和文本”之后,它不应该因为你没有看到输出中的矩形。

托尼必须检查矩形部分

托尼2请求....

托尼:第二次处理矩形

Tony Rectangle的信心是:1.0

Tony Rect大小是:1413

托尼在直接请求后得到了参与

托尼完成检查庄稼和文字

Tony recCrop数量为0

托尼必须结束或返回

Tony recCrop的第二笔金额是1

ios swift rectangles completion
1个回答
0
投票

我能够通过删除DispachQueue.main来解决这个问题。当我去主要线程显示图像时,它似乎很早就出来了。

© www.soinside.com 2019 - 2024. All rights reserved.