Skip to content

Commit 056884a

Browse files
committed
cmit
1 parent c73923b commit 056884a

6 files changed

+93
-72
lines changed

Board.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ def createBoard(self):
4242
self.lastSquare = None
4343
for x in range(0,self.length):
4444
for y in range(0,self.length):
45-
4645
if self.sudokuarray[x][y] == 0:
4746
self.squareNumber += 1
4847
self.newSquare = EmptySquare(self.row[x], self.column[y], self.box[self.countBox-1], self.length, 0,self)
@@ -76,6 +75,15 @@ def createBoard(self):
7675

7776

7877

78+
# array = np.array([[2, 0, 0, 0, 0, 5, 0, 0, 8],
79+
# [4, 0, 0, 0, 0, 0, 0, 4, 0],
80+
# [0, 0, 3, 0, 0, 0, 0, 5, 0],
81+
# [0, 0, 8, 0, 0, 0, 0, 0, 0],
82+
# [0, 0, 0, 0, 0, 6, 0, 0, 0],
83+
# [0, 0, 1, 0, 0, 0, 8, 0, 0],
84+
# [8, 0, 0, 0, 0, 0, 0, 0, 0],
85+
# [7, 0, 0, 0, 0, 0, 0, 0, 0],
86+
# [0, 0, 0, 0, 0, 1, 0, 0, 0]])
7987
# array = np.array([[7,0,0,8,0,0,0,0,0], [9,0,0,0,0,0,0,6,0], [0,6,0,2,0,1,5,0,0],
8088
# [0,0,0,0,4,0,8,0,0], [0,0,7,0,0,0,3,0,0], [4,0,0,0,2,5,0,0,7],
8189
# [0,4,6,0,0,0,0,8,3], [0,0,3,5,0,0,0,0,0], [0,0,0,0,9,0,0,2,0]])

CNN_MNIST.py

+35-23
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
dir = os.path.dirname(os.path.realpath(__file__))
1515

1616
# Parameters for training
17-
learning_rate = 0.001
17+
learning_rate = 1e-4
1818
training_iters = 50000
1919
batch_size = 128
2020

@@ -108,7 +108,7 @@ def conv_net(x, weights, biases, dropout):
108108
# Define loss and optimizer
109109
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
110110
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
111-
111+
rmsprop = tf.train.RMSPropOptimizer(learning_rate=learning_rate).minimize(cost)
112112
# Evaluate model
113113
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
114114
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
@@ -120,28 +120,40 @@ def conv_net(x, weights, biases, dropout):
120120
with tf.Session() as sess:
121121
sess.run(init)
122122
step = 1
123-
# Keep training until reach max iterations
124-
while step * batch_size < training_iters:
123+
124+
''' Alternative Method '''
125+
for i in range(15000):
125126
batch_x, batch_y = mnist.train.next_batch(batch_size)
126-
# Run optimization op (backprop)
127-
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y,
128-
keep_prob: dropout})
129-
if step % 10 == 0:
130-
# Calculate batch loss and accuracy
131-
loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
132-
y: batch_y,
133-
keep_prob: 1.})
134-
print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
135-
"{:.6f}".format(loss) + ", Training Accuracy= " + \
136-
"{:.5f}".format(acc))
137-
step += 1
138-
print("Optimization Finished!")
139-
140-
# Calculate accuracy for 256 mnist test images
141-
print("Testing Accuracy:", \
142-
sess.run(accuracy, feed_dict={x: mnist.test.images[:256],
143-
y: mnist.test.labels[:256],
144-
keep_prob: 1.}))
127+
if i%100 == 0:
128+
train_accuracy = accuracy.eval(feed_dict={
129+
x: batch_x, y: batch_y, keep_prob: 1.0})
130+
print("step %d, training accuracy %g"%(i, train_accuracy))
131+
rmsprop.run(feed_dict={x: batch_x, y: batch_y, keep_prob: 0.5})
132+
133+
print("test accuracy %g"%accuracy.eval(feed_dict={x: mnist.test.images, y: mnist.test.labels, keep_prob: 1.0}))
134+
135+
''' First training Method '''
136+
# while step * batch_size < training_iters:
137+
# batch_x, batch_y = mnist.train.next_batch(batch_size)
138+
# # Run optimization op (backprop)
139+
# sess.run(optimizer, feed_dict={x: batch_x, y: batch_y,
140+
# keep_prob: dropout})
141+
# if step % 10 == 0:
142+
# # Calculate batch loss and accuracy
143+
# loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
144+
# y: batch_y,
145+
# keep_prob: 1.})
146+
# print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
147+
# "{:.6f}".format(loss) + ", Training Accuracy= " + \
148+
# "{:.5f}".format(acc))
149+
# step += 1
150+
# print("Optimization Finished!")
151+
#
152+
# # Calculate accuracy for 256 mnist test images
153+
# print("Testing Accuracy:", \
154+
# sess.run(accuracy, feed_dict={x: mnist.test.images[:256],
155+
# y: mnist.test.labels[:256],
156+
# keep_prob: 1.}))
145157

146158
saver = tf.train.Saver()
147159
path = saver.save(sess, dir + '/vars.ckpt')

SudokuSolver.py

+49-48
Original file line numberDiff line numberDiff line change
@@ -17,47 +17,47 @@ def __init__(self):
1717
def video_capture(self):
1818
cap = cv2.VideoCapture(0)
1919
while(True):
20-
a = raw_input('.')
20+
a = 'a'
2121

2222
# Capture frame-by-frame
2323
ret, orig_img = cap.read()
2424
self.img_list.append(orig_img)
2525

2626
''' 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)
2829
board_img = self.crop_image(orig_img, box_points)
2930
gray_board_img = cv2.cvtColor(board_img, cv2.COLOR_RGB2GRAY)
3031
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)
3234

3335
''' 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('.')
6161

6262
''' --- Show --- '''
6363
self.display_images()
@@ -67,7 +67,7 @@ def video_capture(self):
6767

6868

6969
def solve_sudoku_board(self, board_to_solve):
70-
b = Board(array, 9,3,3)
70+
b = Board(board_to_solve, 9,3,3)
7171
b.createBoard()
7272
solved_board = b.solveBoard()
7373
print(solved_board)
@@ -77,7 +77,7 @@ def create_array_with_prefilled(self, prefilled):
7777
sudoku_to_solve = np.zeros(81)
7878
for i in range(0, len(prefilled[0])):
7979
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)
8181

8282
def classify_cells(self, board_img, mapped_grid):
8383
cells = np.asarray(self.crop_grid(board_img, mapped_grid))
@@ -86,30 +86,32 @@ def classify_cells(self, board_img, mapped_grid):
8686
idx_list = []
8787
print('Success')
8888
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#
9190
c[:5,:] = 0.0
9291
c[:,:5] = 0.0
9392
c[25:,:] = 0.0
9493
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))
9798
if np.sum(c[5:20,10:18]) > 10.0:
9899
cl_cells.append(c)
99100
idx_list.append(idx)
101+
self.img_list.append(c)
102+
print('to classify: ', len(cl_cells))
100103
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])
104107
prefilled = []
105108
prefilled.append(pred)
106109
prefilled.append(idx_list)
107-
print('pred -> ', pred)
108-
print('idx list -> ', idx_list)
110+
# print('pred -> ', pred)
111+
# print('idx list -> ', idx_list)
109112
return prefilled
110113

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):
113115
#gray_board_img = cv2.cvtColor(orig_img, cv2.COLOR_BGR2GRAY)
114116
#processed_img = self.preprocess_for_grid_detection(gray_board_img)
115117
contour = self.find_contours(processed_img)
@@ -139,10 +141,8 @@ def map_grid(self, img, grid_points):
139141
width = img.shape[0]
140142
height = img.shape[1]
141143
mapped_grid = None
142-
print('grid_points before -> ', len(grid_points))
143144
grid_points = self.cleanup_grid_points(grid_points)
144145
grid_points = sorted(grid_points,key=lambda x: (x[1],x[0]))
145-
print('grid_points -> ', len(grid_points))
146146
if len(grid_points) == 100: # Only 9x9 sudokuboard
147147
grid_points = np.asarray(grid_points).reshape((10,10,2))
148148
mapped_grid = np.zeros_like(grid_points)
@@ -320,7 +320,7 @@ def add_to_list(rho, theta):
320320
y2 = int(y0 - 1000*(a))
321321
if x < 3 or x > 18:
322322
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
324324
add_to_list(f_rho, f_theta)
325325
self.img_list.append(orig_img)
326326
return sorted(point_list,key=lambda x: x[0])
@@ -371,8 +371,9 @@ def harris_corner_detection(self, gray_img):
371371

372372
def canny_edge_detector(self, img):
373373
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))
376377
return canny_img
377378

378379
def sobel(self, img):

vars.ckpt.data-00000-of-00001

25 MB
Binary file not shown.

vars.ckpt.index

527 Bytes
Binary file not shown.

vars.ckpt.meta

41.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)