-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
152 lines (129 loc) · 6.51 KB
/
run.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import inputgen
import log
import model as nn
import cupy as np
import uuid
import sys
import os
from tqdm.auto import tqdm
# How to use: run.py <model file>
# Getting all arguments from terminal
argv = sys.argv
def tensorflow_shutup():
"""
Make Tensorflow less verbose
"""
try:
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
# noinspection PyPackageRequirements
import tensorflow as tf
from tensorflow.python.util import deprecation
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
# Monkey patching deprecation utils to shut it up! Maybe good idea to disable this once after upgrade
# noinspection PyUnusedLocal
def deprecated(date, instructions, warn_once=True): # pylint: disable=unused-argument
def deprecated_wrapper(func):
return func
return deprecated_wrapper
deprecation.deprecated = deprecated
except ImportError:
pass
def rgb_to_hex(r, g, b):
"""Convert an RGB color tuple to a hexadecimal string"""
# Convert the values to integers
r, g, b = int(r), int(g), int(b)
# Clamp the values to the range 0-255
r = max(0, min(255, r))
g = max(0, min(255, g))
b = max(0, min(255, b))
# Convert the values to hexadecimal strings
hex_r = hex(r)[2:].zfill(2)
hex_g = hex(g)[2:].zfill(2)
hex_b = hex(b)[2:].zfill(2)
# Concatenate the hex strings into a single hexadecimal string
return f"#{hex_r}{hex_g}{hex_b}"
tensorflow_shutup()
log.info("Starting XAML-STYLEGEN")
if not len(argv) > 1:
log.error("The first required argument is missing: the name of the model file without extension in the models folder")
sys.exit(1)
else:
model = nn.load_model(f"models\\{argv[1]}.h5")
log.info(f"Loaded model {argv[1]}.h5")
rng = int(input("How many styles you need to generate: "))
log.info("Generating xml style files")
for i in tqdm(range(rng)):
x = inputgen.generate_inputs(nn.get_batch_size()).get()
indices = np.arange(x.shape[0])
np.random.shuffle(indices)
x = x[indices.get()]
predictions = model.predict(x)
np.set_printoptions(suppress=True, precision=2)
predictions_res = np.round(predictions, decimals=2)
predictions_res = np.fabs(predictions_res).get()
isfocused = f"""<Trigger Property="Button.IsFocused" Value="True">
<Setter TargetName="border" Property="BorderThickness" Value="{int(predictions_res[0][13])}" />
<Setter TargetName="border" Property="BorderBrush" Value="{rgb_to_hex(int(predictions_res[0][14]), int(predictions_res[0][15]), int(predictions_res[0][16]))}" />
</Trigger>"""
isenabled = f"""<Trigger Property="Button.IsEnabled" Value="False">
<Setter Property="Background" Value="{rgb_to_hex(int(predictions_res[0][18]), int(predictions_res[0][19]), int(predictions_res[0][20]))}" />
<Setter TargetName="border" Property="BorderBrush" Value="{rgb_to_hex(int(predictions_res[0][21]), int(predictions_res[0][22]), int(predictions_res[0][23]))}" />
</Trigger>"""
rounding = f"""<Style.Resources>
<Style TargetType="Border">
<Setter Property="CornerRadius" Value="{int(predictions_res[0][25])}" />
</Style>
</Style.Resources>"""
mouseentered = f"""<EventTrigger RoutedEvent="Button.MouseEnter">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)"
To="{rgb_to_hex(int(predictions_res[0][27]), int(predictions_res[0][28]), int(predictions_res[0][29]))}" Duration="0:0:{float(predictions_res[0][30])}" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>"""
mouseleave = f"""<EventTrigger RoutedEvent="Button.MouseLeave">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)"
To="{rgb_to_hex(int(predictions_res[0][32]), int(predictions_res[0][33]), int(predictions_res[0][34]))}" Duration="0:0:{float(predictions_res[0][35])}" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>"""
with open(f'outputs\\{str(uuid.uuid4())}.xml', 'w') as xml:
xml.write(
f'''<Style x:Key="CustomButtonStyle" TargetType="Button">
<Setter Property="Background" Value="{rgb_to_hex(int(predictions_res[0][0]), int(predictions_res[0][1]), int(predictions_res[0][2]))}" />
<Setter Property="Foreground" Value="{rgb_to_hex(int(predictions_res[0][3]), int(predictions_res[0][4]), int(predictions_res[0][5]))}" />
<Setter Property="FontSize" Value="{int(predictions_res[0][6])}" />
{'<Setter Property="FocusVisualStyle" Value="{{x:Null}}" />' if int(predictions_res[0][7]) == 1 else ''}
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{{x:Type Button}}">
<Border Background="{{TemplateBinding Background}}" Cursor="Hand">
<Border x:Name="border" BorderBrush="{rgb_to_hex(int(predictions_res[0][8]), int(predictions_res[0][9]), int(predictions_res[0][10]))}" BorderThickness="{int(predictions_res[0][11])}" Background="Transparent">
<ContentPresenter VerticalAlignment="{{TemplateBinding VerticalContentAlignment}}" HorizontalAlignment="{{TemplateBinding HorizontalContentAlignment}}" ContentSource="{{TemplateBinding Content}}" />
</Border>
</Border>
<ControlTemplate.Triggers>
{isfocused if int(predictions_res[0][12]) == 1 else ''}
{isenabled if int(predictions_res[0][17]) == 1 else ''}
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
{rounding if int(predictions_res[0][24]) == 1 else ''}
<Style.Triggers>
{mouseentered if int(predictions_res[0][26]) == 1 else ''}
{mouseleave if int(predictions_res[0][31]) == 1 else ''}
</Style.Triggers>
</Style>
''')