clementBE commited on
Commit
480e561
·
verified ·
1 Parent(s): ef28aea

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -15
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,47 @@ 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 +259,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']}, Emotion: {f['dominant_emotion']}; "
135
+ else:
136
  face_info = f"Age: {faces['age']}, Gender: {faces['gender']}, 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 (handles gender dict)
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 parsing
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 ≤ 90%)
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 ≤ 90% Confidence)")
228
+ ax3.set_ylabel("Sum of Confidence")
 
229
  buf3 = io.BytesIO()
230
  plt.savefig(buf3, format="png")
231
  plt.close(fig3)
 
259
  gr.File(label="Download XLSX"),
260
  gr.Image(type="pil", label="Basic Color Frequency"),
261
  gr.Image(type="pil", label="Top Prediction Distribution"),
262
+ gr.Image(type="pil", label="Gender Distribution (Weighted ≤90%)"),
263
  gr.Image(type="pil", label="Age Distribution"),
264
  ],
265
  title="Image Classifier with Color & Face Analysis",