OCR text detection with google APIs C#

After searching more than 3 hrs to know how to implement text detection using google API i decided to write this post, hope it help c# developer to know how to use and implement Vision API,
Source Code : https://github.com/msm2020/OCR-google-APIs

1- Create server key on google console

  • From Overview page enable vision API

  • You need to create new bill information to use vision API, it's fully free for 1st 1000 request/ month you can check https://cloud.google.com/vision/docs/pricing
  • After Enable Google Cloud Vision API go to Credentials page and create new server key
  • Create key by fill all requested data and download Json file

2- Create Visual studio project I'll use vs 2015 and .NET framework 4.5
  • Add google Nuget packages : Google.Apis.Vision.v1 by right click on project and then manage nuget package .
  • Add json file to your project and from properties windows change copy to output directory to true
  • Now create new class and add path of your file to use
  1. privatestring JsonKeypath
  2. {
  3. get { return Application.StartupPath + "\\your file name.json";
  4. }
 privatestring JsonKeypath
{
     get { return Application.StartupPath + "\\your file name.json"; 
}
  • We need to create credential object to call with our api
  1. private GoogleCredential CreateCredential()
  2. {
  3. using (var stream = new FileStream(JsonKeypath, FileMode.Open, FileAccess.Read))
  4. {
  5. string[] scopes = { VisionService.Scope.CloudPlatform };
  6. var credential = GoogleCredential.FromStream(stream);
  7. credential = credential.CreateScoped(scopes);
  8. return credential;
  9. }
private GoogleCredential CreateCredential()
{
    using (var stream = new FileStream(JsonKeypath, FileMode.Open, FileAccess.Read))
          {
              string[] scopes = {  VisionService.Scope.CloudPlatform };
              var credential = GoogleCredential.FromStream(stream);
              credential = credential.CreateScoped(scopes);
              return credential;
}
  • And create service for vision to request api
  1. private VisionService CreateService(GoogleCredential credential)
  2. {
  3. var service = new VisionService(new BaseClientService.Initializer()
  4. {
  5. HttpClientInitializer = credential,
  6. ApplicationName =ApplicationName,
  7. GZipEnabled = true,
  8. });
  9. return service;
  10. }
        private VisionService CreateService(GoogleCredential credential)
        {
                var service = new VisionService(new  BaseClientService.Initializer()
                {
                     HttpClientInitializer = credential,
                     ApplicationName =ApplicationName,
                     GZipEnabled = true,
                });
                return service;
        }
  1. BatchAnnotateImagesRequest batchRequest = new BatchAnnotateImagesRequest();
  2. batchRequest.Requests = new List<annotateimagerequest>();
  3. batchRequest.Requests.Add(new AnnotateImageRequest()
  4. {
  5. Features = new List<feature>() { new Feature() { Type = "TEXT_DETECTION", MaxResults = 1 }, },
  6. ImageContext = new ImageContext() { LanguageHints = new List<string>() { language } },
  7. Image = new Image() { Content = Convert.ToBase64String(file) }
  8. });
  9. </string></feature></annotateimagerequest>
BatchAnnotateImagesRequest batchRequest = new BatchAnnotateImagesRequest();
batchRequest.Requests = new List<annotateimagerequest>();
batchRequest.Requests.Add(new AnnotateImageRequest()
{
 Features = new List<feature>() { new Feature() { Type = "TEXT_DETECTION", MaxResults = 1 }, },
 ImageContext = new ImageContext() { LanguageHints = new List<string>() { language } },
 Image = new Image() { Content = Convert.ToBase64String(file) }
});
</string></feature></annotateimagerequest>
  1. var annotate = service.Images.Annotate(batchRequest);
  2. BatchAnnotateImagesResponse batchAnnotateImagesResponse = annotate.Execute();
 var annotate = service.Images.Annotate(batchRequest);
 BatchAnnotateImagesResponse batchAnnotateImagesResponse = annotate.Execute();
  • Response result have Error variable and different type of classes related to Type uses on the api call and will use annotateImageResponse.TextAnnotations to get result and values

Output

Google reference :

Popular posts from this blog

Could not load file or assembly 'Microsoft.ReportViewer.Common, Version=xx.0.0.0, Culture=neutral, PublicKeyToken='xxx' or one of its dependencies.

C# Crop white space from around the image

The specified version string contains wildcards, which are not compatible with determinism.