def test_custom_2(self) -> None:
        puzzle = [
            [0, 7, 0, 9, 3, 0, 2, 0, 0],
            [5, 0, 8, 0, 0, 0, 3, 0, 9],
            [0, 0, 0, 2, 7, 0, 0, 6, 0],
            [8, 0, 0, 0, 4, 9, 0, 1, 0],
            [0, 6, 0, 0, 0, 0, 0, 5, 0],
            [0, 9, 0, 5, 8, 0, 0, 0, 3],
            [0, 8, 0, 0, 9, 1, 0, 0, 0],
            [7, 0, 3, 0, 0, 0, 1, 0, 5],
            [0, 0, 6, 0, 5, 3, 0, 2, 0],
        ]

        solution = [
            [6, 7, 1, 9, 3, 5, 2, 8, 4],
            [5, 2, 8, 6, 1, 4, 3, 7, 9],
            [4, 3, 9, 2, 7, 8, 5, 6, 1],
            [8, 5, 7, 3, 4, 9, 6, 1, 2],
            [3, 6, 4, 1, 2, 7, 9, 5, 8],
            [1, 9, 2, 5, 8, 6, 7, 4, 3],
            [2, 8, 5, 7, 9, 1, 4, 3, 6],
            [7, 4, 3, 8, 6, 2, 1, 9, 5],
            [9, 1, 6, 4, 5, 3, 8, 2, 7],
        ]

        assert sudoku(puzzle) == solution
    def test_sample(self) -> None:
        puzzle = [
            [5, 3, 0, 0, 7, 0, 0, 0, 0],
            [6, 0, 0, 1, 9, 5, 0, 0, 0],
            [0, 9, 8, 0, 0, 0, 0, 6, 0],
            [8, 0, 0, 0, 6, 0, 0, 0, 3],
            [4, 0, 0, 8, 0, 3, 0, 0, 1],
            [7, 0, 0, 0, 2, 0, 0, 0, 6],
            [0, 6, 0, 0, 0, 0, 2, 8, 0],
            [0, 0, 0, 4, 1, 9, 0, 0, 5],
            [0, 0, 0, 0, 8, 0, 0, 7, 9],
        ]

        solution = [
            [5, 3, 4, 6, 7, 8, 9, 1, 2],
            [6, 7, 2, 1, 9, 5, 3, 4, 8],
            [1, 9, 8, 3, 4, 2, 5, 6, 7],
            [8, 5, 9, 7, 6, 1, 4, 2, 3],
            [4, 2, 6, 8, 5, 3, 7, 9, 1],
            [7, 1, 3, 9, 2, 4, 8, 5, 6],
            [9, 6, 1, 5, 3, 7, 2, 8, 4],
            [2, 8, 7, 4, 1, 9, 6, 3, 5],
            [3, 4, 5, 2, 8, 6, 1, 7, 9],
        ]

        assert sudoku(puzzle) == solution
    def test_custom_1(self) -> None:
        puzzle = [
            [0, 3, 1, 0, 0, 6, 8, 9, 0],
            [0, 0, 0, 0, 9, 0, 4, 3, 2],
            [4, 8, 9, 3, 0, 0, 0, 0, 0],
            [0, 1, 0, 7, 0, 5, 0, 0, 4],
            [0, 0, 0, 9, 6, 8, 0, 0, 0],
            [9, 0, 0, 2, 0, 4, 0, 7, 0],
            [0, 0, 0, 0, 0, 3, 5, 2, 7],
            [3, 5, 8, 0, 2, 0, 0, 0, 0],
            [0, 2, 7, 5, 0, 0, 6, 8, 0],
        ]

        solution = [
            [2, 3, 1, 4, 7, 6, 8, 9, 5],
            [5, 7, 6, 8, 9, 1, 4, 3, 2],
            [4, 8, 9, 3, 5, 2, 7, 1, 6],
            [8, 1, 2, 7, 3, 5, 9, 6, 4],
            [7, 4, 3, 9, 6, 8, 2, 5, 1],
            [9, 6, 5, 2, 1, 4, 3, 7, 8],
            [6, 9, 4, 1, 8, 3, 5, 2, 7],
            [3, 5, 8, 6, 2, 7, 1, 4, 9],
            [1, 2, 7, 5, 4, 9, 6, 8, 3],
        ]

        assert sudoku(puzzle) == solution
Beispiel #4
0
def read_sudoku(reader_network, png_file, size=9):
    img = mpimg.imread(png_file)
    s = list()
    for i in range(size):
        row = list()
        for j in range(size):
            digit_mtr = pixel_cluster(coord_of_img(i, j, img))
            spectorum = predict(reader_network, digit_mtr).tolist()
            digit = spectorum.index(max(spectorum))
            row.append(digit)
        s.append(row)

    return sudoku(s)
Beispiel #5
0
    img_name = sys.argv[1]

    if len(sys.argv) == 2:  # Expect exactly one argument: the image
        DEMO = True
        img, corners, labels = process_img(img_name, 'knn_typed.npz', 1, DEMO)

        ext_puzzle = solver.stringToArray(''.join(map(str, labels)))
        print 'Extracted Sudoku:\n{}'.format(ext_puzzle)
        puzzle = solver.stringToArray(''.join(map(str, PUZZLES[img_name])))
        print 'Actual Sudoku:\n{}'.format(puzzle)

        matches = np.array(PUZZLES[img_name]) == np.array(labels)
        correct = np.count_nonzero(matches)
        accuracy = correct * 100.0 / len(labels)
        print 'Digit Recognition Accuracy: {}'.format(accuracy)

        solution = solver.sudoku(puzzle.copy())
        print 'Solution:\n{}'.format(solution)

        draw_solution(img, corners, puzzle, solution)
        show_img('Solution', img)

    else:
        for k in range(1, 21):
            labels = process_img(img_name, 'knn_typed.npz', k, False)
            #print labels
            matches = np.array(PUZZLES[img_name]) == np.array(labels)
            correct = np.count_nonzero(matches)
            accuracy = correct * 100.0 / len(labels)
            print '{}: {}'.format(k, accuracy)
Beispiel #6
0
             y2max = (y + 1) * case - marge
             x2min = x * case + marge
             x2max = (x + 1) * case - marge
             prediction = s.run(sortie,
                                feed_dict={
                                    images: [
                                        grille[y2min:y2max,
                                               x2min:x2max].reshape(
                                                   28, 28, 1)
                                    ],
                                    is_training:
                                    False
                                })
             ligne += "{:d}".format(np.argmax(prediction[0]))
         grille_txt.append(ligne)
     result = ss.sudoku(grille_txt)
     print("Resultat:", result)
 #result=None
 if result is not None:
     flag = 1
     fond = np.zeros(shape=(taille_grille, taille_grille, 3),
                     dtype=np.float32)
     for y in range(len(result)):
         for x in range(len(result[y])):
             if grille_txt[y][x] == "0":
                 cv2.putText(fond, "{:d}".format(result[y][x]),
                             ((x) * case + marge + 3,
                              (y + 1) * case - marge - 3),
                             cv2.FONT_HERSHEY_SCRIPT_COMPLEX, 0.9,
                             (0, 0, 255), 1)
     M = cv2.getPerspectiveTransform(pts2, pts1)
Beispiel #7
0
                    y2min = y * case + marge
                    y2max = (y + 1) * case - marge
                    x2min = x * case + marge
                    x2max = (x + 1) * case - marge
                    cv2.imwrite("mat" + str(y) + str(x) + ".png",
                                grille[y2min:y2max, x2min:x2max])
                    img = grille[y2min:y2max, x2min:x2max]
                    x = img.reshape(1, 28, 28, 1)
                    if x.sum() > 10000:
                        prediction = classifier.predict_classes(x)
                        ligne += "{:d}".format(prediction[0])
                    else:
                        ligne += "{:d}".format(0)
                grille_txt.append(ligne)
            print(grille_txt)
            result = sol.sudoku(grille_txt)
        print("Resultat:", result)

        if result is not None:
            flag = 1
            fond = np.zeros(shape=(taille_grille, taille_grille, 3),
                            dtype=np.float32)
            for y in range(len(result)):
                for x in range(len(result[y])):
                    if grille_txt[y][x] == "0":
                        cv2.putText(fond, "{:d}".format(result[y][x]),
                                    ((x) * case + marge + 3,
                                     (y + 1) * case - marge - 3),
                                    cv2.FONT_HERSHEY_SCRIPT_COMPLEX, 0.9,
                                    (0, 0, 255), 1)
            M = cv2.getPerspectiveTransform(pts2, pts1)