hibana2077 commited on
Commit
2e2787d
·
1 Parent(s): 1b86742

add face attribute parsing utility and update .gitignore to exclude __pycache__

Browse files
Files changed (3) hide show
  1. .gitignore +2 -1
  2. main/mapping.py +21 -0
  3. main/utils.py +127 -0
.gitignore CHANGED
@@ -1 +1,2 @@
1
- main/temp_data
 
 
1
+ main/temp_data
2
+ main/__pycache__
main/mapping.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import sys
4
+ import pickle
5
+ from utils import parse_face_attributes
6
+
7
+ def mapping(img_folders:str, label_folder:str):
8
+ new_data = []
9
+ img_folders = [folder for folder in os.listdir(img_folders) if os.path.isdir(os.path.join(img_folders, folder))]
10
+ labels = os.listdir(label_folder)
11
+ for folder in img_folders:
12
+ pass
13
+
14
+
15
+
16
+ if __name__ == "__main__":
17
+ img_folder = "./temp_data"
18
+ label_folder = "./temp_data/ffhq-features-dataset/json"
19
+ # mapping(img_folder, label_folder)
20
+ print(os.listdir(img_folder))
21
+ print(os.path.isdir("./temp_data/00000"))
main/utils.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
6
+
7
+ data = data[0]
8
+
9
+ # 取出 faceAttributes 部分
10
+ face_attr = data.get('faceAttributes', {})
11
+
12
+ result = {}
13
+
14
+ # 1. 笑容:若 smile 大於 0.1 則視為 "smiling",否則 "no smile"
15
+ smile_value = face_attr.get("smile", 0)
16
+ result['smile'] = "smiling" if smile_value > 0.1 else "no smile"
17
+
18
+ # 2. 頭部姿勢 (headPose)
19
+ head_pose = face_attr.get("headPose", {})
20
+ pitch = head_pose.get("pitch", 0)
21
+ roll = head_pose.get("roll", 0)
22
+ yaw = head_pose.get("yaw", 0)
23
+
24
+ # pitch:大於 5 為 "upward",小於 -5 為 "downward",否則 "neutral"
25
+ if pitch > 5:
26
+ pitch_cat = "upward"
27
+ elif pitch < -5:
28
+ pitch_cat = "downward"
29
+ else:
30
+ pitch_cat = "neutral"
31
+
32
+ # roll:絕對值大於 5 為 "tilted",否則 "neutral"
33
+ roll_cat = "tilted" if abs(roll) > 5 else "neutral"
34
+
35
+ # yaw:大於 15 為 "turned right",小於 -15 為 "turned left",否則 "frontal"
36
+ if yaw > 15:
37
+ yaw_cat = "turned right"
38
+ elif yaw < -15:
39
+ yaw_cat = "turned left"
40
+ else:
41
+ yaw_cat = "frontal"
42
+
43
+ result['headPose'] = {"pitch": pitch_cat, "roll": roll_cat, "yaw": yaw_cat}
44
+
45
+ # 3. 性別 (gender)
46
+ result["gender"] = face_attr.get("gender", "unknown")
47
+
48
+ # 4. 年齡 (age):依年齡數值分類
49
+ age = face_attr.get("age", 0)
50
+ if age < 2:
51
+ age_cat = "baby"
52
+ elif age < 10:
53
+ age_cat = "child"
54
+ elif age < 20:
55
+ age_cat = "teenager"
56
+ elif age < 60:
57
+ age_cat = "adult"
58
+ else:
59
+ age_cat = "senior"
60
+ result["age"] = age_cat
61
+
62
+ # 5. 面部毛髮 (facialHair):若各項均不超過 0.1 則為 "none"
63
+ facial_hair = face_attr.get("facialHair", {})
64
+ hair_types = []
65
+ for key, value in facial_hair.items():
66
+ if value > 0.1:
67
+ hair_types.append(key)
68
+ result["facialHair"] = "none" if not hair_types else ", ".join(hair_types)
69
+
70
+ # 6. 眼鏡狀態 (glasses)
71
+ result["glasses"] = face_attr.get("glasses", "NoGlasses")
72
+
73
+ # 7. 情緒 (emotion):取分數最高的情緒
74
+ emotion = face_attr.get("emotion", {})
75
+ if emotion:
76
+ emotion_category = max(emotion, key=emotion.get)
77
+ else:
78
+ emotion_category = "unknown"
79
+ result["emotion"] = emotion_category
80
+
81
+ # 8. 模糊度 (blur):以 blurLevel 為類別
82
+ blur = face_attr.get("blur", {})
83
+ result["blur"] = blur.get("blurLevel", "unknown")
84
+
85
+ # 9. 曝光 (exposure):以 exposureLevel 為類別
86
+ exposure = face_attr.get("exposure", {})
87
+ result["exposure"] = exposure.get("exposureLevel", "unknown")
88
+
89
+ # 10. 噪音 (noise):以 noiseLevel 為類別
90
+ noise = face_attr.get("noise", {})
91
+ result["noise"] = noise.get("noiseLevel", "unknown")
92
+
93
+ # 11. 妝容 (makeup):根據 eyeMakeup 與 lipMakeup 判斷
94
+ makeup = face_attr.get("makeup", {})
95
+ makeup_categories = []
96
+ if makeup.get("eyeMakeup", False):
97
+ makeup_categories.append("eye makeup")
98
+ if makeup.get("lipMakeup", False):
99
+ makeup_categories.append("lip makeup")
100
+ result["makeup"] = "no makeup" if not makeup_categories else ", ".join(makeup_categories)
101
+
102
+ # 12. 配件 (accessories):若無配件則回傳 "none"
103
+ accessories = face_attr.get("accessories", [])
104
+ result["accessories"] = "none" if not accessories else ", ".join([acc.get("type", "unknown") for acc in accessories])
105
+
106
+ # 13. 遮擋 (occlusion):若皆為 False 則為 "none",否則列出被遮擋的部位
107
+ occlusion = face_attr.get("occlusion", {})
108
+ occlusion_list = [k for k, v in occlusion.items() if v]
109
+ result["occlusion"] = "none" if not occlusion_list else ", ".join(occlusion_list)
110
+
111
+ # 14. 頭髮 (hair):若 bald 大於 0.5 則為 "bald",否則取 hairColor 中信心最高的顏色
112
+ hair = face_attr.get("hair", {})
113
+ if hair:
114
+ if hair.get("bald", 0) > 0.5:
115
+ hair_cat = "bald"
116
+ else:
117
+ hair_colors = hair.get("hairColor", [])
118
+ if hair_colors:
119
+ dominant_color = max(hair_colors, key=lambda x: x.get("confidence", 0))["color"]
120
+ else:
121
+ dominant_color = "unknown"
122
+ hair_cat = dominant_color
123
+ else:
124
+ hair_cat = "unknown"
125
+ result["hair"] = hair_cat
126
+
127
+ return result