最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ocr - How to correctly read numbers from a sudoku image using Tesseract in Python? - Stack Overflow

programmeradmin5浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论