|
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] |
|
|
|
|
|
face_attr = data.get('faceAttributes', {}) |
|
|
|
result = {} |
|
|
|
|
|
smile_value = face_attr.get("smile", 0) |
|
result['smile'] = "smiling" if smile_value > 0.1 else "no smile" |
|
|
|
|
|
head_pose = face_attr.get("headPose", {}) |
|
pitch = head_pose.get("pitch", 0) |
|
roll = head_pose.get("roll", 0) |
|
yaw = head_pose.get("yaw", 0) |
|
|
|
|
|
if pitch > 5: |
|
pitch_cat = "upward" |
|
elif pitch < -5: |
|
pitch_cat = "downward" |
|
else: |
|
pitch_cat = "neutral" |
|
|
|
|
|
roll_cat = "tilted" if abs(roll) > 5 else "neutral" |
|
|
|
|
|
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} |
|
|
|
|
|
result["gender"] = face_attr.get("gender", "unknown") |
|
|
|
|
|
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 |
|
|
|
|
|
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) |
|
|
|
|
|
result["glasses"] = face_attr.get("glasses", "NoGlasses") |
|
|
|
|
|
emotion = face_attr.get("emotion", {}) |
|
if emotion: |
|
emotion_category = max(emotion, key=emotion.get) |
|
else: |
|
emotion_category = "unknown" |
|
result["emotion"] = emotion_category |
|
|
|
|
|
blur = face_attr.get("blur", {}) |
|
result["blur"] = blur.get("blurLevel", "unknown") |
|
|
|
|
|
exposure = face_attr.get("exposure", {}) |
|
result["exposure"] = exposure.get("exposureLevel", "unknown") |
|
|
|
|
|
noise = face_attr.get("noise", {}) |
|
result["noise"] = noise.get("noiseLevel", "unknown") |
|
|
|
|
|
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) |
|
|
|
|
|
accessories = face_attr.get("accessories", []) |
|
result["accessories"] = "none" if not accessories else ", ".join([acc.get("type", "unknown") for acc in accessories]) |
|
|
|
|
|
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) |
|
|
|
|
|
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 = {} |
|
|
|
|
|
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 |
|
|
|
continue |
|
|
|
|
|
for key, value in parsed_result.items(): |
|
|
|
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__": |
|
|
|
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)}") |