Spaces:
Sleeping
Sleeping
aryrk
commited on
Commit
·
2d422fa
1
Parent(s):
3d681f3
[feat] optimize file and make output image instead of galery
Browse files
app.py
CHANGED
|
@@ -33,16 +33,21 @@ def clear_uploaded_images():
|
|
| 33 |
if os.path.isfile(file_path):
|
| 34 |
os.remove(file_path)
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
def reflection_removal(input_image, preprocess_type="resize_and_crop"):
|
| 37 |
if not input_image.lower().endswith((".jpg", ".jpeg", ".png")):
|
| 38 |
-
return
|
| 39 |
|
| 40 |
file_path = os.path.join(UPLOAD_DIR, os.path.basename(input_image))
|
| 41 |
shutil.copy(input_image, file_path)
|
| 42 |
|
| 43 |
input_filename = os.path.splitext(os.path.basename(file_path))[0]
|
| 44 |
|
| 45 |
-
|
| 46 |
cmd = [
|
| 47 |
"python", "test.py",
|
| 48 |
"--dataroot", UPLOAD_DIR,
|
|
@@ -63,11 +68,13 @@ def reflection_removal(input_image, preprocess_type="resize_and_crop"):
|
|
| 63 |
output_image = Image.open(result_path)
|
| 64 |
|
| 65 |
clear_uploaded_images()
|
|
|
|
| 66 |
|
| 67 |
-
return
|
| 68 |
|
| 69 |
clear_uploaded_images()
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
def use_sample_image(sample_image_name):
|
| 73 |
sample_image_path = os.path.join(SAMPLE_DIR, sample_image_name)
|
|
@@ -90,7 +97,7 @@ iface = gr.Interface(
|
|
| 90 |
gr.Image(type="filepath", label="Upload Image (JPG/PNG)"),
|
| 91 |
gr.Dropdown(choices=preprocess_options, label="Preprocessing Type", value="resize_and_crop")
|
| 92 |
],
|
| 93 |
-
outputs=gr.
|
| 94 |
examples=[
|
| 95 |
[os.path.join("sample_images", img), "resize_and_crop"]
|
| 96 |
for img in os.listdir("sample_images") if img.endswith((".jpg", ".jpeg", ".png"))
|
|
@@ -99,6 +106,5 @@ iface = gr.Interface(
|
|
| 99 |
description="Upload images to remove reflections using a Pix2Pix model. You can also try the sample images below."
|
| 100 |
)
|
| 101 |
|
| 102 |
-
|
| 103 |
if __name__ == "__main__":
|
| 104 |
iface.launch()
|
|
|
|
| 33 |
if os.path.isfile(file_path):
|
| 34 |
os.remove(file_path)
|
| 35 |
|
| 36 |
+
def clear_results():
|
| 37 |
+
for root, _, files in os.walk(RESULTS_DIR):
|
| 38 |
+
for file in files:
|
| 39 |
+
file_path = os.path.join(root, file)
|
| 40 |
+
os.remove(file_path)
|
| 41 |
+
|
| 42 |
def reflection_removal(input_image, preprocess_type="resize_and_crop"):
|
| 43 |
if not input_image.lower().endswith((".jpg", ".jpeg", ".png")):
|
| 44 |
+
return "File is not supported (only .jpg, .jpeg, .png)."
|
| 45 |
|
| 46 |
file_path = os.path.join(UPLOAD_DIR, os.path.basename(input_image))
|
| 47 |
shutil.copy(input_image, file_path)
|
| 48 |
|
| 49 |
input_filename = os.path.splitext(os.path.basename(file_path))[0]
|
| 50 |
|
|
|
|
| 51 |
cmd = [
|
| 52 |
"python", "test.py",
|
| 53 |
"--dataroot", UPLOAD_DIR,
|
|
|
|
| 68 |
output_image = Image.open(result_path)
|
| 69 |
|
| 70 |
clear_uploaded_images()
|
| 71 |
+
clear_results()
|
| 72 |
|
| 73 |
+
return output_image
|
| 74 |
|
| 75 |
clear_uploaded_images()
|
| 76 |
+
clear_results()
|
| 77 |
+
return "No results found."
|
| 78 |
|
| 79 |
def use_sample_image(sample_image_name):
|
| 80 |
sample_image_path = os.path.join(SAMPLE_DIR, sample_image_name)
|
|
|
|
| 97 |
gr.Image(type="filepath", label="Upload Image (JPG/PNG)"),
|
| 98 |
gr.Dropdown(choices=preprocess_options, label="Preprocessing Type", value="resize_and_crop")
|
| 99 |
],
|
| 100 |
+
outputs=gr.Image(label="Result after Reflection Removal"),
|
| 101 |
examples=[
|
| 102 |
[os.path.join("sample_images", img), "resize_and_crop"]
|
| 103 |
for img in os.listdir("sample_images") if img.endswith((".jpg", ".jpeg", ".png"))
|
|
|
|
| 106 |
description="Upload images to remove reflections using a Pix2Pix model. You can also try the sample images below."
|
| 107 |
)
|
| 108 |
|
|
|
|
| 109 |
if __name__ == "__main__":
|
| 110 |
iface.launch()
|