[Python] How to correctly read numbers from a sudoku image using Tesseract in Python?

Discussão em 'Python' iniciado por Stack, Março 24, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to read numbers from a sudoku image using Tesseract in Python, but I am not getting accurate results. I have followed several tutorials and have set up Tesseract to detect individual digits, but the predictions are incorrect or inconsistent.

    The code works, but the detected numbers are not accurate. For example, in a cell containing the number “5”, Tesseract might predict “3” or “8”. I have tried adjusting the clipping margin and the Tesseract settings, but I have not been able to improve the accuracy.

    Here is the code I am using:

    import cv2
    import pytesseract
    import numpy as np

    sudoku_image = cv2.imread('sudoku.png', cv2.IMREAD_GRAYSCALE)

    tesseract_config = "--psm 10 --oem 3 -c tessedit_char_whitelist=123456789"

    rows, cols = 9, 9
    cell_size = sudoku_image.shape[1] // cols
    margin = 15

    sudoku_grid = np.zeros((rows, cols), dtype=int)

    for row in range(rows):
    for col in range(cols):
    x_start = col * cell_size + margin
    y_start = row * cell_size + margin
    x_end = (col + 1) * cell_size - margin
    y_end = (row + 1) * cell_size - margin

    cell_image = sudoku_image[y_start:y_end, x_start:x_end]

    detected_text = pytesseract.image_to_string(cell_image, config=tesseract_config)

    if detected_text.strip().isdigit():
    sudoku_grid[row, col] = int(detected_text.strip())

    print("Detected Sudoku Grid:")
    print(sudoku_grid)


    example of the sudoku image: sudoku

    Continue reading...

Compartilhe esta Página