Worked on rust compatibility
This commit is contained in:
@@ -6,6 +6,7 @@ from typing import Any, Dict, Iterable, List, Optional, Tuple, Union
|
|||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import json
|
||||||
|
|
||||||
from ..board import Board, Card, NumberCard, SpecialCard
|
from ..board import Board, Card, NumberCard, SpecialCard
|
||||||
from . import adjustment, card_finder
|
from . import adjustment, card_finder
|
||||||
@@ -230,3 +231,41 @@ def parse_board(image: np.ndarray, conf: Configuration) -> Board:
|
|||||||
result.bunker = parse_bunker(image, conf)
|
result.bunker = parse_bunker(image, conf)
|
||||||
result.goal = parse_goal(image, conf)
|
result.goal = parse_goal(image, conf)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def field_card_to_str(card: Card):
|
||||||
|
if card == SpecialCard.Hua:
|
||||||
|
return "Hua"
|
||||||
|
if isinstance(card, SpecialCard):
|
||||||
|
return {"Special": card.name}
|
||||||
|
elif isinstance(card, NumberCard):
|
||||||
|
return {"Number": {"value": card.number, "suit": card.suit.name}}
|
||||||
|
|
||||||
|
|
||||||
|
def bunker_card_to_str(card: Union[Tuple[SpecialCard, int], Optional[Card]]):
|
||||||
|
if card is None:
|
||||||
|
return "Empty"
|
||||||
|
if isinstance(card, tuple):
|
||||||
|
return {"Blocked": card[0].name}
|
||||||
|
return {"Stashed": field_card_to_str(card)}
|
||||||
|
|
||||||
|
|
||||||
|
def goal_card_to_str(card: Optional[NumberCard]):
|
||||||
|
if card is None:
|
||||||
|
return None
|
||||||
|
return {"value": card.number, "suit": card.suit.name}
|
||||||
|
|
||||||
|
|
||||||
|
def parse_to_json(image: np.ndarray, conf: Configuration) -> str:
|
||||||
|
field = parse_field(image, conf)
|
||||||
|
flower_gone = parse_hua(image, conf)
|
||||||
|
bunker = parse_bunker(image, conf)
|
||||||
|
goal = parse_goal(image, conf)
|
||||||
|
|
||||||
|
mystruct = {
|
||||||
|
"field": [[field_card_to_str(card) for card in row] for row in field],
|
||||||
|
"hua_set": flower_gone,
|
||||||
|
"bunker": [bunker_card_to_str(card) for card in bunker],
|
||||||
|
"goal": [goal_card_to_str(card) for card in goal],
|
||||||
|
}
|
||||||
|
return json.dumps(mystruct)
|
||||||
|
|||||||
19
tools/to_json.py
Normal file
19
tools/to_json.py
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
from shenzhen_solitaire.card_detection.board_parser import parse_to_json
|
||||||
|
import shenzhen_solitaire.card_detection.configuration as configuration
|
||||||
|
import cv2
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
if len(sys.argv) < 2:
|
||||||
|
print("Give filename pls")
|
||||||
|
return
|
||||||
|
image = cv2.imread(str(sys.argv[1]))
|
||||||
|
|
||||||
|
conf = configuration.load("test_config.zip")
|
||||||
|
|
||||||
|
print(parse_to_json(image, conf))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user