-
Notifications
You must be signed in to change notification settings - Fork 0
/
img_processing.py
48 lines (37 loc) · 1.5 KB
/
img_processing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import cv2
import numpy as np
import pytesseract
import re
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
custom_tesseract_config = r'--psm 10 --oem 3 -c tessedit_char_whitelist=0123456789'
board = np.zeros((9, 9), dtype=int).tolist()
# this function process image nad return board
def img_processing(path):
img = cv2.imread(path, 0)
# processing image
img = cv2.resize(img, (900, 900))
img = cv2.adaptiveThreshold(
img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 9, 2)
edges = cv2.Canny(img, 100, 400)
# find lines in sudoku
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100,
minLineLength=100, maxLineGap=30)
# remove lines from sudoku
for i in range(len(lines)):
for x1, y1, x2, y2 in lines[i]:
cv2.line(img, (x1, y1), (x2, y2), (255, 255, 255), 12)
img = cv2.GaussianBlur(img, (9, 9), 0)
# run OCR for every square in sudoku:
img_height, img_weight = img.shape
for y in range(9):
for x in range(9):
# square is a section of photo with only 1 number shown
square = img[y*(img_height//9):(y+1)*(img_height//9),
x*(img_weight//9):(x+1)*(img_weight//9)]
num = pytesseract.image_to_string(
square, config=custom_tesseract_config)
num = re.findall('\d+', num)
if num:
board[y][x] = int(num[0])
cv2.imwrite('imgs/test.png', img)
return board