hibana2077 commited on
Commit
1b86742
·
1 Parent(s): 4b851bd

add face attribute labels to README and implement category recording function

Browse files
Files changed (2) hide show
  1. README.md +20 -0
  2. main/main.py +59 -44
README.md CHANGED
@@ -2,6 +2,26 @@
2
  license: mit
3
  ---
4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  ## Acknowledgements
7
 
 
2
  license: mit
3
  ---
4
 
5
+ ## Labels
6
+
7
+ ```txt
8
+ • smile:smiling, no smile
9
+ • pitch:downward, neutral, upward
10
+ • roll:tilted, neutral
11
+ • yaw:turned left, frontal, turned right
12
+ • gender:male, female
13
+ • age:adult, baby, child, teenager, senior
14
+ • facialHair:moustache, beard, sideburns, none
15
+ • glasses:NoGlasses, SwimmingGoggles, Sunglasses, ReadingGlasses
16
+ • emotion:happiness, sadness, surprise, contempt, disgust, neutral, anger, fear
17
+ • blur:high, medium, low
18
+ • exposure:goodExposure, underExposure, overExposure
19
+ • noise:high, medium, low
20
+ • makeup:eye makeup, lip makeup, no makeup
21
+ • accessories:glasses, mask, headwear, none
22
+ • occlusion:foreheadOccluded, mouthOccluded, eyeOccluded, none
23
+ • hair:other, blond, gray, bald, unknown, red, brown, black
24
+ ```
25
 
26
  ## Acknowledgements
27
 
main/main.py CHANGED
@@ -1,6 +1,5 @@
1
  import json
2
  import os
3
- import sys
4
 
5
  def parse_face_attributes(json_file):
6
  with open(json_file, 'r', encoding='utf-8') as file:
@@ -13,18 +12,17 @@ def parse_face_attributes(json_file):
13
 
14
  result = {}
15
 
16
- # 1. 處理笑容:根據 smile 值轉換為類別
17
- # 若 smile 大於 0.1 則視為「smiling」,否則視為「no smile」
18
  smile_value = face_attr.get("smile", 0)
19
  result['smile'] = "smiling" if smile_value > 0.1 else "no smile"
20
 
21
- # 2. 處理頭部姿勢 (headPose)
22
  head_pose = face_attr.get("headPose", {})
23
  pitch = head_pose.get("pitch", 0)
24
  roll = head_pose.get("roll", 0)
25
  yaw = head_pose.get("yaw", 0)
26
 
27
- # pitch:若大於 5 則「upward」,小於 -5 則「downward」,否則「neutral
28
  if pitch > 5:
29
  pitch_cat = "upward"
30
  elif pitch < -5:
@@ -32,10 +30,10 @@ def parse_face_attributes(json_file):
32
  else:
33
  pitch_cat = "neutral"
34
 
35
- # roll:若絕對值大於 5 則「tilted」,否則「neutral
36
  roll_cat = "tilted" if abs(roll) > 5 else "neutral"
37
 
38
- # yaw:若大於 15 則「turned right」,小於 -15 則「turned left」,否則「frontal
39
  if yaw > 15:
40
  yaw_cat = "turned right"
41
  elif yaw < -15:
@@ -45,11 +43,10 @@ def parse_face_attributes(json_file):
45
 
46
  result['headPose'] = {"pitch": pitch_cat, "roll": roll_cat, "yaw": yaw_cat}
47
 
48
- # 3. 性別 (gender) 為類別型資料,直接取用
49
  result["gender"] = face_attr.get("gender", "unknown")
50
 
51
- # 4. 年齡 (age):將數值轉換為類別
52
- # 定義閾值:小於 2 歲視為 "baby",2-10 歲視為 "child",10-20 歲視為 "teenager",20-60 歲視為 "adult",60 歲以上視為 "senior"
53
  age = face_attr.get("age", 0)
54
  if age < 2:
55
  age_cat = "baby"
@@ -63,18 +60,18 @@ def parse_face_attributes(json_file):
63
  age_cat = "senior"
64
  result["age"] = age_cat
65
 
66
- # 5. 面部毛髮 (facialHair):檢查各項是否大於 0.1,若皆不超過則視為 "none"
67
  facial_hair = face_attr.get("facialHair", {})
68
  hair_types = []
69
  for key, value in facial_hair.items():
70
- if value > 0.1: # 可調整閾值
71
  hair_types.append(key)
72
  result["facialHair"] = "none" if not hair_types else ", ".join(hair_types)
73
 
74
- # 6. 眼鏡狀態 (glasses) 直接取用原始資料
75
  result["glasses"] = face_attr.get("glasses", "NoGlasses")
76
 
77
- # 7. 情緒 (emotion):取出分數最高的情緒作為類別
78
  emotion = face_attr.get("emotion", {})
79
  if emotion:
80
  emotion_category = max(emotion, key=emotion.get)
@@ -82,15 +79,15 @@ def parse_face_attributes(json_file):
82
  emotion_category = "unknown"
83
  result["emotion"] = emotion_category
84
 
85
- # 8. 模糊度 (blur):採用 blurLevel 作為類別
86
  blur = face_attr.get("blur", {})
87
  result["blur"] = blur.get("blurLevel", "unknown")
88
 
89
- # 9. 曝光 (exposure):採用 exposureLevel 作為類別
90
  exposure = face_attr.get("exposure", {})
91
  result["exposure"] = exposure.get("exposureLevel", "unknown")
92
 
93
- # 10. 噪音 (noise):採用 noiseLevel 作為類別
94
  noise = face_attr.get("noise", {})
95
  result["noise"] = noise.get("noiseLevel", "unknown")
96
 
@@ -103,16 +100,16 @@ def parse_face_attributes(json_file):
103
  makeup_categories.append("lip makeup")
104
  result["makeup"] = "no makeup" if not makeup_categories else ", ".join(makeup_categories)
105
 
106
- # 12. 配件 (accessories):若空則回傳 "none"
107
  accessories = face_attr.get("accessories", [])
108
  result["accessories"] = "none" if not accessories else ", ".join([acc.get("type", "unknown") for acc in accessories])
109
 
110
- # 13. 遮擋 (occlusion):若皆為 False "none",否則列出被遮擋的部位
111
  occlusion = face_attr.get("occlusion", {})
112
  occlusion_list = [k for k, v in occlusion.items() if v]
113
  result["occlusion"] = "none" if not occlusion_list else ", ".join(occlusion_list)
114
 
115
- # 14. 頭髮 (hair):若 bald 大於 0.5 則視為 bald,否則取 hairColor 中信心最高的顏色
116
  hair = face_attr.get("hair", {})
117
  if hair:
118
  if hair.get("bald", 0) > 0.5:
@@ -130,29 +127,47 @@ def parse_face_attributes(json_file):
130
 
131
  return result
132
 
133
- def test_json(file_path):
134
- with open(file_path, 'r', encoding='utf-8') as file:
135
- data = json.load(file)
136
- return len(data)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137
 
138
- # 範例使用方式:
139
  if __name__ == "__main__":
140
- from pprint import pprint
141
- # 假設 JSON 檔案名稱為 "face_data.json"
142
- parsed_data = parse_face_attributes("/root/siglip-recursion-ffhq-thumbnails/main/temp_data/ffhq-features-dataset/json/01111.json")
143
- pprint(parsed_data)
144
- # file_list = os.listdir("/root/siglip-recursion-ffhq-thumbnails/main/temp_data/ffhq-features-dataset/json")
145
- # cnt = {}
146
- # outlier = {}
147
- # for file in file_list:
148
- # file_path = os.path.join("/root/siglip-recursion-ffhq-thumbnails/main/temp_data/ffhq-features-dataset/json", file)
149
- # length = test_json(file_path)
150
- # if length in cnt:
151
- # cnt[length] += 1
152
- # else:
153
- # cnt[length] = 1
154
- # if length > 1:
155
- # outlier[file] = length
156
- # print(outlier)
157
- # print(cnt)
158
- # print(f"total: {sum(cnt.values())}")
 
1
  import json
2
  import os
 
3
 
4
  def parse_face_attributes(json_file):
5
  with open(json_file, 'r', encoding='utf-8') as file:
 
12
 
13
  result = {}
14
 
15
+ # 1. 笑容:若 smile 大於 0.1 則視為 "smiling",否則 "no smile"
 
16
  smile_value = face_attr.get("smile", 0)
17
  result['smile'] = "smiling" if smile_value > 0.1 else "no smile"
18
 
19
+ # 2. 頭部姿勢 (headPose)
20
  head_pose = face_attr.get("headPose", {})
21
  pitch = head_pose.get("pitch", 0)
22
  roll = head_pose.get("roll", 0)
23
  yaw = head_pose.get("yaw", 0)
24
 
25
+ # pitch:大於 5 為 "upward",小於 -5 為 "downward",否則 "neutral"
26
  if pitch > 5:
27
  pitch_cat = "upward"
28
  elif pitch < -5:
 
30
  else:
31
  pitch_cat = "neutral"
32
 
33
+ # roll:絕對值大於 5 為 "tilted",否則 "neutral"
34
  roll_cat = "tilted" if abs(roll) > 5 else "neutral"
35
 
36
+ # yaw:大於 15 為 "turned right",小於 -15 為 "turned left",否則 "frontal"
37
  if yaw > 15:
38
  yaw_cat = "turned right"
39
  elif yaw < -15:
 
43
 
44
  result['headPose'] = {"pitch": pitch_cat, "roll": roll_cat, "yaw": yaw_cat}
45
 
46
+ # 3. 性別 (gender)
47
  result["gender"] = face_attr.get("gender", "unknown")
48
 
49
+ # 4. 年齡 (age):依年齡數值分類
 
50
  age = face_attr.get("age", 0)
51
  if age < 2:
52
  age_cat = "baby"
 
60
  age_cat = "senior"
61
  result["age"] = age_cat
62
 
63
+ # 5. 面部毛髮 (facialHair):若各項均不超過 0.1 則為 "none"
64
  facial_hair = face_attr.get("facialHair", {})
65
  hair_types = []
66
  for key, value in facial_hair.items():
67
+ if value > 0.1:
68
  hair_types.append(key)
69
  result["facialHair"] = "none" if not hair_types else ", ".join(hair_types)
70
 
71
+ # 6. 眼鏡狀態 (glasses)
72
  result["glasses"] = face_attr.get("glasses", "NoGlasses")
73
 
74
+ # 7. 情緒 (emotion):取分數最高的情緒
75
  emotion = face_attr.get("emotion", {})
76
  if emotion:
77
  emotion_category = max(emotion, key=emotion.get)
 
79
  emotion_category = "unknown"
80
  result["emotion"] = emotion_category
81
 
82
+ # 8. 模糊度 (blur):以 blurLevel 為類別
83
  blur = face_attr.get("blur", {})
84
  result["blur"] = blur.get("blurLevel", "unknown")
85
 
86
+ # 9. 曝光 (exposure):以 exposureLevel 為類別
87
  exposure = face_attr.get("exposure", {})
88
  result["exposure"] = exposure.get("exposureLevel", "unknown")
89
 
90
+ # 10. 噪音 (noise):以 noiseLevel 為類別
91
  noise = face_attr.get("noise", {})
92
  result["noise"] = noise.get("noiseLevel", "unknown")
93
 
 
100
  makeup_categories.append("lip makeup")
101
  result["makeup"] = "no makeup" if not makeup_categories else ", ".join(makeup_categories)
102
 
103
+ # 12. 配件 (accessories):若無配件則回傳 "none"
104
  accessories = face_attr.get("accessories", [])
105
  result["accessories"] = "none" if not accessories else ", ".join([acc.get("type", "unknown") for acc in accessories])
106
 
107
+ # 13. 遮擋 (occlusion):若皆為 False 則為 "none",否則列出被遮擋的部位
108
  occlusion = face_attr.get("occlusion", {})
109
  occlusion_list = [k for k, v in occlusion.items() if v]
110
  result["occlusion"] = "none" if not occlusion_list else ", ".join(occlusion_list)
111
 
112
+ # 14. 頭髮 (hair):若 bald 大於 0.5 則為 "bald",否則取 hairColor 中信心最高的顏色
113
  hair = face_attr.get("hair", {})
114
  if hair:
115
  if hair.get("bald", 0) > 0.5:
 
127
 
128
  return result
129
 
130
+ def record_all_categories(directory):
131
+ """
132
+ 讀取指定目錄下所有 JSON 檔案,並統計每個屬性出現的所有類別
133
+ """
134
+ categories = {} # 用來儲存各屬性的所有類別 (以 set 方式儲存避免重複)
135
+
136
+ # 遍歷資料夾中所有 JSON 檔案
137
+ error_cnt = 0
138
+ for filename in os.listdir(directory):
139
+ if not filename.endswith(".json"):
140
+ continue
141
+ file_path = os.path.join(directory, filename)
142
+ try:
143
+ parsed_result = parse_face_attributes(file_path)
144
+ except Exception as e:
145
+ error_cnt += 1
146
+ # print(f"處理檔案 {filename} 時發生錯誤: {e}")
147
+ continue
148
+
149
+ # 將每個屬性對應的類別加入集合中
150
+ for key, value in parsed_result.items():
151
+ # 若屬性值為字典(例如 headPose),則進一步處理各子項
152
+ if isinstance(value, dict):
153
+ for sub_key, sub_val in value.items():
154
+ if sub_key not in categories:
155
+ categories[sub_key] = set()
156
+ categories[sub_key].add(sub_val)
157
+ else:
158
+ if key not in categories:
159
+ categories[key] = set()
160
+ categories[key].add(value)
161
+ print(f"總共處理 {len(os.listdir(directory))} 個檔案,其中 {error_cnt} 個檔案發生錯誤。")
162
+ return categories
163
 
 
164
  if __name__ == "__main__":
165
+ # 設定 JSON 檔案所在目錄(請根據實際路徑修改)
166
+ directory = "/root/siglip-recursion-ffhq-thumbnails/main/temp_data/ffhq-features-dataset/json"
167
+
168
+ # 紀錄所有類別
169
+ all_categories = record_all_categories(directory)
170
+
171
+ # 印出各屬性出現的所有類別
172
+ for attribute, values in all_categories.items():
173
+ print(f"{attribute}: {', '.join(values)}")