Spaces:
Runtime error
Runtime error
Zengyf-CVer
commited on
Commit
•
aa56ecb
1
Parent(s):
bead993
app update
Browse files- img_examples/bus.jpg +0 -0
- img_examples/zidane.jpg +0 -0
- util/fonts_opt.py +67 -0
img_examples/bus.jpg
ADDED
img_examples/zidane.jpg
ADDED
util/fonts_opt.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# 字体管理
|
2 |
+
# 创建人:曾逸夫
|
3 |
+
# 创建时间:2022-05-01
|
4 |
+
|
5 |
+
import os
|
6 |
+
import sys
|
7 |
+
from pathlib import Path
|
8 |
+
|
9 |
+
import wget
|
10 |
+
from rich.console import Console
|
11 |
+
|
12 |
+
ROOT_PATH = sys.path[0] # 项目根目录
|
13 |
+
|
14 |
+
# 中文、英文、俄语、西班牙语、阿拉伯语、韩语
|
15 |
+
fonts_list = ["SimSun.ttf", "TimesNewRoman.ttf", "malgun.ttf"] # 字体列表
|
16 |
+
fonts_suffix = ["ttc", "ttf", "otf"] # 字体后缀
|
17 |
+
|
18 |
+
data_url_dict = {
|
19 |
+
"SimSun.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053539/download/SimSun.ttf",
|
20 |
+
"TimesNewRoman.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053537/download/TimesNewRoman.ttf",
|
21 |
+
"malgun.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053538/download/malgun.ttf",}
|
22 |
+
|
23 |
+
console = Console()
|
24 |
+
|
25 |
+
|
26 |
+
# 创建字体库
|
27 |
+
def add_fronts(font_diff):
|
28 |
+
|
29 |
+
global font_name
|
30 |
+
|
31 |
+
for k, v in data_url_dict.items():
|
32 |
+
if k in font_diff:
|
33 |
+
font_name = v.split("/")[-1] # 字体名称
|
34 |
+
Path(f"{ROOT_PATH}/fonts").mkdir(parents=True, exist_ok=True) # 创建目录
|
35 |
+
|
36 |
+
file_path = f"{ROOT_PATH}/fonts/{font_name}" # 字体路径
|
37 |
+
|
38 |
+
try:
|
39 |
+
# 下载字体文件
|
40 |
+
wget.download(v, file_path)
|
41 |
+
except Exception as e:
|
42 |
+
print("路径错误!程序结束!")
|
43 |
+
print(e)
|
44 |
+
sys.exit()
|
45 |
+
else:
|
46 |
+
print()
|
47 |
+
console.print(f"{font_name} [bold green]字体文件下载完成![/bold green] 已保存至:{file_path}")
|
48 |
+
|
49 |
+
|
50 |
+
# 判断字体文件
|
51 |
+
def is_fonts(fonts_dir):
|
52 |
+
if os.path.isdir(fonts_dir):
|
53 |
+
# 如果字体库存在
|
54 |
+
f_list = os.listdir(fonts_dir) # 本地字体库
|
55 |
+
|
56 |
+
font_diff = list(set(fonts_list).difference(set(f_list)))
|
57 |
+
|
58 |
+
if font_diff != []:
|
59 |
+
# 字体不存在
|
60 |
+
console.print("[bold red]字体不存在,正在加载。。。[/bold red]")
|
61 |
+
add_fronts(font_diff) # 创建字体库
|
62 |
+
else:
|
63 |
+
console.print(f"{fonts_list}[bold green]字体已存在![/bold green]")
|
64 |
+
else:
|
65 |
+
# 字体库不存在,创建字体库
|
66 |
+
console.print("[bold red]字体库不存在,正在创建。。。[/bold red]")
|
67 |
+
add_fronts(fonts_list) # 创建字体库
|