67 lines
2.3 KiB
Python
67 lines
2.3 KiB
Python
import os
|
|
import cv2
|
|
from PIL import Image
|
|
|
|
def rename_files(directory_path: str):
|
|
extensions = set()
|
|
|
|
for file in os.listdir(directory_path):
|
|
filename, extension = file.split(".")
|
|
extensions.add(extension)
|
|
old_filename = os.path.join(directory_path, file)
|
|
new_filename = os.path.join(directory_path, f"{filename}.jpg")
|
|
if old_filename != new_filename:
|
|
print(f"{old_filename} -> {new_filename}")
|
|
os.rename(old_filename, new_filename)
|
|
|
|
def check_images(directory_path: str):
|
|
for file in os.listdir(directory_path):
|
|
try:
|
|
img = cv2.imread(os.path.join(directory_path, file))
|
|
if img is None:
|
|
print(file)
|
|
except Exception:
|
|
print(file)
|
|
|
|
def check_labels(dir_path: str):
|
|
for filename in os.listdir(dir_path):
|
|
name, extension = filename.split(".")
|
|
if len(name) != 17:
|
|
print(filename)
|
|
|
|
def check_symbols(dir_path: str):
|
|
with open(os.path.join(dir_path, "dict.txt"), "r") as dict_file:
|
|
dict_content = dict_file.readlines()
|
|
dict_chars = set(char.strip() for char in dict_content)
|
|
for filename in os.listdir(os.path.join(dir_path, "images")):
|
|
label, extension = filename.split(".")
|
|
if any([char not in dict_chars for char in label]):
|
|
print(filename)
|
|
|
|
def max_height(dir_path: str):
|
|
max_height = 0
|
|
max_filename = ''
|
|
for filename in os.listdir(dir_path):
|
|
im = Image.open(os.path.join(dir_path, filename))
|
|
if im.height > max_height:
|
|
max_height = im.height
|
|
max_filename = filename
|
|
|
|
print(max_filename, max_height)
|
|
|
|
def resize_to_height(dir_path: str, target_height=48):
|
|
for filename in os.listdir(dir_path):
|
|
with Image.open(os.path.join(dir_path, filename)) as img:
|
|
width_percent = target_height / float(img.height)
|
|
new_width = int(float(img.width) * width_percent)
|
|
resized_img = img.resize((new_width, target_height), Image.LANZOS)
|
|
resized_img.save(os.path.join(dir_path, filename))
|
|
|
|
|
|
|
|
# rename_files("train_data/images/")
|
|
# check_images("train_data/images/")
|
|
# check_labels("train_data/images/")
|
|
# check_symbols("train_data/")
|
|
# max_height("train_data/images")
|