diff --git a/colors.txt b/colors.txt deleted file mode 100644 index 696b0d8..0000000 --- a/colors.txt +++ /dev/null @@ -1,6 +0,0 @@ -65, 65, 65 bai -0, 0, 0 black -22, 48, 178 red -76, 111, 19 green - - diff --git a/test/cv_helper.py b/test/cv_helper.py index 1e0b383..ea851d5 100644 --- a/test/cv_helper.py +++ b/test/cv_helper.py @@ -1,8 +1,12 @@ import itertools from typing import Tuple, List, Dict +import zipfile +import io +import json +import dataclasses + import numpy as np # type: ignore import cv2 # type: ignore -import zipfile from .context import shenzhen_solitaire from shenzhen_solitaire.cv import adjustment @@ -30,52 +34,47 @@ def simplify(image: np.ndarray) -> None: cv2.waitKey(0) cv2.destroyWindow("Window") +def calibrate(image: np.ndarray) -> None: + adj = adjustment.adjust_field(image) + squares = card_finder.get_field_squares(image, adj) + catalogue = card_finder.catalague_cards(squares) + simplified_catalogue = [] + for square, card in catalogue: + simplified_catalogue.append((card_finder.simplify(square), card)) + + zip_stream = io.BytesIO() + with zipfile.ZipFile(zip_stream, "w") as zip_file: + zip_file.writestr('adjustment.json', json.dumps(dataclasses.asdict(adj))) + + file_stream = io.BytesIO() + np.save(file_stream, squares[0], allow_pickle=False) + print(file_stream.getvalue()) + zip_file.writestr('0.dat', file_stream.getvalue()) + print() + print(zip_stream.getvalue()) + with open('myzip.zip', 'wb') as fd: + fd.write(zip_stream.getvalue()) + def main() -> None: - #image = cv2.imread("Solitaire.png") with open("Solitaire.png", 'rb') as fd: img_str = fd.read() nparr = np.frombuffer(img_str, np.uint8) image = cv2.imdecode(nparr, cv2.IMREAD_COLOR) - #image2 = cv2.imread("Solitaire2.png") - #image2 = cv2.resize(image2, (1000, 629)) - image2 = cv2.imread("Solitaire.png") - adj = adjustment.adjust_field(image) - squares = card_finder.get_field_squares(image, adj) - print(squares[0]) - np.save('0.dat', squares[0], allow_pickle=False) - assert 0 - with open("0.dat", 'wb') as fd: - fd.write(squares[0].tobytes()) - catalogue = card_finder.catalague_cards(squares[:5]) - my_zip = zipfile.ZipFile('myzip.zip', mode='w') - for index, x in enumerate(catalogue): - my_zip.writestr(f"{index}.dat", x[0].tobytes()) - my_zip.close() - assert 0 + #print(squares[0]) - squares = card_finder.get_simplified_squares(image, adj) - print("Simplified") - - adj.x -= 2 - adj.y -= 2 - adj.w += 5 - adj.h += 5 - - image_squares = card_finder.get_field_squares(image2, adj) - for i in range(10, 20): - image_squares[i], _ = card_finder.simplify(image_squares[i]) - print("Finding...") - found_image, certainty = card_finder.find_square( - image_squares[i], squares) - print(certainty) def main2() -> None: - A = np.load('0.dat.npy') + file_stream = None + with zipfile.ZipFile('myzip.zip', "r") as zip_file: + file_stream = io.BytesIO(zip_file.read('0.dat')) + + A = np.load(file_stream) print(A) if __name__ == "__main__": + main() main2()