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

Code from glare images

parent a096efdd
No related branches found
No related tags found
No related merge requests found
...@@ -29,6 +29,16 @@ def find_longest_lines(img): ...@@ -29,6 +29,16 @@ def find_longest_lines(img):
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray_img, 50, 100, apertureSize=3) edges = cv2.Canny(gray_img, 50, 100, apertureSize=3)
h,w = edges.shape
# don't detect any lines at the edges (make the edges black)
cutoff = 5
for y in range(0,h):
for x in range(0,w):
if x < cutoff or x > w - cutoff or y < cutoff or y > h - cutoff:
# print(edges[y, x])
edges[y, x] = 0
if (show_cv): if (show_cv):
# cv2.imshow('Canny Filter', edges) # cv2.imshow('Canny Filter', edges)
# cv2.waitKey(0) # cv2.waitKey(0)
...@@ -47,8 +57,8 @@ def find_longest_lines(img): ...@@ -47,8 +57,8 @@ def find_longest_lines(img):
filtered_horizontal = filter_lines(horizontal_line_points, 50) filtered_horizontal = filter_lines(horizontal_line_points, 50)
# get the 9 largest lines # get the 9 largest lines
sorted_vertical = sorted(filtered_vertical, key=lambda line: min(line[0][1], line[0][3]))[:9] sorted_vertical = sorted(filtered_vertical, key=lambda line: min(line[0][1], line[0][3]))[:12]
sorted_horizontal = sorted(filtered_horizontal, key=lambda line: min(line[0][0], line[0][2]))[:9] sorted_horizontal = sorted(filtered_horizontal, key=lambda line: min(line[0][0], line[0][2]))[:12]
return sorted_vertical, sorted_horizontal return sorted_vertical, sorted_horizontal
...@@ -86,6 +96,9 @@ def filter_lines(lines, min_distance): ...@@ -86,6 +96,9 @@ def filter_lines(lines, min_distance):
keep_line = True keep_line = True
for line2 in filtered_lines: for line2 in filtered_lines:
x3, y3, x4, y4 = line2[0] x3, y3, x4, y4 = line2[0]
line2_x_avg = (x3 + x4) / 2 line2_x_avg = (x3 + x4) / 2
line2_y_avg = (y3 + y4) / 2 line2_y_avg = (y3 + y4) / 2
......
...@@ -14,16 +14,28 @@ import time ...@@ -14,16 +14,28 @@ import time
from picamera2 import Picamera2, Preview from picamera2 import Picamera2, Preview
class ChessGame: class ChessGame:
def __init__(self, difficulty, show_cv, show_cam, test_img = None): def __init__(self, difficulty, show_cv, show_cam, calibrate, 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.show_cam = show_cam self.show_cam = show_cam
self.test_img = test_img self.test_img = test_img
self.calibrate = calibrate
self.img_idx = 0 self.img_idx = 0
self.left_cut = 30
self.right_cut = 290
self.top_cut = 0
self.bottom_cut = 290
self.picam2 = Picamera2() self.picam2 = Picamera2()
def calibrate_cam():
pass
def start_game(self): def start_game(self):
if (self.calibrate):
self.calibrate_cam()
print(f"Starting chess game (difficulty: {self.difficulty})") print(f"Starting chess game (difficulty: {self.difficulty})")
# TODO - call initialize board in board_detector, initialize colors for color analysis, # TODO - call initialize board in board_detector, initialize colors for color analysis,
...@@ -32,7 +44,7 @@ class ChessGame: ...@@ -32,7 +44,7 @@ class ChessGame:
init_show_cv(self.show_cv) init_show_cv(self.show_cv)
preview_config = self.picam2.create_preview_configuration(main={"size": (2000, 2000)}) preview_config = self.picam2.create_preview_configuration(main={"size": (2464, 2464)})
self.picam2.configure(preview_config) self.picam2.configure(preview_config)
if (self.show_cam): if (self.show_cam):
self.picam2.start_preview(Preview.QTGL) self.picam2.start_preview(Preview.QTGL)
...@@ -50,15 +62,16 @@ class ChessGame: ...@@ -50,15 +62,16 @@ class ChessGame:
# 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.take_pic() orig_img = self.take_pic()
h,w,c = orig_img.shape h,w,c = orig_img.shape
cropped_img = orig_img[0:h-100, 0:w-100] # 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 = cv2.resize(cropped_img, (512, 512))
# img = orig_img # img = orig_img
if (self.show_cv): if (self.show_cv):
# cv2.imshow('Original Image Before Processing', img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
display_img([orig_img, img]) display_img([orig_img, img])
# display_img([img])
warped_img, sorted_warped_points = find_board(img) warped_img, sorted_warped_points = find_board(img)
...@@ -108,8 +121,9 @@ if __name__ == "__main__": ...@@ -108,8 +121,9 @@ if __name__ == "__main__":
default="medium", help="Chess AI difficulty (how far it looks ahead)") 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_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("--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("--test_img", help="If specified, will use said image in test_images folder rather than camera input") parser.add_argument("--test_img", help="If specified, will use said image in test_images folder rather than camera input")
args = parser.parse_args() args = parser.parse_args()
game = ChessGame(args.difficulty, args.show_cv, args.show_cam, args.test_img) game = ChessGame(args.difficulty, args.show_cv, args.show_cam, args.calibrate, args.test_img)
game.start_game() game.start_game()
\ No newline at end of file
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