Spaces:
Sleeping
Sleeping
chore: Refactor get_mask function to improve readability and performance
Browse files- app.py +12 -5
- config/cn_car/config.json +1 -1
- config/cn_car/config.yaml +1 -1
- config/cn_car/template.png +0 -0
- config/cn_truck/config.json +1 -1
- config/cn_truck/config.yaml +1 -1
- config/cn_truck/template.png +0 -0
app.py
CHANGED
|
@@ -6,18 +6,24 @@ import random
|
|
| 6 |
|
| 7 |
import numpy as np
|
| 8 |
import gradio as gr
|
|
|
|
| 9 |
from PIL import Image, ImageDraw, ImageFilter
|
| 10 |
|
| 11 |
|
| 12 |
def get_template(path):
|
| 13 |
# RGB to HSV
|
| 14 |
img = Image.open(path).convert('HSV')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# pick the H channel
|
| 16 |
img = img.split()[0]
|
| 17 |
# divide by 16
|
| 18 |
img = np.array(img) + 8
|
| 19 |
img = Image.fromarray(img // 16)
|
| 20 |
-
return img
|
| 21 |
|
| 22 |
|
| 23 |
# resize via height, keep the aspect ratio
|
|
@@ -65,7 +71,7 @@ def get_mask(cfg):
|
|
| 65 |
# if none key, return default value
|
| 66 |
plateTextColor = cfg.get("plateTextColor", "#000000").lstrip("#")
|
| 67 |
|
| 68 |
-
img = get_template(os.path.join(root_dir, "template.png"))
|
| 69 |
mask = Image.new("L", img.size, 0)
|
| 70 |
ord_ = Image.new("L", img.size, 0)
|
| 71 |
count = 0
|
|
@@ -89,6 +95,8 @@ def get_mask(cfg):
|
|
| 89 |
m_char = Image.fromarray(m_char)
|
| 90 |
ord_.paste(count*16, (x, h_min), m_char)
|
| 91 |
|
|
|
|
|
|
|
| 92 |
mask, _ = resize_height(mask, 512)
|
| 93 |
mask_wo_border = mask.copy()
|
| 94 |
mask_wo_border = mask_wo_border.filter(ImageFilter.MaxFilter(3))
|
|
@@ -98,7 +106,7 @@ def get_mask(cfg):
|
|
| 98 |
|
| 99 |
plate_bg = get_background(name)
|
| 100 |
mask_wo_border = mask_wo_border.resize(plate_bg.size)
|
| 101 |
-
|
| 102 |
# make the mask as alpha channel
|
| 103 |
bg = Image.new("RGBA", plate_bg.size, (0, 0, 0, 0))
|
| 104 |
bg.paste(plate_bg, (0, 0))
|
|
@@ -116,8 +124,7 @@ plates = [f for f in os.listdir(config_path) if os.path.isdir(os.path.join(confi
|
|
| 116 |
|
| 117 |
def generate(name):
|
| 118 |
cfg = load_config(os.path.join(config_path, name, "config.json"))
|
| 119 |
-
text, bg, mask, ord_
|
| 120 |
-
return text, bg, mask, ord_
|
| 121 |
|
| 122 |
|
| 123 |
demo = gr.Interface(
|
|
|
|
| 6 |
|
| 7 |
import numpy as np
|
| 8 |
import gradio as gr
|
| 9 |
+
from matplotlib import pyplot as plt
|
| 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 |
+
copy = img.convert('L').copy()
|
| 17 |
+
# where is white
|
| 18 |
+
copy = np.where(np.array(copy) == 255, 255, 0).astype(np.uint8)
|
| 19 |
+
# convert numpy array to image
|
| 20 |
+
copy = Image.fromarray(copy, "L")
|
| 21 |
# pick the H channel
|
| 22 |
img = img.split()[0]
|
| 23 |
# divide by 16
|
| 24 |
img = np.array(img) + 8
|
| 25 |
img = Image.fromarray(img // 16)
|
| 26 |
+
return img, copy
|
| 27 |
|
| 28 |
|
| 29 |
# resize via height, keep the aspect ratio
|
|
|
|
| 71 |
# if none key, return default value
|
| 72 |
plateTextColor = cfg.get("plateTextColor", "#000000").lstrip("#")
|
| 73 |
|
| 74 |
+
img, dot = get_template(os.path.join(root_dir, "template.png"))
|
| 75 |
mask = Image.new("L", img.size, 0)
|
| 76 |
ord_ = Image.new("L", img.size, 0)
|
| 77 |
count = 0
|
|
|
|
| 95 |
m_char = Image.fromarray(m_char)
|
| 96 |
ord_.paste(count*16, (x, h_min), m_char)
|
| 97 |
|
| 98 |
+
# add the dot img on the mask
|
| 99 |
+
mask = Image.composite(dot, mask, dot)
|
| 100 |
mask, _ = resize_height(mask, 512)
|
| 101 |
mask_wo_border = mask.copy()
|
| 102 |
mask_wo_border = mask_wo_border.filter(ImageFilter.MaxFilter(3))
|
|
|
|
| 106 |
|
| 107 |
plate_bg = get_background(name)
|
| 108 |
mask_wo_border = mask_wo_border.resize(plate_bg.size)
|
| 109 |
+
|
| 110 |
# make the mask as alpha channel
|
| 111 |
bg = Image.new("RGBA", plate_bg.size, (0, 0, 0, 0))
|
| 112 |
bg.paste(plate_bg, (0, 0))
|
|
|
|
| 124 |
|
| 125 |
def generate(name):
|
| 126 |
cfg = load_config(os.path.join(config_path, name, "config.json"))
|
| 127 |
+
return get_mask(cfg) # text, bg, mask, ord_
|
|
|
|
| 128 |
|
| 129 |
|
| 130 |
demo = gr.Interface(
|
config/cn_car/config.json
CHANGED
|
@@ -41,6 +41,6 @@
|
|
| 41 |
"plateCities": "京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新",
|
| 42 |
"plateTextColor": "#FFFFFF",
|
| 43 |
"plateFormat": [
|
| 44 |
-
"@A
|
| 45 |
]
|
| 46 |
}
|
|
|
|
| 41 |
"plateCities": "京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新",
|
| 42 |
"plateTextColor": "#FFFFFF",
|
| 43 |
"plateFormat": [
|
| 44 |
+
"@A#####"
|
| 45 |
]
|
| 46 |
}
|
config/cn_car/config.yaml
CHANGED
|
@@ -9,4 +9,4 @@ plateTextColor: "#FFFFFF"
|
|
| 9 |
# A representing any letter
|
| 10 |
# 0 representing any number
|
| 11 |
# @ representing any city
|
| 12 |
-
plateFormat: ["@A
|
|
|
|
| 9 |
# A representing any letter
|
| 10 |
# 0 representing any number
|
| 11 |
# @ representing any city
|
| 12 |
+
plateFormat: ["@A#####"]
|
config/cn_car/template.png
CHANGED
|
|
config/cn_truck/config.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
| 7 |
"plateCities": "京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新",
|
| 8 |
"plateTextColor": "#111111",
|
| 9 |
"plateFormat": [
|
| 10 |
-
"
|
| 11 |
"#0000"
|
| 12 |
]
|
| 13 |
}
|
|
|
|
| 7 |
"plateCities": "京津冀晋蒙辽吉黑沪苏浙皖闽赣鲁豫鄂湘粤桂琼渝川贵云藏陕甘青宁新",
|
| 8 |
"plateTextColor": "#111111",
|
| 9 |
"plateFormat": [
|
| 10 |
+
"@A",
|
| 11 |
"#0000"
|
| 12 |
]
|
| 13 |
}
|
config/cn_truck/config.yaml
CHANGED
|
@@ -11,7 +11,7 @@ plateTextColor: "#111111"
|
|
| 11 |
# 0 representing any number (if multi-line, 0 means any number in the first line)
|
| 12 |
# 1 representing any number (if multi-line, 1 means any number in the second line)
|
| 13 |
# @ representing any city
|
| 14 |
-
plateFormat: ["
|
| 15 |
|
| 16 |
# single-line plate
|
| 17 |
# +----------+
|
|
|
|
| 11 |
# 0 representing any number (if multi-line, 0 means any number in the first line)
|
| 12 |
# 1 representing any number (if multi-line, 1 means any number in the second line)
|
| 13 |
# @ representing any city
|
| 14 |
+
plateFormat: ["@A", "#0000"]
|
| 15 |
|
| 16 |
# single-line plate
|
| 17 |
# +----------+
|
config/cn_truck/template.png
CHANGED
|
|