簡體   English   中英

ObjectDetection:Output 不同於 CreateML 與編程方式

[英]ObjectDetection: Output different from CreateML vs programatically

我想從圖像中提取已知對象。 我使用 CreateML App 創建了一個ObjectDetector model。 當我使用 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)")
    }
}

預測選項卡的屏幕截圖

您沒有說邊界框出了什么問題,但我的猜測是它們是正確的,但它們根本沒有被繪制在正確的位置。 我寫了一篇關於這個的博客文章: https://machinethink.net/blog/bounding-boxes/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM