-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandwrite.py
71 lines (57 loc) · 2.52 KB
/
handwrite.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
import argparse
import os
import generate
import pickle
import matplotlib
# import tensorflow as tf
import tensorflow.compat.v1 as tf
def textToHandWritting(args):
'''
parser = argparse.ArgumentParser()
parser.add_argument('--model', dest='model_path', type=str, default=os.path.join('pretrained', 'model-29'),
help='(optional) DL model to use')
parser.add_argument('--text', dest='text', type=str, help='Text to write')
parser.add_argument('--text-file', dest='file', type=str, default=None, help='Path to the input text file')
parser.add_argument('--style', dest='style', type=int, default=0, help='Style of handwriting (1 to 7)')
parser.add_argument('--bias', dest='bias', type=float, default=0.9,
help='Bias in handwriting. More bias is more unclear handwriting (0.00 to 1.00)')
parser.add_argument('--force', dest='force', action='store_true', default=False)
parser.add_argument('--color', dest='color_text', type=str, default='0,0,150',
help='Color of handwriting in RGB format')
parser.add_argument('--output', dest='output', type=str, default='./handwritten.pdf',
help='Output PDF file path and name')
args = parser.parse_args()
'''
if args.file:
text = open(args.file, 'r').read()
else:
text = args.text
if text is not None:
if len(text) > 50:
pass
else:
print("Text too short!")
return "Text too short!"
else:
print("Please provide either --text or --text-file in arguments")
return "Please provide either --text or --text-file in arguments"
tf.disable_v2_behavior()
matplotlib.use('agg')
pdf = generatePdf(args,text)
return pdf
def generatePdf(args,text):
with open(os.path.join('data', 'translation.pkl'), 'rb') as file:
translation = pickle.load(file)
rev_translation = {v: k for k, v in translation.items()}
charset = [rev_translation[i] for i in range(len(rev_translation))]
charset[0] = ''
config = tf.ConfigProto(
device_count={'GPU': 0}
)
with tf.Session(config=config) as sess:
saver = tf.train.import_meta_graph(args.model_path + '.meta')
saver.restore(sess, args.model_path)
print("\n\nInitialization Complete!\n\n\n\n")
color = [int(i) for i in args.color_text.replace(' ', '').split(',')]
pdf = generate.generate(text.replace('1', 'I'), args, sess, translation, color[:3])
return pdf