File size: 2,405 Bytes
ad2ecf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# font management
# author: Zeng Yifu(曾逸夫)
# creation time: 2022-05-01
# email: zyfiy1314@163.com
# project homepage: https://gitee.com/CV_Lab/gradio_yolov5_det

import os
import sys
from pathlib import Path

import wget
from rich.console import Console

ROOT_PATH = sys.path[0] # Project root directory

# Chinese, English, Russian, Spanish, Arabic, Korean
fonts_list = ["SimSun.ttf", "TimesNewRoman.ttf", "malgun.ttf"] # font list
fonts_suffix = ["ttc", "ttf", "otf"] # font suffix

data_url_dict = {
    "SimSun.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053539/download/SimSun.ttf",
    "TimesNewRoman.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053537/download/TimesNewRoman.ttf",
    "malgun.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053538/download/malgun.ttf",}

console = Console()


# create font library
def add_fronts(font_diff):

    global font_name

    for k, v in data_url_dict.items():
        if k in font_diff:
            font_name = v.split("/")[-1] # font name
            Path(f"{ROOT_PATH}/fonts").mkdir(parents=True, exist_ok=True) # Create a directory

            file_path = f"{ROOT_PATH}/fonts/{font_name}" # font path

            try:
                # Download font file
                wget.download(v, file_path)
            except Exception as e:
                print("Path error! Program ended!")
                print(e)
                sys.exit()
            else:
                print()
                console.print(f"{font_name} [bold green]font file download complete![/bold green] has been saved to: {file_path}")


# Determine the font file
def is_fonts(fonts_dir):
    if os.path.isdir(fonts_dir):
        # if the font library exists
        f_list = os.listdir(fonts_dir) # local font library

        font_diff = list(set(fonts_list).difference(set(f_list)))

        if font_diff != []:
            # font does not exist
            console.print("[bold red] font does not exist, loading...[/bold red]")
            add_fronts(font_diff) # Create a font library
        else:
            console.print(f"{fonts_list}[bold green]font already exists![/bold green]")
    else:
        # The font library does not exist, create a font library
        console.print("[bold red]font library does not exist, creating...[/bold red]")
        add_fronts(fonts_list) # Create a font library