Search code examples
arraysswiftcoreml

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


I am appending the results from my object detection model (latestObservation ) to a list (listofObservation), which works fine, but I would like to add a conditions such that the latest observation is added only if it is different from the previous 3 observations.

When comparing the latest result with previous observations I have a problem to access the variable.

This is my code:

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)    

the print shows the following content of the variables (in case I make the append unconditional):

  1. latestObservation:

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

  1. listofObservation:

[(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)]

on the code line:

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

I do get the following message: Value of type '[(topIdentifier: String, topConfidence: Float, scndIdentifier: String, scndConfidence: Float)]' has no member 'topIdentifier'

I am very new to swift and thus have to apologize since this is probably a quite naive question... I have struggled now for 2 days with this and cannot find the right hint anywhere on how to solve this. Any comment is highly appreciated. Many thanks!


Solution

  • Your listofObservation is an array of tuples, so you need to specify index of certain tuple to get certain tuple and then its property

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

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

    Or in Swift 4.2+ you can use allSatisfy

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