ios - 物体检测 : Output different from CreateML vs programatically

标签 ios swift macos coreml apple-vision

我想从图像中提取已知对象。我使用 CreateML 应用程序创建了一个 ObjectDetector 模型。当我使用 CreateML 预览进行测试时,检测工作完全正常,但是通过代码,似乎出现了问题。

下面是我编写的示例代码部分。我使用 boundingbox 保存图片,但是,当我使用 CreateML 预览进行测试时,预测的图像完全不同。我已尝试了所有选项,请让我知道我的代码有什么问题。

func extractSpecifcSectioninImage(image: NSImage){
    var requests = [VNRequest]()
    var picCount = 1
    let modelURL = Bundle.main.url(forResource: "ObjectDetection", withExtension: "mlmodelc")!
    
    do {
        let visionModel = try VNCoreMLModel(for: MLModel(contentsOf: modelURL))
        let objectRecognition = VNCoreMLRequest(model: visionModel, completionHandler: { (request, error) in
            if let results = request.results {
                for observation in results where observation is VNRecognizedObjectObservation {
                    guard let objectObservation = observation as? VNRecognizedObjectObservation else {
                        continue
                    }
                    let cropsize = VNImageRectForNormalizedRect(objectObservation.boundingBox, Int((image.size.width)), Int((image.size.height)))
                    let topLabelObservation = objectObservation.labels[0]
                    guard let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil) else{break}
                    guard let cutImageRef: CGImage = cgImage.cropping(to:cropsize)else {break}
                    let sie = NSSize(width: cropsize.width,height: cropsize.height)
                    let objectImg = NSImage(cgImage: cutImageRef, size: sie)
                    if objectImg.save(as: "CroppedImage\(picCount)") {
                        picCount += 1
                    }
                }
            }
        })
        objectRecognition.imageCropAndScaleOption = .scaleFill
        guard let cgImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil) else{
            print("Failed to get cgimage from input image")
            return
        }
        let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
        do {
            try handler.perform([objectRecognition])
        } catch {
            print(error)
        }
        requests = [objectRecognition]
    } catch let error as NSError {
        print("Model loading went wrong: \(error)")
    }
}

Screenshot of Predictions tab

最佳答案

您没有说边界框有什么问题,但我的猜测是它们是正确的,但它们只是没有绘制在正确的位置。我写了一篇关于此的博文:https://machinethink.net/blog/bounding-boxes/

关于ios - 物体检测 : Output different from CreateML vs programatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65200280/

相关文章:

ios - 文件播放后我的应用程序崩溃 (Swift)

javascript - Videojs HLS m3u8 文件无法在 iOS 设备和 Safari 浏览器中播放

ios - 重新创建 Siri 按钮发光动画

python - 如何在 Mac OS X 上为 Python 3 安装 pip?

objective-c - Macos x 应用程序的快捷方式

python - cmake osx部署目标是 '10.11'但CMAKE_OSX_SYSROOT错误

ios - CKRecord 获取返回 1 条 RecordID 为空的记录

ios - 跟踪 iOS 上的设备使用情况

swift - 如何在自定义表格单元格中正确对齐以编程方式创建的开关

ios - SideMenuController 不适用于某些 View