aryrk commited on
Commit
02a2aa5
·
1 Parent(s): af1a755

[optimization] clear uploads after process is completed

Browse files
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -23,10 +23,16 @@ model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILE, cache_dir=CHE
23
  expected_model_path = os.path.join(CHECKPOINTS_DIR, MODEL_FILE)
24
  if not os.path.exists(expected_model_path):
25
  copyfile(model_path, expected_model_path)
26
-
27
  def count_files(directory):
28
  return sum([len(files) for _, _, files in os.walk(directory)])
29
 
 
 
 
 
 
 
30
  def reflection_removal(input_image):
31
  if not input_image.lower().endswith((".jpg", ".jpeg", ".png")):
32
  return ["File is not supported (only .jpg, .jpeg, .png)."]
@@ -35,7 +41,6 @@ def reflection_removal(input_image):
35
  shutil.copy(input_image, file_path)
36
 
37
  input_filename = os.path.splitext(os.path.basename(file_path))[0]
38
- print(f"Processing {input_filename}...")
39
 
40
  cmd = [
41
  "python", "test.py",
@@ -49,19 +54,19 @@ def reflection_removal(input_image):
49
  ]
50
  subprocess.run(cmd, check=True)
51
 
52
- for root, _, files in os.walk(RESULTS_DIR):
53
- for file in files:
54
- print(os.path.join(root, file))
55
-
56
  for root, _, files in os.walk(RESULTS_DIR):
57
  for file in files:
58
  if file.startswith(input_filename) and file.endswith("_fake.png"):
59
  result_path = os.path.join(root, file)
60
- return [Image.open(result_path)]
 
 
 
 
61
 
 
62
  return ["No results found."]
63
 
64
-
65
  def use_sample_image(sample_image_name):
66
  sample_image_path = os.path.join(SAMPLE_DIR, sample_image_name)
67
  if not os.path.exists(sample_image_path):
@@ -75,9 +80,7 @@ sample_images = [
75
 
76
  iface = gr.Interface(
77
  fn=reflection_removal,
78
- inputs=[
79
- gr.Image(type="filepath", label="Upload Image (JPG/PNG)")
80
- ],
81
  outputs=gr.Gallery(label="Results after Reflection Removal"),
82
  examples=[
83
  os.path.join("sample_images", img) for img in os.listdir("sample_images") if img.endswith((".jpg", ".jpeg", ".png"))
 
23
  expected_model_path = os.path.join(CHECKPOINTS_DIR, MODEL_FILE)
24
  if not os.path.exists(expected_model_path):
25
  copyfile(model_path, expected_model_path)
26
+
27
  def count_files(directory):
28
  return sum([len(files) for _, _, files in os.walk(directory)])
29
 
30
+ def clear_uploaded_images():
31
+ for filename in os.listdir(UPLOAD_DIR):
32
+ file_path = os.path.join(UPLOAD_DIR, filename)
33
+ if os.path.isfile(file_path):
34
+ os.remove(file_path)
35
+
36
  def reflection_removal(input_image):
37
  if not input_image.lower().endswith((".jpg", ".jpeg", ".png")):
38
  return ["File is not supported (only .jpg, .jpeg, .png)."]
 
41
  shutil.copy(input_image, file_path)
42
 
43
  input_filename = os.path.splitext(os.path.basename(file_path))[0]
 
44
 
45
  cmd = [
46
  "python", "test.py",
 
54
  ]
55
  subprocess.run(cmd, check=True)
56
 
 
 
 
 
57
  for root, _, files in os.walk(RESULTS_DIR):
58
  for file in files:
59
  if file.startswith(input_filename) and file.endswith("_fake.png"):
60
  result_path = os.path.join(root, file)
61
+ output_image = Image.open(result_path)
62
+
63
+ clear_uploaded_images()
64
+
65
+ return [output_image]
66
 
67
+ clear_uploaded_images()
68
  return ["No results found."]
69
 
 
70
  def use_sample_image(sample_image_name):
71
  sample_image_path = os.path.join(SAMPLE_DIR, sample_image_name)
72
  if not os.path.exists(sample_image_path):
 
80
 
81
  iface = gr.Interface(
82
  fn=reflection_removal,
83
+ inputs=[gr.Image(type="filepath", label="Upload Image (JPG/PNG)")],
 
 
84
  outputs=gr.Gallery(label="Results after Reflection Removal"),
85
  examples=[
86
  os.path.join("sample_images", img) for img in os.listdir("sample_images") if img.endswith((".jpg", ".jpeg", ".png"))