Search code examples
c#loopsgoogle-vision

How to process Google Vision API one by one and not in a block?


I got a problem when using the Google Vision API. I'm looping the process, to analyze several pictures, but when I print the results, all is coming in a block after 5 minutes of process, but I wanted to know if it's possible to start the program and make it print the results after each picture analyzis ?

Here's my code for the algorithm :

bool space = false;
        // var image = Google.Cloud.Vision.V1.Image.FromFile("C:\\temp\\sequence\\n" + i + ".jpg");
            var image = Google.Cloud.Vision.V1.Image.FromFile(path);
            var client = ImageAnnotatorClient.Create();
            var response = client.DetectLabels(image);
            CropHintsAnnotation confidence = client.DetectCropHints(image);

        bool car = false;
        bool vehicle = false;
        bool land = false;
        int score = 0;
        //System.IO.Directory

        foreach (var annotation in response)
        {
            textBox1.Text += annotation.Description + "\r\n";
            textBox1.Text += "Score : " + annotation.Score + "\r\n";
            vehicle = !annotation.Description.Equals("vehicle");
            car = !annotation.Description.Equals("car");
            land = !annotation.Description.Equals("land vehicle");

            if (car == false)
            {
                score += 1;
                //textBox1.Text += "\r\nEmpty ?       " + car + "\r\n\r\n";
            }
            else if (vehicle == false)
            {
                score += 1;
                //textBox1.Text += "\r\nEmpty ?       " + vehicle + "\r\n\r\n";
            }
            else if (land == false)
            {
                score += 1;
                //textBox1.Text += "\r\nEmpty ?       " + land + "\r\n\r\n";
            }
            else if (annotation.Description.Equals("asphalt"))
            {
                score += -20;
                //textBox1.Text += "\r\nEmpty ?  True \r\n\r\n";
            }
            else
            {
                score += 0;
            }
        }
        if ( score > 0)
        {
            //textBox1.Text += "The parking space is taken \r\n\r\n";
            space = true;
        }
        else
        {
            //textBox1.Text += "The parking space is empty \r\n\r\n";
            space = false;
        }
        return space;

I'm looping this with a foreach(Image file in Directory).

Any ideas to help me ?

Thanks a lot !


Solution

  • Even though you update textBox1.Text, the UI will not update since the UI thread is busy doing the calculation.

    As such, you need to call textBox1.Refresh() or Application.DoEvents() after updating textBox1.Text.