从VNClassificationObservation
获取问题。如何从VNClassificationObservation获取对象矩形/坐标
我的目标ID识别对象并显示弹出对象名称,我能够获取名称,但我无法获取对象坐标或框架。
这里是代码:
let handler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: requestOptions)
do {
try handler.perform([classificationRequest, detectFaceRequest])
} catch {
print(error)
}
然后我处理
func handleClassification(request: VNRequest, error: Error?) {
guard let observations = request.results as? [VNClassificationObservation] else {
fatalError("unexpected result type from VNCoreMLRequest")
}
// Filter observation
let filteredOservations = observations[0...10].filter({ $0.confidence > 0.1 })
// Update UI
DispatchQueue.main.async { [weak self] in
for observation in filteredOservations {
print("observation: ",observation.identifier)
//HERE: I need to display popup with observation name
}
}
}
更新:
lazy var classificationRequest: VNCoreMLRequest = {
// Load the ML model through its generated class and create a Vision request for it.
do {
let model = try VNCoreMLModel(for: Inceptionv3().model)
let request = VNCoreMLRequest(model: model, completionHandler: self.handleClassification)
request.imageCropAndScaleOption = VNImageCropAndScaleOptionCenterCrop
return request
} catch {
fatalError("can't load Vision ML model: \(error)")
}
}()
我使用Inceptionv3()。模型,它看起来我无法获得坐标。 – Svitlana
这是因为Inception-v3不给你坐标,只有类名的字典和这些类的概率。 –
好的,非常感谢你,会搜索另一个模型 – Svitlana