@@ -17,47 +17,47 @@ def __init__(self):
17
17
def video_capture (self ):
18
18
cap = cv2 .VideoCapture (0 )
19
19
while (True ):
20
- a = raw_input ( '.' )
20
+ a = 'a'
21
21
22
22
# Capture frame-by-frame
23
23
ret , orig_img = cap .read ()
24
24
self .img_list .append (orig_img )
25
25
26
26
''' Finding board, return contour and coordinates to box'''
27
- box_points , contour_img , processed_img = self .find_sudoku_board (deepcopy (orig_img ))
27
+ processed_img = self .canny_edge_detector (deepcopy (orig_img ))
28
+ box_points , contour_img , processed_img = self .find_sudoku_board (orig_img , processed_img )
28
29
board_img = self .crop_image (orig_img , box_points )
29
30
gray_board_img = cv2 .cvtColor (board_img , cv2 .COLOR_RGB2GRAY )
30
31
board_processed_img = self .crop_image (processed_img , box_points )
31
- self .img_list .append (board_processed_img )
32
+ #self.img_list.append(board_processed_img)
33
+ self .img_list .append (processed_img )
32
34
33
35
''' We have a board_img '''
34
- if len (board_img > 0 ):
35
-
36
- ''' Computing hough lines in board '''
37
- # Find HoughLines, merges and return #
38
- merged_lines = self .hough_lines (deepcopy (board_img ), board_processed_img )
39
- self .img_list .append (board_img )
40
- if len (merged_lines ) > 0 :
41
- print (merged_lines )
42
- print ('lines ->' , len (merged_lines ))
43
- self .visualize_grid_lines (board_img , merged_lines )
44
- if len (merged_lines ) == 20 :
45
- print ('Correct grid detected!' )
46
- #self.visualize_grid(board_img, merged_lines)
47
- # Extract grid coordinates #
48
- grid_points = self .extract_grid (board_img , merged_lines )
49
- # # Maps the grid points to cells #
50
- mapped_grid = self .map_grid (board_img , grid_points )
51
- # ''' We have a confirmed grid '''
52
- print (mapped_grid )
53
- if mapped_grid is not None :
54
- print ('map_grid ->' , len (mapped_grid ))
55
- prefilled = self .classify_cells (gray_board_img , mapped_grid )
56
- sudoku_to_solve = self .create_array_with_prefilled (prefilled )
57
- print (sudoku_to_solve )
58
- self .solve_sudoku_board (sudoku_to_solve )
59
-
60
-
36
+ # if len(board_img > 0):
37
+ #
38
+ # ''' Computing hough lines in board '''
39
+ # # Find HoughLines, merges and return #
40
+ # merged_lines = self.hough_lines(deepcopy(board_img), board_processed_img)
41
+ # # if len(merged_lines) > 0:
42
+ # # print(merged_lines)
43
+ # # print('lines ->', len(merged_lines))
44
+ # self.visualize_grid_lines(board_img, merged_lines)
45
+ # if len(merged_lines) == 20:
46
+ # print('Correct grid detected!')
47
+ # #self.visualize_grid(board_img, merged_lines)
48
+ # # Extract grid coordinates #
49
+ # grid_points = self.extract_grid(board_img, merged_lines)
50
+ # # # Maps the grid points to cells #
51
+ # mapped_grid = self.map_grid(board_img, grid_points)
52
+ # # ''' We have a confirmed grid '''
53
+ # if mapped_grid is not None:
54
+ # #print('map_grid ->', len(mapped_grid))
55
+ # prefilled = self.classify_cells(gray_board_img, mapped_grid)
56
+ # sudoku_to_solve = self.create_array_with_prefilled(prefilled)
57
+ # print(sudoku_to_solve)
58
+ # # print(sudoku_to_solve)
59
+ # # self.solve_sudoku_board(sudoku_to_solve)
60
+ # a = raw_input('.')
61
61
62
62
''' --- Show --- '''
63
63
self .display_images ()
@@ -67,7 +67,7 @@ def video_capture(self):
67
67
68
68
69
69
def solve_sudoku_board (self , board_to_solve ):
70
- b = Board (array , 9 ,3 ,3 )
70
+ b = Board (board_to_solve , 9 ,3 ,3 )
71
71
b .createBoard ()
72
72
solved_board = b .solveBoard ()
73
73
print (solved_board )
@@ -77,7 +77,7 @@ def create_array_with_prefilled(self, prefilled):
77
77
sudoku_to_solve = np .zeros (81 )
78
78
for i in range (0 , len (prefilled [0 ])):
79
79
sudoku_to_solve [prefilled [1 ][i ]] = prefilled [0 ][i ]
80
- return np .reshape (sudoku_to_solve , (9 ,9 ))
80
+ return np .reshape (sudoku_to_solve , (9 ,9 )). astype ( int )
81
81
82
82
def classify_cells (self , board_img , mapped_grid ):
83
83
cells = np .asarray (self .crop_grid (board_img , mapped_grid ))
@@ -86,30 +86,32 @@ def classify_cells(self, board_img, mapped_grid):
86
86
idx_list = []
87
87
print ('Success' )
88
88
for idx , c in enumerate (cells ):
89
- self .img_list .append (c )
90
- c [c < 0.5 ] = 0.0
89
+ c [c < 0.6 ] = 0.0 #Threshhold works for now#
91
90
c [:5 ,:] = 0.0
92
91
c [:,:5 ] = 0.0
93
92
c [25 :,:] = 0.0
94
93
c [:,25 :] = 0.0
95
- c = c * 1.4
96
- print (np .sum (c [5 :20 ,10 :18 ]))
94
+ c = c * 1.2
95
+ if idx == 0 :
96
+ print (c )
97
+ print (np .sum (c ))
97
98
if np .sum (c [5 :20 ,10 :18 ]) > 10.0 :
98
99
cl_cells .append (c )
99
100
idx_list .append (idx )
101
+ self .img_list .append (c )
102
+ print ('to classify: ' , len (cl_cells ))
100
103
pred = np .argmax (self .nc .classify_images (np .asarray (cl_cells )), axis = 1 )
101
- y_label = [2 , 5 , 8 , 4 , 3 , 3 , 6 , 1 , 9 , 2 , 7 , 1 ]
102
- for i in range (0 , len (pred )):
103
- print (y_label [i ], pred [i ])
104
+ # y_label = [2, 5, 8, 4, 3, 8 , 6, 1, 9, 8 , 7, 1]
105
+ # for i in range(0, len(pred)):
106
+ # print(y_label[i], pred[i])
104
107
prefilled = []
105
108
prefilled .append (pred )
106
109
prefilled .append (idx_list )
107
- print ('pred -> ' , pred )
108
- print ('idx list -> ' , idx_list )
110
+ # print('pred -> ', pred)
111
+ # print('idx list -> ', idx_list)
109
112
return prefilled
110
113
111
- def find_sudoku_board (self , orig_img ):
112
- processed_img = self .canny_edge_detector (orig_img )
114
+ def find_sudoku_board (self , orig_img , processed_img ):
113
115
#gray_board_img = cv2.cvtColor(orig_img, cv2.COLOR_BGR2GRAY)
114
116
#processed_img = self.preprocess_for_grid_detection(gray_board_img)
115
117
contour = self .find_contours (processed_img )
@@ -139,10 +141,8 @@ def map_grid(self, img, grid_points):
139
141
width = img .shape [0 ]
140
142
height = img .shape [1 ]
141
143
mapped_grid = None
142
- print ('grid_points before -> ' , len (grid_points ))
143
144
grid_points = self .cleanup_grid_points (grid_points )
144
145
grid_points = sorted (grid_points ,key = lambda x : (x [1 ],x [0 ]))
145
- print ('grid_points -> ' , len (grid_points ))
146
146
if len (grid_points ) == 100 : # Only 9x9 sudokuboard
147
147
grid_points = np .asarray (grid_points ).reshape ((10 ,10 ,2 ))
148
148
mapped_grid = np .zeros_like (grid_points )
@@ -320,7 +320,7 @@ def add_to_list(rho, theta):
320
320
y2 = int (y0 - 1000 * (a ))
321
321
if x < 3 or x > 18 :
322
322
cv2 .line (orig_img , (x1 , y1 ), (x2 , y2 ), (0 ,255 ,0 ), 1 )
323
- if f_theta > 3.0 or f_rho == 0.0 : continue
323
+ if f_theta > 3.0 : continue # or f_rho == 0.0: continue
324
324
add_to_list (f_rho , f_theta )
325
325
self .img_list .append (orig_img )
326
326
return sorted (point_list ,key = lambda x : x [0 ])
@@ -371,8 +371,9 @@ def harris_corner_detection(self, gray_img):
371
371
372
372
def canny_edge_detector (self , img ):
373
373
apertureSize = 3
374
- canny_img = cv2 .Canny (img , 150 , 250 , apertureSize )
375
- canny_img = cv2 .dilate (canny_img , (13 ,13 ))
374
+ canny_img = cv2 .Canny (img , 100 , 150 , apertureSize )
375
+ canny_img = cv2 .dilate (canny_img , kernel = (3 ,3 ), iterations = 2 )
376
+ #canny_img = cv2.erode(canny_img, (33,33))
376
377
return canny_img
377
378
378
379
def sobel (self , img ):
0 commit comments