Datasets:
Commit
·
4b851bd
1
Parent(s):
05ac09e
add face attribute parsing functionality and example JSON data
Browse files- main/ex.json +89 -0
- main/main.py +158 -1
main/ex.json
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[
|
2 |
+
{
|
3 |
+
"faceId": "866db6af-66ee-4828-b565-994f42571d2f",
|
4 |
+
"faceRectangle": {
|
5 |
+
"top": 42,
|
6 |
+
"left": 32,
|
7 |
+
"width": 63,
|
8 |
+
"height": 63
|
9 |
+
},
|
10 |
+
"faceAttributes": {
|
11 |
+
"smile": 0.003,
|
12 |
+
"headPose": {
|
13 |
+
"pitch": 5.4,
|
14 |
+
"roll": 1.5,
|
15 |
+
"yaw": 15.3
|
16 |
+
},
|
17 |
+
"gender": "female",
|
18 |
+
"age": 0.0,
|
19 |
+
"facialHair": {
|
20 |
+
"moustache": 0.0,
|
21 |
+
"beard": 0.0,
|
22 |
+
"sideburns": 0.0
|
23 |
+
},
|
24 |
+
"glasses": "NoGlasses",
|
25 |
+
"emotion": {
|
26 |
+
"anger": 0.0,
|
27 |
+
"contempt": 0.0,
|
28 |
+
"disgust": 0.0,
|
29 |
+
"fear": 0.0,
|
30 |
+
"happiness": 0.003,
|
31 |
+
"neutral": 0.993,
|
32 |
+
"sadness": 0.003,
|
33 |
+
"surprise": 0.0
|
34 |
+
},
|
35 |
+
"blur": {
|
36 |
+
"blurLevel": "medium",
|
37 |
+
"value": 0.55
|
38 |
+
},
|
39 |
+
"exposure": {
|
40 |
+
"exposureLevel": "goodExposure",
|
41 |
+
"value": 0.6
|
42 |
+
},
|
43 |
+
"noise": {
|
44 |
+
"noiseLevel": "medium",
|
45 |
+
"value": 0.44
|
46 |
+
},
|
47 |
+
"makeup": {
|
48 |
+
"eyeMakeup": false,
|
49 |
+
"lipMakeup": false
|
50 |
+
},
|
51 |
+
"accessories": [],
|
52 |
+
"occlusion": {
|
53 |
+
"foreheadOccluded": false,
|
54 |
+
"eyeOccluded": false,
|
55 |
+
"mouthOccluded": false
|
56 |
+
},
|
57 |
+
"hair": {
|
58 |
+
"bald": 0.07,
|
59 |
+
"invisible": false,
|
60 |
+
"hairColor": [
|
61 |
+
{
|
62 |
+
"color": "brown",
|
63 |
+
"confidence": 0.93
|
64 |
+
},
|
65 |
+
{
|
66 |
+
"color": "blond",
|
67 |
+
"confidence": 0.8
|
68 |
+
},
|
69 |
+
{
|
70 |
+
"color": "black",
|
71 |
+
"confidence": 0.41
|
72 |
+
},
|
73 |
+
{
|
74 |
+
"color": "red",
|
75 |
+
"confidence": 0.28
|
76 |
+
},
|
77 |
+
{
|
78 |
+
"color": "gray",
|
79 |
+
"confidence": 0.22
|
80 |
+
},
|
81 |
+
{
|
82 |
+
"color": "other",
|
83 |
+
"confidence": 0.2
|
84 |
+
}
|
85 |
+
]
|
86 |
+
}
|
87 |
+
}
|
88 |
+
}
|
89 |
+
]
|
main/main.py
CHANGED
@@ -1 +1,158 @@
|
|
1 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
7 |
+
data = json.load(file)
|
8 |
+
|
9 |
+
data = data[0]
|
10 |
+
|
11 |
+
# 取出 faceAttributes 部分
|
12 |
+
face_attr = data.get('faceAttributes', {})
|
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:
|
31 |
+
pitch_cat = "downward"
|
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:
|
42 |
+
yaw_cat = "turned left"
|
43 |
+
else:
|
44 |
+
yaw_cat = "frontal"
|
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"
|
56 |
+
elif age < 10:
|
57 |
+
age_cat = "child"
|
58 |
+
elif age < 20:
|
59 |
+
age_cat = "teenager"
|
60 |
+
elif age < 60:
|
61 |
+
age_cat = "adult"
|
62 |
+
else:
|
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)
|
81 |
+
else:
|
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 |
+
|
97 |
+
# 11. 妝容 (makeup):根據 eyeMakeup 與 lipMakeup 判斷
|
98 |
+
makeup = face_attr.get("makeup", {})
|
99 |
+
makeup_categories = []
|
100 |
+
if makeup.get("eyeMakeup", False):
|
101 |
+
makeup_categories.append("eye makeup")
|
102 |
+
if makeup.get("lipMakeup", False):
|
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:
|
119 |
+
hair_cat = "bald"
|
120 |
+
else:
|
121 |
+
hair_colors = hair.get("hairColor", [])
|
122 |
+
if hair_colors:
|
123 |
+
dominant_color = max(hair_colors, key=lambda x: x.get("confidence", 0))["color"]
|
124 |
+
else:
|
125 |
+
dominant_color = "unknown"
|
126 |
+
hair_cat = dominant_color
|
127 |
+
else:
|
128 |
+
hair_cat = "unknown"
|
129 |
+
result["hair"] = hair_cat
|
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())}")
|