当另一个结束时,我试图调用一个函数,但我不断收到一个错误,告诉我图像不高于0像素高和宽,所以我认为它没有得到调用函数之前的图像。当我添加一个断点来调用OCR的功能时,此时应用程序不会在屏幕上显示图像,这就是为什么我得出这个结论的原因。当另一个结束时运行一个函数,iOS,Swift
这里是我从ocr得到的错误。
NSAssert(widthOfImage> 0 & & heightOfImage> 0,@ “合格图像不能为空 - 它应该是至少1px的高和宽”);
下面是我的控制台读数,我在其中放置打印件以查看流量。
托尼1请求.... 托尼3运行OCR .... 托尼2手柄矩形.... 托尼:这里Corected图像...... (LLDB)
以下是我的代码。我应该有一个完成,确保函数不被调用,直到图像到位?
func startOCR() {
swiftOCRInstance.recognize(correctedImageView.image!) {recognizedString in
print(recognizedString)
self.classificationLabel.text = recognizedString
}
}
lazy var rectanglesRequest: VNDetectRectanglesRequest = {
print("Tony 1 Requested....")
return VNDetectRectanglesRequest(completionHandler: self.handleRectangles)
}()
func handleRectangles(request: VNRequest, error: Error?) {
guard let observations = request.results as? [VNRectangleObservation]
else { fatalError("unexpected result type from VNDetectRectanglesRequest") }
guard let detectedRectangle = observations.first else {
DispatchQueue.main.async {
self.classificationLabel.text = "No rectangles detected."
}
return
}
let imageSize = inputImage.extent.size
// Verify detected rectangle is valid.
let boundingBox = detectedRectangle.boundingBox.scaled(to: imageSize)
guard inputImage.extent.contains(boundingBox)
else { print("invalid detected rectangle"); return }
// Rectify the detected image and reduce it to inverted grayscale for applying model.
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 correctedImage = inputImage
.cropped(to: boundingBox)
.applyingFilter("CIPerspectiveCorrection", parameters: [
"inputTopLeft": CIVector(cgPoint: topLeft),
"inputTopRight": CIVector(cgPoint: topRight),
"inputBottomLeft": CIVector(cgPoint: bottomLeft),
"inputBottomRight": CIVector(cgPoint: bottomRight)
])
// .applyingFilter("CIColorControls", parameters: [
// kCIInputSaturationKey: 0,
// kCIInputContrastKey: 32
// ])
// Show the pre-processed image
DispatchQueue.main.async {
self.correctedImageView.image = UIImage(ciImage: correctedImage)
if self.correctedImageView.image != nil {
print("Tony 2 Handle Rectangle....")
print("Tony: Corected image here......")
}else {
print("Tony: No corected image......")
}
}
print("Tony 3 run OCR....")
self.startOCR()
}
我也得到一个紫色的错误说的UIImage应该在下面的PIC主线程上使用...
这个错误告诉你应该从主线程使用UIImageView,而不是使用UIImage。 –