Datasets:
File size: 6,323 Bytes
4b851bd 1b86742 4b851bd 1b86742 4b851bd 1b86742 4b851bd 1b86742 4b851bd 1b86742 4b851bd 1b86742 4b851bd 1b86742 4b851bd 1b86742 4b851bd 1b86742 4b851bd 1b86742 4b851bd 1b86742 4b851bd 1b86742 4b851bd 1b86742 4b851bd 1b86742 4b851bd 1b86742 4b851bd 1b86742 4b851bd 1b86742 4b851bd 1b86742 4b851bd 1b86742 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
import json
import os
def parse_face_attributes(json_file):
with open(json_file, 'r', encoding='utf-8') as file:
data = json.load(file)
data = data[0]
# 取出 faceAttributes 部分
face_attr = data.get('faceAttributes', {})
result = {}
# 1. 笑容:若 smile 大於 0.1 則視為 "smiling",否則 "no smile"
smile_value = face_attr.get("smile", 0)
result['smile'] = "smiling" if smile_value > 0.1 else "no smile"
# 2. 頭部姿勢 (headPose)
head_pose = face_attr.get("headPose", {})
pitch = head_pose.get("pitch", 0)
roll = head_pose.get("roll", 0)
yaw = head_pose.get("yaw", 0)
# pitch:大於 5 為 "upward",小於 -5 為 "downward",否則 "neutral"
if pitch > 5:
pitch_cat = "upward"
elif pitch < -5:
pitch_cat = "downward"
else:
pitch_cat = "neutral"
# roll:絕對值大於 5 為 "tilted",否則 "neutral"
roll_cat = "tilted" if abs(roll) > 5 else "neutral"
# yaw:大於 15 為 "turned right",小於 -15 為 "turned left",否則 "frontal"
if yaw > 15:
yaw_cat = "turned right"
elif yaw < -15:
yaw_cat = "turned left"
else:
yaw_cat = "frontal"
result['headPose'] = {"pitch": pitch_cat, "roll": roll_cat, "yaw": yaw_cat}
# 3. 性別 (gender)
result["gender"] = face_attr.get("gender", "unknown")
# 4. 年齡 (age):依年齡數值分類
age = face_attr.get("age", 0)
if age < 2:
age_cat = "baby"
elif age < 10:
age_cat = "child"
elif age < 20:
age_cat = "teenager"
elif age < 60:
age_cat = "adult"
else:
age_cat = "senior"
result["age"] = age_cat
# 5. 面部毛髮 (facialHair):若各項均不超過 0.1 則為 "none"
facial_hair = face_attr.get("facialHair", {})
hair_types = []
for key, value in facial_hair.items():
if value > 0.1:
hair_types.append(key)
result["facialHair"] = "none" if not hair_types else ", ".join(hair_types)
# 6. 眼鏡狀態 (glasses)
result["glasses"] = face_attr.get("glasses", "NoGlasses")
# 7. 情緒 (emotion):取分數最高的情緒
emotion = face_attr.get("emotion", {})
if emotion:
emotion_category = max(emotion, key=emotion.get)
else:
emotion_category = "unknown"
result["emotion"] = emotion_category
# 8. 模糊度 (blur):以 blurLevel 為類別
blur = face_attr.get("blur", {})
result["blur"] = blur.get("blurLevel", "unknown")
# 9. 曝光 (exposure):以 exposureLevel 為類別
exposure = face_attr.get("exposure", {})
result["exposure"] = exposure.get("exposureLevel", "unknown")
# 10. 噪音 (noise):以 noiseLevel 為類別
noise = face_attr.get("noise", {})
result["noise"] = noise.get("noiseLevel", "unknown")
# 11. 妝容 (makeup):根據 eyeMakeup 與 lipMakeup 判斷
makeup = face_attr.get("makeup", {})
makeup_categories = []
if makeup.get("eyeMakeup", False):
makeup_categories.append("eye makeup")
if makeup.get("lipMakeup", False):
makeup_categories.append("lip makeup")
result["makeup"] = "no makeup" if not makeup_categories else ", ".join(makeup_categories)
# 12. 配件 (accessories):若無配件則回傳 "none"
accessories = face_attr.get("accessories", [])
result["accessories"] = "none" if not accessories else ", ".join([acc.get("type", "unknown") for acc in accessories])
# 13. 遮擋 (occlusion):若皆為 False 則為 "none",否則列出被遮擋的部位
occlusion = face_attr.get("occlusion", {})
occlusion_list = [k for k, v in occlusion.items() if v]
result["occlusion"] = "none" if not occlusion_list else ", ".join(occlusion_list)
# 14. 頭髮 (hair):若 bald 大於 0.5 則為 "bald",否則取 hairColor 中信心最高的顏色
hair = face_attr.get("hair", {})
if hair:
if hair.get("bald", 0) > 0.5:
hair_cat = "bald"
else:
hair_colors = hair.get("hairColor", [])
if hair_colors:
dominant_color = max(hair_colors, key=lambda x: x.get("confidence", 0))["color"]
else:
dominant_color = "unknown"
hair_cat = dominant_color
else:
hair_cat = "unknown"
result["hair"] = hair_cat
return result
def record_all_categories(directory):
"""
讀取指定目錄下所有 JSON 檔案,並統計每個屬性出現的所有類別
"""
categories = {} # 用來儲存各屬性的所有類別 (以 set 方式儲存避免重複)
# 遍歷資料夾中所有 JSON 檔案
error_cnt = 0
for filename in os.listdir(directory):
if not filename.endswith(".json"):
continue
file_path = os.path.join(directory, filename)
try:
parsed_result = parse_face_attributes(file_path)
except Exception as e:
error_cnt += 1
# print(f"處理檔案 {filename} 時發生錯誤: {e}")
continue
# 將每個屬性對應的類別加入集合中
for key, value in parsed_result.items():
# 若屬性值為字典(例如 headPose),則進一步處理各子項
if isinstance(value, dict):
for sub_key, sub_val in value.items():
if sub_key not in categories:
categories[sub_key] = set()
categories[sub_key].add(sub_val)
else:
if key not in categories:
categories[key] = set()
categories[key].add(value)
print(f"總共處理 {len(os.listdir(directory))} 個檔案,其中 {error_cnt} 個檔案發生錯誤。")
return categories
if __name__ == "__main__":
# 設定 JSON 檔案所在目錄(請根據實際路徑修改)
directory = "/root/siglip-recursion-ffhq-thumbnails/main/temp_data/ffhq-features-dataset/json"
# 紀錄所有類別
all_categories = record_all_categories(directory)
# 印出各屬性出現的所有類別
for attribute, values in all_categories.items():
print(f"{attribute}: {', '.join(values)}") |