From 0f702640a31b4256beae0c66613f0e8a680771e5 Mon Sep 17 00:00:00 2001 From: "Casper V. Kristensen" Date: Wed, 17 Apr 2019 18:20:37 +0200 Subject: [PATCH] Yay! --- README.md | 22 ++++++++++++++++++++++ adapter.py | 7 +++---- runner.py | 21 +++++++++++++-------- 3 files changed, 38 insertions(+), 12 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 00000000..18b383a1 --- /dev/null +++ b/README.md @@ -0,0 +1,22 @@ +## Test Adapter: +```bash +base64 --wrap=0 < whole_boards/board_118_1554110522.620303_.png| python3 adapter.py +``` + +## Build OpenCV +```bash +mkdir opencv && curl -L https://github.com/opencv/opencv/archive/4.1.0.tar.gz | tar -xzv --directory=opencv_contrib --strip-components=1 +mkdir opencv_contrib && curl -L https://github.com/opencv/opencv_contrib/archive/4.1.0.tar.gz | tar -xzv --directory=opencv_contrib --strip-components=1 + +mkdir -p opencv/build +cd opencv/build + +cmake -D CMAKE_BUILD_TYPE=Release -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules/ -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_ENABLE_NONFREE=ON -D BUILD_PERF_TESTS=OFF -D BUILD_TESTS=OFF .. +make -j $(nproc) + +sudo make install +sudo ldconfig + +python3 -c "import cv2; print(cv2.__version__)" +``` + diff --git a/adapter.py b/adapter.py index 0fb943b8..199471a6 100644 --- a/adapter.py +++ b/adapter.py @@ -5,7 +5,7 @@ import sys import cv2 import numpy as np -from runner import find_keypoints +from runner import find_homography # Load base64 encoded image from stdin stdin = sys.stdin.readline() @@ -15,12 +15,11 @@ camera_img = cv2.imdecode(img_array, flags=cv2.COLOR_BGR2RGB) camera_img = cv2.cvtColor(camera_img, cv2.COLOR_BGR2RGB) # Find keypoints in image and pass them back to unity -src_points, dst_points = find_keypoints(camera_img) +homography = find_homography(camera_img) # Finally, output to stdout for unity to read result = { - "src_points": [p.tolist() for p in src_points], - "dst_points": [p.tolist() for p in dst_points], + "homography": homography.tolist(), } print(json.dumps(result)) diff --git a/runner.py b/runner.py index 43fd9971..a71c7f28 100644 --- a/runner.py +++ b/runner.py @@ -133,9 +133,7 @@ def train_pieces_svm_canny() -> None: joblib.dump(classifier, f"classifiers/classifier_empty/white_piece_on_{square_color}_square.pkl") -def find_keypoints(camera_image: np.ndarray, - baseline: np.ndarray = cv2.imread(str(here.joinpath("new_baseline_board.png"))), - debug=False) -> Tuple[np.ndarray, np.ndarray]: +def find_keypoints(camera_image: np.ndarray, baseline: np.ndarray, debug=False) -> Tuple[np.ndarray, np.ndarray]: """ Find keypoints in raw camera image of board. @@ -200,14 +198,21 @@ def find_keypoints(camera_image: np.ndarray, return src_points, dst_points -def warp_board(camera_image: np.ndarray, debug=False) -> np.ndarray: - baseline = cv2.imread(str(here.joinpath("new_baseline_board.png"))) +def find_homography(camera_image: np.ndarray, + baseline: np.ndarray = cv2.imread(str(here.joinpath("new_baseline_board.png"))), + debug=False) -> np.ndarray: src_points, dst_points = find_keypoints(camera_image, baseline, debug=debug) - h, mask = cv2.findHomography(src_points, dst_points, cv2.RANSAC) - height, width, channels = baseline.shape - return cv2.warpPerspective(camera_image, h, (width, height)) + return h + + +def warp_board(camera_image: np.ndarray, homography: np.ndarray = None, debug=False) -> np.ndarray: + baseline = cv2.imread(str(here.joinpath("new_baseline_board.png"))) + homography = homography or find_homography(camera_image, baseline, debug=debug) + + height, width, channels = baseline.shape + return cv2.warpPerspective(camera_image, homography, (width, height)) def get_square(warped_board: np.ndarray, position: POSITION) -> np.ndarray: