在 CoreML App 的变量列表中使用未解析的标识符

Use of unresolved identifier in a list of variables in CoreML App

我将我的对象检测模型 (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 天,但在任何地方都找不到正确的提示解决这个。 任何评论都非常感谢。 非常感谢!

你的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 { [=12=].topIdentifier == latestObservation.topIdentifier } {...}