【问题标题】:Google Vision API - Analyse a sequence of imagesGoogle Vision API - 分析图像序列
【发布时间】:2017-06-23 09:47:56
【问题描述】:

让我把问题暴露给你:

我目前正在 Visual Studio Community 2017 版上进行开发。 我制作了一个使用 Google API 分析图像内容的 Windows 窗体。我设法使它适用于一张单张图片,所以过程是: 我启动程序,出现 Windows,按下“分析图像”按钮,然后在 TextBox 中打印标签检测的结果。

这部分效果很好。

但是现在,我想分析一个文件的所有图像,所以我从 5 个图像开始,只是为了尝试。 所以我做了一个foreach循环,并用“1.jpg”、“2.jpg”等重命名了我的图片……直到“5.jpg”。 这是代码:

    private void Button1_Click(object sender, EventArgs e)
    {


        textBox1.Text = "";

        for (int i = 1; i < 6; i++)
        {

            int number = i;
            textBox1.Text = "Image number : " + number + "\r\n";

            var image = Google.Cloud.Vision.V1.Image.FromFile("C:\\temp\\sequence\\" + number + ".jpg");
            var client = ImageAnnotatorClient.Create();
            var response = client.DetectLabels(image);

            foreach (var annotation in response)
            {
                textBox1.Text += annotation.Description + "\r\n";
            }
            textBox1.Text = "Next image processing.... \r\n";
        }
    }

当我按下“分析按钮”( Button1 )时,程序会打印 “图 1:” 然后我必须等待几秒钟,然后它会打印“下一个图像处理...”,它再也不会打印任何东西了。

所以有什么想法吗? 是不是因为我必须清除 TextBox 才能在其中重新写入?

有人知道我的问题吗? :(

提前感谢您的帮助!

【问题讨论】:

    标签: c# visual-studio-2017 google-cloud-vision


    【解决方案1】:

    我在这里没有看到任何重大的技术问题。 几点观察: 客户端创建最好在 for 循环之外完成。无需一次又一次地创建它的实例。 在分配文本时最好做 += 从您的参考代码中,文本的最终结果将始终是“下一个图像处理...” 但是,如果您认为代码没有在 i = 2 的下一个值时被执行,这在代码中不太可能发生。 您可以对其进行调试以至少确保它是否被调用。 我在自己的环境中使用自己的 ImageAnnotatorClient 实例。下面的代码表示非常适合我。 希望这会有所帮助

                textBox1.Text = "";
                var client = ImageAnnotatorClient.Create();
                try
                {
                    for (int i = 1; i < 6; i++)
                    {
    
                        int number = i;
                        textBox1.Text += "Image number : " + number + "\r\n";
    
                        var image = Google.Cloud.Vision.V1.Image.FromFile("C:\\temp\\sequence\\" + number + ".jpg");
    
                        var response = client.DetectLabels(image);
    
                        foreach (var annotation in response)
                        {
                            textBox1.Text += annotation.Description + "\r\n";
                        }
                        textBox1.Text += "Next image processing.... \r\n";
                    }
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-08
      • 1970-01-01
      • 2016-12-16
      • 1970-01-01
      • 2020-04-03
      • 2019-05-11
      • 2021-03-01
      • 2016-06-10
      相关资源
      最近更新 更多