コード例 #1
0
ファイル: video.py プロジェクト: ryan570/opencv-sudoku-solver
    def read_and_solve(self, frame, polygon):
        warped = warp(frame, polygon,
                      ((0, 0), (WARPED_SIZE, 0), (WARPED_SIZE, WARPED_SIZE),
                       (0, WARPED_SIZE)), (WARPED_SIZE, WARPED_SIZE))
        no_grid = remove_grid(warped)

        puzzle = np.zeros((9, 9))
        for i in range(9):
            for j in range(9):
                square = no_grid[round(i * STEP):round((i + 1) * STEP),
                                 j * STEP:(j + 1) * STEP]
                found, digit = find_digit(square)

                if found:
                    digit = pad(digit)
                    puzzle[i][j] = predict(digit)

        to_solve = [str(int(s)) for s in puzzle.flatten()]
        solved = solve(to_solve)
        try:
            solution = [*solved.values()]
            solution = np.reshape(solution, (9, 9))

            self.solution = solution
            self.solved = True
        except:
            pass
コード例 #2
0
def run(img, data_gen=False):
    processed = preprocess(img)

    contours, hierarchy = cv2.findContours(processed, cv2.RETR_EXTERNAL,
                                           cv2.CHAIN_APPROX_NONE)
    if contours:
        biggest = sorted(contours, key=area, reverse=True)[:3]

        for cnt in biggest:
            polygon = cv2.approxPolyDP(cnt, .02 * cv2.arcLength(cnt, True),
                                       True)
            x, y, w, h = cv2.boundingRect(polygon)
            if w / h < 0.7 or w / h > 1.3 or len(polygon) != 4:
                continue
            polygon = polygon.reshape(4, 2)
            polygon = order_points(polygon)
            break

        warped = warp(img, polygon,
                      ((0, 0), (WARPED_SIZE, 0), (WARPED_SIZE, WARPED_SIZE),
                       (0, WARPED_SIZE)), (WARPED_SIZE, WARPED_SIZE))
        no_grid = remove_grid(warped)

        step = WARPED_SIZE // 9
        puzzle = np.zeros((9, 9))
        for i in range(9):
            for j in range(9):
                square = no_grid[round(i * step):round((i + 1) * step),
                                 j * step:(j + 1) * step]
                found, digit = find_digit(square)

                if found:
                    digit = pad(digit)
                    puzzle[i][j] = predict(digit)

        to_solve = [str(int(s)) for s in puzzle.flatten()]
        solved = solve(to_solve)
        if type(solved) == bool:
            print("Puzzle was read incorrectly, skipping...")
            return
        solution = [*solved.values()]
        solution = np.reshape(solution, (9, 9))

        blank = np.zeros((*img.shape[:2][::-1], 4), dtype=np.uint8)
        for (x, y), num in np.ndenumerate(solution):
            cv2.putText(blank, str(int(num)),
                        ((y) * step + 12, (1 + x) * step - 10),
                        cv2.FONT_HERSHEY_PLAIN, 3, (255, 255, 255, 255), 3)

        overlay = warp(blank, ((0, 0), (WARPED_SIZE, 0),
                               (WARPED_SIZE, WARPED_SIZE), (0, WARPED_SIZE)),
                       polygon, (img.shape[1], img.shape[0]))
        img = cv2.cvtColor(img, cv2.COLOR_RGB2RGBA)
        img = cv2.add(img, overlay)

        cv2.imshow("process", processed)
        cv2.imshow("warp", warped)
        cv2.imshow("no grid", no_grid)
        cv2.imshow("solved", img)
        cv2.waitKey(0)
コード例 #3
0
                            """
                            print(pred)
                            """

                            try:
                                grid_digits[i] = (str(int(pred[0]) + 1))
                            except:
                                pass

                        except:
                            pass

            # Finally, we have the grid.
            # Predict the solution using the Norvig Sudoku Solving Algorithm
            if len(grid_digits) == 81:
                solved = solve(grid_digits)

                if solved != False:
                    solved = list(solved.values())

                    # Print the text on our image
                    for e in range(81):
                        if grid_digits[e] != '0':
                            continue
                        sudoku_clr = cv2.putText(
                            sudoku_clr,
                            solved[e],
                            ((rois[e][2] + rois[e][3]) // 2, rois[e][1]),
                            cv2.FONT_HERSHEY_SIMPLEX,
                            1, (0, 0, 255),
                            thickness=2)
コード例 #4
0
            [[0, 0], [cropped.shape[0] - 1, 0],
             [cropped.shape[0] - 1, cropped.shape[1] - 1],
             [0, cropped.shape[1] - 1]])

        squares = infer_grid(cropped)

        side = cropped.shape[:1]
        side = side[0] / 9
        #fetch digit boxes
        digits = get_digits(cropped, squares, 28)

        if len(digits) == 81:
            digits = np.array(digits).reshape(9, 9, 28, 28)
            digits = np.transpose(digits, (1, 0, 2, 3)).reshape(81, 28, 28, 1)

            grid, solved = solve(digits)
            if grid == ".................................................................................":
                continue

            if solved:

                digits = np.array(digits).reshape(9, 9, 28, 28)
                digits = np.transpose(digits, (1, 0, 2, 3)).reshape(81, 28, 28)
                display(solved)
                solved = list(
                    (np.array(list(solved.values())).reshape(9,
                                                             9).T).reshape(81))

                # Print the text on our image

                for number in range(81):