Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -121,21 +121,27 @@ def classify_zip_and_analyze_color(zip_file):
|
|
| 121 |
# ---------------------------
|
| 122 |
# Face detection & characterization
|
| 123 |
# ---------------------------
|
| 124 |
-
|
| 125 |
try:
|
| 126 |
img_cv2 = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 127 |
-
|
| 128 |
-
img_cv2,
|
| 129 |
-
actions=["age", "gender", "emotion"],
|
| 130 |
-
enforce_detection=False
|
| 131 |
)
|
| 132 |
-
if isinstance(
|
| 133 |
-
for f in
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
else:
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
except Exception:
|
| 138 |
-
|
| 139 |
|
| 140 |
results.append((
|
| 141 |
fname,
|
|
@@ -143,7 +149,7 @@ def classify_zip_and_analyze_color(zip_file):
|
|
| 143 |
", ".join([p[1] for p in preds]),
|
| 144 |
hex_color,
|
| 145 |
basic_color,
|
| 146 |
-
|
| 147 |
))
|
| 148 |
|
| 149 |
# Build dataframe
|
|
@@ -185,46 +191,29 @@ def classify_zip_and_analyze_color(zip_file):
|
|
| 185 |
plot2_img = Image.open(buf2)
|
| 186 |
|
| 187 |
# ---------------------------
|
| 188 |
-
# Extract age and weighted gender (
|
| 189 |
# ---------------------------
|
| 190 |
ages = []
|
| 191 |
gender_confidence = {"Man": 0, "Woman": 0}
|
| 192 |
|
| 193 |
-
for
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
gender_part = face_str.split(",")[1]
|
| 205 |
-
gender_dict_str = gender_part.replace("Gender:", "").strip()
|
| 206 |
-
try:
|
| 207 |
-
gender_dict = eval(gender_dict_str)
|
| 208 |
-
except:
|
| 209 |
-
continue
|
| 210 |
-
|
| 211 |
-
# Take the highest probability as gender
|
| 212 |
-
gender = max(gender_dict, key=gender_dict.get)
|
| 213 |
-
conf = float(gender_dict[gender]) / 100 # convert % to 0-1
|
| 214 |
-
|
| 215 |
-
# Weight capped at 0.9
|
| 216 |
-
weight = min(conf, 0.9)
|
| 217 |
-
if gender in gender_confidence:
|
| 218 |
-
gender_confidence[gender] += weight
|
| 219 |
-
else:
|
| 220 |
-
gender_confidence[gender] = weight
|
| 221 |
|
| 222 |
# ---------------------------
|
| 223 |
-
# Plot 3: Gender distribution (weighted ≤
|
| 224 |
# ---------------------------
|
| 225 |
fig3, ax3 = plt.subplots()
|
| 226 |
ax3.bar(gender_confidence.keys(), gender_confidence.values(), color=["lightblue", "pink"])
|
| 227 |
-
ax3.set_title("Gender Distribution (Weighted ≤
|
| 228 |
ax3.set_ylabel("Sum of Confidence")
|
| 229 |
buf3 = io.BytesIO()
|
| 230 |
plt.savefig(buf3, format="png")
|
|
|
|
| 121 |
# ---------------------------
|
| 122 |
# Face detection & characterization
|
| 123 |
# ---------------------------
|
| 124 |
+
faces_data = []
|
| 125 |
try:
|
| 126 |
img_cv2 = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
| 127 |
+
detected_faces = DeepFace.analyze(
|
| 128 |
+
img_cv2, actions=["age", "gender", "emotion"], enforce_detection=False
|
|
|
|
|
|
|
| 129 |
)
|
| 130 |
+
if isinstance(detected_faces, list):
|
| 131 |
+
for f in detected_faces:
|
| 132 |
+
faces_data.append({
|
| 133 |
+
"age": f["age"],
|
| 134 |
+
"gender": f["gender"], # dict of probabilities
|
| 135 |
+
"emotion": f["dominant_emotion"]
|
| 136 |
+
})
|
| 137 |
else:
|
| 138 |
+
faces_data.append({
|
| 139 |
+
"age": detected_faces["age"],
|
| 140 |
+
"gender": detected_faces["gender"], # dict
|
| 141 |
+
"emotion": detected_faces["dominant_emotion"]
|
| 142 |
+
})
|
| 143 |
except Exception:
|
| 144 |
+
faces_data = []
|
| 145 |
|
| 146 |
results.append((
|
| 147 |
fname,
|
|
|
|
| 149 |
", ".join([p[1] for p in preds]),
|
| 150 |
hex_color,
|
| 151 |
basic_color,
|
| 152 |
+
faces_data
|
| 153 |
))
|
| 154 |
|
| 155 |
# Build dataframe
|
|
|
|
| 191 |
plot2_img = Image.open(buf2)
|
| 192 |
|
| 193 |
# ---------------------------
|
| 194 |
+
# Extract age and weighted gender (confidence ≤ 0.9)
|
| 195 |
# ---------------------------
|
| 196 |
ages = []
|
| 197 |
gender_confidence = {"Man": 0, "Woman": 0}
|
| 198 |
|
| 199 |
+
for face_list in df["Face Info"]:
|
| 200 |
+
for face in face_list: # each face is a dict
|
| 201 |
+
ages.append(face["age"])
|
| 202 |
+
gender_dict = face["gender"] # dict of probabilities
|
| 203 |
+
gender = max(gender_dict, key=gender_dict.get)
|
| 204 |
+
conf = float(gender_dict[gender]) / 100 # convert % to 0-1
|
| 205 |
+
weight = min(conf, 0.9)
|
| 206 |
+
if gender in gender_confidence:
|
| 207 |
+
gender_confidence[gender] += weight
|
| 208 |
+
else:
|
| 209 |
+
gender_confidence[gender] = weight
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
# ---------------------------
|
| 212 |
+
# Plot 3: Gender distribution (weighted ≤ 0.9)
|
| 213 |
# ---------------------------
|
| 214 |
fig3, ax3 = plt.subplots()
|
| 215 |
ax3.bar(gender_confidence.keys(), gender_confidence.values(), color=["lightblue", "pink"])
|
| 216 |
+
ax3.set_title("Gender Distribution (Weighted ≤90% Confidence)")
|
| 217 |
ax3.set_ylabel("Sum of Confidence")
|
| 218 |
buf3 = io.BytesIO()
|
| 219 |
plt.savefig(buf3, format="png")
|