Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCR on bounding boxes of an image #1564

Open
cometta opened this issue May 24, 2024 · 1 comment
Open

OCR on bounding boxes of an image #1564

cometta opened this issue May 24, 2024 · 1 comment

Comments

@cometta
Copy link

cometta commented May 24, 2024

I want to know if it's possible to input multiple bounding boxes and have TrOCR perform OCR only on those specified areas of my image. Could you please advise on this?

@maifeeulasad
Copy link

I don't think TrOcr for that. All you need is a utility function with opencv or some other preferred library, like this:

import cv2

def crop_images_from_bounding_boxes(image, bounding_boxes):
    """
    Crops images from the original image based on the provided bounding boxes.
    
    Parameters:
    image (numpy.ndarray): The original image.
    bounding_boxes (list of tuples): A list of bounding boxes, where each bounding box is represented by a tuple
                                     (x, y, width, height).
    
    Returns:
    list of numpy.ndarray: A list of cropped images.
    """
    cropped_images = []
    
    for (x, y, w, h) in bounding_boxes:
        cropped_image = image[y:y+h, x:x+w]
        cropped_images.append(cropped_image)
    
    return cropped_images

If you are worried about the performance, be assured that as the images are cropped so it will be faster, at least a bit. And you can you use multi-threading for this, if you don't have a GPU. If you have a GPU, this will be fast as hell.

I hope this answers your question.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants