Update app.py
Browse files
app.py
CHANGED
@@ -255,8 +255,8 @@ PADDING = 8
|
|
255 |
LABEL_DIR = "./labels"
|
256 |
|
257 |
def generate_labels(font_file=None, num_labels: int = 25):
|
|
|
258 |
global font_path
|
259 |
-
os.makedirs(LABEL_DIR, exist_ok=True)
|
260 |
|
261 |
try:
|
262 |
if font_file:
|
@@ -264,36 +264,40 @@ def generate_labels(font_file=None, num_labels: int = 25):
|
|
264 |
with open(font_file.name, "rb") as uploaded:
|
265 |
with open(font_path, "wb") as f:
|
266 |
f.write(uploaded.read())
|
267 |
-
|
|
|
268 |
font = ImageFont.load_default()
|
269 |
else:
|
270 |
-
font = ImageFont.truetype(font_path,
|
271 |
|
|
|
|
|
272 |
images = []
|
273 |
-
labels = [
|
274 |
-
"".join(np.random.choice(list(CHARS), np.random.randint(4, 7)))
|
275 |
-
for _ in range(num_labels)
|
276 |
-
]
|
277 |
|
278 |
for label in labels:
|
279 |
-
# Measure
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
|
284 |
-
text_width = bbox[2] - bbox[0]
|
285 |
-
text_height = bbox[3] - bbox[1]
|
286 |
|
287 |
-
#
|
288 |
-
|
289 |
-
|
|
|
290 |
|
291 |
-
img = Image.new("L", (
|
292 |
draw = ImageDraw.Draw(img)
|
293 |
-
draw.text((
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
294 |
|
295 |
-
filename = f"{LABEL_DIR}/{label}_{datetime.now().strftime('%Y%m%d%H%M%S%f')}.png"
|
296 |
-
img.save(filename)
|
297 |
images.append(img)
|
298 |
|
299 |
return images
|
|
|
255 |
LABEL_DIR = "./labels"
|
256 |
|
257 |
def generate_labels(font_file=None, num_labels: int = 25):
|
258 |
+
import time
|
259 |
global font_path
|
|
|
260 |
|
261 |
try:
|
262 |
if font_file:
|
|
|
264 |
with open(font_file.name, "rb") as uploaded:
|
265 |
with open(font_path, "wb") as f:
|
266 |
f.write(uploaded.read())
|
267 |
+
|
268 |
+
if font_path is None or not os.path.exists(font_path):
|
269 |
font = ImageFont.load_default()
|
270 |
else:
|
271 |
+
font = ImageFont.truetype(font_path, 32)
|
272 |
|
273 |
+
os.makedirs("./labels", exist_ok=True)
|
274 |
+
labels = ["".join(np.random.choice(list(CHARS), np.random.randint(4, 7))) for _ in range(num_labels)]
|
275 |
images = []
|
|
|
|
|
|
|
|
|
276 |
|
277 |
for label in labels:
|
278 |
+
# Measure text size
|
279 |
+
bbox = font.getbbox(label)
|
280 |
+
text_w = bbox[2] - bbox[0]
|
281 |
+
text_h = bbox[3] - bbox[1]
|
|
|
|
|
|
|
282 |
|
283 |
+
# Add 8px padding
|
284 |
+
pad = 8
|
285 |
+
img_w = text_w + pad * 2
|
286 |
+
img_h = text_h + pad * 2
|
287 |
|
288 |
+
img = Image.new("L", (img_w, img_h), color=255)
|
289 |
draw = ImageDraw.Draw(img)
|
290 |
+
draw.text((pad, pad), label, font=font, fill=0)
|
291 |
+
|
292 |
+
# Save image to label-specific folder
|
293 |
+
safe_label = ''.join(c if c.isalnum() else '_' for c in label)
|
294 |
+
label_dir = f"./labels/{safe_label}"
|
295 |
+
os.makedirs(label_dir, exist_ok=True)
|
296 |
+
|
297 |
+
timestamp = time.strftime("%Y%m%d%H%M%S%f")
|
298 |
+
filepath = os.path.join(label_dir, f"_{timestamp}.png")
|
299 |
+
img.save(filepath)
|
300 |
|
|
|
|
|
301 |
images.append(img)
|
302 |
|
303 |
return images
|