Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -127,9 +127,9 @@ def classify_zip_and_analyze_color(zip_file):
|
|
| 127 |
faces = DeepFace.analyze(img_cv2, actions=["age", "gender", "emotion"], enforce_detection=False)
|
| 128 |
if isinstance(faces, list): # multiple faces
|
| 129 |
for f in faces:
|
| 130 |
-
face_info += f"Age: {f['age']}, Gender: {f['gender']},
|
| 131 |
else: # single face
|
| 132 |
-
face_info = f"Age: {faces['age']}, Gender: {faces['gender']},
|
| 133 |
except Exception as e:
|
| 134 |
face_info = "No face detected"
|
| 135 |
|
|
@@ -181,45 +181,32 @@ def classify_zip_and_analyze_color(zip_file):
|
|
| 181 |
plot2_img = Image.open(buf2)
|
| 182 |
|
| 183 |
# ---------------------------
|
| 184 |
-
# Extract age and gender
|
| 185 |
# ---------------------------
|
| 186 |
ages = []
|
| 187 |
-
|
| 188 |
|
| 189 |
for info in df["Face Info"]:
|
| 190 |
if info != "No face detected":
|
| 191 |
for face_str in info.split(";"):
|
| 192 |
face_str = face_str.strip()
|
| 193 |
if face_str:
|
| 194 |
-
# Age
|
| 195 |
age_part = face_str.split(",")[0]
|
| 196 |
age = int(age_part.replace("Age:", "").strip())
|
| 197 |
ages.append(age)
|
| 198 |
|
| 199 |
-
# Gender and confidence
|
| 200 |
gender_part = face_str.split(",")[1]
|
| 201 |
gender = gender_part.replace("Gender:", "").strip()
|
| 202 |
-
|
| 203 |
-
# Extract confidence
|
| 204 |
-
conf = 1.0
|
| 205 |
-
for part in face_str.split(","):
|
| 206 |
-
if "Gender Confidence:" in part:
|
| 207 |
-
conf = float(part.split("Gender Confidence:")[1].strip()) / 100 # convert % to 0-1
|
| 208 |
-
|
| 209 |
-
# Only include if confidence ≤ 0.8
|
| 210 |
-
if conf <= 0.8:
|
| 211 |
-
if gender in gender_confidence:
|
| 212 |
-
gender_confidence[gender] += conf
|
| 213 |
-
else:
|
| 214 |
-
gender_confidence[gender] = conf
|
| 215 |
|
| 216 |
# ---------------------------
|
| 217 |
-
# Plot 3: Gender distribution
|
| 218 |
# ---------------------------
|
| 219 |
fig3, ax3 = plt.subplots()
|
| 220 |
-
|
| 221 |
-
ax3.
|
| 222 |
-
ax3.
|
|
|
|
| 223 |
buf3 = io.BytesIO()
|
| 224 |
plt.savefig(buf3, format="png")
|
| 225 |
plt.close(fig3)
|
|
@@ -253,7 +240,7 @@ demo = gr.Interface(
|
|
| 253 |
gr.File(label="Download XLSX"),
|
| 254 |
gr.Image(type="pil", label="Basic Color Frequency"),
|
| 255 |
gr.Image(type="pil", label="Top Prediction Distribution"),
|
| 256 |
-
gr.Image(type="pil", label="Gender Distribution
|
| 257 |
gr.Image(type="pil", label="Age Distribution"),
|
| 258 |
],
|
| 259 |
title="Image Classifier with Color & Face Analysis",
|
|
@@ -262,4 +249,3 @@ demo = gr.Interface(
|
|
| 262 |
|
| 263 |
if __name__ == "__main__":
|
| 264 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
| 265 |
-
|
|
|
|
| 127 |
faces = DeepFace.analyze(img_cv2, actions=["age", "gender", "emotion"], enforce_detection=False)
|
| 128 |
if isinstance(faces, list): # multiple faces
|
| 129 |
for f in faces:
|
| 130 |
+
face_info += f"Age: {f['age']}, Gender: {f['gender']}, Emotion: {f['dominant_emotion']}; "
|
| 131 |
else: # single face
|
| 132 |
+
face_info = f"Age: {faces['age']}, Gender: {faces['gender']}, Emotion: {faces['dominant_emotion']}"
|
| 133 |
except Exception as e:
|
| 134 |
face_info = "No face detected"
|
| 135 |
|
|
|
|
| 181 |
plot2_img = Image.open(buf2)
|
| 182 |
|
| 183 |
# ---------------------------
|
| 184 |
+
# Extract age and gender
|
| 185 |
# ---------------------------
|
| 186 |
ages = []
|
| 187 |
+
genders = []
|
| 188 |
|
| 189 |
for info in df["Face Info"]:
|
| 190 |
if info != "No face detected":
|
| 191 |
for face_str in info.split(";"):
|
| 192 |
face_str = face_str.strip()
|
| 193 |
if face_str:
|
|
|
|
| 194 |
age_part = face_str.split(",")[0]
|
| 195 |
age = int(age_part.replace("Age:", "").strip())
|
| 196 |
ages.append(age)
|
| 197 |
|
|
|
|
| 198 |
gender_part = face_str.split(",")[1]
|
| 199 |
gender = gender_part.replace("Gender:", "").strip()
|
| 200 |
+
genders.append(gender)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
# ---------------------------
|
| 203 |
+
# Plot 3: Gender distribution
|
| 204 |
# ---------------------------
|
| 205 |
fig3, ax3 = plt.subplots()
|
| 206 |
+
gender_counts = pd.Series(genders).value_counts()
|
| 207 |
+
ax3.bar(gender_counts.index, gender_counts.values, color=["lightblue", "pink"])
|
| 208 |
+
ax3.set_title("Gender Distribution")
|
| 209 |
+
ax3.set_ylabel("Count")
|
| 210 |
buf3 = io.BytesIO()
|
| 211 |
plt.savefig(buf3, format="png")
|
| 212 |
plt.close(fig3)
|
|
|
|
| 240 |
gr.File(label="Download XLSX"),
|
| 241 |
gr.Image(type="pil", label="Basic Color Frequency"),
|
| 242 |
gr.Image(type="pil", label="Top Prediction Distribution"),
|
| 243 |
+
gr.Image(type="pil", label="Gender Distribution"),
|
| 244 |
gr.Image(type="pil", label="Age Distribution"),
|
| 245 |
],
|
| 246 |
title="Image Classifier with Color & Face Analysis",
|
|
|
|
| 249 |
|
| 250 |
if __name__ == "__main__":
|
| 251 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|