-
Notifications
You must be signed in to change notification settings - Fork 0
/
combinator.py
executable file
·1147 lines (823 loc) · 41 KB
/
combinator.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/Users/krister/anaconda3/envs/public-foundry-combinator/bin/python
import os, tempfile, datetime
from pathlib import Path
import copy
import re
import base64
import logging
import warnings
import absl.app
from absl import flags
from flask import Flask, jsonify, request, abort
from flask_cors import CORS
from flask_socketio import SocketIO
from bs4 import BeautifulSoup as soup
from svgpathtools import parse_path
import numpy as np
import PIL.PngImagePlugin
import PIL.ImageOps
import PIL.ImageEnhance
from PIL import ImageDraw
import tensorflow as tf
from tensor2tensor.data_generators import generator_utils
from tensor2tensor.utils import registry
from tensor2tensor.utils import contrib
from tensor2tensor.utils import trainer_lib
from tensor2tensor.layers import common_layers
from magenta.models import svg_vae
import fontforge
import svg_utils
from svg_t2t import generate_t2t_example
##########################################################################################
# STDOUT
##########################################################################################
# we could use colorama instead...
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
##########################################################################################
# PATHS
##########################################################################################
inferencepath = Path('./inference')
basepath = Path('.')
t2tpath = basepath/'t2t'
##########################################################################################
# LOGGING
##########################################################################################
# Suppress Flask's info logging.
log = logging.getLogger("werkzeug")
log.setLevel(logging.WARNING)
# shut up and play the hits
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
# warnings.simplefilter("ignore")
##########################################################################################
# FLASK
##########################################################################################
# Reference: https://github.com/tensorflow/minigo/blob/master/minigui/serve.py
app = Flask(__name__)
CORS(app)
socketio = SocketIO(app, logger=log, engineio_logger=log)
##########################################################################################
# TF
##########################################################################################
# only needed if we use initialize_model_with_t2t
# tfe = tf.contrib.eager
Modes = tf.estimator.ModeKeys
tf.compat.v1.enable_eager_execution()
problem_name = 'glyph_azzn_problem'
##########################################################################################
# HERE WE GO
##########################################################################################
# for now we just use our initial model
# once we add model switching we may want to put this in a dictionary
with tf.io.gfile.GFile(os.fspath(t2tpath/'mean.npz'), 'rb') as f: mean_npz = np.load(f)
with tf.io.gfile.GFile(os.fspath(t2tpath/'stdev.npz'), 'rb') as f: stdev_npz = np.load(f)
# our old order
# glyphs = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
# order used internally
glyphs = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
# probably useless -- we need a more intelligent way of producing filled glyphs
glyphMaxVoids = {
'0': 1,
'4': 1,
'6': 1,
'8': 2,
'9': 1,
'A': 1,
'B': 1,
'D': 1,
'O': 1,
'P': 1,
'Q': 1,
'R': 1,
'a': 1,
'b': 1,
'd': 1,
'e': 1,
'g': 1,
'o': 1,
'p': 1,
'q': 1,
}
maxpaths = 50
# borrowed from glyphtracer
font_ascent = 1000
font_height = 780
##########################################################################################
# MODEL
##########################################################################################
# UNUSED -- HERE FOR REFERENCE
# def initialize_model_with_t2t(hparam_set, add_hparams, model_name, ckpt_dir):
# """Returns an initialized model and hparams using our trained t2t data."""
#
# data_dir = os.fspath(t2tpath)
#
# tf.reset_default_graph()
#
# # create hparams and get glyphazzn problem definition
# # we don't need to add data_dir=data_dir
# hparams = trainer_lib.create_hparams(hparam_set, add_hparams, problem_name=problem_name)
# problem = registry.problem(problem_name)
#
# # get model definition
# ModelClass = registry.model(model_name)
# model = ModelClass(hparams, mode=Modes.PREDICT, problem_hparams=hparams.problem_hparams)
#
# # create dataset iterator from problem definition
# dataset = problem.dataset(Modes.PREDICT, dataset_split=Modes.TRAIN,
# data_dir=data_dir, shuffle_files=False, hparams=hparams).batch(1)
# iterator = tfe.Iterator(dataset)
#
# # finalize/initialize model
# output, extra_losses = model(iterator.next()) # creates ops to be initialized
# model.initialize_from_ckpt(ckpt_dir) # initializes ops
#
# return model, hparams
def initialize_model_with_example(example, hparam_set, add_hparams, model_name, ckpt_dir):
"""Returns an initialized model and hparams using our trained t2t data."""
tf.reset_default_graph()
# create hparams and get glyphazzn problem definition
# we don't need to add data_dir=data_dir
hparams = trainer_lib.create_hparams(hparam_set, add_hparams, problem_name=problem_name)
problem = registry.problem(problem_name)
# get model definition
ModelClass = registry.model(model_name)
model = ModelClass(hparams, mode=Modes.PREDICT, problem_hparams=hparams.problem_hparams)
# finalize/initialize model
features1 = preprocess_example(example, hparams) # passed by reference
output, extra_losses = model(features1) # creates ops to be initialized
model.initialize_from_ckpt(ckpt_dir) # initializes ops
return model, hparams, features1
def get_bottleneck(features, model):
"""Retrieve latent encoding for given input pixel image in features dict."""
features = features.copy()
# the presence of a 'bottleneck' feature with 0 dimensions indicates that the
# model should return the bottleneck from the input image
features['bottleneck'] = tf.zeros((0, 128))
return model(features)[0]
def infer_from_bottleneck(features, bottleneck, model, out='svg'):
"""Returns a sample from a decoder, conditioned on the given a latent."""
features = features.copy()
# set bottleneck which we're decoding from
features['bottleneck'] = bottleneck
# reset inputs/targets. This guarantees that the decoder is only being
# conditioned on the given bottleneck.
batch_size = tf.shape(bottleneck)[:1].numpy().tolist()
features['inputs'] = tf.zeros(
batch_size + tf.shape(features['inputs'])[1:].numpy().tolist())
features['targets'] = tf.zeros(
batch_size + tf.shape(features['targets'])[1:].numpy().tolist())
features['targets_psr'] = tf.zeros(
batch_size + tf.shape(features['targets_psr'])[1:].numpy().tolist())
if out == 'svg':
return model.infer(features, decode_length=0)
else:
return model(features)
##########################################################################################
# INFERENCE
##########################################################################################
def _tile(features, key, dims):
"""Helper that creates copies of features['keys'] across given dims."""
features[key] = tf.tile(features[key], dims)
return features
def decode_example(serialized_example):
"""Return a dict of Tensors from a serialized tensorflow.Example."""
data_fields = {'targets_rel': tf.FixedLenFeature([51*10], tf.float32),
'targets_rnd': tf.FixedLenFeature([64*64], tf.float32),
'targets_sln': tf.FixedLenFeature([1], tf.int64),
'targets_cls': tf.FixedLenFeature([1], tf.int64)}
# Necessary to rejoin examples in the correct order with the Cloud ML Engine
# batch prediction API.
data_fields["batch_prediction_key"] = tf.FixedLenFeature([1], tf.int64, 0)
data_items_to_decoders = {
field: contrib.slim().tfexample_decoder.Tensor(field) for field in data_fields
}
decoder = contrib.slim().tfexample_decoder.TFExampleDecoder(data_fields, data_items_to_decoders)
decode_items = list(sorted(data_items_to_decoders))
decoded = decoder.decode(serialized_example, items=decode_items)
return dict(zip(decode_items, decoded))
def preprocess_example(example, hparams):
""" Preprocess our example based on our magenta problem """
example['targets_cls'] = tf.reshape(example['targets_cls'], [1])
example['targets_sln'] = tf.reshape(example['targets_sln'], [1])
example['targets_rel'] = tf.reshape(example['targets_rel'], [51, 1, 10])
# normalize (via gaussian)
example['targets_rel'] = (example['targets_rel'] - mean_npz) / stdev_npz
# redefine shape inside model!
example['targets_psr'] = tf.reshape(example['targets_rnd'], [1, 64 * 64]) / 255.
del example['targets_rnd']
if hparams.just_render:
# training vae mode, use the last image (rendered icon) as input & output
example['inputs'] = example['targets_psr'][-1, :]
example['targets'] = example['targets_psr'][-1, :]
else:
example['inputs'] = tf.identity(example['targets_rel'])
example['targets'] = tf.identity(example['targets_rel'])
# our shaping
example["batch_prediction_key"] = tf.expand_dims(example["batch_prediction_key"], 0)
example["inputs"] = tf.expand_dims(example["inputs"], 0)
example["targets"] = tf.expand_dims(example["targets"], 0)
example["targets_cls"] = tf.expand_dims(example["targets_cls"], 0)
example["targets_psr"] = tf.expand_dims(example["targets_psr"], 0)
example["targets_rel"] = tf.expand_dims(example["targets_rel"], 0)
example["targets_sln"] = tf.expand_dims(example["targets_sln"], 0)
# we pass by reference so example is modified even if we don't return
return example
def infer_from_file(example_file, hparam_set, add_hparams, model_name, ckpt_dir, out='svg', bitmap_depth=8, bitmap_contrast=1, bitmap_fill=False):
""" Load, decode and infer our example """
# https://www.tensorflow.org/tutorials/load_data/tfrecord
raw_dataset = tf.data.TFRecordDataset([ os.fspath(example_file) ])
for raw_record in raw_dataset.take(1):
example = raw_record # we decode_example in infer()
return infer(example, hparam_set, add_hparams, model_name, ckpt_dir, out, bitmap_depth, bitmap_contrast, bitmap_fill)
def infer(example, hparam_set, add_hparams, model_name, ckpt_dir, out='svg', bitmap_depth=8, bitmap_contrast=1, bitmap_fill=False):
"""Decodes one example of each class, conditioned on the example."""
# initialize with t2t data
# model, hparams = initialize_model_with_t2t(hparam_set, add_hparams, model_name, ckpt_dir)
# features1 = preprocess_example(example, hparams) # passed by reference
# OR initialize with example
model, hparams, features1 = initialize_model_with_example(decode_example(example),
hparam_set, add_hparams, model_name, ckpt_dir)
# == the number of glyphs
num_classes = hparams.num_categories
# get bottleneck of the features we selected before
bottleneck1 = get_bottleneck(features1, model)
bottleneck1 = tf.tile(bottleneck1, [num_classes, 1])
# create class batch
new_features = copy.copy(features1)
clss_batch = tf.reshape([tf.constant([[clss]], dtype=tf.int64)
for clss in range(num_classes)], [-1, 1])
new_features['targets_cls'] = clss_batch
new_features = _tile(new_features, 'targets_psr', [num_classes, 1, 1])
inp_target_dim = [num_classes, 1, 1, 1] if out == 'svg' else [num_classes, 1]
new_features = _tile(new_features, 'inputs', inp_target_dim)
new_features = _tile(new_features, 'targets', inp_target_dim)
# run model
output_batch = infer_from_bottleneck(new_features, bottleneck1, model, out)
# render outputs to svg
# (our inference example is features1['inputs'])
output_batch = output_batch['outputs'] if out == 'svg' else output_batch[0]
out_list = []
for i, output in enumerate(tf.split(output_batch, num_classes)):
if out == 'svg':
out_list.append(svg_render(output))
elif out == 'img':
out_list.append(bitmap_render(output, glyph=glyphs[i], depth=bitmap_depth, contrast=bitmap_contrast, fill=bitmap_fill))
else:
out_list.append(bitmap_render(output, glyph=glyphs[i], depth=bitmap_depth, contrast=bitmap_contrast, fill=bitmap_fill, render_html=False))
return out_list
##########################################################################################
# Bitmap
##########################################################################################
def bitmap_render(tensor, glyph='0', depth=8, contrast=1, fill=False, render_html=True):
"""Converts Image VAE output into HTML svg."""
# depth is not 1 we don't fill
if not depth == 1:
fill=False
# adapted from matplotlib code below:
# imsave(tempbitmappath, np.reshape(tensor, [64, 64]), vmin=0, vmax=1, cmap='gray_r')
arr = np.reshape(tensor, [64, 64])
arr = np.clip(arr, 0, 1)
arr = (arr * 255).round().astype(np.uint8)
image = PIL.Image.fromarray(arr, "L")
image = PIL.ImageOps.invert(image)
if isinstance(contrast, (int, float)):
if contrast is not 1:
# 1 = no change
enhancer = PIL.ImageEnhance.Contrast(image)
image = enhancer.enhance(contrast)
elif contrast == "auto":
image = PIL.ImageOps.autocontrast(image, cutoff=0, ignore=255)
# another option for enhancement
# enhancer = PIL.ImageEnhance.Sharpness(image)
# image = enhancer.enhance(20)
if depth == 1:
# we can set this to darken (max should be < 0.5)
darken = 0
arr = (np.array(image).astype(np.float64)) / 255.0
arr = arr - darken
arr = np.around(arr)
arr = (arr * 255).round().astype(np.uint8)
image = PIL.Image.fromarray(arr, "L")
# this is a stopgap -- we shouldn't be having to fill in our glyphs
# if fill:
# mask = image.copy()
# image = image.convert("RGB")
#
# # fill black
# ImageDraw.floodfill(mask, xy=(0, 0), value=0)
#
# if glyph in glyphMaxVoids:
# pass
# else:
# # we don't expect any voids, just add our mask
# image.paste((0, 0, 0), (0, 0), mask)
if render_html:
# create a temporary file
tempbitmapfile = tempfile.NamedTemporaryFile(mode='w', suffix='.png', delete=False)
tempbitmapfile.close()
tempbitmappath = Path(tempbitmapfile.name)
image.save(tempbitmappath, format="png")
# load back and convert to html
data_uri = base64.b64encode(tempbitmappath.read_bytes()).decode('utf-8')
html = '<img src="data:image/png;base64,{0}">'.format(data_uri)
# remove our temporary file
tempbitmappath.unlink()
return html
else:
return image
##########################################################################################
# SVG
##########################################################################################
def clean_and_center_svg(svg, glyph='', ymin=None, ymax=None, tag='path', flipv=False):
# extract the first svg glyph path and bbox
svg_tree = soup(svg, 'lxml')
svg_tag = svg_tree.find(tag)
path_obj = parse_path(svg_tag['d'])
xmin, xmax, nymin, nymax = path_obj.bbox()
ymin = min(ymin, nymin) if ymin is not None else nymin
ymax = max(ymax, nymax) if ymax is not None else nymax
print(f'{glyph}: {xmin}, {xmax}, {ymin}, {ymax}')
svg_start_inputs = f'<svg width="50px" height="50px" viewBox="{xmin} {ymin} {xmax-xmin} {ymax-ymin}" version="1.1" xmlns="http://www.w3.org/2000/svg">'
if flipv:
svg_path = f'<path transform="scale(1, -1) translate(0, -{ymax+ymin})" d="{path_obj.d()}" />'
else:
svg_path = f'<path d="{path_obj.d()}" />'
return f'{svg_start_inputs}{svg_path}</svg>'
# Alternative:
# Precompute ymin / ymax
def get_svg_path_ymin_ymax(svg, ymin=None, ymax=None, tag='path'):
# extract the first svg glyph path and bbox
svg_tree = soup(svg, 'lxml')
svg_tag = svg_tree.find(tag)
path_obj = None
try:
path_obj = parse_path(svg_tag['d'])
xmin, xmax, nymin, nymax = path_obj.bbox()
ymin = min(ymin, nymin) if ymin is not None else nymin
ymax = max(ymax, nymax) if ymax is not None else nymax
except KeyError:
print(f'{datetime.datetime.now()}: {bcolors.FAIL}Unable to parse SVG tag "{tag}" ({svg_tag}){bcolors.ENDC}')
return (path_obj, ymin, ymax)
# Center based on min ymin / max ymax
def compose_svg(path_obj, ymin=None, ymax=None, flipv=False):
svg_start_inputs = f'<svg width="50px" height="50px" version="1.1" xmlns="http://www.w3.org/2000/svg">'
svg_path = ''
try:
xmin, xmax, _, _ = path_obj.bbox()
svg_start_inputs = f'<svg width="50px" height="50px" viewBox="{xmin} {ymin} {xmax-xmin} {ymax-ymin}" version="1.1" xmlns="http://www.w3.org/2000/svg">'
if flipv:
svg_path = f'<path transform="scale(1, -1) translate(0, -{ymax+ymin})" d="{path_obj.d()}" />'
else:
svg_path = f'<path d="{path_obj.d()}" />'
except AttributeError:
# this happens when autotrace fails -- but we want to be notified of other errors
pass
return f'{svg_start_inputs}{svg_path}</svg>'
##########################################################################################
svg_start = ("""<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www."""
"""w3.org/1999/xlink" width="256px" height="256px" style="-ms-trans"""
"""form: rotate(360deg); -webkit-transform: rotate(360deg); transfo"""
"""rm: rotate(360deg);" preserveAspectRatio="xMidYMid meet" viewBox"""
"""="0 0 24 24"><path d=\"""")
svg_end = """\" fill="currentColor"/></svg>"""
COMMAND_RX = re.compile("([MmLlHhVvCcSsQqTtAaZz])")
FLOAT_RX = re.compile("[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?")
def svg_render(tensor):
"""Converts SVG decoder output into HTML svg."""
# undo normalization
tensor = (tensor * stdev_npz) + mean_npz
# convert to html
tensor = svg_utils.make_simple_cmds_long(tensor)
vector = tf.squeeze(tensor, [0, 2])
html = svg_utils.vector_to_svg(vector.numpy(), stop_at_eos=True, categorical=True)
# some aesthetic postprocessing
html = postprocess(html)
html = html.replace('256px', '50px')
return html
def svg_html_to_path_string(svg):
return svg.replace(svg_start, '').replace(svg_end, '')
def _tokenize(pathdef):
"""Returns each svg token from path list."""
# e.g.: 'm0.1-.5c0,6' -> m', '0.1, '-.5', 'c', '0', '6'
for x in COMMAND_RX.split(pathdef):
if x != '' and x in 'MmLlHhVvCcSsQqTtAaZz':
yield x
for token in FLOAT_RX.findall(x):
yield token
def path_string_to_tokenized_commands(path):
"""Tokenizes the given path string.
E.g.:
Given M 0.5 0.5 l 0.25 0.25 z
Returns [['M', '0.5', '0.5'], ['l', '0.25', '0.25'], ['z']]
"""
new_path = []
current_cmd = []
for token in _tokenize(path):
if len(current_cmd) > 0:
if token in 'MmLlHhVvCcSsQqTtAaZz':
# cmd ended, convert to vector and add to new_path
new_path.append(current_cmd)
current_cmd = [token]
else:
# add arg to command
current_cmd.append(token)
else:
# add to start new cmd
current_cmd.append(token)
if current_cmd:
# process command still unprocessed
new_path.append(current_cmd)
return new_path
def separate_substructures(tokenized_commands):
"""Returns a list of SVG substructures."""
# every moveTo command starts a new substructure
# an SVG substructure is a subpath that closes on itself
# such as the outter and the inner edge of the character `o`
substructures = []
curr = []
for cmd in tokenized_commands:
if cmd[0] in 'mM' and len(curr) > 0:
substructures.append(curr)
curr = []
curr.append(cmd)
if len(curr) > 0:
substructures.append(curr)
return substructures
def postprocess(svg, dist_thresh=2., skip=False):
path = svg_html_to_path_string(svg)
svg_template = svg.replace(path, '{}')
tokenized_commands = path_string_to_tokenized_commands(path)
dist = lambda a, b: np.sqrt((float(a[0]) - float(b[0]))**2 + (float(a[1]) - float(b[1]))**2)
are_close_together = lambda a, b, t: dist(a, b) < t
# first, go through each start/end point and merge if they're close enough
# together (that is, make end point the same as the start point).
# TODO: there are better ways of doing this, in a way that propagates error
# back (so if total error is 0.2, go through all N commands in this
# substructure and fix each by 0.2/N (unless they have 0 vertical change))
substructures = separate_substructures(tokenized_commands)
previous_substructure_endpoint = (0., 0.,)
for substructure in substructures:
# first, if the last substructure's endpoint was updated, we must update
# the start point of this one to reflect the opposite update
substructure[0][-2] = str(float(substructure[0][-2]) -
previous_substructure_endpoint[0])
substructure[0][-1] = str(float(substructure[0][-1]) -
previous_substructure_endpoint[1])
start = list(map(float, substructure[0][-2:]))
curr_pos = (0., 0.)
for cmd in substructure:
curr_pos, _ = svg_utils._update_curr_pos(curr_pos, cmd, (0., 0.))
if are_close_together(start, curr_pos, dist_thresh):
new_point = np.array(start)
previous_substructure_endpoint = ((new_point[0] - curr_pos[0]), (new_point[1] - curr_pos[1]))
substructure[-1][-2] = str(float(substructure[-1][-2]) + (new_point[0] - curr_pos[0]))
substructure[-1][-1] = str(float(substructure[-1][-1]) + (new_point[1] - curr_pos[1]))
if substructure[-1][0] in 'cC':
substructure[-1][-4] = str(float(substructure[-1][-4]) + (new_point[0] - curr_pos[0]))
substructure[-1][-3] = str(float(substructure[-1][-3]) + (new_point[1] - curr_pos[1]))
if skip:
return svg_template.format(' '.join([' '.join(' '.join(cmd) for cmd in s) for s in substructures]))
cosa = lambda x, y: (x[0] * y[0] + x[1] * y[1]) / ((np.sqrt(x[0]**2 + x[1]**2) * np.sqrt(y[0]**2 + y[1]**2)))
rotate = lambda a, x, y: (x * np.cos(a) - y * np.sin(a), y * np.cos(a) + x * np.sin(a))
# second, find adjacent bezier curves and, if their control points are almost aligned,
# fully align them
for substructure in substructures:
curr_pos = (0., 0.)
new_curr_pos, _ = svg_utils._update_curr_pos((0., 0.,), substructure[0], (0., 0.))
for cmd_idx in range(1, len(substructure)):
prev_cmd = substructure[cmd_idx-1]
cmd = substructure[cmd_idx]
new_new_curr_pos, _ = svg_utils._update_curr_pos(new_curr_pos, cmd, (0., 0.))
if cmd[0] == 'c':
if prev_cmd[0] == 'c':
# check the vectors and update if needed
# previous control pt wrt new curr point
prev_ctr_point = (curr_pos[0] + float(prev_cmd[3]) - new_curr_pos[0],
curr_pos[1] + float(prev_cmd[4]) - new_curr_pos[1])
ctr_point = (float(cmd[1]), float(cmd[2]))
if -1. < cosa(prev_ctr_point, ctr_point) < -0.95:
# calculate exact angle between the two vectors
angle_diff = (np.pi - np.arccos(cosa(prev_ctr_point, ctr_point)))/2
# rotate each vector by angle/2 in the correct direction for each.
sign = np.sign(np.cross(prev_ctr_point, ctr_point))
new_ctr_point = rotate(sign * angle_diff, *ctr_point)
new_prev_ctr_point = rotate(-sign * angle_diff, *prev_ctr_point)
# override the previous control points
# (which has to be wrt previous curr position)
substructure[cmd_idx-1][3] = str(new_prev_ctr_point[0] -
curr_pos[0] + new_curr_pos[0])
substructure[cmd_idx-1][4] = str(new_prev_ctr_point[1] -
curr_pos[1] + new_curr_pos[1])
substructure[cmd_idx][1] = str(new_ctr_point[0])
substructure[cmd_idx][2] = str(new_ctr_point[1])
curr_pos = new_curr_pos
new_curr_pos = new_new_curr_pos
return svg_template.format(' '.join([' '.join(' '.join(cmd) for cmd in s)
for s in substructures]))
##########################################################################################
def test_font_glyph_inference(fontname, glyph, inputt2tpath):
print(f'{bcolors.BOLD}Testing font glyph inference ({fontname}: {glyph})...{bcolors.ENDC}')
modelbasepath = basepath/'models-google'
modelsuffix = '_external'
uni = ord(glyph)
glyphpath = inputt2tpath/f'{fontname}-{uni}'
hparam_set = 'svg_decoder'
vae_ckpt_dir = os.fspath(modelbasepath/f'image_vae{modelsuffix}')
add_hparams = f'vae_ckpt_dir={vae_ckpt_dir},vae_hparam_set=image_vae'
model_name = 'svg_decoder'
ckpt_dir = os.fspath(modelbasepath/f'svg_decoder{modelsuffix}')
Path('./out-font-glyph-inference.html').write_text('\n'.join(infer_from_file(
glyphpath, hparam_set, add_hparams, model_name, ckpt_dir)))
def test_svg_inference():
print(f'{bcolors.BOLD}Testing SVG inference...{bcolors.ENDC}')
modelbasepath = basepath/'models-google'
modelsuffix = '_external'
uni = ord('U')
svg = '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="50px" height="50px" style="-ms-transform: rotate(360deg); -webkit-transform: rotate(360deg); transform: rotate(360deg);" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><path d="m 9.46800041199 5.86666679382 l 1.7039999961853027 0.0 l 0.0 15.095999717712402 l 1.656000018119812 0.0 l 0.0 -15.095999717712402 l 1.7039999961853027 0.0 l 0.0 16.799999237060547 l -5.064000129699707 0.0 l 1.1920928955078125e-07 -16.799999237060547" fill="currentColor"/></svg>'
example = generate_t2t_example(uni, svg)
hparam_set = 'svg_decoder'
vae_ckpt_dir = os.fspath(modelbasepath/f'image_vae{modelsuffix}')
add_hparams = f'vae_ckpt_dir={vae_ckpt_dir},vae_hparam_set=image_vae'
model_name = 'svg_decoder'
ckpt_dir = os.fspath(modelbasepath/f'svg_decoder{modelsuffix}')
Path('./out-svg-inference.html').write_text('\n'.join(infer(
example, hparam_set, add_hparams, model_name, ckpt_dir)))
##########################################################################################
@app.route('/api')
def index():
return "Public Foundry Combinator"
##########################################################################################
# @app.route('/run-tests')
# def run_tests():
# fontname = FLAGS.font
#
# inputpath = inferencepath/fontname/'input'
# inputt2tpath = inputpath/'t2t'
#
# test_font_glyph_inference(fontname, 'C', inputt2tpath)
# test_svg_inference()
#
# return "Tests Complete"
##########################################################################################
@app.route('/api/fonts')
def get_fonts():
fontdirs = inferencepath.glob('*/')
fonts = [f.name for f in fontdirs if f.is_dir()]
return jsonify({ 'fonts': fonts })
##########################################################################################
@app.route('/api/inputs/<string:fontname>', methods=['GET'])
def get_inputs(fontname):
# sample:
# http://127.0.0.1:5959/api/inputs/Unica?json=False
use_json = request.args.get('json', default='true').lower() == 'true'
glyphspath = inferencepath/fontname/'input'/'glyphs'
glyphpaths = glyphspath.glob('*.sfd')
inputs = {}
ymin = None
ymax = None
print(f'{datetime.datetime.now()}: {bcolors.BOLD}Getting {fontname} inputs as SVGs...{bcolors.ENDC}', end='')
print(f'{bcolors.OKGREEN}SUCCESS{bcolors.ENDC}')
# preprocess -- get ymin / ymax
for glyphindex, glyphpath in enumerate(glyphpaths):
uni = int(glyphpath.with_suffix('').name)
# open the glyph to get the paths
f = fontforge.open(os.fspath(glyphpath))
tempsvgfile = tempfile.NamedTemporaryFile(mode='w', suffix='.svg', delete=False)
tempsvgfile.close()
f.generate(tempsvgfile.name)
svg = Path(tempsvgfile.name).read_text()
path_obj, ymin, ymax = get_svg_path_ymin_ymax(svg, ymin=ymin, ymax=ymax, tag='glyph')
inputs[chr(uni)] = path_obj
Path(tempsvgfile.name).unlink()
f.close()
# compose
for i, (key, value) in enumerate(inputs.items()):
inputs[key] = compose_svg(inputs[key], ymin=ymin, ymax=ymax, flipv=True)
if not use_json:
return '\n'.join(inputs.values())
return jsonify({'inputs': inputs})
##########################################################################################
@app.route('/api/infer/autotrace/<string:modelname>/<string:modelsuffix>/<string:fontname>/<string:glyph>', methods=['GET'])
def infer_autotrace_from_font(modelname, modelsuffix, fontname, glyph):
# sample:
# http://127.0.0.1:5959/api/infer/bitmap/models-google/external/Unica/A?json=False
use_json = request.args.get('json', default='true').lower() == 'true'
bitmap_depth = 1 # bitmap_depth is always 1
bitmap_contrast = request.args.get('contrast', default='auto')
try:
bitmap_contrast = float(bitmap_contrast)
except ValueError:
bitmap_contrast = "auto"
bitmap_fill = True # always fill outlines
modelbasepath = basepath/modelname
modelsuffix = '' if modelsuffix == '-' else f'_{modelsuffix}'
inputpath = inferencepath/fontname/'input'
inputt2tpath = inputpath/'t2t'
uni = ord(glyph)
glyphpath = inputt2tpath/f'{fontname}-{uni}'
print(f'{datetime.datetime.now()}: {bcolors.BOLD}autotrace/{fontname} "{glyph}" inference using {modelname}{modelsuffix} (use_json: {use_json}, bitmap_contrast: {bitmap_contrast})...{bcolors.ENDC}', end='')
hparam_set = 'image_vae'
add_hparams = ''
model_name = 'image_vae'
ckpt_dir = os.fspath(modelbasepath/f'image_vae{modelsuffix}')
inf = infer_from_file(
glyphpath,
hparam_set,
add_hparams,
model_name,
ckpt_dir,
out="PIL_image",
bitmap_depth=bitmap_depth,
bitmap_contrast=bitmap_contrast,
bitmap_fill=bitmap_fill
)
print(f'{bcolors.OKGREEN}SUCCESS{bcolors.ENDC}')
inferences = {}
ymin = None
ymax = None
# zip up our autotraced inferences with our glyphs
# preprocess -- get ymin / ymax
for i, image in enumerate(inf):
glyph = glyphs[i]
# save to bmp
tempbitmapfile = tempfile.NamedTemporaryFile(mode='w', suffix='.png', delete=False)
tempsvgfile = tempfile.NamedTemporaryFile(mode='w', suffix='.svg', delete=False)
tempbitmapfile.close()
tempsvgfile.close()
tempbitmappath = Path(tempbitmapfile.name)
tempsvgpath = Path(tempsvgfile.name)
image.save(tempbitmappath, format="png")
# create a new sfd
vwidth = font_height
f = fontforge.open('./blank.sfd')
f.ascent = font_ascent
f.descent = font_height - font_ascent
# paste in the bitmap and autotrace
char = f.createChar(ord(glyph))
char.importOutlines(tempbitmapfile.name)
char.autoTrace()
char.vwidth = vwidth
f.selection.all()
f.autoWidth(100, 30)
f.autoHint()
# save to svg
f.generate(tempsvgfile.name)
f.close()
# read svg
svg = tempsvgpath.read_text()
path_obj, ymin, ymax = get_svg_path_ymin_ymax(svg, ymin=ymin, ymax=ymax, tag='glyph')
inferences[glyph] = path_obj
# cleanup
tempbitmappath.unlink()
tempsvgpath.unlink()
# compose
for i, (key, value) in enumerate(inferences.items()):
inferences[key] = compose_svg(inferences[key], ymin=ymin, ymax=ymax, flipv=True)
if not use_json:
return '\n'.join(inferences.values())
return jsonify({'inferences': inferences})
##########################################################################################
@app.route('/api/infer/bitmap/<string:modelname>/<string:modelsuffix>/<string:fontname>/<string:glyph>', methods=['GET'])
def infer_bitmap_from_font(modelname, modelsuffix, fontname, glyph):
# sample:
# http://127.0.0.1:5959/api/infer/bitmap/models-google/external/Unica/A?json=False
use_json = request.args.get('json', default='true').lower() == 'true'
bitmap_depth = int(request.args.get('depth', default='8'))
bitmap_contrast = request.args.get('contrast', default='auto')
try:
bitmap_contrast = float(bitmap_contrast)
except ValueError:
bitmap_contrast = "auto"
bitmap_fill = request.args.get('fill', default='true').lower() == 'true'
modelbasepath = basepath/modelname
modelsuffix = '' if modelsuffix == '-' else f'_{modelsuffix}'
inputpath = inferencepath/fontname/'input'
inputt2tpath = inputpath/'t2t'
uni = str(ord(glyph))
glyphpath = inputt2tpath/f'{fontname}-{uni}'
print(f'{datetime.datetime.now()}: {bcolors.BOLD}bitmap/{fontname} "{glyph}" inference using {modelname}{modelsuffix} (use_json: {use_json}, bitmap_depth: {bitmap_depth}-bit, bitmap_contrast: {bitmap_contrast}, bitmap_fill: {bitmap_fill})...{bcolors.ENDC}', end='')
hparam_set = 'image_vae'
add_hparams = ''
model_name = 'image_vae'
ckpt_dir = os.fspath(modelbasepath/f'image_vae{modelsuffix}')
inf = infer_from_file(
glyphpath,
hparam_set,
add_hparams,
model_name,
ckpt_dir,
out="img",
bitmap_depth=bitmap_depth,
bitmap_contrast=bitmap_contrast,
bitmap_fill=bitmap_fill
)
print(f'{bcolors.OKGREEN}SUCCESS{bcolors.ENDC}')
inferences = {}
# zip up our bitmap inferences with our glyphs
for i, img in enumerate(inf):
glyph = glyphs[i]
inferences[glyph] = img
if not use_json: