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
 privatestring JsonKeypath
{
     get { return Application.StartupPath + "\\your file name.json"; 
}
  • We need to create credential object to call with our api
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
        private VisionService CreateService(GoogleCredential credential)
        {
                var service = new VisionService(new  BaseClientService.Initializer()
                {
                     HttpClientInitializer = credential,
                     ApplicationName =ApplicationName,
                     GZipEnabled = true,
                });
                return service;
        }
BatchAnnotateImagesRequest batchRequest = new BatchAnnotateImagesRequest();
batchRequest.Requests = new List();
batchRequest.Requests.Add(new AnnotateImageRequest()
{
 Features = new List() { new Feature() { Type = "TEXT_DETECTION", MaxResults = 1 }, },
 ImageContext = new ImageContext() { LanguageHints = new List() { language } },
 Image = new Image() { Content = Convert.ToBase64String(file) }
});
 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 :

Comments

Popular posts from this blog

C# Crop white space from around the image

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

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