Computer Science

Google Vision API

The Google Vision API is a powerful service that allows developers to integrate AI-based image analysis capabilities into their applications. It's primarily used to understand the content of an image and extract information about that content.It's a paid service.

Google Vision API, google panel

First, enable the Google Vision API at https://console.cloud.google.com/. Create a service account and finally download the generated ".json" file. This ".json" file is used in the project for authentication.

Photo 2

Code .net core


• Create Index.cshtml
• Create Result.cshtml
• Create AnalyzeImage method
• Download Google.Cloud.Vision.V1 (nuget package)

                        public class HomeController : Controller
  {
      private readonly IWebHostEnvironment _env;

      public HomeController(IWebHostEnvironment env)
      {
          _env = env;
      }
      public IActionResult Index()
      {
          return View();
      }
      [HttpPost]
      public async Task<IActionResult> AnalyzeImage(IFormFile file)
      {
          if (file == null || file.Length == 0)
          {
              ViewBag.ErrorMessage = "Lütfen bir resim dosyası seçin.";
              return View("Index");
          }

          var credentialsPath = Path.Combine(_env.ContentRootPath, "your_json_file.json");
          System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credentialsPath);

          byte[] imageBytes;
          using (var ms = new MemoryStream())
          {
              await file.CopyToAsync(ms);
              imageBytes = ms.ToArray();
          }

          try
          {
              
              ImageAnnotatorClient client = ImageAnnotatorClient.Create();

              var response = await client.DetectLabelsAsync(Image.FromBytes(imageBytes));

              var labels = new List<string>();
              foreach (var annotation in response)
              {
                  if (annotation.Description != null)
                  {
                      labels.Add($"{annotation.Description} (Güvenilirlik: {annotation.Score:P2})");
                  }
              }

              ViewBag.Labels = labels;
          }
          catch (Exception ex)
          {
              ViewBag.ErrorMessage = $"Hata oluştu: {ex.Message}";
          }

          return View("Result");
      }
      

      public IActionResult Result()
      {
          return View();
      }
}    
                    

Index.cshtml
                        <form asp-action="AnalyzeImage" method="post" enctype="multipart/form-data">
    <div class="form-group">
        <label for="file">Resim Seçin:</label>
        <input type="file" name="file" id="file" class="form-control" required />
    </div>
    <br />
    <button type="submit" class="btn btn-primary">Analiz Et</button>
</form>         
                    

Result.cshtml
                        <h1>Resim Analiz Sonuçları</h1>

@if (ViewBag.Labels != null && ViewBag.Labels.Count > 0)
{
    <h3>Algılanan Etiketler:</h3>
    <ul>
        @foreach (var label in ViewBag.Labels)
        {
            <li>@label</li>
        }
    </ul>
}
else
{
    <p>Resimde herhangi bir etiket algılanamadı.</p>
}

<a asp-action="Index" class="btn btn-secondary">Yeni Resim Analiz Et</a>