Skip to content

Commit 0d2c1e8

Browse files
committed
push for pelle
1 parent 5502573 commit 0d2c1e8

File tree

3 files changed

+65
-27
lines changed

3 files changed

+65
-27
lines changed

SudokuSolver.py

+53-27
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,18 @@ class SudokuSolver:
1010
def __init__(self):
1111
print('init')
1212
self.nc = NumberClassification()
13+
self.idx_c = 0
14+
self.data = []
15+
self.train_data = []
16+
self.label_data = []
1317
self.img_list = []
14-
1518
self.video_capture()
1619

1720
def video_capture(self):
1821
cap = cv2.VideoCapture(0)
1922
while(True):
2023
found = False
21-
a = raw_input('Ready to find a sudokuboard?')
24+
a = ''
2225

2326
# Capture frame-by-frame
2427
ret, orig_img = cap.read()
@@ -29,16 +32,16 @@ def video_capture(self):
2932
processed_img = self.preprocess_for_grid_detection(deepcopy(orig_img))
3033
box_points, board_img = self.find_sudoku_board(orig_img, processed_img)
3134
board_processed_img = self.preprocess_for_grid_detection(deepcopy(board_img))
32-
self.img_list.append(processed_img)
33-
self.img_list.append(board_img)
35+
#self.img_list.append(processed_img)
36+
#self.img_list.append(board_img)
3437

3538
''' We have a board_img '''
3639
if len(board_img > 0):
3740

3841
''' Computing hough lines in board '''
3942
# Find HoughLines, merges and return #
4043
merged_lines = self.hough_lines(deepcopy(board_img), board_processed_img)
41-
self.visualize_grid_lines(board_img, merged_lines)
44+
#self.visualize_grid_lines(board_img, merged_lines)
4245

4346
if len(merged_lines) == 20:
4447
print('Correct grid detected!')
@@ -50,15 +53,17 @@ def video_capture(self):
5053
# ''' We have a confirmed grid '''
5154
if mapped_grid is not None:
5255
#print('map_grid ->', len(mapped_grid))
53-
54-
prefilled = self.classify_cells_processed(board_processed_img, mapped_grid)
56+
data_to_save = []
57+
#prefilled, data_to_save = self.classify_cells_processed(board_processed_img, mapped_grid)
58+
data_to_save = self.classify_cells_processed(board_processed_img, mapped_grid)
5559

5660
# gray_board_img = cv2.cvtColor(board_img, cv2.COLOR_RGB2GRAY)
5761
# prefilled = self.classify_cells(gray_board_img, mapped_grid)
5862

59-
sudoku_to_solve = self.create_array_with_prefilled(prefilled)
60-
print(sudoku_to_solve)
63+
#sudoku_to_solve = self.create_array_with_prefilled(prefilled)
64+
#print(sudoku_to_solve)
6165
#self.solve_sudoku_board(sudoku_to_solve)
66+
self.save_data(data_to_save)
6267
found = True
6368

6469
''' --- Show --- '''
@@ -67,7 +72,7 @@ def video_capture(self):
6772
if cv2.waitKey(1) & 0xFF == ord('q') or a=='q':
6873
self.quit_program(cap)
6974
if found:
70-
a = raw_input('Sudokuboard found!')
75+
a = raw_input('.')
7176

7277

7378
def solve_sudoku_board(self, board_to_solve):
@@ -86,23 +91,23 @@ def create_array_with_prefilled(self, prefilled):
8691

8792
def classify_cells_processed(self, board_img, mapped_grid):
8893
cells = np.asarray(self.crop_grid(board_img, mapped_grid))
89-
cl_cells = []
90-
idx_list = []
91-
for idx, c in enumerate(cells):
92-
c[:4,:] = 0.0
93-
c[:,:4] = 0.0
94-
c[24:,:] = 0.0
95-
c[:,24:] = 0.0
96-
if np.sum(c)/784 > 8:
97-
cl_cells.append(c)
98-
idx_list.append(idx)
99-
self.img_list.append(c)
100-
print('to classify: ', len(cl_cells))
101-
pred = np.argmax(self.nc.classify_images(np.asarray(cl_cells)), axis=1)
102-
prefilled = []
103-
prefilled.append(pred)
104-
prefilled.append(idx_list)
105-
return prefilled
94+
#cl_cells = []
95+
#idx_list = []
96+
# for idx, c in enumerate(cells):
97+
# c[:4,:] = 0.0
98+
# c[:,:4] = 0.0
99+
# c[24:,:] = 0.0
100+
# c[:,24:] = 0.0
101+
# print(np.sum(c))
102+
# if np.sum(c)/784 > 8:
103+
# cl_cells.append(c)
104+
# idx_list.append(idx)
105+
# self.img_list.append(c)
106+
# pred = np.argmax(self.nc.classify_images(np.asarray(cl_cells)), axis=1)
107+
# prefilled = []
108+
# prefilled.append(pred)
109+
# prefilled.append(idx_list)
110+
return cells #prefilled, cl_cells
106111

107112
def classify_cells(self, board_img, mapped_grid):
108113
cells = np.asarray(self.crop_grid(board_img, mapped_grid))
@@ -415,6 +420,27 @@ def sobel(self, img):
415420
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=3)
416421
return np.sqrt(sobelx**2 + sobely**2).astype(np.uint8)
417422

423+
424+
def save_data(self, img_list):
425+
for idx, img in enumerate(img_list):
426+
cv2.imshow('img'+str(idx), img)
427+
if cv2.waitKey(1) & 0xFF == ord('q'):
428+
exit(0)
429+
inp = raw_input('Label? ')
430+
if inp == 's':
431+
print('Skip!')
432+
elif inp == 'save':
433+
self.data.append(self.train_data)
434+
self.data.append(self.label_data)
435+
np.save('training_data', self.data)
436+
exit(0)
437+
elif inp == '':
438+
print('Enter!')
439+
else:
440+
self.train_data.append(img)
441+
self.label_data.append(int(inp))
442+
cv2.destroyAllWindows()
443+
418444
def show_image(self, img):
419445
cv2.imshow('img', img)
420446
cv2.waitKey(0)

test.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import numpy as np
2+
import cv2
3+
4+
data = np.load('validation.npy')
5+
train, labels = data
6+
for i in range(0, len(labels)):
7+
print(train[i])
8+
cv2.imshow('img'+str(i), train[i])
9+
print('Label: ', labels[i])
10+
if cv2.waitKey(1) & 0xFF == ord('q'):
11+
exit(0)
12+
inp = raw_input('')

validation.npy

178 KB
Binary file not shown.

0 commit comments

Comments
 (0)