step by step , jpg file to text . good , but explore. using cloud services.
- Get link
- X
- Other Apps
Prompt
write java program , importin jpg files , converting ocr telugu language in single output file, which including paragraph ? provide libary files also
29 tokens
Response
import com.google.cloud.vision.v1.*;
import com.google.protobuf.ByteString;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class TeluguOCR {
// Replace with your Google Cloud Vision API key
private static final String API_KEY = "YOUR_API_KEY";
public static void main(String[] args) throws IOException {
// Input directory containing JPG files
String inputDir = "path/to/your/input/directory";
// Output file path
String outputFilePath = "path/to/your/output.txt";
// List of JPG files in the directory
List<String> imageFiles = new ArrayList<>();
Files.walk(Paths.get(inputDir))
.filter(Files::isRegularFile)
.filter(path -> path.toString().toLowerCase().endsWith(".jpg"))
.forEach(imageFiles::add);
// Initialize Vision API client
ImageAnnotatorClient vision = ImageAnnotatorClient.create();
// Create StringBuilder to store OCR text
StringBuilder ocrText = new StringBuilder();
// Process each image file
for (String imagePath : imageFiles) {
// Read image data
byte[] imageData = Files.readAllBytes(Paths.get(imagePath));
// Convert image data to ByteString
ByteString image = ByteString.copyFrom(imageData);
// Perform OCR on image
List<AnnotateImageResponse> responses =
vision.batchAnnotateImages(
AnnotateImageRequest.newBuilder()
.setImage(Image.newBuilder().setContent(image).build())
.addFeatures(
Feature.newBuilder()
.setType(Feature.Type.TEXT_DETECTION)
.build())
.build());
// Extract text from OCR response
AnnotateImageResponse response = responses.get(0);
for (TextAnnotation text : response.getTextAnnotationsList()) {
ocrText.append(text.getDescription()).append("\n");
}
}
// Write OCR text to output file
FileOutputStream outputStream = new FileOutputStream(outputFilePath);
outputStream.write(ocrText.toString().getBytes());
outputStream.close();
// Close Vision API client
vision.close();
System.out.println("OCR complete. Output saved to " + outputFilePath);
}
}
Required Libraries:
- google-cloud-vision: Provides the Google Cloud Vision API client for OCR operations.
# Use Maven to add the dependency
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-vision</artifactId>
<version>1.98.1</version>
</dependency>
# Or use Gradle
dependencies {
implementation 'com.google.cloud:google-cloud-vision:1.98.1'
}
Explanation:
- Import Libraries: Imports necessary classes for Google Cloud Vision API and file handling.
- API Key: Replace
YOUR_API_KEY
with your actual Google Cloud Vision API key. You can obtain it from the Google Cloud Console. - Input and Output:
inputDir
: Path to the directory containing the JPG files.outputFilePath
: Path where you want to save the OCR text file.
- Image File Listing: Gets a list of all JPG files from the specified directory.
- Vision API Client: Creates an instance of
ImageAnnotatorClient
to access the Vision API. - OCR Processing Loop:
- Reads Image Data: Reads each JPG file into a byte array.
- Create ByteString: Converts the image data to a
ByteString
for sending to the Vision API. - Perform OCR: Sends an
AnnotateImageRequest
withTEXT_DETECTION
feature to the API. - Extract Text: Processes the OCR response and extracts the detected text.
- Append Text to StringBuilder: Appends each OCR result to a
StringBuilder
to build the complete text.
- Write to File: Writes the OCR text from the
StringBuilder
to the specified output file. - Close Connections: Closes the Vision API client and the file output stream.
Notes:
- This code assumes that your Google Cloud Vision API is properly configured and that you have the necessary permissions.
- You need to enable the Cloud Vision API and create a service account to get your API key.
- Make sure the input directory contains JPG images only and the output file is writable.
To Run the Program:
- Create a Java project in your IDE.
- Add the required libraries using Maven or Gradle as explained above.
- Replace the placeholders for
API_KEY
,inputDir
, andoutputFilePath
with your actual values. - Run the Java program.
This program will iterate through the input JPG files, perform OCR on each image using the Google Cloud Vision API, and write all the detected Telugu text into a single output file, preserving paragraph breaks.
- Get link
- X
- Other Apps