-
Notifications
You must be signed in to change notification settings - Fork 30
/
sudoku.py
63 lines (56 loc) · 1.71 KB
/
sudoku.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import cv2
import sys
from time import time
import matplotlib.pyplot as plt
from SudokuExtractor import extract_sudoku
from NumberExtractor import extract_number
from SolveSudoku import sudoku_solver
def output(a):
sys.stdout.write(str(a))
def display_sudoku(sudoku):
for i in range(9):
for j in range(9):
cell = sudoku[i][j]
if cell == 0 or isinstance(cell, set):
output('.')
else:
output(cell)
if (j + 1) % 3 == 0 and j < 8:
output(' |')
if j != 8:
output(' ')
output('\n')
if (i + 1) % 3 == 0 and i < 8:
output("--------+----------+---------\n")
def show_image(image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
plt.imshow(image)
plt.show()
def main(image_path):
image = extract_sudoku(image_path)
# show_image(image)
grid = extract_number(image)
print('Sudoku:')
display_sudoku(grid.tolist())
solution = sudoku_solver(grid)
print('Solution:')
# print(solution)
display_sudoku(solution.tolist())
def convert_sec_to_hms(seconds):
seconds = seconds % (24 * 3600)
hour = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
return "%d:%02d:%08d" % (hour, minutes, seconds)
if __name__ == '__main__':
# image_path = 'images/sudoku.jpg'
# main(image_path)
try:
start_time = time()
main(image_path = sys.argv[1])
print("TAT: ", round(time() - start_time, 3))
except: # except IndexError:
fmt = 'usage: {} image_path'
print(fmt.format(__file__.split('/')[-1]))
print('[ERROR]: Image not found')