Skip to content
Snippets Groups Projects
Commit 51c867af authored by zalonzo2's avatar zalonzo2
Browse files

Trying to implement camera. Saving small changes

parent af94d6f7
No related branches found
No related tags found
No related merge requests found
...@@ -85,12 +85,12 @@ def filter_lines(lines, min_distance): ...@@ -85,12 +85,12 @@ def filter_lines(lines, min_distance):
def find_board(img): def find_board(img):
vertical_lines, horizontal_lines = find_longest_lines(img) vertical_lines, horizontal_lines = find_longest_lines(img)
print("# of Vertical:",len(vertical_lines))
print("# of Horizontal:",len(horizontal_lines))
if (len(vertical_lines) != 9 or len(horizontal_lines) != 9): if (len(vertical_lines) != 9 or len(horizontal_lines) != 9):
print("Error: Grid does not match expected 9x9") print("Error: Grid does not match expected 9x9")
return print("# of Vertical:",len(vertical_lines))
print("# of Horizontal:",len(horizontal_lines))
return None, None
# create bitmasks for vert and horiz so we can get lines and intersections # create bitmasks for vert and horiz so we can get lines and intersections
height, width, _ = img.shape height, width, _ = img.shape
......
...@@ -9,12 +9,17 @@ import move_translator ...@@ -9,12 +9,17 @@ import move_translator
import cv2 import cv2
import os import os
import time
from picamera2 import Picamera2, Preview
class ChessGame: class ChessGame:
def __init__(self, difficulty, show_cv, test_img = None): def __init__(self, difficulty, show_cv, test_img = None):
self.board = chess.Board() self.board = chess.Board()
self.difficulty = difficulty self.difficulty = difficulty
self.show_cv = show_cv self.show_cv = show_cv
self.test_img = test_img self.test_img = test_img
self.img_idx = 0
self.picam2 = Picamera2()
def start_game(self): def start_game(self):
print(f"Starting chess game (difficulty: {self.difficulty})") print(f"Starting chess game (difficulty: {self.difficulty})")
...@@ -24,11 +29,17 @@ class ChessGame: ...@@ -24,11 +29,17 @@ class ChessGame:
init_show_cv(self.show_cv) init_show_cv(self.show_cv)
preview_config = self.picam2.create_preview_configuration(main={"size": (512, 512)})
self.picam2.configure(preview_config)
# self.get_img()
if (self.test_img): if (self.test_img):
img_path = os.path.join('test_images', self.test_img) img_path = os.path.join('test_images', self.test_img)
orig_img = cv2.imread(img_path) orig_img = cv2.imread(img_path)
else: else:
orig_img = cv2.imread('test_images/board1.jpg') # TODO - CHANGE TO MAKE IT RECEIVE INPUT FROM CAMERA # orig_img = cv2.imread('test_images/board1.jpg') # TODO - CHANGE TO MAKE IT RECEIVE INPUT FROM CAMERA
orig_img = self.get_img()
h,w,c = orig_img.shape h,w,c = orig_img.shape
cropped_img = orig_img[0:h, int(w/2 - h/2 - 60):int(w/2 + h/2 + 100)] cropped_img = orig_img[0:h, int(w/2 - h/2 - 60):int(w/2 + h/2 + 100)]
img = cv2.resize(cropped_img, (512, 512)) img = cv2.resize(cropped_img, (512, 512))
...@@ -39,6 +50,10 @@ class ChessGame: ...@@ -39,6 +50,10 @@ class ChessGame:
cv2.destroyAllWindows() cv2.destroyAllWindows()
warped_img, sorted_warped_points = find_board(img) warped_img, sorted_warped_points = find_board(img)
if (warped_img is None):
return
color_grid = find_pieces(warped_img, sorted_warped_points) color_grid = find_pieces(warped_img, sorted_warped_points)
while(1): # game loop while(1): # game loop
...@@ -67,6 +82,17 @@ class ChessGame: ...@@ -67,6 +82,17 @@ class ChessGame:
# 4. call move_translator with argument of best move # 4. call move_translator with argument of best move
pass pass
def get_img(self):
self.img_idx += 1
self.picam2.start_preview(Preview.QTGL)
self.picam2.start()
time.sleep(2)
img_txt = 'board' + str(self.img_idx) + '.jpg'
print(img_txt)
img_path = os.path.join('game_images', img_txt)
metadata = self.picam2.capture_file(img_path)
return cv2.imread(img_path)
if __name__ == "__main__": if __name__ == "__main__":
parser = argparse.ArgumentParser(description="AI Chess Robot with Computer Vision") parser = argparse.ArgumentParser(description="AI Chess Robot with Computer Vision")
parser.add_argument("--difficulty", choices=["easy", "medium", "hard"], parser.add_argument("--difficulty", choices=["easy", "medium", "hard"],
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment