@@ -10,15 +10,18 @@ class SudokuSolver:
10
10
def __init__ (self ):
11
11
print ('init' )
12
12
self .nc = NumberClassification ()
13
+ self .idx_c = 0
14
+ self .data = []
15
+ self .train_data = []
16
+ self .label_data = []
13
17
self .img_list = []
14
-
15
18
self .video_capture ()
16
19
17
20
def video_capture (self ):
18
21
cap = cv2 .VideoCapture (0 )
19
22
while (True ):
20
23
found = False
21
- a = raw_input ( 'Ready to find a sudokuboard?' )
24
+ a = ''
22
25
23
26
# Capture frame-by-frame
24
27
ret , orig_img = cap .read ()
@@ -29,16 +32,16 @@ def video_capture(self):
29
32
processed_img = self .preprocess_for_grid_detection (deepcopy (orig_img ))
30
33
box_points , board_img = self .find_sudoku_board (orig_img , processed_img )
31
34
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)
34
37
35
38
''' We have a board_img '''
36
39
if len (board_img > 0 ):
37
40
38
41
''' Computing hough lines in board '''
39
42
# Find HoughLines, merges and return #
40
43
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)
42
45
43
46
if len (merged_lines ) == 20 :
44
47
print ('Correct grid detected!' )
@@ -50,15 +53,17 @@ def video_capture(self):
50
53
# ''' We have a confirmed grid '''
51
54
if mapped_grid is not None :
52
55
#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 )
55
59
56
60
# gray_board_img = cv2.cvtColor(board_img, cv2.COLOR_RGB2GRAY)
57
61
# prefilled = self.classify_cells(gray_board_img, mapped_grid)
58
62
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)
61
65
#self.solve_sudoku_board(sudoku_to_solve)
66
+ self .save_data (data_to_save )
62
67
found = True
63
68
64
69
''' --- Show --- '''
@@ -67,7 +72,7 @@ def video_capture(self):
67
72
if cv2 .waitKey (1 ) & 0xFF == ord ('q' ) or a == 'q' :
68
73
self .quit_program (cap )
69
74
if found :
70
- a = raw_input ('Sudokuboard found! ' )
75
+ a = raw_input ('. ' )
71
76
72
77
73
78
def solve_sudoku_board (self , board_to_solve ):
@@ -86,23 +91,23 @@ def create_array_with_prefilled(self, prefilled):
86
91
87
92
def classify_cells_processed (self , board_img , mapped_grid ):
88
93
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
106
111
107
112
def classify_cells (self , board_img , mapped_grid ):
108
113
cells = np .asarray (self .crop_grid (board_img , mapped_grid ))
@@ -415,6 +420,27 @@ def sobel(self, img):
415
420
sobely = cv2 .Sobel (img ,cv2 .CV_64F ,0 ,1 ,ksize = 3 )
416
421
return np .sqrt (sobelx ** 2 + sobely ** 2 ).astype (np .uint8 )
417
422
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
+
418
444
def show_image (self , img ):
419
445
cv2 .imshow ('img' , img )
420
446
cv2 .waitKey (0 )
0 commit comments