[Python] Pytesseract OCR not detecting numbers with less than 3 digits

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024.

  1. Stack

    Stack Membro Participativo

    I have a python script that captures a segment of the screen and reads what the text value of it is and converts the text to only numerical characters returns it. My problem is that pytesseract only reads numbers that have digits that are 3 or greater and never 2 or lower.

    I captured a small segment of my screen and had pytesseract read and extract text from it, but this only works when there are 3 or more digits.

    Sample Image:Captured segment of my screen

    import cv2
    import numpy as np
    import pytesseract
    import time
    import pydirectinput
    import pygetwindow as gw

    from mss import mss
    import re

    def filter_numbers(text):
    filtered_text = re.sub(r'\D', '', text)
    return filtered_text

    topMost = 1005
    Pheight = 45
    Pwidth = 55
    leftMost = 170

    useButtonCoordinateX, useButtonCoordinateY = 403, 726

    pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'


    def capture_screen_and_read_text():

    region = {'top': topMost, 'left': leftMost, 'width': Pwidth, 'height': Pheight}
    time.sleep(2)
    with mss() as sct:
    screen = sct.grab(region)

    img = np.array(screen)

    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    _, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)

    resized_img = cv2.resize(thresh, None, fx=2, fy=2, interpolation=cv2.INTER_LINEAR)
    cv2.imshow('Screen Capture', resized_img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    text = pytesseract.image_to_string(resized_img)
    return text

    capture_screen_and_read_text()

    Continue reading...

Compartilhe esta Página