我想从图像中提取已知对象。我使用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)")
}
}

发布于 2020-12-09 19:18:23
您没有说边界框有什么问题,但我猜它们是正确的,但它们只是没有被绘制在正确的位置。我为此写了一篇博文:https://machinethink.net/blog/bounding-boxes/
https://stackoverflow.com/questions/65200280
复制相似问题