Spaces:
Sleeping
Sleeping
feat: add plate text input field to UI
Browse files- app.py +8 -6
- config/us_cali/background/us.png +0 -0
app.py
CHANGED
|
@@ -48,7 +48,6 @@ def parse_config(config):
|
|
| 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 |
|
|
@@ -62,6 +61,7 @@ def get_background(name):
|
|
| 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("#")
|
|
@@ -70,10 +70,12 @@ def get_mask(cfg):
|
|
| 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)
|
|
@@ -103,7 +105,7 @@ def get_mask(cfg):
|
|
| 103 |
plateTextColor = tuple(int(plateTextColor[i:i + 2], 16) for i in (0, 2, 4))
|
| 104 |
bg.paste(plateTextColor, (0, 0), mask_wo_border)
|
| 105 |
|
| 106 |
-
return mask, ord_
|
| 107 |
|
| 108 |
|
| 109 |
# get all folders in the config folder
|
|
@@ -112,10 +114,9 @@ plates = [f for f in os.listdir(config_path) if os.path.isdir(os.path.join(confi
|
|
| 112 |
|
| 113 |
|
| 114 |
def generate(name):
|
| 115 |
-
print(name)
|
| 116 |
cfg = load_config(os.path.join(config_path, name, "config.yaml"))
|
| 117 |
-
mask, ord_
|
| 118 |
-
return bg, mask, ord_
|
| 119 |
|
| 120 |
|
| 121 |
demo = gr.Interface(
|
|
@@ -124,6 +125,7 @@ demo = gr.Interface(
|
|
| 124 |
gr.Dropdown(plates, value="cn_truck", label="Plate Style"),
|
| 125 |
],
|
| 126 |
[
|
|
|
|
| 127 |
gr.Image(label="Background"),
|
| 128 |
gr.Image(label="Mask"),
|
| 129 |
gr.Image(label="Ord"),
|
|
@@ -132,4 +134,4 @@ demo = gr.Interface(
|
|
| 132 |
|
| 133 |
|
| 134 |
if __name__ == "__main__":
|
| 135 |
-
demo.launch()
|
|
|
|
| 48 |
return char
|
| 49 |
|
| 50 |
plate_patch = [(random_choice(char) for char in plate) for plate in config["plateFormat"]]
|
|
|
|
| 51 |
return config["name"], plate_patch
|
| 52 |
|
| 53 |
|
|
|
|
| 61 |
# get mask and paste the file from folder to generate the mask
|
| 62 |
def get_mask(cfg):
|
| 63 |
name, plates = parse_config(cfg)
|
| 64 |
+
|
| 65 |
root_dir = os.path.join("config", name)
|
| 66 |
# if none key, return default value
|
| 67 |
plateTextColor = cfg.get("plateTextColor", "#000000").lstrip("#")
|
|
|
|
| 70 |
mask = Image.new("L", img.size, 0)
|
| 71 |
ord_ = Image.new("L", img.size, 0)
|
| 72 |
count = 0
|
| 73 |
+
plateText = ""
|
| 74 |
|
| 75 |
for idx, plate in enumerate(plates):
|
| 76 |
for char in plate:
|
| 77 |
count += 1
|
| 78 |
+
plateText += char
|
| 79 |
m_char_path = random.choice(glob.glob(f"{root_dir}/{idx}/{char}_*.png"))
|
| 80 |
mask_rect = np.where(np.array(img) == count)
|
| 81 |
h_min, w_min = np.min(mask_rect, axis=1)
|
|
|
|
| 105 |
plateTextColor = tuple(int(plateTextColor[i:i + 2], 16) for i in (0, 2, 4))
|
| 106 |
bg.paste(plateTextColor, (0, 0), mask_wo_border)
|
| 107 |
|
| 108 |
+
return plateText, bg, mask, ord_
|
| 109 |
|
| 110 |
|
| 111 |
# get all folders in the config folder
|
|
|
|
| 114 |
|
| 115 |
|
| 116 |
def generate(name):
|
|
|
|
| 117 |
cfg = load_config(os.path.join(config_path, name, "config.yaml"))
|
| 118 |
+
text, bg, mask, ord_ = get_mask(cfg)
|
| 119 |
+
return text, bg, mask, ord_
|
| 120 |
|
| 121 |
|
| 122 |
demo = gr.Interface(
|
|
|
|
| 125 |
gr.Dropdown(plates, value="cn_truck", label="Plate Style"),
|
| 126 |
],
|
| 127 |
[
|
| 128 |
+
gr.Text(label="Plate Text"),
|
| 129 |
gr.Image(label="Background"),
|
| 130 |
gr.Image(label="Mask"),
|
| 131 |
gr.Image(label="Ord"),
|
|
|
|
| 134 |
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|
| 137 |
+
demo.launch(debug=True)
|
config/us_cali/background/us.png
ADDED
|