Made calibration more ergonomic. Crashes because loading empty 'empty_card' directory

This commit is contained in:
Lukas Wölfer
2020-06-12 17:31:12 +02:00
parent 6565792030
commit f4ac445f61
5 changed files with 79 additions and 29 deletions

View File

@@ -1,43 +1,53 @@
import os
import tempfile
import time
from pathlib import Path
from typing import List
import cv2
import numpy as np
import os
import pyautogui
import shenzhen_solitaire.card_detection.configuration as configuration
import shenzhen_solitaire.clicker as clicker
import shenzhen_solitaire.solver.solver as solver
from shenzhen_solitaire.card_detection.board_parser import parse_board
from shenzhen_solitaire.board import Board
from shenzhen_solitaire.card_detection.board_parser import parse_start_board
from shenzhen_solitaire.solver.board_actions import Action
OFFSET = (0, 0)
SIZE = (2560, 1440)
# SIZE = (2560, 1440)
SIZE = (1366, 768)
NEW_BUTTON = (1900, 1100)
SAVE_UNSOLVED = False
UNSOLVED_DIR = "E:/shenzhen-solitaire/unsolved"
def solve() -> None:
with tempfile.TemporaryDirectory() as screenshot_dir:
def extern_solve(board: Board) -> List[Action]:
pass
def solve(conf: configuration.Configuration) -> None:
with tempfile.TemporaryDirectory(prefix="shenzhen_solitaire") as screenshot_dir:
print("Taking screenshot")
screenshot_file = Path(screenshot_dir) / "screenshot.png"
screenshot = pyautogui.screenshot(region=(*OFFSET, *SIZE))
screenshot.save(screenshot_file)
image = cv2.imread(str(screenshot_file))
input()
print("Solving")
conf = configuration.load("test_config.zip")
board = parse_board(image, conf)
board = parse_start_board(image, conf)
print(board.to_json())
assert board.check_correct()
input()
solution_iterator = next(solver.solve(board, timeout=10, verbose=True), None)
if solution_iterator is None:
clicker.click(NEW_BUTTON, OFFSET)
time.sleep(10)
if SAVE_UNSOLVED:
fd, outfile = tempfile.mkstemp(
dir="E:/shenzhen-solitaire/unsolved", suffix=".png"
)
fd, outfile = tempfile.mkstemp(dir=UNSOLVED_DIR, suffix=".png")
sock = os.fdopen(fd, "w")
sock.close()
cv2.imwrite(outfile, image)
@@ -53,8 +63,9 @@ def solve() -> None:
def main() -> None:
time.sleep(3)
conf = configuration.load("test_config.zip")
while True:
solve()
solve(conf)
if __name__ == "__main__":

View File

@@ -49,25 +49,30 @@ def main() -> None:
image, count_x=8, count_y=13, adjustment=copy.deepcopy(conf.field_adjustment)
)
print("Field borders")
border_adjustment = adjustment.adjust_squares(
conf.border_adjustment = adjustment.adjust_squares(
image, count_x=8, count_y=13, adjustment=copy.deepcopy(conf.field_adjustment)
)
conf.bunker_adjustment.w = conf.field_adjustment.w
conf.bunker_adjustment.h = conf.field_adjustment.h
for adj in (conf.bunker_adjustment, conf.goal_adjustment,conf.hua_adjustment):
adj.w = conf.field_adjustment.w
adj.h = conf.field_adjustment.h
adj.dx = conf.field_adjustment.dx
adj.dy = conf.field_adjustment.dy
conf.bunker_adjustment.x = conf.field_adjustment.x
print("Bunker cards")
bunker_adjustment = adjustment.adjust_squares(
conf.bunker_adjustment = adjustment.adjust_squares(
image, count_x=3, count_y=1, adjustment=copy.deepcopy(conf.bunker_adjustment)
)
conf.goal_adjustment.w = conf.field_adjustment.w
conf.goal_adjustment.h = conf.field_adjustment.h
conf.goal_adjustment.x = conf.field_adjustment.x + 5 * conf.field_adjustment.dx
conf.goal_adjustment.y = conf.bunker_adjustment.y
print("Goal cards")
goal_adjustment = adjustment.adjust_squares(
conf.goal_adjustment = adjustment.adjust_squares(
image, count_x=3, count_y=1, adjustment=copy.deepcopy(conf.goal_adjustment)
)
conf.hua_adjustment.w = conf.field_adjustment.w
conf.hua_adjustment.h = conf.field_adjustment.h
conf.hua_adjustment.y = conf.bunker_adjustment.y
print("Hua card")
hua_adjustment = adjustment.adjust_squares(
conf.hua_adjustment = adjustment.adjust_squares(
image, count_x=1, count_y=1, adjustment=copy.deepcopy(conf.hua_adjustment)
)

View File

@@ -21,7 +21,7 @@ def main() -> None:
help="Path to the screenshot",
)
parser.add_argument(
"--conf",
"--config",
dest="config_path",
type=str,
default="config.zip",
@@ -29,9 +29,8 @@ def main() -> None:
)
args = parser.parse_args()
print(args.screenshot_path)
image = cv2.imread(args.screenshot_path)
conf = configuration.load(args.config)
conf = configuration.load(args.config_path)
squares = card_finder.get_field_squares(image, conf.field_adjustment, 5, 8)
catalogue = card_finder.catalogue_cards(squares)
conf.card_border.extend(