0
import cv2
import pytesseract

img = cv2.imread(filename)
boxes = pytesseract.image_to_boxes(img).split("\n")

This gives me the bounding boxes for each character like so 'r 134 855 148 871 0` and also does not include the space character. I need the bounding boxes for each line, where a line is a group character who's bounding box intersects the same horizontal line.

So I require something like:

boxes = image_to_line_boxes(img)

where boxes is a list like something [("Hello, this is the first line!", "134 855 148 871 0"), ("This is the second line", "264 816 288 832 0")]

1 Answers1

0
import os
import cv2
import pytesseract

img = cv2.imread(filename)
lines = pytesseract.image_to_string(img).split(os.linesep)

The image_to_string function returns the entire text of the image as a single string

  • This only separates the lines, and does not give me their bounding boxes. – RandomPersonOnline Jul 25 '22 at 09:46
  • @RandomPersonOnline Ah, my bad, didn't notice that part. Unfortunately, there is no short method or function to get bounding boxes of the entire line yet. You could use OpenCV operations to find just the bounding rectangle of a line. Or maybe you could group the words returned by `image_to_boxes` based on some criteria such as Y-coordinate. See [this question](https://stackoverflow.com/questions/55406993/how-to-get-confidence-of-each-line-using-pytesseract) for a more detailed explanation involving `image_to_data`. – Kaushik Narayan Jul 25 '22 at 15:26