完成矩形请求,iOS,Swift

时间:2018-09-12 12:31:44

标签: ios swift rectangles completion

我正在尝试以视觉和速度来检测矩形。

我运行了所有代码,但是发现下一个print语句在矩形请求功能完成之前显示,这引起了问题,因为我正在尝试处理尚不存在的矩形。

我可以写一个能使rect检测先完成的补全吗?

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(request:VNRequest,error:Error?){

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

这是输出。如您所见,“ Tony已结束或rects要返回”出现在“ Tony Finished检查农作物和文本”之后,这是不应该的,因为输出中没有rect可以处理。

  

Tony必须检查直肠部分

     

托尼2请求了......

     

Tony:第二次处理矩形

     

Tony矩形的置信度是:1.0

     

Tony Rect的大小是:1413

     

托尼(Tony)在正确要求后得到了参与

     

Tony完成了对农作物和文本的检查

     

Tony recCrop量为0

     

托尼(Tony)结束或整顿要回来

     

Tony recCrop第二个数量是1

0 个答案:

没有答案