Spaces:
Runtime error
Runtime error
Zengyf-CVer
commited on
Commit
•
579aa53
1
Parent(s):
0ee665b
v03 update
Browse files- app.py +5 -5
- util/fonts_opt.py +26 -24
- util/pdf_opt.py +12 -12
app.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
# Gradio YOLOv5 Det v0.3
|
2 |
-
# author
|
3 |
-
# creation time
|
4 |
-
# email
|
5 |
-
#
|
6 |
|
7 |
import os
|
8 |
|
@@ -74,7 +74,7 @@ def parse_args(known=False):
|
|
74 |
parser.add_argument(
|
75 |
"--device",
|
76 |
"-dev",
|
77 |
-
default="
|
78 |
type=str,
|
79 |
help="cuda or cpu",
|
80 |
)
|
|
|
1 |
# Gradio YOLOv5 Det v0.3
|
2 |
+
# author: Zeng Yifu(曾逸夫)
|
3 |
+
# creation time: 2022-05-09
|
4 |
+
# email: zyfiy1314@163.com
|
5 |
+
# project homepage: https://gitee.com/CV_Lab/gradio_yolov5_det
|
6 |
|
7 |
import os
|
8 |
|
|
|
74 |
parser.add_argument(
|
75 |
"--device",
|
76 |
"-dev",
|
77 |
+
default="cpu",
|
78 |
type=str,
|
79 |
help="cuda or cpu",
|
80 |
)
|
util/fonts_opt.py
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
-
#
|
2 |
-
#
|
3 |
-
#
|
|
|
|
|
4 |
|
5 |
import os
|
6 |
import sys
|
@@ -9,11 +11,11 @@ from pathlib import Path
|
|
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",
|
@@ -23,45 +25,45 @@ data_url_dict = {
|
|
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]
|
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]
|
61 |
-
add_fronts(font_diff)
|
62 |
else:
|
63 |
-
console.print(f"{fonts_list}[bold green]
|
64 |
else:
|
65 |
-
#
|
66 |
-
console.print("[bold red]
|
67 |
-
add_fronts(fonts_list)
|
|
|
1 |
+
# font management
|
2 |
+
# author: Zeng Yifu(曾逸夫)
|
3 |
+
# creation time: 2022-05-01
|
4 |
+
# email: zyfiy1314@163.com
|
5 |
+
# project homepage: https://gitee.com/CV_Lab/gradio_yolov5_det
|
6 |
|
7 |
import os
|
8 |
import sys
|
|
|
11 |
import wget
|
12 |
from rich.console import Console
|
13 |
|
14 |
+
ROOT_PATH = sys.path[0] # Project root directory
|
15 |
|
16 |
+
# Chinese, English, Russian, Spanish, Arabic, Korean
|
17 |
+
fonts_list = ["SimSun.ttf", "TimesNewRoman.ttf", "malgun.ttf"] # font list
|
18 |
+
fonts_suffix = ["ttc", "ttf", "otf"] # font suffix
|
19 |
|
20 |
data_url_dict = {
|
21 |
"SimSun.ttf": "https://gitee.com/CV_Lab/gradio_yolov5_det/attach_files/1053539/download/SimSun.ttf",
|
|
|
25 |
console = Console()
|
26 |
|
27 |
|
28 |
+
# create font library
|
29 |
def add_fronts(font_diff):
|
30 |
|
31 |
global font_name
|
32 |
|
33 |
for k, v in data_url_dict.items():
|
34 |
if k in font_diff:
|
35 |
+
font_name = v.split("/")[-1] # font name
|
36 |
+
Path(f"{ROOT_PATH}/fonts").mkdir(parents=True, exist_ok=True) # Create a directory
|
37 |
|
38 |
+
file_path = f"{ROOT_PATH}/fonts/{font_name}" # font path
|
39 |
|
40 |
try:
|
41 |
+
# Download font file
|
42 |
wget.download(v, file_path)
|
43 |
except Exception as e:
|
44 |
+
print("Path error! Program ended!")
|
45 |
print(e)
|
46 |
sys.exit()
|
47 |
else:
|
48 |
print()
|
49 |
+
console.print(f"{font_name} [bold green]font file download complete![/bold green] has been saved to: {file_path}")
|
50 |
|
51 |
|
52 |
+
# Determine the font file
|
53 |
def is_fonts(fonts_dir):
|
54 |
if os.path.isdir(fonts_dir):
|
55 |
+
# if the font library exists
|
56 |
+
f_list = os.listdir(fonts_dir) # local font library
|
57 |
|
58 |
font_diff = list(set(fonts_list).difference(set(f_list)))
|
59 |
|
60 |
if font_diff != []:
|
61 |
+
# font does not exist
|
62 |
+
console.print("[bold red] font does not exist, loading...[/bold red]")
|
63 |
+
add_fronts(font_diff) # Create a font library
|
64 |
else:
|
65 |
+
console.print(f"{fonts_list}[bold green]font already exists![/bold green]")
|
66 |
else:
|
67 |
+
# The font library does not exist, create a font library
|
68 |
+
console.print("[bold red]font library does not exist, creating...[/bold red]")
|
69 |
+
add_fronts(fonts_list) # Create a font library
|
util/pdf_opt.py
CHANGED
@@ -1,15 +1,15 @@
|
|
1 |
-
# PDF
|
2 |
-
#
|
3 |
-
#
|
4 |
|
5 |
from fpdf import FPDF
|
6 |
|
7 |
|
8 |
-
# PDF
|
9 |
class PDF(FPDF):
|
10 |
-
#
|
11 |
def header(self):
|
12 |
-
#
|
13 |
self.add_font("SimSun", "", "./fonts/SimSun.ttf", uni=True)
|
14 |
self.set_font("SimSun", "", 16)
|
15 |
# Calculate width of title and position
|
@@ -29,7 +29,7 @@ class PDF(FPDF):
|
|
29 |
def footer(self):
|
30 |
# Position at 1.5 cm from bottom
|
31 |
self.set_y(-15)
|
32 |
-
#
|
33 |
self.add_font("SimSun", "", "./fonts/SimSun.ttf", uni=True)
|
34 |
self.set_font("SimSun", "", 12)
|
35 |
# Text color in gray
|
@@ -38,20 +38,20 @@ class PDF(FPDF):
|
|
38 |
self.cell(0, 10, "Page " + str(self.page_no()), 0, 0, "C")
|
39 |
|
40 |
def chapter_title(self, num, label):
|
41 |
-
#
|
42 |
self.add_font("SimSun", "", "./fonts/SimSun.ttf", uni=True)
|
43 |
self.set_font("SimSun", "", 12)
|
44 |
# Background color
|
45 |
self.set_fill_color(200, 220, 255)
|
46 |
# Title
|
47 |
# self.cell(0, 6, 'Chapter %d : %s' % (num, label), 0, 1, 'L', 1)
|
48 |
-
self.cell(0, 6, "
|
49 |
# Line break
|
50 |
self.ln(4)
|
51 |
|
52 |
def chapter_body(self, name):
|
53 |
|
54 |
-
#
|
55 |
self.add_font("SimSun", "", "./fonts/SimSun.ttf", uni=True)
|
56 |
self.set_font("SimSun", "", 12)
|
57 |
# Output justified text
|
@@ -66,7 +66,7 @@ class PDF(FPDF):
|
|
66 |
self.chapter_body(name)
|
67 |
|
68 |
|
69 |
-
# pdf
|
70 |
def pdf_generate(input_file, output_file, title_):
|
71 |
global title
|
72 |
|
@@ -75,4 +75,4 @@ def pdf_generate(input_file, output_file, title_):
|
|
75 |
pdf.set_title(title)
|
76 |
pdf.set_author("Zeng Yifu")
|
77 |
pdf.print_chapter(1, "A RUNAWAY REEF", input_file)
|
78 |
-
pdf.output(output_file)
|
|
|
1 |
+
# PDF management
|
2 |
+
# author: Zeng Yifu
|
3 |
+
# creation time: 2022-05-05
|
4 |
|
5 |
from fpdf import FPDF
|
6 |
|
7 |
|
8 |
+
# PDF generation class
|
9 |
class PDF(FPDF):
|
10 |
+
# Reference: https://pyfpdf.readthedocs.io/en/latest/Tutorial/index.html
|
11 |
def header(self):
|
12 |
+
# Set Chinese font
|
13 |
self.add_font("SimSun", "", "./fonts/SimSun.ttf", uni=True)
|
14 |
self.set_font("SimSun", "", 16)
|
15 |
# Calculate width of title and position
|
|
|
29 |
def footer(self):
|
30 |
# Position at 1.5 cm from bottom
|
31 |
self.set_y(-15)
|
32 |
+
# Set Chinese font
|
33 |
self.add_font("SimSun", "", "./fonts/SimSun.ttf", uni=True)
|
34 |
self.set_font("SimSun", "", 12)
|
35 |
# Text color in gray
|
|
|
38 |
self.cell(0, 10, "Page " + str(self.page_no()), 0, 0, "C")
|
39 |
|
40 |
def chapter_title(self, num, label):
|
41 |
+
# Set Chinese font
|
42 |
self.add_font("SimSun", "", "./fonts/SimSun.ttf", uni=True)
|
43 |
self.set_font("SimSun", "", 12)
|
44 |
# Background color
|
45 |
self.set_fill_color(200, 220, 255)
|
46 |
# Title
|
47 |
# self.cell(0, 6, 'Chapter %d : %s' % (num, label), 0, 1, 'L', 1)
|
48 |
+
self.cell(0, 6, "Test result:", 0, 1, "L", 1)
|
49 |
# Line break
|
50 |
self.ln(4)
|
51 |
|
52 |
def chapter_body(self, name):
|
53 |
|
54 |
+
# Set Chinese font
|
55 |
self.add_font("SimSun", "", "./fonts/SimSun.ttf", uni=True)
|
56 |
self.set_font("SimSun", "", 12)
|
57 |
# Output justified text
|
|
|
66 |
self.chapter_body(name)
|
67 |
|
68 |
|
69 |
+
# pdf generation function
|
70 |
def pdf_generate(input_file, output_file, title_):
|
71 |
global title
|
72 |
|
|
|
75 |
pdf.set_title(title)
|
76 |
pdf.set_author("Zeng Yifu")
|
77 |
pdf.print_chapter(1, "A RUNAWAY REEF", input_file)
|
78 |
+
pdf.output(output_file)
|