ankankbhunia commited on
Commit
8bd6e03
Β·
verified Β·
1 Parent(s): cd3f0ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -27
app.py CHANGED
@@ -18,6 +18,8 @@ import pickle
18
  from PIL import Image
19
  import tqdm
20
  import shutil
 
 
21
 
22
  wellcomingMessage = """
23
  <h1>πŸ’₯ Handwriting Synthesis - Generate text in anyone's handwriting πŸ’₯ </h1>
@@ -39,35 +41,46 @@ model.eval()
39
  def generate_image(text,folder, _ch3, images):
40
  # Your image generation logic goes here (replace with your actual implementation)
41
  # For demonstration purposes, we'll just concatenate the uploaded images horizontally.
42
- if images:
43
- style_inputs, width_length = load_itw_samples(images)
44
- elif folder:
45
- style_inputs, width_length = load_itw_samples(folder)
46
- else:
47
- return None
48
- # Load images
49
- text = text.replace("\n", "").replace("\t", "")
50
- text_encode = [j.encode() for j in text.split(' ')]
51
- eval_text_encode, eval_len_text = model.netconverter.encode(text_encode)
52
- eval_text_encode = eval_text_encode.to(DEVICE).repeat(batch_size, 1, 1)
53
-
54
- input_styles, page_val = model._generate_page(style_inputs.to(DEVICE).clone(), width_length, eval_text_encode, eval_len_text, no_concat = True)
55
- page_val = crop_(page_val[0]*255)
56
- input_styles = crop_(input_styles[0]*255)
57
-
58
- max_width = max(page_val.shape[1],input_styles.shape[1])
59
-
60
- if page_val.shape[1]!=max_width:
61
- page_val = np.concatenate([page_val, np.ones((page_val.shape[0],max_width-page_val.shape[1]))*255], 1)
62
- else:
63
- input_styles = np.concatenate([input_styles, np.ones((input_styles.shape[0],max_width-input_styles.shape[1]))*255], 1)
 
 
 
 
 
 
 
 
64
 
65
- upper_pad = np.ones((45,input_styles.shape[1]))*255
66
- input_styles = np.concatenate([upper_pad, input_styles], 0)
67
- page_val = np.concatenate([upper_pad, page_val], 0)
 
 
68
 
69
- page_val = Image.fromarray(page_val).convert('RGB')
70
- input_styles = Image.fromarray(input_styles).convert('RGB')
 
71
 
72
  return input_styles, page_val
73
 
 
18
  from PIL import Image
19
  import tqdm
20
  import shutil
21
+ from datetime import datetime
22
+
23
 
24
  wellcomingMessage = """
25
  <h1>πŸ’₯ Handwriting Synthesis - Generate text in anyone's handwriting πŸ’₯ </h1>
 
41
  def generate_image(text,folder, _ch3, images):
42
  # Your image generation logic goes here (replace with your actual implementation)
43
  # For demonstration purposes, we'll just concatenate the uploaded images horizontally.
44
+ try:
45
+ text_copy = text.copy()
46
+ if images:
47
+ style_log = images
48
+ style_inputs, width_length = load_itw_samples(images)
49
+ elif folder:
50
+ style_log = folder
51
+ style_inputs, width_length = load_itw_samples(folder)
52
+ else:
53
+ return None
54
+ # Load images
55
+ text = text.replace("\n", "").replace("\t", "")
56
+ text_encode = [j.encode() for j in text.split(' ')]
57
+ eval_text_encode, eval_len_text = model.netconverter.encode(text_encode)
58
+ eval_text_encode = eval_text_encode.to(DEVICE).repeat(batch_size, 1, 1)
59
+
60
+ input_styles, page_val = model._generate_page(style_inputs.to(DEVICE).clone(), width_length, eval_text_encode, eval_len_text, no_concat = True)
61
+ page_val = crop_(page_val[0]*255)
62
+ input_styles = crop_(input_styles[0]*255)
63
+
64
+ max_width = max(page_val.shape[1],input_styles.shape[1])
65
+
66
+ if page_val.shape[1]!=max_width:
67
+ page_val = np.concatenate([page_val, np.ones((page_val.shape[0],max_width-page_val.shape[1]))*255], 1)
68
+ else:
69
+ input_styles = np.concatenate([input_styles, np.ones((input_styles.shape[0],max_width-input_styles.shape[1]))*255], 1)
70
+
71
+ upper_pad = np.ones((45,input_styles.shape[1]))*255
72
+ input_styles = np.concatenate([upper_pad, input_styles], 0)
73
+ page_val = np.concatenate([upper_pad, page_val], 0)
74
 
75
+ page_val = Image.fromarray(page_val).convert('RGB')
76
+ input_styles = Image.fromarray(input_styles).convert('RGB')
77
+
78
+ current_datetime = datetime.now()
79
+ formatted_datetime = current_datetime.strftime("%Y-%m-%d %H:%M:%S")
80
 
81
+ print (f'{formatted_datetime}: input_string - {text_copy}, style_input - {style_log}')
82
+ except:
83
+ print ('ERROR! Try again.')
84
 
85
  return input_styles, page_val
86