Worked on assistant
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
import argparse
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
import time
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
from typing import List, Dict, Any
|
||||
import json
|
||||
import cv2
|
||||
import numpy as np
|
||||
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.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)
|
||||
@@ -22,39 +22,29 @@ NEW_BUTTON = (1900, 1100)
|
||||
|
||||
SAVE_UNSOLVED = False
|
||||
UNSOLVED_DIR = "E:/shenzhen-solitaire/unsolved"
|
||||
SOLVER_PATH = '/home/lukas/documents/coding/rust/shenzhen-solitaire/target/release/solver'
|
||||
|
||||
def extern_solve(board: Board) -> List[Dict[str, Any]]:
|
||||
result = subprocess.run([SOLVER_PATH], input=board.to_json(), capture_output=True, text=True)
|
||||
return json.loads(result.stdout)
|
||||
|
||||
def extern_solve(board: Board) -> List[Action]:
|
||||
pass
|
||||
|
||||
|
||||
def solve(conf: configuration.Configuration) -> None:
|
||||
def take_screenshot() :
|
||||
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()
|
||||
return image
|
||||
|
||||
print("Solving")
|
||||
def solve(conf: configuration.Configuration) -> None:
|
||||
image = take_screenshot()
|
||||
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=UNSOLVED_DIR, suffix=".png")
|
||||
sock = os.fdopen(fd, "w")
|
||||
sock.close()
|
||||
cv2.imwrite(outfile, image)
|
||||
return
|
||||
solution = list(solution_iterator)
|
||||
print(f"Solved in {len(solution)} steps")
|
||||
clicker.handle_actions(solution, OFFSET, conf)
|
||||
actions = extern_solve(board)
|
||||
assert 0
|
||||
print(f"Solved in {len(actions)} steps")
|
||||
clicker.handle_actions(actions, OFFSET, conf)
|
||||
print("Solved")
|
||||
time.sleep(2)
|
||||
clicker.click(NEW_BUTTON, OFFSET)
|
||||
@@ -62,8 +52,18 @@ def solve(conf: configuration.Configuration) -> None:
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Solve board"
|
||||
)
|
||||
parser.add_argument(
|
||||
"config_path",
|
||||
type=str,
|
||||
help="Config path",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
time.sleep(3)
|
||||
conf = configuration.load("test_config.zip")
|
||||
conf = configuration.load(args.config_path)
|
||||
while True:
|
||||
solve(conf)
|
||||
|
||||
|
||||
@@ -1,41 +0,0 @@
|
||||
import copy
|
||||
import dataclasses
|
||||
import json
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
import shenzhen_solitaire.card_detection.adjustment as adjustment
|
||||
import shenzhen_solitaire.card_detection.card_finder as card_finder
|
||||
from shenzhen_solitaire.card_detection.configuration import Configuration
|
||||
import argparse
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Generate a configuration"""
|
||||
|
||||
parser = argparse.ArgumentParser(description='Process some integers.')
|
||||
parser.add_argument('screenshot_path', metavar='screenshot_path', type=str,
|
||||
help='Path to the screenshot')
|
||||
|
||||
args = parser.parse_args()
|
||||
print(args.screenshot_path)
|
||||
image = cv2.imread(args.screenshot_path)
|
||||
|
||||
border_adjustment = adjustment.adjust_squares(image, count_x=8, count_y=13)
|
||||
border_square_pos = adjustment.adjust_squares(
|
||||
image, count_x=1, count_y=1, adjustment=copy.deepcopy(border_adjustment)
|
||||
)
|
||||
border_square = card_finder.get_field_squares(image, border_square_pos, 1, 1)
|
||||
empty_square_pos = adjustment.adjust_squares(
|
||||
image, count_x=1, count_y=1, adjustment=copy.deepcopy(border_adjustment)
|
||||
)
|
||||
empty_square = card_finder.get_field_squares(image, empty_square_pos, 1, 1)
|
||||
|
||||
cv2.imwrite("/tmp/border_square.png", border_square[0])
|
||||
cv2.imwrite("/tmp/empty_square.png", empty_square[0])
|
||||
print(json.dumps(dataclasses.asdict(border_adjustment)))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,43 +0,0 @@
|
||||
import copy
|
||||
import dataclasses
|
||||
import json
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
import shenzhen_solitaire.card_detection.adjustment as adjustment
|
||||
import shenzhen_solitaire.card_detection.card_finder as card_finder
|
||||
from shenzhen_solitaire.card_detection.configuration import Configuration
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Generate a configuration"""
|
||||
image = cv2.imread("pictures/specific/BunkerCards.jpg")
|
||||
|
||||
bunker_adjustment = adjustment.adjust_squares(
|
||||
image,
|
||||
count_x=3,
|
||||
count_y=1,
|
||||
adjustment=adjustment.Adjustment(
|
||||
**{"x": 730, "y": 310, "w": 19, "h": 21, "dx": 152, "dy": 0}
|
||||
),
|
||||
)
|
||||
print(json.dumps(dataclasses.asdict(bunker_adjustment)))
|
||||
|
||||
back_image = cv2.imread("pictures/specific/BaiShiny.jpg")
|
||||
back_squares = card_finder.get_field_squares(
|
||||
back_image, count_x=1, count_y=3, adjustment=copy.deepcopy(bunker_adjustment)
|
||||
)
|
||||
|
||||
green_image = cv2.imread("pictures/20190809172213_1.jpg")
|
||||
green_squares = card_finder.get_field_squares(
|
||||
green_image, count_x=1, count_y=3, adjustment=copy.deepcopy(bunker_adjustment)
|
||||
)
|
||||
|
||||
cv2.imwrite("/tmp/bunker_green_1.png", green_squares[0])
|
||||
cv2.imwrite("/tmp/bunker_green_2.png", green_squares[1])
|
||||
cv2.imwrite("/tmp/bunker_green_3.png", green_squares[2])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -2,7 +2,7 @@ import argparse
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
import copy
|
||||
from shenzhen_solitaire.card_detection import configuration, adjustment, card_finder
|
||||
from shenzhen_solitaire.card_detection.configuration import Configuration
|
||||
|
||||
@@ -31,11 +31,19 @@ def main() -> None:
|
||||
args = parser.parse_args()
|
||||
image = cv2.imread(args.screenshot_path)
|
||||
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.catalogue.extend(catalogue)
|
||||
|
||||
conf.card_border.extend(
|
||||
card_finder.get_field_squares(image, conf.border_adjustment, 1, 1)
|
||||
)
|
||||
|
||||
empty_adjust = copy.deepcopy(conf.border_adjustment)
|
||||
empty_adjust.y = empty_adjust.y + 4 * empty_adjust.dy
|
||||
conf.empty_card.extend(card_finder.get_field_squares(image, empty_adjust, 1, 1))
|
||||
|
||||
conf.green_card.extend(
|
||||
card_finder.get_field_squares(image, conf.bunker_adjustment, 1, 3)
|
||||
)
|
||||
@@ -45,7 +53,7 @@ def main() -> None:
|
||||
conf.green_card.extend(
|
||||
card_finder.get_field_squares(image, conf.hua_adjustment, 1, 1)
|
||||
)
|
||||
conf.catalogue.extend(catalogue)
|
||||
|
||||
configuration.save(conf, args.config_path)
|
||||
|
||||
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
import argparse
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
from shenzhen_solitaire.card_detection import configuration, adjustment, card_finder
|
||||
from shenzhen_solitaire.card_detection.configuration import Configuration
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Generate a configuration"""
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Generate pictures for symbols, "
|
||||
"requires screenshot of field with no moved cards, "
|
||||
"so 8 columns of 5 cards each"
|
||||
)
|
||||
parser.add_argument(
|
||||
"screenshot_path",
|
||||
metavar="screenshot_path",
|
||||
type=str,
|
||||
help="Path to the screenshot",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--conf",
|
||||
dest="config_path",
|
||||
type=str,
|
||||
default="config.zip",
|
||||
help="Path to existing config to be merged, or new config",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
print(args.screenshot_path)
|
||||
image = cv2.imread(args.screenshot_path)
|
||||
|
||||
adj = adjustment.adjust_field(image)
|
||||
squares = card_finder.get_field_squares(image, adj, 5, 8)
|
||||
catalogue = card_finder.catalogue_cards(squares)
|
||||
generated_config = Configuration(field_adjustment=adj, catalogue=catalogue, meta={})
|
||||
configuration.save(generated_config, args.config_path)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,38 +0,0 @@
|
||||
import copy
|
||||
import dataclasses
|
||||
import json
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
import shenzhen_solitaire.card_detection.adjustment as adjustment
|
||||
import shenzhen_solitaire.card_detection.card_finder as card_finder
|
||||
from shenzhen_solitaire.card_detection.configuration import Configuration
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Generate a configuration"""
|
||||
image = cv2.imread("pictures/specific/BaiShiny.jpg")
|
||||
|
||||
goal_adjustment = adjustment.adjust_squares(
|
||||
image,
|
||||
count_x=3,
|
||||
count_y=1,
|
||||
adjustment=adjustment.Adjustment(
|
||||
**{"x": 1490, "y": 310, "w": 19, "h": 21, "dx": 152, "dy": 0}
|
||||
),
|
||||
)
|
||||
print(json.dumps(dataclasses.asdict(goal_adjustment)))
|
||||
|
||||
green_image = cv2.imread("pictures/20190809172213_1.jpg")
|
||||
green_squares = card_finder.get_field_squares(
|
||||
green_image, count_x=1, count_y=3, adjustment=copy.deepcopy(goal_adjustment)
|
||||
)
|
||||
|
||||
cv2.imwrite("/tmp/goal_green_1.png", green_squares[0])
|
||||
cv2.imwrite("/tmp/goal_green_2.png", green_squares[1])
|
||||
cv2.imwrite("/tmp/goal_green_3.png", green_squares[2])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,34 +0,0 @@
|
||||
import copy
|
||||
import dataclasses
|
||||
import json
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
import shenzhen_solitaire.card_detection.adjustment as adjustment
|
||||
import shenzhen_solitaire.card_detection.card_finder as card_finder
|
||||
from shenzhen_solitaire.card_detection.configuration import Configuration
|
||||
|
||||
|
||||
def main() -> None:
|
||||
"""Generate a configuration"""
|
||||
image = cv2.imread("pictures/specific/BunkerCards.jpg")
|
||||
|
||||
hua_adjustment = adjustment.adjust_squares(
|
||||
image,
|
||||
count_x=1,
|
||||
count_y=1,
|
||||
adjustment=adjustment.Adjustment(
|
||||
**{"x": 1299, "y": 314, "w": 19, "h": 21, "dx": 0, "dy": 0}
|
||||
),
|
||||
)
|
||||
print(json.dumps(dataclasses.asdict(hua_adjustment)))
|
||||
green_image = cv2.imread("pictures/specific/ZhongShiny.jpg")
|
||||
hua_green = card_finder.get_field_squares(
|
||||
green_image, hua_adjustment, count_x=1, count_y=1
|
||||
)
|
||||
cv2.imwrite("/tmp/hua_green.png", hua_green[0])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -1,14 +1,21 @@
|
||||
from shenzhen_solitaire.card_detection.board_parser import parse_to_json
|
||||
import shenzhen_solitaire.card_detection.configuration as configuration
|
||||
import cv2
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
import cv2
|
||||
|
||||
import shenzhen_solitaire.card_detection.configuration as configuration
|
||||
from shenzhen_solitaire.card_detection.board_parser import parse_to_json
|
||||
|
||||
|
||||
def main() -> None:
|
||||
if len(sys.argv) < 2:
|
||||
print("Give filename pls")
|
||||
return
|
||||
image = cv2.imread(str(sys.argv[1]))
|
||||
parser = argparse.ArgumentParser(description="Parse board to json")
|
||||
parser.add_argument("board_path", type=str, help="Path to image of board")
|
||||
parser.add_argument(
|
||||
"--config", dest="config_path", type=str, help="Config path",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
image = cv2.imread(args.board_path)
|
||||
|
||||
conf = configuration.load("test_config.zip")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user