Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
raspberryturk/raspberryturk/embedded/vision/chess_camera.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
27 lines (24 sloc)
1.06 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import chess | |
from raspberryturk.core.vision.chessboard_frame import ChessboardFrame | |
from raspberryturk.core.vision.constants import BOARD_SIZE, SQUARE_SIZE | |
from raspberryturk.embedded.vision.chessboard_perspective_transform import get_chessboard_perspective_transform | |
from raspberryturk.embedded.vision.square_color_detector import SquareColorDetector | |
class ChessCamera(object): | |
def __init__(self): | |
self._capture = cv2.VideoCapture(0) | |
self._color_detector = SquareColorDetector() | |
def current_chessboard_frame(self): | |
_, frame = self._capture.read() | |
h, w = frame.shape[:2] | |
M = get_chessboard_perspective_transform() | |
bgr_img = cv2.warpPerspective(frame, M, (BOARD_SIZE,BOARD_SIZE)) | |
img = cv2.cvtColor(bgr_img, cv2.COLOR_BGR2RGB) | |
return ChessboardFrame(img) | |
def current_colored_board_mask(self): | |
cbf = self.current_chessboard_frame() | |
cbm = [None] * 64 | |
for i in range(64): | |
sq = cbf.square_at(i) | |
cbm[i] = self._color_detector.detect(sq) | |
return cbm |