gyrojeff commited on
Commit
3cfd2df
·
1 Parent(s): 711eab8

feat: add load font cache support

Browse files
Files changed (1) hide show
  1. demo.py +20 -7
demo.py CHANGED
@@ -1,11 +1,12 @@
1
  import argparse
2
  import os
3
  import gradio as gr
 
4
  from PIL import Image
5
  from torchvision import transforms
6
  from detector.model import *
7
  from detector import config
8
- from font_dataset.font import load_fonts, load_font_with_exclusion
9
  from huggingface_hub import hf_hub_download
10
 
11
  parser = argparse.ArgumentParser()
@@ -129,14 +130,26 @@ transform = transforms.Compose(
129
  ]
130
  )
131
 
132
- print("Preparing fonts ...")
133
- font_list, exclusion_rule = load_fonts()
134
 
135
- font_list = list(filter(lambda x: not exclusion_rule(x), font_list))
136
- font_list.sort(key=lambda x: x.path)
 
 
137
 
138
- for i in range(len(font_list)):
139
- font_list[i].path = font_list[i].path[18:] # remove ./dataset/fonts/./ prefix
 
 
 
 
 
 
 
 
 
 
 
 
140
 
141
  font_demo_images = []
142
 
 
1
  import argparse
2
  import os
3
  import gradio as gr
4
+ import pickle
5
  from PIL import Image
6
  from torchvision import transforms
7
  from detector.model import *
8
  from detector import config
9
+ from font_dataset.font import load_fonts
10
  from huggingface_hub import hf_hub_download
11
 
12
  parser = argparse.ArgumentParser()
 
130
  ]
131
  )
132
 
 
 
133
 
134
+ def prepare_fonts(cache_path="font_demo_cache.bin"):
135
+ print("Preparing fonts ...")
136
+ if os.path.exists(cache_path):
137
+ return pickle.load(open(cache_path, "rb"))
138
 
139
+ font_list, exclusion_rule = load_fonts()
140
+
141
+ font_list = list(filter(lambda x: not exclusion_rule(x), font_list))
142
+ font_list.sort(key=lambda x: x.path)
143
+
144
+ for i in range(len(font_list)):
145
+ font_list[i].path = font_list[i].path[18:] # remove ./dataset/fonts/./ prefix
146
+
147
+ with open(cache_path, "wb") as f:
148
+ pickle.dump(font_list, f)
149
+ return font_list
150
+
151
+
152
+ font_list = prepare_fonts()
153
 
154
  font_demo_images = []
155