clementBE commited on
Commit
86aceed
·
verified ·
1 Parent(s): 64c0fa8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -23
app.py CHANGED
@@ -73,7 +73,7 @@ def get_dominant_color(image,num_colors=5):
73
  # ---------------------------
74
  def classify_zip_and_analyze_color(zip_file):
75
  results = []
76
- images_dict = {}
77
  zip_name = os.path.splitext(os.path.basename(zip_file.name))[0]
78
  date_str = datetime.now().strftime("%Y%m%d")
79
 
@@ -86,7 +86,7 @@ def classify_zip_and_analyze_color(zip_file):
86
  img_path = os.path.join(tmpdir,fname)
87
  try:
88
  image = Image.open(img_path).convert("RGB")
89
- images_dict[fname] = image.copy()
90
  except:
91
  continue
92
 
@@ -128,7 +128,6 @@ def classify_zip_and_analyze_color(zip_file):
128
  # ---------------------------
129
  # Plots
130
  # ---------------------------
131
- # Basic color
132
  fig1, ax1 = plt.subplots()
133
  color_counts = df["Basic Color"].value_counts()
134
  ax1.bar(color_counts.index, color_counts.values, color="skyblue")
@@ -136,7 +135,6 @@ def classify_zip_and_analyze_color(zip_file):
136
  ax1.set_ylabel("Count")
137
  buf1 = io.BytesIO(); plt.savefig(buf1, format="png"); plt.close(fig1); buf1.seek(0); plot1_img = Image.open(buf1)
138
 
139
- # Top predictions
140
  fig2, ax2 = plt.subplots()
141
  preds_flat = []
142
  for p in df["Top 3 Predictions"]: preds_flat.extend(p.split(", "))
@@ -146,7 +144,6 @@ def classify_zip_and_analyze_color(zip_file):
146
  ax2.set_xlabel("Count")
147
  buf2 = io.BytesIO(); plt.savefig(buf2, format="png", bbox_inches="tight"); plt.close(fig2); buf2.seek(0); plot2_img = Image.open(buf2)
148
 
149
- # Gender and age
150
  ages_male, ages_female = [], []
151
  gender_confidence = {"Homme":0, "Femme":0}
152
  for face_list in df["Face Info"]:
@@ -174,16 +171,7 @@ def classify_zip_and_analyze_color(zip_file):
174
  ax4.set_xlabel("Age"); ax4.set_ylabel("Count"); ax4.legend()
175
  buf4 = io.BytesIO(); plt.savefig(buf4, format="png"); plt.close(fig4); buf4.seek(0); plot4_img = Image.open(buf4)
176
 
177
- return df, images_dict, out_xlsx, plot1_img, plot2_img, plot3_img, plot4_img
178
-
179
- # ---------------------------
180
- # Preview callback
181
- # ---------------------------
182
- def show_preview(selected_row, images_state):
183
- if images_state is None or selected_row is None:
184
- return None
185
- filename = selected_row[0] # first column is filename
186
- return images_state.get(filename, None)
187
 
188
  # ---------------------------
189
  # Gradio interface
@@ -192,10 +180,9 @@ with gr.Blocks() as demo:
192
  uploaded_zip = gr.File(label="Upload ZIP of images", file_types=[".zip"])
193
  analyze_btn = gr.Button("Run Analysis")
194
 
195
- output_df = gr.Dataframe(headers=["Filename","Top 3 Predictions","Confidence","Dominant Color","Basic Color","Face Info"], interactive=True)
196
- image_preview = gr.Image(label="Image Preview")
197
  download_file = gr.File(label="Download XLSX")
198
- images_state = gr.State()
199
 
200
  plot1 = gr.Image(label="Basic Color Frequency")
201
  plot2 = gr.Image(label="Top Prediction Distribution")
@@ -203,15 +190,13 @@ with gr.Blocks() as demo:
203
  plot4 = gr.Image(label="Age Distribution by Gender")
204
 
205
  def run_analysis(zip_file):
206
- df, images_dict, out_xlsx, p1, p2, p3, p4 = classify_zip_and_analyze_color(zip_file)
207
- return df, images_dict, out_xlsx, p1, p2, p3, p4
208
 
209
  analyze_btn.click(
210
  run_analysis,
211
  inputs=uploaded_zip,
212
- outputs=[output_df, images_state, download_file, plot1, plot2, plot3, plot4]
213
  )
214
 
215
- output_df.select(show_preview, inputs=[output_df, images_state], outputs=image_preview)
216
-
217
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
73
  # ---------------------------
74
  def classify_zip_and_analyze_color(zip_file):
75
  results = []
76
+ images_list = [] # list of (image, label) for gallery
77
  zip_name = os.path.splitext(os.path.basename(zip_file.name))[0]
78
  date_str = datetime.now().strftime("%Y%m%d")
79
 
 
86
  img_path = os.path.join(tmpdir,fname)
87
  try:
88
  image = Image.open(img_path).convert("RGB")
89
+ images_list.append((image.copy(), fname))
90
  except:
91
  continue
92
 
 
128
  # ---------------------------
129
  # Plots
130
  # ---------------------------
 
131
  fig1, ax1 = plt.subplots()
132
  color_counts = df["Basic Color"].value_counts()
133
  ax1.bar(color_counts.index, color_counts.values, color="skyblue")
 
135
  ax1.set_ylabel("Count")
136
  buf1 = io.BytesIO(); plt.savefig(buf1, format="png"); plt.close(fig1); buf1.seek(0); plot1_img = Image.open(buf1)
137
 
 
138
  fig2, ax2 = plt.subplots()
139
  preds_flat = []
140
  for p in df["Top 3 Predictions"]: preds_flat.extend(p.split(", "))
 
144
  ax2.set_xlabel("Count")
145
  buf2 = io.BytesIO(); plt.savefig(buf2, format="png", bbox_inches="tight"); plt.close(fig2); buf2.seek(0); plot2_img = Image.open(buf2)
146
 
 
147
  ages_male, ages_female = [], []
148
  gender_confidence = {"Homme":0, "Femme":0}
149
  for face_list in df["Face Info"]:
 
171
  ax4.set_xlabel("Age"); ax4.set_ylabel("Count"); ax4.legend()
172
  buf4 = io.BytesIO(); plt.savefig(buf4, format="png"); plt.close(fig4); buf4.seek(0); plot4_img = Image.open(buf4)
173
 
174
+ return df, images_list, out_xlsx, plot1_img, plot2_img, plot3_img, plot4_img
 
 
 
 
 
 
 
 
 
175
 
176
  # ---------------------------
177
  # Gradio interface
 
180
  uploaded_zip = gr.File(label="Upload ZIP of images", file_types=[".zip"])
181
  analyze_btn = gr.Button("Run Analysis")
182
 
183
+ output_df = gr.Dataframe(headers=["Filename","Top 3 Predictions","Confidence","Dominant Color","Basic Color","Face Info"])
184
+ image_gallery = gr.Gallery(label="Preview Images").style(grid=[4], height="auto")
185
  download_file = gr.File(label="Download XLSX")
 
186
 
187
  plot1 = gr.Image(label="Basic Color Frequency")
188
  plot2 = gr.Image(label="Top Prediction Distribution")
 
190
  plot4 = gr.Image(label="Age Distribution by Gender")
191
 
192
  def run_analysis(zip_file):
193
+ df, images_list, out_xlsx, p1, p2, p3, p4 = classify_zip_and_analyze_color(zip_file)
194
+ return df, images_list, out_xlsx, p1, p2, p3, p4
195
 
196
  analyze_btn.click(
197
  run_analysis,
198
  inputs=uploaded_zip,
199
+ outputs=[output_df, image_gallery, download_file, plot1, plot2, plot3, plot4]
200
  )
201
 
 
 
202
  demo.launch(server_name="0.0.0.0", server_port=7860)