ritz26 commited on
Commit
d721dd6
·
1 Parent(s): d08b3d5

update the backend to fully clear session and uploaded files on Change Person.

Browse files
Files changed (1) hide show
  1. app.py +21 -3
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from flask import Flask, render_template, request, send_from_directory, session
2
  from PIL import Image
3
  import os, torch, cv2, mediapipe as mp
4
  from transformers import SamModel, SamProcessor, logging as hf_logging
@@ -270,8 +270,26 @@ def change_person():
270
  """Clear cached person data to allow new person upload"""
271
  session.pop('person_coordinates', None)
272
  session.pop('person_image_path', None)
273
- print("[INFO] Cleared cached person data")
274
- return render_template('index.html')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
 
276
  @app.route('/cleanup', methods=['POST'])
277
  def cleanup():
 
1
+ from flask import Flask, render_template, request, send_from_directory, session, redirect, url_for
2
  from PIL import Image
3
  import os, torch, cv2, mediapipe as mp
4
  from transformers import SamModel, SamProcessor, logging as hf_logging
 
270
  """Clear cached person data to allow new person upload"""
271
  session.pop('person_coordinates', None)
272
  session.pop('person_image_path', None)
273
+
274
+ # Remove uploaded and output files to reset state
275
+ try:
276
+ person_disk_path = os.path.join(UPLOAD_FOLDER, 'person.jpg')
277
+ tshirt_disk_path = os.path.join(UPLOAD_FOLDER, 'tshirt.png')
278
+ if os.path.exists(person_disk_path):
279
+ os.remove(person_disk_path)
280
+ if os.path.exists(tshirt_disk_path):
281
+ os.remove(tshirt_disk_path)
282
+ if os.path.exists(OUTPUT_FOLDER):
283
+ for file in os.listdir(OUTPUT_FOLDER):
284
+ file_path = os.path.join(OUTPUT_FOLDER, file)
285
+ if os.path.isfile(file_path):
286
+ os.remove(file_path)
287
+ print("[INFO] Cleared cached person data and temp files")
288
+ except Exception as e:
289
+ print(f"[WARNING] Failed to clear files: {e}")
290
+
291
+ # Redirect to GET / so the app reloads fresh
292
+ return redirect(url_for('index'))
293
 
294
  @app.route('/cleanup', methods=['POST'])
295
  def cleanup():