Skip to content

Commit bf16f08

Browse files
authored
Merge pull request #13 from script-money/fix/space
1. feat: check all images then show all problem images 2. fix: two parts with string contain relation will always select first part 3. feat: add weight and trait folder validator
2 parents af973ea + 0a29a91 commit bf16f08

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

src/generate.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import random
77
from numpy.random import choice
88
from get_table import files_path
9+
from pathlib import Path
910
from config import (
1011
W,
1112
H,
@@ -112,9 +113,9 @@ def generate_func(
112113
next(
113114
path
114115
for path in files_path
115-
if attr["trait_type"] in path.split(os.sep)[1]
116-
and attr["value"][0] in path.split(os.sep)[0]
117-
and attr["value"][2] in path.split(os.sep)[2]
116+
if attr["trait_type"] == path.split(os.sep)[1].split("_")[1]
117+
and attr["value"][0] == path.split(os.sep)[0]
118+
and attr["value"][2] == Path(path).stem
118119
)
119120
for attr in attributes
120121
]

src/get_table.py

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from PIL import Image
33
import pandas as pd
44
import shutil
5-
from config import FOLDERS, EXTENSION, W, H
5+
from config import FOLDERS, EXTENSION, W, H, WEIGHTS
66

77

88
def get_files_path(folders=FOLDERS):
@@ -44,22 +44,58 @@ def get_files_path(folders=FOLDERS):
4444
print(f"{folder} folder cleaned")
4545
os.mkdir(folder)
4646

47+
# Validate weights
48+
assert (
49+
sum(WEIGHTS) == 1
50+
), f"sum of PARTS_DICT's value in config.py should be 1, now is {sum(WEIGHTS)}"
51+
4752
# Validate image format and size
53+
error = 0
4854
for path in files_path:
49-
assert (
50-
path.split(".")[-1] in EXTENSION
51-
), f"{path}'s extension is not {EXTENSION} "
52-
im = Image.open(path)
53-
w, h = im.size
54-
assert w == W, f"{path} width not equal {W}"
55-
assert h == H, f"{path} height not equal {H}"
55+
try:
56+
assert (
57+
path.split(".")[-1] in EXTENSION
58+
), f"{path}'s extension is not {EXTENSION} "
59+
im = Image.open(path)
60+
w, h = im.size
61+
assert w == W, f"{path} width not equal {W}"
62+
assert h == H, f"{path} height not equal {H}"
63+
except Exception as e:
64+
print(e)
65+
error += 1
66+
if error != 0:
67+
print(f"{error} images have error, fix and try again")
68+
exit()
5669

5770
# Validate path name has -
5871
for path in files_path:
5972
assert (
6073
"-" not in path.split("/")[-1]
6174
), f"{path} is invalid, files should not have '-' symbol"
6275

76+
folder_set = set()
77+
folder_error = 0
78+
# check all parts folders has same order
79+
for folder in FOLDERS:
80+
try:
81+
for root, subfolders, _ in os.walk(folder):
82+
if root == folder:
83+
for subfolder in subfolders:
84+
if subfolder not in folder_set and subfolder.split("_")[
85+
1
86+
] in map(lambda f: f.split("_")[1], folder_set):
87+
raise Exception(
88+
f"in '{root}' folder '{subfolder}' is invalid, index of subfolder with same trait should be same."
89+
)
90+
else:
91+
folder_set.add(subfolder)
92+
except Exception as e:
93+
print(e)
94+
folder_error += 1
95+
if folder_error != 0:
96+
print("Exited, please fix subfolder error and retry")
97+
exit()
98+
6399
# export tables
64100
attrs = [os.path.split(path) for path in files_path]
65101
d = {

0 commit comments

Comments
 (0)