Giter Site home page Giter Site logo

google_drive_ocr's Introduction

Google OCR (Drive API v3)

https://img.shields.io/pypi/v/google_drive_ocr?color=success Documentation Status Python Version Support GitHub Issues GitHub Followers Twitter Followers

Perform OCR using Google's Drive API v3

Features

  • Perform OCR using Google's Drive API v3
  • Class GoogleOCRApplication() for use in projects
  • Highly configurable CLI
  • Run OCR on a single image file
  • Run OCR on multiple image files
  • Run OCR on all images in directory
  • Use multiple workers (multiprocessing)
  • Work on a PDF document directly

Usage

Using in a Project

Create a GoogleOCRApplication application instance:

from google_drive_ocr import GoogleOCRApplication

app = GoogleOCRApplication('client_secret.json')

Perform OCR on a single image:

app.perform_ocr('image.png')

Perform OCR on mupltiple images:

app.perform_ocr_batch(['image_1.png', 'image_2.png', 'image_3.png'])

Perform OCR on multiple images using multiple workers (multiprocessing):

app.perform_ocr_batch(['image_1.png', 'image_3.png', 'image_2.png'], workers=2)

Using Command Line Interface

Typical usage with several options:

google-ocr --client-secret client_secret.json \
--upload-folder-id <google-drive-folder-id>  \
--image-dir images/ --extension .jpg \
--workers 4 --no-keep

Show help message with the full set of options:

google-ocr --help

Configuration

The default location for configuration is ~/.gdo.cfg. If configuration is written to this location with a set of options, we don't have to specify those options again on the subsequent runs.

Save configuration and exit:

google-ocr --client-secret client_secret.json --write-config ~/.gdo.cfg

Read configuration from a custom location (if it was written to a custom location):

google-ocr --config ~/.my_config_file ..

Performing OCR

Note: It is assumed that the client-secret option is saved in configuration file.

Single image file:

google-ocr -i image.png

Multiple image files:

google-ocr -b image_1.png image_2.png image_3.png

All image files from a directory with a specific extension:

google-ocr --image-dir images/ --extension .png

Multiple workers (multiprocessing):

google-ocr -b image_1.png image_2.png image_3.png --workers 2

PDF files:

google-ocr --pdf document.pdf --pages 1-3 5 7-10 13

Note: You must setup a Google application and download client_secrets.json file before using google_drive_ocr.

Setup Instructions

Create a project on Google Cloud Platform

Wizard: https://console.developers.google.com/start/api?id=drive

Instructions:

google_drive_ocr's People

Contributors

hrishikeshrt avatar pedroeagle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

google_drive_ocr's Issues

BUG: Accès bloqué

Accès bloqué : [.....] n'a pas terminé la procédure de validation de Google

[QUESTION] How to refresh to new client ID?

  • google-drive-ocr version: 0.2.6
  • Python version: Python 3.12.2
  • Operating System: macOS Sonoma 14.2.1

Question

The token had expired, so I deleted the project and re-created a new project. (Both the old and new projects are just for doing OCR)
I downloaded the new client_secret.json file and tried to perform OCR, but I get the following error.
Is it something to do with cache, etc.?

google.auth.exceptions.RefreshError: ('invalid_client: Unauthorized', {'error': 'invalid_client', 'error_description': 'Unauthorized'})

Is application working atm?

  • google-drive-ocr version: 0.2.6
  • Python version: 3.10.11
  • Operating System: windows 10

Question:

Can you confirm what this application still working as intended?
I first set it up about a month ago and successfully used for a while. Today I attempted to OCR some images, but now then I try to use it in command line mode I am getting this
Here is the error block:

Traceback (most recent call last):
File "C:\Utils\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Utils\Python\Python310\lib\runpy.py", line 86, in run_code
exec(code, run_globals)
File "C:\Utils\Python\Python310\Scripts\google-ocr.exe_main
.py", line 7, in
File "C:\Utils\Python\Python310\lib\site-packages\google_drive_ocr\cli.py", line 117, in main
app = GoogleOCRApplication(
File "", line 9, in init
File "C:\Utils\Python\Python310\lib\site-packages\google_drive_ocr\application.py", line 98, in post_init
creds = self.get_credentials()
File "C:\Utils\Python\Python310\lib\site-packages\google_drive_ocr\application.py", line 144, in get_credentials
creds.refresh(Request())
File "C:\Utils\Python\Python310\lib\site-packages\google\oauth2\credentials.py", line 339, in refresh
) = reauth.refresh_grant(
File "C:\Utils\Python\Python310\lib\site-packages\google\oauth2\reauth.py", line 367, in refresh_grant
_client._handle_error_response(response_data, retryable_error)
File "C:\Utils\Python\Python310\lib\site-packages\google\oauth2_client.py", line 74, in _handle_error_response
raise exceptions.RefreshError(
google.auth.exceptions.RefreshError: ('invalid_grant: Bad Request', {'error': 'invalid_grant', 'error_description': 'Bad Request'})

I tried creating and using new secret.json but nothing changes.
Did I managed to mess something up on my side between then and now, or is this just google changing stuff over time?

Support write output to file

It seems the package does not support write the output to file itself. It is great if the command line has an option to write the ocr result to output file like .txt or docs or any text extension.

Thank you

Runtime error

  • Google OCR (Drive API v3) version:
  • Python version: 3.10
  • Operating System: Arm64

Description

Installed via Source
Runtime error...
Got x3 separate pages in my dir though ...

What I Did

~ $ google-ocr --config /storage/28E1-1BF4/GoogleOCR/gdo.cfg --pdf /storage/emulated/0/Download/In.pdf
Traceback (most recent call last):
  File "/data/data/com.termux/files/usr/bin/google-ocr", line 33, in <module>
    sys.exit(load_entry_point('google-drive-ocr==0.2.5', 'console_scripts', 'google-ocr')())
  File "/data/data/com.termux/files/usr/lib/python3.10/site-packages/google_drive_ocr/cli.py", line 153, in main
    app.perform_ocr_batch(
  File "/data/data/com.termux/files/usr/lib/python3.10/site-packages/google_drive_ocr/application.py", line 266, in perform_ocr_batch
    _workload = workload + (idx < extra)
UnboundLocalError: local variable 'extra' referenced before assignment

google-drive-ocr runtime error ...Installed successfully Android

  • Google OCR (Drive API v3) version: dont know
  • Python version: 3.10
  • Operating System: Android arm64 Termux

Description

Runtime error

What I Did

Install via

pkg install popper
pip install google_drive_ocr
cd <libDir>
ln -s liblcms2.so liblcms2.so.2

#google-ocr --config /storage/28E1-1BF4/GoogleOCR/gdo.cfg --pdf /storage/emulated/0/Download/In.pdf --pages 1-3 --verbose

Storing credentials to /data/data/com.termux/files/home/.credentials/token.json file_cache is only supported with oauth2client<4.0.0 Extracting 3 pages from '/storage/emulated/0/Download/In.pdf' .. Extracted 3 pages: 1 to 3. Traceback (most recent call last): File "/data/data/com.termux/files/usr/bin/google-ocr", line 8, in sys.exit(main()) File "/data/data/com.termux/files/usr/lib/python3.10/site-packages/google_drive_ocr/cli.py", line 153, in main app.perform_ocr_batch( File "/data/data/com.termux/files/usr/lib/python3.10/site-packages/google_drive_ocr/application.py", line 266, in perform_ocr_batch _workload = workload + (idx < extra) UnboundLocalError: local variable 'extra' referenced before assignment

google-ocr stopped working suddenly

  • google-drive-ocr version: google-ocr 0.2.6
  • Python version: 3.11.5
  • Operating System: MacOS 14.0 (23A344)

Description

It was working earlier but stopped a few days ago. Following is the command I ran and the error.

What I Did

> google-ocr -i image.png --client-secret client_secret_11_oct_2023.json --debug                                                                                                   

{'__module__': 'google_drive_ocr.cli', 'image': 'image.png', 'batch': None, 'image_dir': None, 'output': None, 'extension': '.png', 'suffix': '.google.txt', 'pdf': None, 'pages': None, 'client_secret': 'client_secret_11_oct_2023.json', 'upload_folder_id': None, 'workers': 1, 'no_keep': False, 'verbose': False, 'debug': True, '__dict__': <attribute '__dict__' of 'Config' objects>, '__weakref__': <attribute '__weakref__' of 'Config' objects>, '__doc__': None, 'config_file': None, 'write_out_config_file_to_this_path': None}
Making request: POST https://oauth2.googleapis.com/token
Starting new HTTPS connection (1): oauth2.googleapis.com:443
https://oauth2.googleapis.com:443 "POST /token HTTP/1.1" 400 None
Traceback (most recent call last):
  File "/usr/local/bin/google-ocr", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/usr/local/lib/python3.11/site-packages/google_drive_ocr/cli.py", line 117, in main
    app = GoogleOCRApplication(
          ^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 9, in __init__
  File "/usr/local/lib/python3.11/site-packages/google_drive_ocr/application.py", line 98, in __post_init__
    creds = self.get_credentials()
            ^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/google_drive_ocr/application.py", line 144, in get_credentials
    creds.refresh(Request())
  File "/usr/local/lib/python3.11/site-packages/google/oauth2/credentials.py", line 344, in refresh
    ) = reauth.refresh_grant(
        ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/google/oauth2/reauth.py", line 365, in refresh_grant
    _client._handle_error_response(response_data, retryable_error)
  File "/usr/local/lib/python3.11/site-packages/google/oauth2/_client.py", line 72, in _handle_error_response
    raise exceptions.RefreshError(
google.auth.exceptions.RefreshError: ('invalid_grant: Bad Request', {'error': 'invalid_grant', 'error_description': 'Bad Request'})

More customizable CLI

Feature Description

GoogleOCRApplication class supports

  • specifying suffix for output text files
  • specifying output path explicitly when single image

Expose these methods to CLI for a greater control

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.