help me get the position of the different found text: import cv2 import pytesseract…
- User
help me get the position of the different found text:
import cv2 import pytesseract import re x,y,w,h = 0,0,0,0
Load image and convert to grayscale
image = cv2.imread('picture.png') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
text = pytesseract.image_to_string(gray) regions = pytesseract.image_to_boxes(gray)
print(text) name_search = re.search(r'Tovchnebaby|Tovchnèbaby|Tovchne\s*baby', text, re.I)
for region in regions.splitlines(): if name_search.group() in region: x, y, w, h = map(int, region.split()[1:]) break
print(name_search)
Get bounding box coords of name
Crop image horizontally at name position
crop = image[y:y+h, x+w:]
Extract damage number from cropped region
damage = pytesseract.image_to_string(crop) print(damage) damage_num = re.search(r'\d+', damage) if damage_num: print(int(damage_num.group())) else: print("No damage number found")
- Assistant
- User
right now it crops for each letter instead of the words
- Assistant
Apologies for the confusion earlier. The
image_to_boxesfunction actually generates bounding boxes for individual characters, not words. To get the bounding box of a word or a group of words, we should use theimage_to_datafunction instead, which provides detailed information including the bounding box of each word detected by Tesseract.