Skip to content

Instantly share code, notes, and snippets.

@mineshpatel1
mineshpatel1 / predict_digits.py
Created November 2, 2017 14:14
Sudoku Solver 3 - Appendix 2 #blog #bernard
def simple_network():
x = tf.placeholder(tf.float32, shape=[None, 784]) # Placeholder for input
y_ = tf.placeholder(tf.float32, shape=[None, 10]) # Placeholder for true labels (used in training)
hidden_neurons = 16 # Number of neurons in the hidden layer
# Hidden layer
w_1 = weights([784, hidden_neurons])
b_1 = biases([hidden_neurons])
h_1 = tf.nn.sigmoid(tf.matmul(x, w_1) + b_1) # Order of x and w_1 matters here purely syntactically
@mineshpatel1
mineshpatel1 / Dataset.py
Last active November 1, 2017 17:31
Sudoku Solver 3 - Appendix 1 #blog #bernard
import numpy as np
import cv2
def read_image(path):
"""Reads image as grayscale and normalises and flattens the array."""
img = cv2.imread(path, cv2.IMREAD_GRAYSCALE) # Load as grayscale
img = img / 255 # Normalise values
return img.flatten() # Flatten to shape [784]
@mineshpatel1
mineshpatel1 / sudoku_cv.py
Last active September 23, 2020 06:54
Sudoku Solver 2 - Appendix 2 - Complete Solution #blog #bernard
import cv2
import operator
import numpy as np
from matplotlib import pyplot as plt
def plot_many_images(images, titles, rows=1, columns=2):
"""Plots each image in a given list as a grid structure. using Matplotlib."""
for i, image in enumerate(images):
plt.subplot(rows, columns, i+1)
@mineshpatel1
mineshpatel1 / sudoku-solver-1.py
Last active March 20, 2019 17:01
Sudoku Solver 1 - Appendix 2 - Complete Solution #blog #bernard
def display_grid(grid, coords=False):
"""
Displays a 9x9 soduku grid in a nicely formatted way.
Args:
grid (str|dict|list): A string representing a Sudoku grid. Valid characters are digits from 1-9 and empty squares are
specified by 0 or . only. Any other characters are ignored. A `ValueError` will be raised if the input does
not specify exactly 81 valid grid positions.
Can accept a dictionary where each key is the position on the board from A1 to I9.
Can accept a list of strings or integers with empty squares represented by 0.
@mineshpatel1
mineshpatel1 / display_points.py
Last active October 10, 2019 07:55
Sudoku Solver 2 - Appendix 1 #blog #bernard
def display_points(in_img, points, radius=5, colour=(0, 0, 255)):
"""Draws circular points on an image."""
img = in_img.copy()
# Dynamically change to a colour image if necessary
if len(colour) == 3:
if len(img.shape) == 2:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
elif img.shape[2] == 1:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
@mineshpatel1
mineshpatel1 / display_grid.py
Last active October 11, 2017 12:06
Sudoku Solver 1 - Appendix 1 #blog #bernard
def display_grid(grid, coords=False):
"""
Displays a 9x9 soduku grid in a nicely formatted way.
Args:
grid (str|dict|list): A string representing a Sudoku grid. Valid characters are digits from 1-9 and empty squares are
specified by 0 or . only. Any other characters are ignored. A `ValueError` will be raised if the input does
not specify exactly 81 valid grid positions.
Can accept a dictionary where each key is the position on the board from A1 to I9.
Can accept a list of strings or integers with empty squares represented by 0.