A Python script that uses Google Cloud Vision to extract text from images and automatically detects and translates the text into the desired language using Google Cloud Translation API. It can handle multiple images in a directory.
- Extract text from images using Google Cloud Vision API
- Automatically detect the language of the text
- Translate the text to a target language using Google Cloud Translate API
- Process multiple images in a specified directory
- Supports common image formats (JPG, PNG, etc.)
- Python 3.6+
- Google Cloud Service Account credentials
- Enabled Google Cloud Vision and Translate APIs
You can install the required dependencies via pip using the requirements.txt
file:
pip install -r requirements.txt
-
Create a Google Cloud project: Google Cloud Console.
-
Enable the Google Cloud Vision API and Google Cloud Translation API.
-
Create a Service Account with appropriate permissions and download the JSON credentials file.
-
Set the environment variable to point to your credentials file:
export GOOGLE_APPLICATION_CREDENTIALS="path_to_your_service_account.json"
git clone https://github.com/yourusername/translate_images.git
cd image-translator
pip install -r requirements.txt
python image_translator.py <directory_path> [target_language]
- <directory_path>: The directory containing images (JPG/PNG).
- [target_language]: (Optional) The language code to translate to (default is English: en).
pytest -v
To translate all images in the /home/user/images folder to English:
python image_translator.py /home/user/images en
Processing image1.jpg...
Detected language: es
Extracted Text from image1.jpg:
Hola Mundo!
Translated Text from image1.jpg:
Hello World!
Processing examples/image-jp2.png...
Extracted Text from examples/image-jp2.png:
自分で売った
喧嘩やろ
自分で
片つけんのが
筋ちゃうんか!
it
yourself!!!!
Detected language: ja
Translated Text from examples/image-jp2.png:
You started this fight, shouldn't you settle it yourself?!
Contributions are welcome! Please fork the repository and create a pull request. For major changes, please open an issue first to discuss what you'd like to change.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/new-feature
). - Commit your changes (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature/new-feature
). - Open a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.