コード例 #1
0
class SudokuCapturer:
    """
    Class which take capture photos from default camera
    and recognise if there's sudoku on photo
    """

    def __init__(self):
        """C-tor, opens camera"""
        camera_port = 0  # 0 is for default camera
        self._camera_capturer = cv2.VideoCapture(camera_port)
        self._dectector = SudokuDetector()
        logging.info("Camera opened")

    def __del__(self):
        """D-tor, responsible for releasing camera"""
        logging.info("Releasing camera")
        self._camera_capturer.release()

    def capture_sudoku(self):
        """
            takes photo from default camera of computer and checks if there's sudoku on it
            if no sudoku was found it returns None
            if sudoku was found returns image of sudoku
        """
        if self._camera_capturer.isOpened():
            logging.info("Capturing photo")
            _, image = self._camera_capturer.read()
            cv2.waitKey(100)  # used to slow down
            return self._dectector.detect_sudoku(image)
        else:
            logging.info("Couldn't open camera")
            sys.exit(0)
コード例 #2
0
def detect_sudoku(img):
    sudoku_detector = SudokuDetector(img)
    sudoku_frame, sudoku_rectangles, max_approx, found = sudoku_detector.get_sudoku_from_image()

    if not found:
        return [], []

    # Uncomment this section if you would like to see the sudoku with rectangles found.
    # sd.draw_sudoku()
    # cv2.imshow("Sudoku with rectangles found", img)
    # cv2.waitKey(0)

    bounding_boxes = []
    for each in [sudoku_frame] + sudoku_rectangles:
        x, y, w, h = cv2.boundingRect(each)
        x_left, y_left, x_right, y_right = x, y, x + w, y + h

        bounding_boxes.append([(x_left, y_left), (x_right, y_right)])

    # for b in bounding_boxes:
    #     (x_left, y_left), (x_right, y_right) = b
    #     cv2.rectangle(img, (x_left, y_left), (x_right, y_right), color=(0, 0, 255), thickness=2)

    return bounding_boxes, max_approx
コード例 #3
0
 def __init__(self):
     """C-tor, opens camera"""
     camera_port = 0  # 0 is for default camera
     self._camera_capturer = cv2.VideoCapture(camera_port)
     self._dectector = SudokuDetector()
     logging.info("Camera opened")