|
1
|
|
|
import typing as t |
|
2
|
|
|
import os |
|
3
|
|
|
import sys |
|
4
|
|
|
import click |
|
5
|
|
|
from ._main import create_algo_runner |
|
6
|
|
|
from artificial_artwork import __version__ |
|
7
|
|
|
|
|
8
|
|
|
class WithStateAttribute(t.Protocol): |
|
9
|
|
|
"""Protocol for classes that have a state attribute.""" |
|
10
|
|
|
state: t.Any |
|
11
|
|
|
|
|
12
|
|
|
class HandleAlgorithmProgressUpdatesAble(t.Protocol): |
|
13
|
|
|
def update(self, subject: WithStateAttribute) -> None: ... |
|
14
|
|
|
|
|
15
|
|
|
this_file_location = os.path.dirname(os.path.realpath(os.path.abspath(__file__))) |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
# CLI --version flag |
|
19
|
|
|
def version_msg(): |
|
20
|
|
|
"""artificial_artwork CLI version, lib location and Python version. |
|
21
|
|
|
|
|
22
|
|
|
Get message about artificial_artwork version, location |
|
23
|
|
|
and Python version. |
|
24
|
|
|
""" |
|
25
|
|
|
# extract everything about version: major, minor, patch and build notes |
|
26
|
|
|
python_version = sys.version |
|
27
|
|
|
message = u"Neural Style Transfer CLI %(version)s from {} (Python {})" |
|
28
|
|
|
location = os.path.dirname(this_file_location) |
|
29
|
|
|
return message.format(location, python_version) |
|
30
|
|
|
|
|
31
|
|
|
# MAIN |
|
32
|
|
|
@click.group() |
|
33
|
|
|
@click.version_option(__version__, u'-V', u'--version', message=version_msg()) |
|
34
|
|
|
def entry_point(): |
|
35
|
|
|
pass |
|
36
|
|
|
|
|
37
|
|
|
# RUN CMD |
|
38
|
|
|
@click.command() |
|
39
|
|
|
@click.argument('content_image') |
|
40
|
|
|
@click.argument('style_image') |
|
41
|
|
|
@click.option('--iterations', '-it', type=int, default=100, show_default=True) |
|
42
|
|
|
@click.option('--location', '-l', type=str, default='.') |
|
43
|
|
|
def run(content_image, style_image, iterations, location): |
|
44
|
|
|
|
|
45
|
|
|
backend_objs: t.Dict[str, t.Any] = create_algo_runner( |
|
46
|
|
|
iterations=iterations, |
|
47
|
|
|
output_folder=location, |
|
48
|
|
|
noisy_ratio=0.6, |
|
49
|
|
|
) |
|
50
|
|
|
run_nst: t.Callable[[str, str], None] = backend_objs['run'] |
|
51
|
|
|
# subscribe_callback: t.Callable[[HandleAlgorithmProgressUpdatesAble], None] = backend_objs['subscribe'] |
|
52
|
|
|
|
|
53
|
|
|
run_nst(content_image, style_image) |
|
54
|
|
|
|
|
55
|
|
|
### NST CLI Entrypoint ### |
|
56
|
|
|
|
|
57
|
|
|
# ATTACH CMDs |
|
58
|
|
|
entry_point.add_command(run) |
|
59
|
|
|
from .cmd_demo import demo |
|
60
|
|
|
entry_point.add_command(demo) |
|
61
|
|
|
|