Esempio n. 1
0
def main():

    if len(sys.argv) != 3:
        print ">python sudoku.py -auto image"
        print ">python sudoku.py -manual image"
        sys.exit()

    image = Sudoku(sys.argv[2])

    # PART ONE: THRESHOLD THE IMAGE
    image.threshold()

    # PART TWO: FIND THE CORNERS
    if sys.argv[1] == "-auto":
        image.findCorners()
    else:
        image.findCornersManually()

    # PART THREE: CORRECT THE PERSPECTIVE
    #  1. get the perspective transformation matrix using the four corners
    #  2. perform the perspective transformation
    image.correctPerspective()

    # PART FOUR: CROP THE IMAGE
    image.cropImage()

    # PART FOUR: FIND THE REGIONS
    image.findRegions()

    # PART FIVE: CALL THE SUDOKU SOLVER
    solved = sudokuSolver.solve(image.toString)
    print solved
Esempio n. 2
0
def sudoku_main(image):

    ## find sudoku contour
    sudoku_contour = find_sudoku(image)
    if sudoku_contour is None:
        return image

    ## get corners of the contour
    corners = get_corners(sudoku_contour)
    if corners is None:
        return image
    corners = sort_corners(corners)

    ## get top view of the board in square shape
    top_view, transformation_matrix, original_shape = get_top_view(
        image, corners)
    if top_view is None:
        return image

    ## OCR
    grid = read_grid(top_view)
    if grid is None:
        return image
    print(grid)

    # test sudoku
    test = "740030010019068502000004300056370001001800095090020600103407200500200008080001470"
    if grid == test:
        print("true")

    # solvong the sudoku
    solved = solve(test)

    # write the solution over the top view
    empty_boxes = [[0 for j in range(9)] for i in range(9)]
    k = 0
    for i in range(9):
        for j in range(9):
            if grid[k] == '0':
                empty_boxes[i][j] = 1
            k = k + 1
    written = write_solution(top_view, empty_boxes, solved)

    # covert the top view to original size
    resized = cv2.resize(top_view, (original_shape[1], original_shape[0]),
                         interpolation=cv2.INTER_CUBIC)
    # reverse perspective transform
    warped = cv2.warpPerspective(resized,
                                 transformation_matrix,
                                 (image.shape[1], image.shape[0]),
                                 flags=cv2.WARP_INVERSE_MAP)
    # overlay on the original image
    result = np.where(warped.sum(axis=-1, keepdims=True) != 0, warped, image)

    return result
Esempio n. 3
0
    def place(self, val):
        row, col = self.selected
        if self.cubes[row][col].value == 0:
            self.cubes[row][col].set(val)
            self.update_model()

            if valid(self.model, val, (row, col)) and solve(self.model):
                return True
            else:
                self.cubes[row][col].set(0)
                self.cubes[row][col].set_temp(0)
                self.update_model()
                return False
def main():
  resizeImageIntoSquare(input("Enter the name of the image file: "))
  makeIndividualSquares("img_02.png")
  centerCubes()
  grid=makeGrid()
  print("The puzzle is: ")
  sudokuSolver.printGrid(grid)
  if(sudokuSolver.solve(grid)):
    print("\n\nThe solved grid is: ")
    sudokuSolver.printGrid(grid)
  else:
    print("Grid is not solvable")
  deleteAllEvidence()
Esempio n. 5
0
def solve(bo):
    find = find_empty(bo)
    if not find:
        return True
    else:
        row, col = find

    for i in range(1, 10):
        if valid(bo, i, (row, col)):
            bo[row][col] = i

            if solve(bo):
                return True

            bo[row][col] = 0

    return False
Esempio n. 6
0
biggest = reorder(biggest)
pts1 = np.float32(np.reshape(biggest, (4, 2)))
pts2 = np.float32([[0, 0], [width - 1, 0], [0, height - 1],
                   [width - 1, height - 1]])
M = cv.getPerspectiveTransform(pts1, pts2)
new = cv.warpPerspective(img, M, (width, height))
new = removelines(new, 5)
#progress = []
#progress.append(np.array(cv.resize(cv.cvtColor(image,cv.COLOR_GRAY2BGR),(450,450)),np.uint8))

boxes = split(new)
#a = cropbox(boxes) #### REMEMBER TO NORMALIZE "a" ####
a = tf.keras.utils.normalize(a, axis=1)
#plt.imshow(a[25],cmap=plt.cm.binary_r)

pred = model.predict(a)
pred = pred.argmax(axis=1) * (np.amax(pred, axis=1) > 0.8)
progress.append(
    np.array(dispNo(pred, np.zeros((450, 450)), color=(255, 0, 255)),
             np.uint8))

pos = (pred == 0) * np.ones(81)
pos

board = pred.reshape((9, 9))
try:
    sudokuSolver.solve(board)
except:
    pass
board
Esempio n. 7
0
import detector
import sudokuSolver

sudoku = detector.detect_sudoku("sudoku4.jpg")
print(sudoku)
print(sudokuSolver.solve(sudoku))
print(sudoku)