简体   繁体   中英

How to find out all the numbers and the positions of them in an image with Python?

In such a game board, I want to detect all the numbers, together with the positions of these numbers. Any ideas about how to do it?

Do there exist any libraries in Python which I can import to recognize these numbers, together with the positions of them on such a game board?

Game Board在此处输入图像描述

Some results are missing here but you can start with following approach:

  1. Crop image to "cells"
  2. Recognize each "cell" with tesseract :

Code:

import pytesseract
from PIL import Image

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

img = Image.open('image.png')
for x, ltr in enumerate('abcdefghijklmno'):
    for y, nbr in enumerate(range(15, 0, -1)):
        x_start = 20+x*30
        y_start = 20+y*30
        img_crop = img.crop((x_start, y_start, x_start+20, y_start+20))
        res = pytesseract.image_to_string(img_crop).replace('\f', '').replace('\n', '')
        if res:
            print(f'{ltr}{nbr} - {res}')

Output:

b9 - 39
c7 - 44
c6 - 22
d9 - 37
f11 - 31
f10 - 42
f4 - 24
f3 - 23
g11 - 20
h11 - 26
i12 - 25
j11 - 27
j9 - 21
k10 - 28

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM