在检测矩形,Swift,iOS时崩溃

时间:2018-09-07 17:11:39

标签: ios swift rectangles

我正在尝试用视觉检测矩形。我常常因为这种爆炸而崩溃。

  

断言失败:(maxDev!= -1.f),函数StraightLineWLSQ,文件/BuildRoot/Library/Caches/com.apple.xbs/Sources/Vision/Vision-1.5.3/RectangleDetector/QuadDetect/Leq.c ,第217行。

这是崩溃的功能。

func performVisionRequest(image: CGImage, orientation: CGImagePropertyOrientation) {


    DispatchQueue.global(qos: .userInitiated).async {
        do {

            let imageRequestHandler = VNImageRequestHandler(cgImage: image, orientation: orientation, options: [:])
            // This is where I get the error.
            try imageRequestHandler.perform(
                [VNDetectRectanglesRequest(completionHandler:{ req, err in
                    self.rectanglesRequest(request:req, error:err)

                })]
            )
        } catch let error as NSError {
            self.sliceCompletion(UIImage())
            print("Failed to perform vision request: \(error)")

        }
    }
}

1 个答案:

答案 0 :(得分:0)

我的项目遇到了同样的问题,可以通过跳过某些图像来解决,例如跳过ImageWidth高于ImageHeight以及ImageHeight高于ImageWidth

当我们尝试检测某些图像的矩形,例如(ImageWidth:1240,ImageHeight:121 )或(ImageWidth:121,ImageHeight:1240)时,会发生崩溃

func performVisionRequest(image: CGImage, orientation: CGImagePropertyOrientation) {


       let ratioW = image.width / image.height
       let ratioH = image.height / image.width

       if(ratio < 7) //To skip crash image
       {
           DispatchQueue.global(qos: .userInitiated).async {

             do {

                   let imageRequestHandler = VNImageRequestHandler(cgImage: image, orientation: orientation, options: [:])
            // This is where I get the error.
                   try imageRequestHandler.perform(
                [VNDetectRectanglesRequest(completionHandler:{ req, err in
                    self.rectanglesRequest(request:req, error:err)

                })]
            )
        } catch let error as NSError {
            self.sliceCompletion(UIImage())
            print("Failed to perform vision request: \(error)")

        }
    }
  }
}