step by step , jpg file to text . good , but explore. using cloud services.

 

Prompt

write java program , importin jpg files , converting ocr telugu language in single output file, which including paragraph ? provide libary files also

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:

  1. Import Libraries: Imports necessary classes for Google Cloud Vision API and file handling.
  2. API Key: Replace YOUR_API_KEY with your actual Google Cloud Vision API key. You can obtain it from the Google Cloud Console.
  3. Input and Output:
    • inputDir: Path to the directory containing the JPG files.
    • outputFilePath: Path where you want to save the OCR text file.
  4. Image File Listing: Gets a list of all JPG files from the specified directory.
  5. Vision API Client: Creates an instance of ImageAnnotatorClient to access the Vision API.
  6. 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 with TEXT_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.
  7. Write to File: Writes the OCR text from the StringBuilder to the specified output file.
  8. 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:

  1. Create a Java project in your IDE.
  2. Add the required libraries using Maven or Gradle as explained above.
  3. Replace the placeholders for API_KEYinputDir, and outputFilePath with your actual values.
  4. 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.

Popular posts from this blog

pss book : శ్రీకృష్ణుడు దేవుడా, భగవంతుడా completed , second review needed. 26th April 2024

pss book: గురు ప్రార్థనామంజరి . completed 21st july 2024

pss book: కధల జ్ఞానము read review pending. 25th june 2024