2017-06-22 143 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觀察結果的特定模型嗎? –