【问题标题】:Use of unresolved identifier in a list of variables in CoreML App在 CoreML 应用程序的变量列表中使用未解析的标识符
【发布时间】:2019-06-08 12:37:53
【问题描述】:

我将我的对象检测模型 (latestObservation ) 的结果附加到一个列表 (listofObservation) 中,它工作正常,但我想添加一个条件,以便仅在最新观察结果与之前不同时才添加它3 观察。

将最新结果与之前的观察结果进行比较时,我无法访问该变量。

这是我的代码:

var latestObservation = (topIdentifier: String(), topConfidence: Float(), scndIdentifier: String(), scndConfidence: Float()) 
var listofObservation:[(topIdentifier: String, topConfidence: Float, scndIdentifier: String, scndConfidence: Float)] = []
var LastTopIdentifierCounter = 0

// ... and the part of the func drawVisionRequestResults that is relevant here follows:  

for observation in results where observation is VNRecognizedObjectObservation {
        guard let objectObservation = observation as? VNRecognizedObjectObservation else {
            continue
        }
        // Select only the label with the highest confidence and the second highest confidence: 
        let topLabelObservation = objectObservation.labels[0]
        let secondLabelObservation = objectObservation.labels[1]

latestObservation = (topIdentifier: topLabelObservation.identifier, topConfidence: topLabelObservation.confidence, scndIdentifier: secondLabelObservation.identifier, scndConfidence: secondLabelObservation.confidence)

let OFL = listofObservation.count
if (listofObservation.topIdentifier(OFL)) == latestObservation.topIdentifier
   && latestObservation.topidentifier == listofObservation.topIdentifier[OFL-1]
   && latestObservation.topidentifier == listofObservation.topIdentifier[OFL-2]
   {
   LastTopIdentifierCounter += (1)
   }
   else {
   listofObservation.append(latestObservation)
   LastTopIdentifierCounter = 0
print(latestObservation)
print(listofObservation)    

打印显示变量的以下内容(如果我使附加无条件):

  1. 最新观察:

(topIdentifier:“6.no_phase”,topConfidence:0.87878287,scndIdentifier:“4.Faden_abnehmen”,scndConfidence:0.06840562)

  1. 观察列表:

[(topIdentifier:“6.no_phase”,topConfidence:0.87878287,scndIdentifier:“4.Faden_abnehmen”,scndConfidence:0.06840562),(topIdentifier:“6.no_phase”,topConfidence:0.7264241,scndIdentifier:“4.Faden_abnehmen” , scndConfidence: 0.22894023), (topIdentifier: "6.no_phase", topConfidence: 0.92339694, scndIdentifier: "4.Faden_abnehmen", scndConfidence: 0.058480877)]

在代码行上:

if (listofObservation.topIdentifier(OFL)) == latestObservation.topIdentifier

我确实收到以下消息: '[(topIdentifier: String, topConfidence: Float, scndIdentifier: String, scndConfidence: Float)]'类型的值没有成员'topIdentifier'

我对 swift 非常陌生,因此不得不道歉,因为这可能是一个非常幼稚的问题......我已经为此苦苦挣扎了 2 天,在任何地方都找不到关于如何解决这个问题的正确提示。 任何评论都非常感谢。 非常感谢!

【问题讨论】:

    标签: arrays swift coreml


    【解决方案1】:

    你的listofObservation是一个元组数组,所以你需要指定某个元组的索引来获取某个元组,然后是它的属性

    array[index] -> element
          ^ Int      ^ certain tuple
    

    if listofObservation[OFL].topIdentifier  == latestObservation.topIdentifier 
    && listofObservation[OFL-1].topIdentifier  == latestObservation.topidentifier
    && listofObservation[OFL-2].topIdentifier  == latestObservation.topidentifier {...}
    

    或者在 Swift 4.2+ 中你可以使用allSatisfy

    if listofObservation[OFL-2...OFL].allSatisfy { $0.topIdentifier == latestObservation.topIdentifier } {...}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-28
      • 2018-03-12
      • 1970-01-01
      • 1970-01-01
      • 2015-05-13
      相关资源
      最近更新 更多