tuxgo/tuxgo/__main__.py

61 lines
1.5 KiB
Python

# SPDX-License-Identifier: AGPL-3.0-or-later
# SPDX-FileCopyrightText: 2023 Wojtek Porczyk <woju@hackerspace.pl>
import pprint
import click
import click_default_group
import cv2
import pygame
import pygame.display
from loguru import logger
from . import (
detector as _detector,
game_common,
bluetooth,
)
@click.group(
cls=click_default_group.DefaultGroup,
default='game',
default_if_no_args=True,
)
def cli():
pass
@cli.command('game')
@click.option('--mac', required=True)
def game(mac):
logger.debug('pygame.init()')
pygame.init()
pygame.display.set_caption('tuxgo')
pygame.mouse.set_visible(False)
screen = pygame.display.set_mode(flags=pygame.FULLSCREEN)
bot = bluetooth.BluetoothBot(mac)
game_common.Game(screen, bot).run()
@cli.command('debug-image')
@click.option('--blur', type=click.IntRange(min=1), default=1)
@click.argument('inpath', type=click.Path(file_okay=True, dir_okay=False))
@click.argument('outpath', type=click.Path(file_okay=True, dir_okay=False))
def debug_image(blur, inpath, outpath):
detector = _detector.Detector()
frame = cv2.imread(inpath)
if blur > 1:
frame = cv2.blur(frame, (blur, blur))
detections = list(detector.detect_markers(frame))
analyser = _detector.Analyser(detections)
try:
programme = list(analyser.get_programme(debug_frame=frame))
pprint.pprint(programme)
finally:
cv2.imwrite(outpath, frame)
if __name__ == '__main__':
cli()