gpt4 book ai didi

ios - 根据矩形请求完成,iOS,Swift

转载 作者:行者123 更新时间:2023-11-30 11:09:52 29 4
gpt4 key购买 nike

我正在尝试用视觉和敏捷来检测矩形。

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

我可以编写一个完成,允许矩形检测先完成然后再继续吗?

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

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

Tony got to check rect part

Tony 2 Requested....

Tony: Handle rectangles the second time

Tony Rectangle confidence is: 1.0

Tony Rect size is: 1413

Tony got part after rect request

Tony Finished checking crops and text

Tony recCrop amount is is 0

Tony got to end or rects going to return

Tony recCrop second amount is 1

最佳答案

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

关于ios - 根据矩形请求完成,iOS,Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52295265/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com