Spaces:
Sleeping
Sleeping
Add application file
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- app.py +134 -0
- config/cn_car/0/0_1717665599938596000.png +0 -0
- config/cn_car/0/1_1717665599938057000.png +0 -0
- config/cn_car/0/2_1717665599938112000.png +0 -0
- config/cn_car/0/3_1717665599938165000.png +0 -0
- config/cn_car/0/4_1717665599938217000.png +0 -0
- config/cn_car/0/5_1717665599938269000.png +0 -0
- config/cn_car/0/6_1717665599938320000.png +0 -0
- config/cn_car/0/7_1717665599938378000.png +0 -0
- config/cn_car/0/8_1717665599938453000.png +0 -0
- config/cn_car/0/9_1717665599938522000.png +0 -0
- config/cn_car/0/A_1717665599936258000.png +0 -0
- config/cn_car/0/B_1717665599936375000.png +0 -0
- config/cn_car/0/C_1717665599936447000.png +0 -0
- config/cn_car/0/D_1717665599936507000.png +0 -0
- config/cn_car/0/E_1717665599936565000.png +0 -0
- config/cn_car/0/F_1717665599936624000.png +0 -0
- config/cn_car/0/G_1717665599936681000.png +0 -0
- config/cn_car/0/H_1717665599936873000.png +0 -0
- config/cn_car/0/I_1717665599936944000.png +0 -0
- config/cn_car/0/J_1717665599937007000.png +0 -0
- config/cn_car/0/K_1717665599937064000.png +0 -0
- config/cn_car/0/L_1717665599937120000.png +0 -0
- config/cn_car/0/M_1717665599937174000.png +0 -0
- config/cn_car/0/N_1717665599937229000.png +0 -0
- config/cn_car/0/O_1717665599937282000.png +0 -0
- config/cn_car/0/P_1717665599937336000.png +0 -0
- config/cn_car/0/Q_1717665599937392000.png +0 -0
- config/cn_car/0/R_1717665599937453000.png +0 -0
- config/cn_car/0/S_1717665599937509000.png +0 -0
- config/cn_car/0/T_1717665599937680000.png +0 -0
- config/cn_car/0/U_1717665599937735000.png +0 -0
- config/cn_car/0/V_1717665599937791000.png +0 -0
- config/cn_car/0/W_1717665599937845000.png +0 -0
- config/cn_car/0/X_1717665599937899000.png +0 -0
- config/cn_car/0/Y_1717665599937952000.png +0 -0
- config/cn_car/0/Z_1717665599938004000.png +0 -0
- config/cn_car/0//302/267_1717681872614883000.png +0 -0
- config/cn_car/0//344/272/221_1717665656848352000.png +0 -0
- config/cn_car/0//344/272/254_1717665656846792000.png +0 -0
- config/cn_car/0//345/206/200_1717665656846989000.png +0 -0
- config/cn_car/0//345/220/211_1717665656847249000.png +0 -0
- config/cn_car/0//345/256/201_1717665656848784000.png +0 -0
- config/cn_car/0//345/267/235_1717665656848225000.png +0 -0
- config/cn_car/0//346/226/260_1717665656848854000.png +0 -0
- config/cn_car/0//346/231/213_1717665656847056000.png +0 -0
- config/cn_car/0//346/241/202_1717665656848044000.png +0 -0
- config/cn_car/0//346/262/252_1717665656847369000.png +0 -0
- config/cn_car/0//346/264/245_1717665656846913000.png +0 -0
- config/cn_car/0//346/265/231_1717665656847489000.png +0 -0
app.py
ADDED
@@ -0,0 +1,134 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import time
|
4 |
+
import glob
|
5 |
+
import yaml
|
6 |
+
import random
|
7 |
+
|
8 |
+
import numpy as np
|
9 |
+
import gradio as gr
|
10 |
+
from PIL import Image, ImageDraw, ImageFilter
|
11 |
+
|
12 |
+
|
13 |
+
def get_template(path):
|
14 |
+
# RGB to HSV
|
15 |
+
img = Image.open(path).convert('HSV')
|
16 |
+
# pick the H channel
|
17 |
+
img = img.split()[0]
|
18 |
+
# divide by 16
|
19 |
+
img = np.array(img) + 8
|
20 |
+
img = Image.fromarray(img // 16)
|
21 |
+
return img
|
22 |
+
|
23 |
+
|
24 |
+
# resize via height, keep the aspect ratio
|
25 |
+
def resize_height(img, height, nearest=False):
|
26 |
+
width, old_height = img.size
|
27 |
+
new_width = int(width * height / old_height)
|
28 |
+
return img.resize((new_width, height), Image.NEAREST if nearest else Image.BICUBIC), new_width
|
29 |
+
|
30 |
+
|
31 |
+
# load config yaml file
|
32 |
+
def load_config(path):
|
33 |
+
with open(path, "r") as f:
|
34 |
+
config = yaml.load(f, Loader=yaml.FullLoader)
|
35 |
+
return config
|
36 |
+
|
37 |
+
|
38 |
+
def parse_config(config):
|
39 |
+
def random_choice(char):
|
40 |
+
if char == "#":
|
41 |
+
return random.choice(config["plateNums"] + config["plateChars"])
|
42 |
+
elif char == "A":
|
43 |
+
return random.choice(config["plateChars"])
|
44 |
+
elif char == "0":
|
45 |
+
return random.choice(config["plateNums"])
|
46 |
+
elif char == "@":
|
47 |
+
return random.choice(config["plateCities"])
|
48 |
+
return char
|
49 |
+
|
50 |
+
plate_patch = [(random_choice(char) for char in plate) for plate in config["plateFormat"]]
|
51 |
+
|
52 |
+
return config["name"], plate_patch
|
53 |
+
|
54 |
+
|
55 |
+
def get_background(name):
|
56 |
+
bgs = os.path.join("config", name, "background")
|
57 |
+
bg_path = random.choice(glob.glob(f"{bgs}/*.*"))
|
58 |
+
bg = Image.open(bg_path).convert("RGB")
|
59 |
+
return bg
|
60 |
+
|
61 |
+
|
62 |
+
# get mask and paste the file from folder to generate the mask
|
63 |
+
def get_mask(cfg):
|
64 |
+
name, plates = parse_config(cfg)
|
65 |
+
root_dir = os.path.join("config", name)
|
66 |
+
# if none key, return default value
|
67 |
+
plateTextColor = cfg.get("plateTextColor", "#000000").lstrip("#")
|
68 |
+
|
69 |
+
img = get_template(os.path.join(root_dir, "template.png"))
|
70 |
+
mask = Image.new("L", img.size, 0)
|
71 |
+
ord_ = Image.new("L", img.size, 0)
|
72 |
+
count = 0
|
73 |
+
|
74 |
+
for idx, plate in enumerate(plates):
|
75 |
+
for char in plate:
|
76 |
+
count += 1
|
77 |
+
m_char_path = random.choice(glob.glob(f"{root_dir}/{idx}/{char}_*.png"))
|
78 |
+
mask_rect = np.where(np.array(img) == count)
|
79 |
+
h_min, w_min = np.min(mask_rect, axis=1)
|
80 |
+
h_max, w_max = np.max(mask_rect, axis=1)
|
81 |
+
m_char = Image.open(m_char_path).convert("RGBA").split()[-1]
|
82 |
+
m_char, new_width = resize_height(m_char, h_max - h_min)
|
83 |
+
x = (w_min + w_max - new_width) // 2
|
84 |
+
|
85 |
+
mask.paste(m_char, (x, h_min), m_char)
|
86 |
+
|
87 |
+
m_char = np.array(np.array(m_char) > 128, dtype=np.uint8) * 255
|
88 |
+
m_char = Image.fromarray(m_char)
|
89 |
+
ord_.paste(count*16, (x, h_min), m_char)
|
90 |
+
|
91 |
+
mask, _ = resize_height(mask, 512)
|
92 |
+
mask_wo_border = mask.copy()
|
93 |
+
mask = mask.filter(ImageFilter.MaxFilter(5)).filter(ImageFilter.GaussianBlur(1.5))
|
94 |
+
|
95 |
+
ord_, _ = resize_height(ord_, 512, nearest=True)
|
96 |
+
|
97 |
+
plate_bg = get_background(name).resize(mask.size)
|
98 |
+
# make the mask as alpha channel
|
99 |
+
bg = Image.new("RGBA", mask.size, (0, 0, 0, 0))
|
100 |
+
bg.paste(plate_bg, (0, 0))
|
101 |
+
# hex to rgb
|
102 |
+
plateTextColor = tuple(int(plateTextColor[i:i + 2], 16) for i in (0, 2, 4))
|
103 |
+
bg.paste(plateTextColor, (0, 0), mask_wo_border)
|
104 |
+
|
105 |
+
return mask, ord_, bg
|
106 |
+
|
107 |
+
|
108 |
+
# get all folders in the config folder
|
109 |
+
config_path = os.path.join(os.path.dirname(__file__), "config")
|
110 |
+
plates = [f for f in os.listdir(config_path) if os.path.isdir(os.path.join(config_path, f))]
|
111 |
+
|
112 |
+
|
113 |
+
def generate(name):
|
114 |
+
print(name)
|
115 |
+
cfg = load_config(os.path.join(config_path, name, "config.yaml"))
|
116 |
+
mask, ord_, bg = get_mask(cfg)
|
117 |
+
return bg, mask, ord_
|
118 |
+
|
119 |
+
|
120 |
+
demo = gr.Interface(
|
121 |
+
generate,
|
122 |
+
[
|
123 |
+
gr.Dropdown(plates, value="cn_truck", label="Plate Style"),
|
124 |
+
],
|
125 |
+
[
|
126 |
+
gr.Image(label="Background"),
|
127 |
+
gr.Image(label="Mask"),
|
128 |
+
gr.Image(label="Ord"),
|
129 |
+
]
|
130 |
+
)
|
131 |
+
|
132 |
+
|
133 |
+
if __name__ == "__main__":
|
134 |
+
demo.launch()
|
config/cn_car/0/0_1717665599938596000.png
ADDED
config/cn_car/0/1_1717665599938057000.png
ADDED
config/cn_car/0/2_1717665599938112000.png
ADDED
config/cn_car/0/3_1717665599938165000.png
ADDED
config/cn_car/0/4_1717665599938217000.png
ADDED
config/cn_car/0/5_1717665599938269000.png
ADDED
config/cn_car/0/6_1717665599938320000.png
ADDED
config/cn_car/0/7_1717665599938378000.png
ADDED
config/cn_car/0/8_1717665599938453000.png
ADDED
config/cn_car/0/9_1717665599938522000.png
ADDED
config/cn_car/0/A_1717665599936258000.png
ADDED
config/cn_car/0/B_1717665599936375000.png
ADDED
config/cn_car/0/C_1717665599936447000.png
ADDED
config/cn_car/0/D_1717665599936507000.png
ADDED
config/cn_car/0/E_1717665599936565000.png
ADDED
config/cn_car/0/F_1717665599936624000.png
ADDED
config/cn_car/0/G_1717665599936681000.png
ADDED
config/cn_car/0/H_1717665599936873000.png
ADDED
config/cn_car/0/I_1717665599936944000.png
ADDED
config/cn_car/0/J_1717665599937007000.png
ADDED
config/cn_car/0/K_1717665599937064000.png
ADDED
config/cn_car/0/L_1717665599937120000.png
ADDED
config/cn_car/0/M_1717665599937174000.png
ADDED
config/cn_car/0/N_1717665599937229000.png
ADDED
config/cn_car/0/O_1717665599937282000.png
ADDED
config/cn_car/0/P_1717665599937336000.png
ADDED
config/cn_car/0/Q_1717665599937392000.png
ADDED
config/cn_car/0/R_1717665599937453000.png
ADDED
config/cn_car/0/S_1717665599937509000.png
ADDED
config/cn_car/0/T_1717665599937680000.png
ADDED
config/cn_car/0/U_1717665599937735000.png
ADDED
config/cn_car/0/V_1717665599937791000.png
ADDED
config/cn_car/0/W_1717665599937845000.png
ADDED
config/cn_car/0/X_1717665599937899000.png
ADDED
config/cn_car/0/Y_1717665599937952000.png
ADDED
config/cn_car/0/Z_1717665599938004000.png
ADDED
config/cn_car/0//302/267_1717681872614883000.png
ADDED
config/cn_car/0//344/272/221_1717665656848352000.png
ADDED
config/cn_car/0//344/272/254_1717665656846792000.png
ADDED
config/cn_car/0//345/206/200_1717665656846989000.png
ADDED
config/cn_car/0//345/220/211_1717665656847249000.png
ADDED
config/cn_car/0//345/256/201_1717665656848784000.png
ADDED
config/cn_car/0//345/267/235_1717665656848225000.png
ADDED
config/cn_car/0//346/226/260_1717665656848854000.png
ADDED
config/cn_car/0//346/231/213_1717665656847056000.png
ADDED
config/cn_car/0//346/241/202_1717665656848044000.png
ADDED
config/cn_car/0//346/262/252_1717665656847369000.png
ADDED
config/cn_car/0//346/264/245_1717665656846913000.png
ADDED
config/cn_car/0//346/265/231_1717665656847489000.png
ADDED