Kapuchon
Kapuchon

Reputation: 134

C# - boolean and if statement

I'm currently writing code analysing images with the Google API Vision on Visual Studios. But I have a problem that occures during my loop. The analysis returns a list of annotation ( car, vehicle, land vehicle, etc.. ) and I wanted to filter it, with an "if", so i wrote this :

var image = Google.Cloud.Vision.V1.Image.FromFile("C:\\temp\\sequence\\1.jpg");
var client = ImageAnnotatorClient.Create();
var response = client.DetectLabels(image);
CropHintsAnnotation confidence = client.DetectCropHints(image);
bool empty = false;

foreach (var annotation in response)
{
    textBox1.Text += annotation.Description + "\r\n";
    textBox1.Text += "Score : " + annotation.Score + "\r\n";
    if (annotation.Description.Equals("vehicle"))
    {
        empty = false;
    }
    else
    {
        empty = true;
    }

}
textBox1.Text += "\r\nEmpty ?       " + empty + "\r\n\r\n";

So, if I wrote this well, it should say " Empty ? false ", because the analysis returns "vehicle" once. I tried also to replace the :

annotation.Description.Equals("vehicle")

by

annotation.Description.Contains("vehicle") == true

but no way, it still says " Empty ? true ", as it should not.

Any ideas ?

Thanks in advance for reading this, and for the help !

Upvotes: 0

Views: 113

Answers (3)

touchofevil
touchofevil

Reputation: 613

Few possibilities :

  1. Can you check the order of vehicle.It might also be that it actually does find a description of vehicle on say index 0, but index 1 is not vehicle and hence it overwrites the previous value. If this is the case, you might wish to break out of loop when your required condition is met.

    foreach (var annotation in response)
    {
        // not sure if you want this to 
        textBox1.Text += annotation.Description + "\r\n";
        textBox1.Text += "Score : " + annotation.Score + "\r\n";
        if (annotation.Description.Equals("vehicle",  
        StringComparison.InvariantCultureIgnoreCase))
        {
            empty = false;
            textBox1.Text += "\r\nEmpty ?       " + empty + "\r\n\r\n";
            //possibly also break if you've achieved what you want.
            break;
        }
    }
    

Upvotes: 0

Gilad Green
Gilad Green

Reputation: 37281

Not exactly sure what you are trying to do here but assuming the response has more than one item the empty variable will only represent the value for the last one.

Reason is that for each iteration of the loop when it gets to the if statement it will enter it or the else and for sure will enter one of the two, so for each iteration the value of empty will be assigned and override the previous value

As for the code itself, it is neater to write it this way:

empty = !annotation.Description.Equals("vehicle");

What you should change is to move the line of the assigning into the loop:

foreach(/*...*/)
{
    /*...*/
    empty = !annotation.Description.Equals("vehicle");
    textBox1.Text += "\r\nEmpty ?       " + empty + "\r\n\r\n";
}

Upvotes: 2

Racil Hilan
Racil Hilan

Reputation: 25371

Have your considered the case of the returned string? Try to ignore the case:

annotation.Description.Equals("vehicle", StringComparison.InvariantCultureIgnoreCase)

Just a side note: The Equals function returns a Boolean, so you can remove the entire if statement and simplify your code to:

empty = !annotation.Description.Equals("vehicle", StringComparison.InvariantCultureIgnoreCase)

Upvotes: 1

Related Questions