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

IndexError: boolean index did not match indexed array along dimension 2; dimension is 4 but corresponding boolean dimension is 3 #118

Closed
anilpatelia opened this issue May 28, 2024 · 4 comments

Comments

@anilpatelia
Copy link

When I try to run the following code

from PIL import Image
from surya.ocr import run_ocr
from surya.model.detection import segformer
from surya.model.recognition.model import load_model
from surya.model.recognition.processor import load_processor

IMAGE_PATH = "static\images\excerpt_text.png"
image = Image.open(IMAGE_PATH)
langs = ["en"] # Replace with your languages
det_processor, det_model = segformer.load_processor(), segformer.load_model()
rec_model, rec_processor = load_model(), load_processor()

predictions = run_ocr([image], [langs], det_model, det_processor, rec_model, rec_processor)

I am getting error :
IndexError Traceback (most recent call last)
Cell In[11], line 13
10 det_processor, det_model = segformer.load_processor(), segformer.load_model()
11 rec_model, rec_processor = load_model(), load_processor()
---> 13 predictions = run_ocr([image], [langs], det_model, det_processor, rec_model, rec_processor)

File ~\Desktop\GenAI\surya\surya\ocr.py:68, in run_ocr(images, langs, det_model, det_processor, rec_model, rec_processor, batch_size)
66 for idx, (det_pred, image, lang) in enumerate(zip(det_predictions, images, langs)):
67 polygons = [p.polygon for p in det_pred.bboxes]
---> 68 slices = slice_polys_from_image(image, polygons)
69 slice_map.append(len(slices))
70 all_langs.extend([lang] * len(slices))

File ~\Desktop\GenAI\surya\surya\input\processing.py:86, in slice_polys_from_image(image, polys)
84 lines = []
85 for idx, poly in enumerate(polys):
---> 86 lines.append(slice_and_pad_poly(image_array, poly))
87 return lines

File ~\Desktop\GenAI\surya\surya\input\processing.py:104, in slice_and_pad_poly(image_array, coordinates)
101 cv2.fillPoly(mask, [np.int32(coordinates)], 1)
102 mask = np.stack([mask] * 3, axis=-1)
--> 104 cropped_polygon[mask == 0] = settings.RECOGNITION_PAD_VALUE
105 rectangle_image = Image.fromarray(cropped_polygon)
107 return rectangle_image

IndexError: boolean index did not match indexed array along dimension 2; dimension is 4 but corresponding boolean dimension is 3

@VikParuchuri
Copy link
Owner

I cannot reproduce this. I ran the exact same code with the latest version, and it worked fine. Can you share the image you're using? Also verify that you're using the latest version of surya.

@VikParuchuri
Copy link
Owner

Actually, try image = image.convert("RGB") before you do run_ocr. I think your image is RGBA

@anilpatelia
Copy link
Author

anilpatelia commented May 28, 2024

@VikParuchuri
Thanks for the feedback.

It is working after adding image = image.convert("RGB") in the code.

@VikParuchuri
Copy link
Owner

Fixed this in the latest release

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