Datasets:
Commit
·
548d832
1
Parent(s):
3dfc0a3
add data processing scripts and ffhq dataset; implement image-label mapping and visualization
Browse files- data/check.py +14 -0
- data/ffhq_data.pkl +3 -0
- data/output.png +3 -0
- data/plot.py +47 -1
- main/mapping.py +33 -9
- main/utils.py +6 -0
data/check.py
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
import sys
|
4 |
+
import pickle
|
5 |
+
from PIL import Image
|
6 |
+
from tqdm import tqdm
|
7 |
+
|
8 |
+
# load the data(ffhq_data.pkl)
|
9 |
+
with open("ffhq_data.pkl", "rb") as f:
|
10 |
+
data = pickle.load(f)
|
11 |
+
|
12 |
+
# check the data
|
13 |
+
print(len(data))
|
14 |
+
print(data[0])
|
data/ffhq_data.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:013973d521f1c1ce1085d3e29320b05496018f28f8b2aa55343e8b466ab2aaba
|
3 |
+
size 865201251
|
data/output.png
ADDED
![]() |
Git LFS Details
|
data/plot.py
CHANGED
@@ -1,3 +1,49 @@
|
|
1 |
import os
|
|
|
|
|
|
|
|
|
2 |
import matplotlib.pyplot as plt
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
import sys
|
3 |
+
import pickle
|
4 |
+
from PIL import Image
|
5 |
+
import random
|
6 |
import matplotlib.pyplot as plt
|
7 |
+
|
8 |
+
# load the data(ffhq_data.pkl)
|
9 |
+
with open("ffhq_data.pkl", "rb") as f:
|
10 |
+
data = pickle.load(f)
|
11 |
+
|
12 |
+
# 假設 data 已經從 ffhq_data.pkl 載入
|
13 |
+
# 隨機抽取 5 張圖片
|
14 |
+
selected_data = random.sample(data, 5)
|
15 |
+
|
16 |
+
# 建立 1x5 的子圖區域,調整畫布大小以避免文字重疊
|
17 |
+
fig, axes = plt.subplots(1, 5, figsize=(20, 4))
|
18 |
+
|
19 |
+
for ax, item in zip(axes, selected_data):
|
20 |
+
img = item['img'] # PIL 圖片物件
|
21 |
+
label = item['label'] # 標籤字典
|
22 |
+
|
23 |
+
# 分離 headPose 與其他標籤資料
|
24 |
+
head_pose = label.get("headPose", None)
|
25 |
+
other_labels = {k: v for k, v in label.items() if k != "headPose"}
|
26 |
+
|
27 |
+
# 顯示圖片
|
28 |
+
ax.imshow(img)
|
29 |
+
ax.axis('off')
|
30 |
+
|
31 |
+
# 格式化 headPose 文字(若存在),並以多行呈現
|
32 |
+
if head_pose:
|
33 |
+
head_pose_text = "headPose:\n" + "\n".join([f"{k}: {v}" for k, v in head_pose.items()])
|
34 |
+
else:
|
35 |
+
head_pose_text = ""
|
36 |
+
|
37 |
+
# 格式化其他標籤文字
|
38 |
+
other_text = "\n".join([f"{k}: {v}" for k, v in other_labels.items()])
|
39 |
+
|
40 |
+
# 將 headPose 文字顯示在圖片上方(利用 ax.set_title)
|
41 |
+
if head_pose_text:
|
42 |
+
ax.set_title(head_pose_text, fontsize=10, pad=10)
|
43 |
+
|
44 |
+
# 將其他標籤文字顯示在圖片下方
|
45 |
+
ax.text(0.5, -0.1, other_text, transform=ax.transAxes, fontsize=10,
|
46 |
+
ha='center', va='top')
|
47 |
+
|
48 |
+
plt.tight_layout()
|
49 |
+
plt.savefig("output.png", bbox_inches='tight')
|
main/mapping.py
CHANGED
@@ -2,20 +2,44 @@ import json
|
|
2 |
import os
|
3 |
import sys
|
4 |
import pickle
|
5 |
-
from
|
|
|
|
|
6 |
|
7 |
def mapping(img_folders:str, label_folder:str):
|
8 |
new_data = []
|
9 |
-
|
10 |
-
|
11 |
-
for
|
12 |
-
|
13 |
-
|
|
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
if __name__ == "__main__":
|
17 |
img_folder = "./temp_data"
|
18 |
label_folder = "./temp_data/ffhq-features-dataset/json"
|
19 |
-
|
20 |
-
print(os.listdir(img_folder))
|
21 |
-
print(os.path.isdir("./temp_data/00000"))
|
|
|
2 |
import os
|
3 |
import sys
|
4 |
import pickle
|
5 |
+
from PIL import Image
|
6 |
+
from tqdm import tqdm
|
7 |
+
from utils import parse_face_attributes, json_element_cnt
|
8 |
|
9 |
def mapping(img_folders:str, label_folder:str):
|
10 |
new_data = []
|
11 |
+
label_data = {}
|
12 |
+
img_folders = [img_folders+"/"+folder for folder in os.listdir(img_folders) if os.path.isdir(os.path.join(img_folders, folder))]
|
13 |
+
labels = [label for label in os.listdir(label_folder) if json_element_cnt(os.path.join(label_folder, label)) == 1]
|
14 |
+
# for label in labels:
|
15 |
+
for label in tqdm(labels, desc="mapping labels to images"):
|
16 |
+
label_data[label] = parse_face_attributes(os.path.join(label_folder, label))
|
17 |
|
18 |
+
print(len(label_data.keys()))
|
19 |
+
print(list(label_data.keys())[:10])
|
20 |
+
error = 0
|
21 |
+
for label_file_name in tqdm(label_data.keys(), desc="mapping images to labels"):
|
22 |
+
img_idx = label_file_name.split(".")[0]
|
23 |
+
img_idx_str = f"img000{img_idx}.png"
|
24 |
+
img_idx_int = int(img_idx)
|
25 |
+
img_folder_idx = img_idx_int // 1000
|
26 |
+
# print(img_folders[0])
|
27 |
+
# print(img_folders[img_folder_idx])
|
28 |
+
# print(f"loc: {os.path.join(img_folders[img_folder_idx], img_idx_str)}")
|
29 |
+
# print(os.path.exists(os.path.join(img_folders[img_folder_idx], img_idx_str)))
|
30 |
+
if os.path.exists(os.path.join(img_folders[img_folder_idx], img_idx_str)):
|
31 |
+
new_data.append({
|
32 |
+
# "img": os.path.join(img_folders[img_folder_idx], img_idx_str),
|
33 |
+
"img": Image.open(os.path.join(img_folders[img_folder_idx], img_idx_str)),
|
34 |
+
"label": label_data[label_file_name]})
|
35 |
+
else:
|
36 |
+
error += 1
|
37 |
+
print(len(new_data))
|
38 |
+
print(error)
|
39 |
+
with open("ffhq_data.pkl", "wb") as f:
|
40 |
+
pickle.dump(new_data, f)
|
41 |
|
42 |
if __name__ == "__main__":
|
43 |
img_folder = "./temp_data"
|
44 |
label_folder = "./temp_data/ffhq-features-dataset/json"
|
45 |
+
mapping(img_folder, label_folder)
|
|
|
|
main/utils.py
CHANGED
@@ -1,5 +1,11 @@
|
|
1 |
import json
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
def parse_face_attributes(json_file:str):
|
4 |
with open(json_file, 'r', encoding='utf-8') as file:
|
5 |
data = json.load(file)
|
|
|
1 |
import json
|
2 |
|
3 |
+
def json_element_cnt(json_file:str):
|
4 |
+
with open(json_file, 'r', encoding='utf-8') as file:
|
5 |
+
data = json.load(file)
|
6 |
+
|
7 |
+
return len(data)
|
8 |
+
|
9 |
def parse_face_attributes(json_file:str):
|
10 |
with open(json_file, 'r', encoding='utf-8') as file:
|
11 |
data = json.load(file)
|