Passed
Push — master ( ee1e78...515b92 )
by Konstantinos
01:14
created

artificial_artwork.cli.cli()   B

Complexity

Conditions 1

Size

Total Lines 53
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 37
nop 5
dl 0
loc 53
rs 8.9919
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
2
import os
3
import click
4
5
from .disk_operations import Disk
6
from .styling_observer import StylingObserver
7
from .algorithm import NSTAlgorithm, AlogirthmParameters
8
from .nst_tf_algorithm import NSTAlgorithmRunner
9
from .image import ImageFactory, ImageProcessingConfig
10
from .algorithm_progress import NSTAlgorithmProgress
11
from .termination_condition.termination_condition import TerminationConditionFacility
12
from .termination_condition_adapter import TerminationConditionAdapterFactory
13
14
15
def get_vgg_verydeep_19_model():
16
    try:
17
        return os.environ['AA_VGG_19']
18
    except KeyError:
19
        file_path = os.path.join(os.getcwd(), 'imagenet-vgg-verydeep-19.mat')
20
        if os.path.exists(file_path):
21
            return file_path
22
        file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'imagenet-vgg-verydeep-19.mat')
23
        if os.path.exists(file_path):
24
            return file_path
25
    raise NoImageModelSpesifiedError('No pretrained image model found. '
26
        'Please download it and set the AA_VGG_19 environment variable with the'
27
        'path where ou stored the model (*.mat file), to indicate to wher to '
28
        'locate and load it')
29
30
31
class NoImageModelSpesifiedError(Exception): pass
32
33
34
@click.command()
35
@click.argument('content_image')
36
@click.argument('style_image')
37
@click.option('--interactive', '-i', type=bool, default=True)
38
@click.option('--iterations', '-it', type=int, default=100)
39
@click.option('--location', '-l', type=str, default='.')
40
def cli(content_image, style_image, interactive, iterations, location):
41
42
    IMAGE_MODEL_PATH = get_vgg_verydeep_19_model()
43
    TERMINATION_CONDITION = 'max-iterations'
44
    STYLE_LAYERS = [
45
        ('conv1_1', 0.2),
46
        ('conv2_1', 0.2),
47
        ('conv3_1', 0.2),
48
        ('conv4_1', 0.2),
49
        ('conv5_1', 0.2),
50
    ]
51
52
    image_factory = ImageFactory(Disk.load_image)
53
54
    # for now we have hardcoded the config to receive 300 x 400 images with 3 color channels
55
    image_process_config = ImageProcessingConfig.from_image_dimensions()
56
    
57
    termination_condition = TerminationConditionFacility.create(TERMINATION_CONDITION, iterations)
58
    termination_condition_adapter = TerminationConditionAdapterFactory.create(TERMINATION_CONDITION, termination_condition)
59
    print(f' -- Termination Condition: {termination_condition}')
60
61
    algorithm_parameters = AlogirthmParameters(
62
        image_factory.from_disk(content_image),
63
        image_factory.from_disk(style_image),
64
        IMAGE_MODEL_PATH,
65
        STYLE_LAYERS,
66
        termination_condition_adapter,
67
        location,
68
    )
69
70
    algorithm = NSTAlgorithm(algorithm_parameters, image_process_config)
71
72
    algorithm_runner = NSTAlgorithmRunner.default(algorithm, image_factory.image_processor.noisy)
73
74
    algorithm_progress = NSTAlgorithmProgress({})
75
    styling_observer = StylingObserver(Disk.save_image)
76
    
77
    algorithm_runner.progress_subject.add(
78
        algorithm_progress,
79
        termination_condition_adapter,
80
    )
81
    algorithm_runner.peristance_subject.add(
82
        styling_observer
83
    )
84
            
85
86
    algorithm_runner.run()
87
88
89
if __name__ == '__main__':
90
    cli()
91