├── README.md ├── imageGCP.png ├── image_processing.py └── screenshots ├── 1.png ├── 10.png ├── 11.png ├── 12.png ├── 13.png ├── 14.png ├── 2.png ├── 3.png ├── 4.png ├── 5.png ├── 6.png ├── 7.png ├── 8.png ├── 9.png └── test.txt /README.md: -------------------------------------------------------------------------------- 1 | # Image Processing Application using Google Cloud Vision API 2 | 3 | This repository contains instructions and code for creating an image processing application using the Cloud Vision API in Google Cloud. This application allows you to extract text from images and save it to a text file. 4 | 5 | ## Getting Started 6 | 7 | Follow these steps to set up and run the application: 8 | 9 | ### Step 1: Creating a Project in Google Cloud 10 | 11 | 1. Go to the [Google Cloud Console](https://console.cloud.google.com/). 12 | 2. Create a new project or select an existing one. Click on the "Select a project" button and choose "New Project." Provide a project name and select an organization (if necessary), then click "Create." 13 | 14 | ### Step 2: Enabling the Cloud Vision API 15 | 16 | 1. Enable the Cloud Vision API for your project. To do this, go to the Google Cloud Console, select your project, then navigate to the "APIs & Services" > "Library" menu. In the search bar, enter "Cloud Vision API," then click "Enable." 17 | Screenshot 1 18 | Screenshot 2 19 | Screenshot 3 20 | 21 | 22 | ### Step 3: Creating a Service Account and Key 23 | 24 | 1. In the left sidebar, select "APIs & Services" > "Credentials." 25 | Screenshot 4 26 | 27 | 2. Click "Create credentials" and select "Service Account Key." 28 | Screenshot 5 29 | Screenshot 6 30 | 31 | 3. Fill in the necessary information to create the service account and assign the "VisionAI Admin" role to this account. 32 | Screenshot 7 33 | 34 | 4. Choose "JSON" as the key format and click "Create." The JSON key file will be automatically downloaded to your computer 35 | Screenshot 8 36 | Screenshot 9 37 | Screenshot 10 38 | 39 | 40 | ### Step 4: Setting an Environment Variable 41 | 42 | 1. Open the command prompt (Command Prompt) or PowerShell. 43 | 2. Set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable by specifying the path to the JSON key file you downloaded: 44 | 45 | In the command prompt: 46 | 47 | ```bash 48 | set GOOGLE_APPLICATION_CREDENTIALS=path\to\your\service-account-key.json 49 | ``` 50 | 51 | In PowerShell: 52 | 53 | ```powershell 54 | $env:GOOGLE_APPLICATION_CREDENTIALS="path\to\your\service-account-key.json" 55 | ``` 56 | 57 | ### Step 5: Creating a Python Application 58 | 59 | 1. Open a text editor on your computer, such as Notepad, Visual Studio Code, or another. 60 | 2. Create a new file with a `.py` extension. For example, you can name it `image_processing.py`. 61 | 3. You can view the code with comments in the provided Python files. 62 | 63 | ### Step 6: Running the Application 64 | 65 | 1. Navigate to the Command Prompt/PowerShell in the directory where your `.py` file is located. 66 | 2. Execute the following command to run your application: 67 | 68 | ```bash 69 | python image_processing.py 70 | ``` 71 | 72 | After running this command, your application will process the image and write the recognized text to the `output.txt` file. 73 | 74 | ## Result 75 | 76 | This is what the result files look like: 77 | Screenshot 11 78 | 79 | - Take a screenshot and save it with the name "imageGCP.png." 80 | Screenshot 12 81 | 82 | - After running `image_processing.py`, a text file named `output.txt` will appear in the folder. 83 | Screenshot 13 84 | 85 | For example, I have my lab report with code presented as an image in a PDF file. I can take a screenshot of the PDF file, specifically of that image, and extract text from it: 86 | 87 | Screenshot 14 88 | 89 | ### Note 90 | 91 | While this feature is useful, it's important to double-check the extracted text. In some cases, a few characters may be missed, and special characters like underscores might not be recognized. However, it can still save a significant amount of time when dealing with image-based text extraction tasks. 92 | 93 | Feel free to contribute to this repository or provide feedback on its functionality. 94 | 95 | Enjoy using the Google Cloud Vision API for image processing! 96 | -------------------------------------------------------------------------------- /imageGCP.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvulcu/GCP-Image-Processing-App/0460074c120a32102c3f623e3931c6e6319107fa/imageGCP.png -------------------------------------------------------------------------------- /image_processing.py: -------------------------------------------------------------------------------- 1 | # Import necessary libraries 2 | from google.cloud import vision_v1 3 | import io 4 | 5 | 6 | # Define a function to detect text in an image and save it to an output file 7 | def detect_text(image_path, output_file_path): 8 | # Create a client for the Google Cloud Vision API 9 | client = vision_v1.ImageAnnotatorClient() 10 | 11 | # Open the image file in binary read mode 12 | with io.open(image_path, 'rb') as image_file: 13 | content = image_file.read() 14 | 15 | # Create an Image object from the image content 16 | image = vision_v1.Image(content=content) 17 | 18 | # Perform text detection on the image 19 | response = client.text_detection(image=image) 20 | texts = response.text_annotations 21 | 22 | # Check if text was detected in the image 23 | if texts: 24 | # Get the first (most prominent) detected text and remove leading/trailing whitespace 25 | detected_text = texts[0].description.strip() 26 | 27 | # Write the detected text to the output file 28 | with open(output_file_path, 'w', encoding='utf-8') as output_file: 29 | output_file.write(detected_text) 30 | 31 | # Print the detected text to the console 32 | print(f'Detected text: {detected_text}') 33 | else: 34 | # If no text was detected, print a message 35 | print('No text detected on the image.') 36 | 37 | 38 | if __name__ == '__main__': 39 | # Define the paths to the input image and the output text file 40 | image_path = 'path_to_your_image.png' 41 | output_file_path = 'output.txt' 42 | 43 | # Call the detect_text function with the specified image and output file paths 44 | detect_text(image_path, output_file_path) 45 | -------------------------------------------------------------------------------- /screenshots/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvulcu/GCP-Image-Processing-App/0460074c120a32102c3f623e3931c6e6319107fa/screenshots/1.png -------------------------------------------------------------------------------- /screenshots/10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvulcu/GCP-Image-Processing-App/0460074c120a32102c3f623e3931c6e6319107fa/screenshots/10.png -------------------------------------------------------------------------------- /screenshots/11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvulcu/GCP-Image-Processing-App/0460074c120a32102c3f623e3931c6e6319107fa/screenshots/11.png -------------------------------------------------------------------------------- /screenshots/12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvulcu/GCP-Image-Processing-App/0460074c120a32102c3f623e3931c6e6319107fa/screenshots/12.png -------------------------------------------------------------------------------- /screenshots/13.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvulcu/GCP-Image-Processing-App/0460074c120a32102c3f623e3931c6e6319107fa/screenshots/13.png -------------------------------------------------------------------------------- /screenshots/14.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvulcu/GCP-Image-Processing-App/0460074c120a32102c3f623e3931c6e6319107fa/screenshots/14.png -------------------------------------------------------------------------------- /screenshots/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvulcu/GCP-Image-Processing-App/0460074c120a32102c3f623e3931c6e6319107fa/screenshots/2.png -------------------------------------------------------------------------------- /screenshots/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvulcu/GCP-Image-Processing-App/0460074c120a32102c3f623e3931c6e6319107fa/screenshots/3.png -------------------------------------------------------------------------------- /screenshots/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvulcu/GCP-Image-Processing-App/0460074c120a32102c3f623e3931c6e6319107fa/screenshots/4.png -------------------------------------------------------------------------------- /screenshots/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvulcu/GCP-Image-Processing-App/0460074c120a32102c3f623e3931c6e6319107fa/screenshots/5.png -------------------------------------------------------------------------------- /screenshots/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvulcu/GCP-Image-Processing-App/0460074c120a32102c3f623e3931c6e6319107fa/screenshots/6.png -------------------------------------------------------------------------------- /screenshots/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvulcu/GCP-Image-Processing-App/0460074c120a32102c3f623e3931c6e6319107fa/screenshots/7.png -------------------------------------------------------------------------------- /screenshots/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvulcu/GCP-Image-Processing-App/0460074c120a32102c3f623e3931c6e6319107fa/screenshots/8.png -------------------------------------------------------------------------------- /screenshots/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mvulcu/GCP-Image-Processing-App/0460074c120a32102c3f623e3931c6e6319107fa/screenshots/9.png -------------------------------------------------------------------------------- /screenshots/test.txt: -------------------------------------------------------------------------------- 1 | 2 | --------------------------------------------------------------------------------