From 51c867afc6f136260e836da9dfac260ad7480057 Mon Sep 17 00:00:00 2001
From: Zack Alonzo <zalonzo2@illinois.edu>
Date: Tue, 2 Apr 2024 21:30:35 -0500
Subject: [PATCH] Trying to implement camera. Saving small changes

---
 board_detector.py |  6 +++---
 game.py           | 28 +++++++++++++++++++++++++++-
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/board_detector.py b/board_detector.py
index ad838f8..00ed44c 100644
--- a/board_detector.py
+++ b/board_detector.py
@@ -85,12 +85,12 @@ def filter_lines(lines, min_distance):
 
 def find_board(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):
         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
     height, width, _ = img.shape
diff --git a/game.py b/game.py
index 526e992..48abaf9 100644
--- a/game.py
+++ b/game.py
@@ -9,12 +9,17 @@ import move_translator
 import cv2
 import os
 
+import time
+from picamera2 import Picamera2, Preview
+
 class ChessGame:
     def __init__(self, difficulty, show_cv, test_img = None):
         self.board = chess.Board()
         self.difficulty = difficulty
         self.show_cv = show_cv
         self.test_img = test_img
+        self.img_idx = 0
+        self.picam2 = Picamera2()
 
     def start_game(self):
         print(f"Starting chess game (difficulty: {self.difficulty})")
@@ -24,11 +29,17 @@ class ChessGame:
 
         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):
             img_path = os.path.join('test_images', self.test_img)
             orig_img = cv2.imread(img_path)
         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
         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))
@@ -39,6 +50,10 @@ class ChessGame:
             cv2.destroyAllWindows()
 
         warped_img, sorted_warped_points = find_board(img)
+
+        if (warped_img is None):
+            return
+        
         color_grid = find_pieces(warped_img, sorted_warped_points)
 
         while(1): # game loop
@@ -67,6 +82,17 @@ class ChessGame:
         # 4. call move_translator with argument of best move
         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__":
     parser = argparse.ArgumentParser(description="AI Chess Robot with Computer Vision")
     parser.add_argument("--difficulty", choices=["easy", "medium", "hard"], 
-- 
GitLab