2017-06-22 87 views
4

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)") 
    } 
}() 

回答

1

这是因为分类不返回对象坐标或帧。分类器只给出一个类别列表的概率分布。

你在这里使用什么样的模型?

+0

我使用Inceptionv3()。模型,它看起来我无法获得坐标。 – Svitlana

+1

这是因为Inception-v3不给你坐标,只有类名的字典和这些类的概率。 –

+0

好的,非常感谢你,会搜索另一个模型 – Svitlana

4

纯分类器模型只能回答“这是什么图片?”,而不是检测和定位图片中的对象。所有的free models on the Apple developer site(包括Inception v3)都属于这种类型。

当视觉可与这样的模型,它标识该模型基于在MLModel文件中声明的输出分类器,并返回VNClassificationObservation对象作为输出。

如果您发现或创建了一个既能识别又能定位物体的模型,您仍然可以在Vision中使用它。将该模型转换为Core ML格式时,MLModel文件将描述多个输出。当Vision使用具有多个输出的模型时,它会返回一个VNCoreMLFeatureValueObservation对象的数组 - 每个模型的输出对应一个。

模型如何声明其输出将决定哪些特征值代表什么。报告分类和边界框的模型可能会输出一个字符串和四个双精度,或者一个字符串和一个多数组等等。

+0

你能提出一个提供VNCoreMLFeatureValue观察结果的特定模型吗? –