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

updated code to take images easily. pushing 12 images of the small p character

parent 059830cd
No related branches found
No related tags found
No related merge requests found
......@@ -14,28 +14,28 @@ import time
from picamera2 import Picamera2, Preview
class ChessGame:
def __init__(self, difficulty, show_cv, show_cam, calibrate, test_img = None):
def __init__(self, difficulty, show_cv, show_cam, calibrate, img_idx = 1, test_img = None, save_img_as = None):
self.board = chess.Board()
self.difficulty = difficulty
self.show_cv = show_cv
self.show_cam = show_cam
self.test_img = test_img
self.calibrate = calibrate
self.img_idx = 0
self.left_cut = 30
self.save_img_as = save_img_as
self.img_idx = int(img_idx)
self.left_cut = 0
self.right_cut = 290
self.top_cut = 0
self.bottom_cut = 290
self.picam2 = Picamera2()
def calibrate_cam():
pass
# def calibrate_cam():
# pass
def start_game(self):
if (self.calibrate):
self.calibrate_cam()
print(f"Starting chess game (difficulty: {self.difficulty})")
# TODO - call initialize board in board_detector, initialize colors for color analysis,
......@@ -43,35 +43,51 @@ class ChessGame:
init_show_cv(self.show_cv)
if self.save_img_as:
self.calibrate = True
preview_config = self.picam2.create_preview_configuration(main={"size": (2464, 2464)})
self.picam2.configure(preview_config)
if (self.show_cam):
self.picam2.start_preview(Preview.QTGL)
self.picam2.start()
input("Press enter to continue.")
else:
self.picam2.start()
# self.picam2.start()
# input("Press enter to continue.")
# else:
# self.picam2.start()
self.picam2.start()
# 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 = self.take_pic()
h,w,c = orig_img.shape
# w_cut = 30
# h_cut = 100
cropped_img = orig_img[self.top_cut:h-self.bottom_cut, self.left_cut:w-self.right_cut]
# cropped_img = orig_img
img = cv2.resize(cropped_img, (512, 512))
# img = orig_img
if (self.show_cv):
display_img([orig_img, img])
# display_img([img])
while(1): # essentially do_while(self.calibrate)
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 = self.take_pic()
h,w,c = orig_img.shape
# w_cut = 30
# h_cut = 100
cropped_img = orig_img[self.top_cut:h-self.bottom_cut, self.left_cut:w-self.right_cut]
# cropped_img = orig_img
img = cv2.resize(cropped_img, (512, 512))
# img = orig_img
if (self.show_cv):
display_img([orig_img, img])
# display_img([img])
if (self.calibrate):
answer = 0
while(answer != "y" and answer != "n"):
answer = input("Done calibrating? (y/n): ")
if answer == "y":
self.calibrate = False
elif answer == "n":
self.calibrate = True
if (not self.calibrate):
break
warped_img, sorted_warped_points = find_board(img)
......@@ -107,12 +123,15 @@ class ChessGame:
pass
def take_pic(self):
self.img_idx += 1
time.sleep(2)
img_txt = 'board' + str(self.img_idx) + '.jpg'
if (self.save_img_as):
img_txt = self.save_img_as + str(self.img_idx) + '.jpg'
else:
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)
self.img_idx += 1
return cv2.imread(img_path)
if __name__ == "__main__":
......@@ -121,9 +140,11 @@ if __name__ == "__main__":
default="medium", help="Chess AI difficulty (how far it looks ahead)")
parser.add_argument("--show_cv", action="store_true", help="Show opencv images as processing occurs during game")
parser.add_argument("--show_cam", action="store_true", help="Show persistent camera view")
parser.add_argument("--calibrate", action="store_true", help="Loop showing camera with cutoff lines first, then start game")
parser.add_argument("--calibrate", action="store_true", help="Loop before cv")
parser.add_argument("--img_idx", help="Where to start indexing images (for naming them)")
parser.add_argument("--test_img", help="If specified, will use said image in test_images folder rather than camera input")
parser.add_argument("--save_img_as", help="If specified, will save image as given name in game_images")
args = parser.parse_args()
game = ChessGame(args.difficulty, args.show_cv, args.show_cam, args.calibrate, args.test_img)
game = ChessGame(args.difficulty, args.show_cv, args.show_cam, args.calibrate, args.img_idx, args.test_img, args.save_img_as)
game.start_game()
\ No newline at end of file
ocr_test_images/ocr_smallp1.jpg

895 KiB

ocr_test_images/ocr_smallp10.jpg

890 KiB

ocr_test_images/ocr_smallp11.jpg

890 KiB

ocr_test_images/ocr_smallp12.jpg

890 KiB

ocr_test_images/ocr_smallp2.jpg

890 KiB

ocr_test_images/ocr_smallp3.jpg

901 KiB

ocr_test_images/ocr_smallp4.jpg

903 KiB

ocr_test_images/ocr_smallp5.jpg

899 KiB

ocr_test_images/ocr_smallp6.jpg

897 KiB

ocr_test_images/ocr_smallp7.jpg

898 KiB

ocr_test_images/ocr_smallp8.jpg

899 KiB

ocr_test_images/ocr_smallp9.jpg

890 KiB

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