advancedskrald/opencv_video.py

44 lines
1.3 KiB
Python
Raw Normal View History

2019-04-04 10:59:37 +00:00
from datetime import datetime
import cv2
2019-04-04 10:59:37 +00:00
import runner
from util import FILE, RANK, PIECE, COLOR, imwrite, POSITION
2019-04-04 10:59:37 +00:00
cap = cv2.VideoCapture(0)
2019-04-04 10:59:37 +00:00
color = COLOR.BLACK
rank = RANK.EIGHT
2019-04-04 10:59:37 +00:00
pieces = {
PIECE.rook: [POSITION((FILE.A, rank)), POSITION((FILE.F, rank))],
PIECE.knight: [POSITION((FILE.E, rank)), POSITION((FILE.H, rank))],
PIECE.bishop: [POSITION((FILE.C, rank)), POSITION((FILE.D, rank))],
PIECE.queen: [POSITION((FILE.B, rank))],
PIECE.king: [POSITION((FILE.G, rank))],
2019-04-04 10:59:37 +00:00
}
while True:
2019-04-04 10:59:37 +00:00
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow("frame", frame)
2019-04-04 10:59:37 +00:00
if cv2.waitKey(100) & 0xFF == ord("c"):
2019-04-04 10:59:37 +00:00
print(f"capturing frame")
imwrite(f"whole_boards/boards_for_empty/board_{datetime.utcnow().timestamp()}_.png", frame)
2019-04-04 10:59:37 +00:00
warped = runner.warp_board(frame)
runner.save_empty_fields(warped, skip_rank=rank)
for piece, positions in pieces.items():
for position in positions:
square = runner.get_square(warped, *position)
2019-04-04 10:59:37 +00:00
x, y = position
imwrite(f"training_images/{piece}/{position.color}_square/training_{x}{str(y)}_{datetime.utcnow().timestamp()}.png", square)
2019-04-04 10:59:37 +00:00
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()