This is a Python code that uses the Google Cloud Vision OCR API to extract text from images. Here's a step-by-step guide on how to use it:
- Make sure you have a Google Cloud account and have set up billing.
- Create a service account with appropriate permissions to use the Cloud Vision API.
- Download the service account credentials in JSON format and save it to the same directory as this Python script.
- This code requires the following libraries to be installed:
- google-cloud-vision
- google-auth
- google-auth-oauthlib
- google-auth-httplib2
- tkinter
- pillow
- Open the Python script in a code editor of your choice.
- Update the credentials file name in the following line of code to match the name of your credentials file:
- Run the Python script in your terminal.
- Once the script is running, a UI window will appear.
- Click on the "Load Image" button to select an image file from your computer to process.
- Alternatively, click on the "Paste Image" button to paste an image from your clipboard.
- The image will be displayed in the window and the OCR process will begin.
- The extracted text will be displayed in the text box below the image.
- If no text is extracted, a message will be displayed in red text.
- You can turn on or off the printing of the API response payload to the console by clicking on the "Print API Respond On/Off" button.
- The script will create a temporary directory to save the image files processed.
- The path of this temporary directory will be printed to the console upon running the script.
- You can change the prefix for the temporary directory by modifying the
prefix
argument in the following line of code:
To create a Google API Service Account, you need to follow these steps:
To create a Google API Service Account, you must have a Google Cloud Platform (GCP) project. If you don't have one already, follow these steps to create a new project:
- Go to the Google Cloud Console.
- Click the project drop-down in the top navigation bar and click New Project.
- In the New Project dialog, enter a project name and select a billing account. Then click Create.
Before you can create a service account, you need to enable the Google API you want to use. Follow these steps to enable an API:
- Go to the Google Cloud Console.
- Click the Navigation menu and select APIs & Services > Dashboard.
- Click the Enable APIs and Services button.
- Search for the API you want to use and click it.
- Click the Enable button.
Once you have created a Google Cloud Platform project and enabled the API, you can create a service account:
- Go to the Google Cloud Console.
- Click the Navigation menu and select APIs & Services > Credentials.
- Click the Create credentials button and select Service account key.
- In the Create a service account key dialog, select the service account you want to use, and choose a key type. We recommend choosing the JSON key type.
- Click the Create button.
After you create the service account, you will be prompted to download the service account key. The key is a JSON file that contains your private key and other important information. Follow these steps to download and save the service account key:
- Click the Download button to download the service account key.
- Save the key in a secure location on your computer.
To allow the service account to access the API, you need to grant it the appropriate permissions. Follow these steps to grant access:
- Go to the Google Cloud Console.
- Click the Navigation menu and select APIs & Services > Credentials.
- Find the service account you just created and click the Edit button.
- Click the Add Member button.
- In the New members field, enter the email address associated with the service account you just created.
- In the Role field, select the role you want to assign to the service account. For example, if you want to access Google Drive, you can assign the Editor role.
- Click the Save button.