clementBE commited on
Commit
75e369e
·
verified ·
1 Parent(s): c03d70e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -16
app.py CHANGED
@@ -124,13 +124,17 @@ def classify_zip_and_analyze_color(zip_file):
124
  face_info = ""
125
  try:
126
  img_cv2 = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
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
 
136
  results.append((
@@ -181,32 +185,45 @@ 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
- 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,7 +257,7 @@ demo = gr.Interface(
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",
 
124
  face_info = ""
125
  try:
126
  img_cv2 = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
127
+ faces = DeepFace.analyze(
128
+ img_cv2,
129
+ actions=["age", "gender", "emotion"],
130
+ enforce_detection=False
131
+ )
132
+ if isinstance(faces, list):
133
  for f in faces:
134
+ face_info += f"Age: {f['age']}, Gender: {f['gender']}, Gender Confidence: {f['gender_confidence']*100:.2f}, Emotion: {f['dominant_emotion']}; "
135
+ else:
136
+ face_info = f"Age: {faces['age']}, Gender: {faces['gender']}, Gender Confidence: {faces['gender_confidence']*100:.2f}, Emotion: {faces['dominant_emotion']}"
137
+ except Exception:
138
  face_info = "No face detected"
139
 
140
  results.append((
 
185
  plot2_img = Image.open(buf2)
186
 
187
  # ---------------------------
188
+ # Extract age and weighted gender (confidence ≤ 90%)
189
  # ---------------------------
190
  ages = []
191
+ gender_confidence = {"Man": 0, "Woman": 0}
192
 
193
  for info in df["Face Info"]:
194
  if info != "No face detected":
195
  for face_str in info.split(";"):
196
  face_str = face_str.strip()
197
  if face_str:
198
+ # Age
199
  age_part = face_str.split(",")[0]
200
  age = int(age_part.replace("Age:", "").strip())
201
  ages.append(age)
202
+
203
+ # Gender
204
  gender_part = face_str.split(",")[1]
205
  gender = gender_part.replace("Gender:", "").strip()
206
+
207
+ # Extract confidence
208
+ conf = 1.0
209
+ for part in face_str.split(","):
210
+ if "Gender Confidence:" in part:
211
+ conf = float(part.split("Gender Confidence:")[1].strip()) / 100
212
+
213
+ # Only include if confidence ≤ 0.9
214
+ if conf <= 0.9:
215
+ if gender in gender_confidence:
216
+ gender_confidence[gender] += conf
217
+ else:
218
+ gender_confidence[gender] = conf
219
 
220
  # ---------------------------
221
+ # Plot 3: Gender distribution (weighted ≤ 90%)
222
  # ---------------------------
223
  fig3, ax3 = plt.subplots()
224
+ ax3.bar(gender_confidence.keys(), gender_confidence.values(), color=["lightblue", "pink"])
225
+ ax3.set_title("Gender Distribution (Weighted ≤ 90% Confidence)")
226
+ ax3.set_ylabel("Sum of Confidence")
 
227
  buf3 = io.BytesIO()
228
  plt.savefig(buf3, format="png")
229
  plt.close(fig3)
 
257
  gr.File(label="Download XLSX"),
258
  gr.Image(type="pil", label="Basic Color Frequency"),
259
  gr.Image(type="pil", label="Top Prediction Distribution"),
260
+ gr.Image(type="pil", label="Gender Distribution (Weighted ≤90%)"),
261
  gr.Image(type="pil", label="Age Distribution"),
262
  ],
263
  title="Image Classifier with Color & Face Analysis",