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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -18
app.py CHANGED
@@ -124,17 +124,13 @@ 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(
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,7 +181,7 @@ def classify_zip_and_analyze_color(zip_file):
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}
@@ -199,30 +195,30 @@ def classify_zip_and_analyze_color(zip_file):
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 (weighted90%)
222
  # ---------------------------
223
  fig3, ax3 = plt.subplots()
224
  ax3.bar(gender_confidence.keys(), gender_confidence.values(), color=["lightblue", "pink"])
225
- ax3.set_title("Gender Distribution (Weighted90% Confidence)")
226
  ax3.set_ylabel("Sum of Confidence")
227
  buf3 = io.BytesIO()
228
  plt.savefig(buf3, format="png")
@@ -257,7 +253,7 @@ demo = gr.Interface(
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",
@@ -266,3 +262,4 @@ demo = gr.Interface(
266
 
267
  if __name__ == "__main__":
268
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
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']}, Gender Confidence: {f['gender_confidence']*100:.2f}, Emotion: {f['dominant_emotion']}; "
131
+ else: # single face
132
  face_info = f"Age: {faces['age']}, Gender: {faces['gender']}, Gender Confidence: {faces['gender_confidence']*100:.2f}, Emotion: {faces['dominant_emotion']}"
133
+ except Exception as e:
134
  face_info = "No face detected"
135
 
136
  results.append((
 
181
  plot2_img = Image.open(buf2)
182
 
183
  # ---------------------------
184
+ # Extract age and gender (confidence ≤ 80%)
185
  # ---------------------------
186
  ages = []
187
  gender_confidence = {"Man": 0, "Woman": 0}
 
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 (confidence80%)
218
  # ---------------------------
219
  fig3, ax3 = plt.subplots()
220
  ax3.bar(gender_confidence.keys(), gender_confidence.values(), color=["lightblue", "pink"])
221
+ ax3.set_title("Gender Distribution (Confidence80%)")
222
  ax3.set_ylabel("Sum of Confidence")
223
  buf3 = io.BytesIO()
224
  plt.savefig(buf3, format="png")
 
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 (≤80% Confidence)"),
257
  gr.Image(type="pil", label="Age Distribution"),
258
  ],
259
  title="Image Classifier with Color & Face Analysis",
 
262
 
263
  if __name__ == "__main__":
264
  demo.launch(server_name="0.0.0.0", server_port=7860)
265
+