Spaces:
Sleeping
Sleeping
aryrk
commited on
Commit
·
b79726b
1
Parent(s):
f6fd486
[feat] isolate uid by process
Browse files
app.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import shutil
|
| 3 |
import os
|
|
|
|
| 4 |
import subprocess
|
| 5 |
import uuid
|
| 6 |
from PIL import Image
|
| 7 |
-
|
| 8 |
-
from shutil import copyfile
|
| 9 |
|
|
|
|
| 10 |
RESULTS_DIR = "./results"
|
| 11 |
CHECKPOINTS_DIR = "./checkpoints/SingleImageReflectionRemoval"
|
| 12 |
SAMPLE_DIR = "./sample_images"
|
|
@@ -15,6 +14,9 @@ os.makedirs(RESULTS_DIR, exist_ok=True)
|
|
| 15 |
os.makedirs(CHECKPOINTS_DIR, exist_ok=True)
|
| 16 |
os.makedirs(SAMPLE_DIR, exist_ok=True)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
REPO_ID = "hasnafk/SingleImageReflectionRemoval"
|
| 19 |
MODEL_FILE = "310_net_G.pth"
|
| 20 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILE, cache_dir=CHECKPOINTS_DIR)
|
|
@@ -23,20 +25,33 @@ expected_model_path = os.path.join(CHECKPOINTS_DIR, MODEL_FILE)
|
|
| 23 |
if not os.path.exists(expected_model_path):
|
| 24 |
copyfile(model_path, expected_model_path)
|
| 25 |
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
os.makedirs(upload_dir, exist_ok=True)
|
| 32 |
|
| 33 |
if not input_image or not os.path.exists(input_image):
|
| 34 |
return "No image was provided or file was cleared. Please upload a valid image."
|
| 35 |
|
| 36 |
-
|
|
|
|
| 37 |
shutil.copy(input_image, file_path)
|
| 38 |
|
| 39 |
-
input_filename = os.path.splitext(
|
| 40 |
|
| 41 |
cmd = [
|
| 42 |
"python", "test.py",
|
|
@@ -51,39 +66,28 @@ def reflection_removal(input_image, preprocess_type="resize_and_crop", session_i
|
|
| 51 |
]
|
| 52 |
subprocess.run(cmd, check=True)
|
| 53 |
|
| 54 |
-
|
| 55 |
for root, _, files in os.walk(RESULTS_DIR):
|
| 56 |
for file in files:
|
| 57 |
if file.startswith(input_filename) and file.endswith("_fake.png"):
|
| 58 |
result_path = os.path.join(root, file)
|
| 59 |
output_image = Image.open(result_path)
|
| 60 |
-
result_files.append(result_path)
|
| 61 |
-
|
| 62 |
os.remove(result_path)
|
| 63 |
elif file.startswith(input_filename) and file.endswith("_real.png"):
|
| 64 |
real_path = os.path.join(root, file)
|
| 65 |
-
result_files.append(real_path)
|
| 66 |
-
|
| 67 |
os.remove(real_path)
|
| 68 |
|
| 69 |
clear_session_files(session_id)
|
| 70 |
|
| 71 |
-
if
|
| 72 |
return output_image
|
| 73 |
return "No results found."
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
print(f"Session {session_id} files cleared.")
|
| 81 |
-
|
| 82 |
-
def clear_action(session_id=None):
|
| 83 |
-
if session_id:
|
| 84 |
-
clear_session_files(session_id)
|
| 85 |
-
return "Upload cleared!"
|
| 86 |
-
return "No session found to clear."
|
| 87 |
|
| 88 |
sample_images = [
|
| 89 |
file for file in os.listdir(SAMPLE_DIR)
|
|
@@ -94,27 +98,20 @@ preprocess_options = [
|
|
| 94 |
"resize_and_crop", "crop", "scale_width", "scale_width_and_crop", "none"
|
| 95 |
]
|
| 96 |
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
]
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
title="Reflection Remover with Pix2Pix",
|
| 112 |
-
description="Upload images to remove reflections using a Pix2Pix model. You can also try the sample images below.",
|
| 113 |
-
allow_flagging="never",
|
| 114 |
-
live=False
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
os.makedirs("./sessions", exist_ok=True)
|
| 118 |
|
| 119 |
if __name__ == "__main__":
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import shutil
|
| 3 |
import subprocess
|
| 4 |
import uuid
|
| 5 |
from PIL import Image
|
| 6 |
+
import gradio as gr
|
|
|
|
| 7 |
|
| 8 |
+
UPLOAD_DIR = "./sessions"
|
| 9 |
RESULTS_DIR = "./results"
|
| 10 |
CHECKPOINTS_DIR = "./checkpoints/SingleImageReflectionRemoval"
|
| 11 |
SAMPLE_DIR = "./sample_images"
|
|
|
|
| 14 |
os.makedirs(CHECKPOINTS_DIR, exist_ok=True)
|
| 15 |
os.makedirs(SAMPLE_DIR, exist_ok=True)
|
| 16 |
|
| 17 |
+
from huggingface_hub import hf_hub_download
|
| 18 |
+
from shutil import copyfile
|
| 19 |
+
|
| 20 |
REPO_ID = "hasnafk/SingleImageReflectionRemoval"
|
| 21 |
MODEL_FILE = "310_net_G.pth"
|
| 22 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=MODEL_FILE, cache_dir=CHECKPOINTS_DIR)
|
|
|
|
| 25 |
if not os.path.exists(expected_model_path):
|
| 26 |
copyfile(model_path, expected_model_path)
|
| 27 |
|
| 28 |
+
def generate_session_id():
|
| 29 |
+
return str(uuid.uuid4())
|
| 30 |
+
|
| 31 |
+
def randomize_file_name(original_name):
|
| 32 |
+
extension = os.path.splitext(original_name)[1]
|
| 33 |
+
new_name = f"{uuid.uuid4().hex}{extension}"
|
| 34 |
+
return new_name
|
| 35 |
|
| 36 |
+
def clear_session_files(session_id):
|
| 37 |
+
session_dir = os.path.join(UPLOAD_DIR, session_id)
|
| 38 |
+
if os.path.exists(session_dir):
|
| 39 |
+
shutil.rmtree(session_dir)
|
| 40 |
+
|
| 41 |
+
def reflection_removal(input_image, preprocess_type="resize_and_crop"):
|
| 42 |
+
session_id = generate_session_id()
|
| 43 |
+
session_dir = os.path.join(UPLOAD_DIR, session_id)
|
| 44 |
+
upload_dir = os.path.join(session_dir, "uploads")
|
| 45 |
os.makedirs(upload_dir, exist_ok=True)
|
| 46 |
|
| 47 |
if not input_image or not os.path.exists(input_image):
|
| 48 |
return "No image was provided or file was cleared. Please upload a valid image."
|
| 49 |
|
| 50 |
+
randomized_name = randomize_file_name(os.path.basename(input_image))
|
| 51 |
+
file_path = os.path.join(upload_dir, randomized_name)
|
| 52 |
shutil.copy(input_image, file_path)
|
| 53 |
|
| 54 |
+
input_filename = os.path.splitext(randomized_name)[0]
|
| 55 |
|
| 56 |
cmd = [
|
| 57 |
"python", "test.py",
|
|
|
|
| 66 |
]
|
| 67 |
subprocess.run(cmd, check=True)
|
| 68 |
|
| 69 |
+
output_image = None
|
| 70 |
for root, _, files in os.walk(RESULTS_DIR):
|
| 71 |
for file in files:
|
| 72 |
if file.startswith(input_filename) and file.endswith("_fake.png"):
|
| 73 |
result_path = os.path.join(root, file)
|
| 74 |
output_image = Image.open(result_path)
|
|
|
|
|
|
|
| 75 |
os.remove(result_path)
|
| 76 |
elif file.startswith(input_filename) and file.endswith("_real.png"):
|
| 77 |
real_path = os.path.join(root, file)
|
|
|
|
|
|
|
| 78 |
os.remove(real_path)
|
| 79 |
|
| 80 |
clear_session_files(session_id)
|
| 81 |
|
| 82 |
+
if output_image:
|
| 83 |
return output_image
|
| 84 |
return "No results found."
|
| 85 |
|
| 86 |
+
def use_sample_image(sample_image_name):
|
| 87 |
+
sample_image_path = os.path.join(SAMPLE_DIR, sample_image_name)
|
| 88 |
+
if not os.path.exists(sample_image_path):
|
| 89 |
+
return "Sample image not found."
|
| 90 |
+
return sample_image_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
sample_images = [
|
| 93 |
file for file in os.listdir(SAMPLE_DIR)
|
|
|
|
| 98 |
"resize_and_crop", "crop", "scale_width", "scale_width_and_crop", "none"
|
| 99 |
]
|
| 100 |
|
| 101 |
+
iface = gr.Interface(
|
| 102 |
+
fn=reflection_removal,
|
| 103 |
+
inputs=[
|
| 104 |
+
gr.Image(type="filepath", label="Upload Image (JPG/PNG)"),
|
| 105 |
+
gr.Dropdown(choices=preprocess_options, label="Preprocessing Type", value="resize_and_crop")
|
| 106 |
+
],
|
| 107 |
+
outputs=gr.Image(label="Result after Reflection Removal"),
|
| 108 |
+
examples=[
|
| 109 |
+
[os.path.join(SAMPLE_DIR, img), "resize_and_crop"]
|
| 110 |
+
for img in sample_images
|
| 111 |
+
],
|
| 112 |
+
title="Reflection Remover with Pix2Pix",
|
| 113 |
+
description="Upload images to remove reflections using a Pix2Pix model. You can also try the sample images below."
|
| 114 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
if __name__ == "__main__":
|
| 117 |
+
iface.launch()
|