GoogleOcr
Google Vision API is just great. If you ever played with Google Photo App, you will be impressed at how easy it is to extract the texts out of an image, and copy the street address
, business card names
, phone number
etc.
How about a simple C# class to do the OCR in a few lines of code? That is what exactly this repo does for you. This repository wraps Google Vision API with a handy utiity class GoogleOcr
to perform text extraction from images or PDF files.
var text = GoogleOcr.Load("Samples/TIFF/Abc.tif"); // TIFF file
var text = GoogleOcr.Load("Samples/TIFF/Abc.pdf"); // PDF file
// Do whatever with the recognized texts
How to get Started
Set up a project in Google Cloud Console (Method I)
- Follow Quickstart: Using client libraries to create a Google Cloud project.
- After setup, you will get a key json file, download it and save it to a local file, e.g. c:\abc\key.json
Set up a project using Google Cloud Shell (Method II: Recommended)
- Follow this CodeLabs tutorial to create a new project
- Open a Cloud Shell and type below code
gcloud auth list
gcloud services enable vision.googleapis.com
export GOOGLE_CLOUD_PROJECT=$(gcloud config get-value core/project)
gcloud iam service-accounts create my-vision-sa --display-name "my vision service account"
gcloud iam service-accounts keys create ~/key.json --iam-account my-vision-sa@${GOOGLE_CLOUD_PROJECT}.iam.gserviceaccount.com
export GOOGLE_APPLICATION_CREDENTIALS=~/key.json
Then run below code (in the cloud shell, not your own host shell/terminal) and copy the key.json
file content to the clipboard 📋
cat ~/key.json | pbcopy
- In your development/host machine (not
Google Cloud Shell), paste the content and save it to a filekey.json
- Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your service account key.
Setup your json file path locally
- This step is important if you want to test this repo successfully! You have too properly set your json config location!
- If you fail to run the example project, you need first to check this
Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of the JSON file that contains your service account key.
-
Follow this article if you want to do so interactively via point-and-click:
-
Programatically:
setx GOOGLE_APPLICATION_CREDENTIALS /Your/Json/File/Path
Doing is believing: Run the examples to see how it works
- Open the
GoogleOcrLibTests
project - In VisualStudio, Open Test Explorer: Run the test cases (Right click --> Run tests)
Example:
var text = GoogleOcr.Load("Samples/Gif/TestClass.gif");
Assert.IsNotNull(text);
Assert.IsTrue(text.Contains("TestClass"));
Assert.IsTrue(text.Contains("GoogleOcrTests"));