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
Source Code : https://github.com/msm2020/OCR-google-APIs
- Go to http://console.developers.google.com/ and create new project or use existing project
- 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
- 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;
- }
- API need language code reference here : https://cloud.google.com/translate/v2/translate-reference#supported_languages
- Create batch request to upload image and get result :
- 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>
- Vision api have a lot of type like face detection and other types I use text_detiction https://cloud.google.com/vision/reference/rest/v1/images/annotate#Type
- Excute request to get result
- 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
Source Code : https://github.com/msm2020/OCR-google-APIs
Google reference :