8

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

3 回答 3

12

纯分类器模型只能回答“这是什么图片?”,不能检测和定位图片中的物体。Apple 开发者网站上的所有免费模型(包括 Inception v3)都是这种类型。

当 Vision 使用此类模型时,它会根据 MLModel 文件中声明的输出将模型识别为分类器,并将VNClassificationObservation对象作为输出返回。

如果您找到或创建了一个经过训练可以识别和定位对象的模型,您仍然可以将它与 Vision 一起使用。当您将该模型转换为 Core ML 格式时,MLModel 文件将描述多个输出。当 Vision 使用具有多个输出的模型时,它会返回一个VNCoreMLFeatureValueObservation对象数组——模型的每个输出一个对象。

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

附录:这是一个适用于 iOS 11 并返回的模型VNCoreMLFeatureValueObservationTinyYOLO

于 2017-06-23T15:10:17.857 回答
4

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

你在这里使用什么型号?

于 2017-06-22T17:10:58.540 回答
0

为了跟踪和识别对象,您必须使用暗网创建自己的模型。我遇到了同样的问题,并使用 TuriCreate 来训练模型,而不仅仅是向框架提供图像,您还必须为框架提供边界框。Apple 在此处记录了如何创建这些模型: Apple TuriCreate docs

于 2018-09-11T22:05:43.690 回答