Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,112 +4,170 @@ import subprocess
|
|
| 4 |
import cv2
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
import glob
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Function to display images side by side
|
| 11 |
def display(img1, img2):
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Function to read an image
|
| 25 |
def imread(img_path):
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
return None
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Function to run shell commands
|
| 34 |
def run_shell_commands():
|
| 35 |
-
st.info("Creating directories...")
|
| 36 |
-
directories = [
|
| 37 |
-
"results/cropped_faces",
|
| 38 |
-
"results/restored_faces",
|
| 39 |
-
"results/restored_imgs",
|
| 40 |
-
"results/cmp"
|
| 41 |
-
]
|
| 42 |
-
|
| 43 |
-
for directory in directories:
|
| 44 |
-
os.makedirs(directory, exist_ok=True)
|
| 45 |
-
|
| 46 |
-
st.info("Running image enhancement...")
|
| 47 |
-
command = "python inference_gfpgan.py -i inputs/upload -o results -v 1.3 -s 2 --bg_upsampler realesrgan"
|
| 48 |
try:
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
if process.returncode != 0:
|
| 51 |
-
st.error(f"
|
| 52 |
return False
|
| 53 |
return True
|
|
|
|
|
|
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
-
st.error(f"
|
| 56 |
return False
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
st.
|
| 62 |
-
|
| 63 |
-
# File uploader with progress bar
|
| 64 |
-
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
|
| 89 |
-
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
else:
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
display(img_input, img_output)
|
| 102 |
|
|
|
|
| 103 |
# Cleanup
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
except Exception as e:
|
| 111 |
-
st.error(f"
|
| 112 |
-
|
| 113 |
-
finally:
|
| 114 |
-
# Clear status
|
| 115 |
-
status.empty()
|
|
|
|
| 4 |
import cv2
|
| 5 |
import matplotlib.pyplot as plt
|
| 6 |
import glob
|
| 7 |
+
import psutil
|
| 8 |
+
import time
|
| 9 |
|
| 10 |
+
# Page configuration
|
| 11 |
+
st.set_page_config(
|
| 12 |
+
page_title="Image Enhancer",
|
| 13 |
+
page_icon="🖼️",
|
| 14 |
+
layout="wide",
|
| 15 |
+
initial_sidebar_state="expanded"
|
| 16 |
+
)
|
| 17 |
|
| 18 |
# Function to display images side by side
|
| 19 |
def display(img1, img2):
|
| 20 |
+
try:
|
| 21 |
+
fig = plt.figure(figsize=(25, 10))
|
| 22 |
+
ax1 = fig.add_subplot(1, 2, 1)
|
| 23 |
+
plt.title('Input image', fontsize=16)
|
| 24 |
+
ax1.axis('off')
|
| 25 |
+
ax2 = fig.add_subplot(1, 2, 2)
|
| 26 |
+
plt.title('Enhanced output', fontsize=16)
|
| 27 |
+
ax2.axis('off')
|
| 28 |
+
ax1.imshow(img1)
|
| 29 |
+
ax2.imshow(img2)
|
| 30 |
+
st.pyplot(fig, use_container_width=True)
|
| 31 |
+
plt.close(fig)
|
| 32 |
+
except Exception as e:
|
| 33 |
+
st.error(f"Error displaying images: {str(e)}")
|
| 34 |
|
| 35 |
# Function to read an image
|
| 36 |
def imread(img_path):
|
| 37 |
+
try:
|
| 38 |
+
if not os.path.exists(img_path):
|
| 39 |
+
st.error(f"Image not found: {img_path}")
|
| 40 |
+
return None
|
| 41 |
+
img = cv2.imread(img_path)
|
| 42 |
+
if img is None:
|
| 43 |
+
st.error(f"Failed to load image: {img_path}")
|
| 44 |
+
return None
|
| 45 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 46 |
+
return img
|
| 47 |
+
except Exception as e:
|
| 48 |
+
st.error(f"Error reading image: {str(e)}")
|
| 49 |
return None
|
| 50 |
+
|
| 51 |
+
# Function to clean up directories
|
| 52 |
+
def cleanup_directories():
|
| 53 |
+
directories = ['inputs/upload', 'results']
|
| 54 |
+
for directory in directories:
|
| 55 |
+
if os.path.exists(directory):
|
| 56 |
+
try:
|
| 57 |
+
for file in glob.glob(os.path.join(directory, '**/*'), recursive=True):
|
| 58 |
+
if os.path.isfile(file):
|
| 59 |
+
os.remove(file)
|
| 60 |
+
except Exception as e:
|
| 61 |
+
st.sidebar.warning(f"Cleanup warning: {str(e)}")
|
| 62 |
|
| 63 |
# Function to run shell commands
|
| 64 |
def run_shell_commands():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
try:
|
| 66 |
+
directories = [
|
| 67 |
+
"results/cropped_faces",
|
| 68 |
+
"results/restored_faces",
|
| 69 |
+
"results/restored_imgs",
|
| 70 |
+
"results/cmp"
|
| 71 |
+
]
|
| 72 |
+
|
| 73 |
+
for directory in directories:
|
| 74 |
+
os.makedirs(directory, exist_ok=True)
|
| 75 |
+
|
| 76 |
+
command = "python inference_gfpgan.py -i inputs/upload -o results -v 1.3 -s 2 --bg_upsampler realesrgan"
|
| 77 |
+
process = subprocess.run(command, shell=True, capture_output=True, text=True, timeout=300)
|
| 78 |
+
|
| 79 |
if process.returncode != 0:
|
| 80 |
+
st.error(f"Enhancement failed: {process.stderr}")
|
| 81 |
return False
|
| 82 |
return True
|
| 83 |
+
except subprocess.TimeoutExpired:
|
| 84 |
+
st.error("Process timed out after 5 minutes")
|
| 85 |
+
return False
|
| 86 |
except Exception as e:
|
| 87 |
+
st.error(f"Process error: {str(e)}")
|
| 88 |
return False
|
| 89 |
|
| 90 |
+
# Memory monitoring
|
| 91 |
+
def check_memory():
|
| 92 |
+
memory = psutil.Process().memory_info().rss / 1024 / 1024
|
| 93 |
+
st.sidebar.text(f"Memory usage: {memory:.2f} MB")
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
# Main app
|
| 96 |
+
def main():
|
| 97 |
+
st.title('Image Enhancer')
|
| 98 |
+
st.write('Upload an image to enhance its quality')
|
| 99 |
+
st.write('Please wait 30-40 seconds after uploading 🙂')
|
| 100 |
+
|
| 101 |
+
# Sidebar information
|
| 102 |
+
st.sidebar.title("App Info")
|
| 103 |
+
st.sidebar.write("This app enhances image quality using AI")
|
| 104 |
+
check_memory()
|
| 105 |
+
|
| 106 |
+
# Clean up before starting
|
| 107 |
+
cleanup_directories()
|
| 108 |
+
|
| 109 |
+
# File uploader with progress bar
|
| 110 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 111 |
+
|
| 112 |
+
if uploaded_file is not None:
|
| 113 |
+
try:
|
| 114 |
+
# Show processing status
|
| 115 |
+
status = st.empty()
|
| 116 |
+
progress_bar = st.progress(0)
|
| 117 |
+
status.info("Starting process...")
|
| 118 |
+
|
| 119 |
+
# Create input directory
|
| 120 |
+
input_path = os.path.join('inputs', 'upload')
|
| 121 |
+
os.makedirs(input_path, exist_ok=True)
|
| 122 |
|
| 123 |
+
# Save uploaded file
|
| 124 |
+
file_path = os.path.join(input_path, uploaded_file.name)
|
| 125 |
+
with open(file_path, 'wb') as f:
|
| 126 |
+
f.write(uploaded_file.getbuffer())
|
| 127 |
|
| 128 |
+
progress_bar.progress(25)
|
| 129 |
+
status.info("File uploaded successfully. Processing image...")
|
| 130 |
|
| 131 |
+
# Run enhancement
|
| 132 |
+
if run_shell_commands():
|
| 133 |
+
progress_bar.progress(75)
|
| 134 |
+
status.success("Processing complete!")
|
| 135 |
+
|
| 136 |
+
# Display results
|
| 137 |
+
input_folder = 'results/cropped_faces'
|
| 138 |
+
result_folder = 'results/restored_faces'
|
| 139 |
+
|
| 140 |
+
input_list = sorted(glob.glob(os.path.join(input_folder, '*')))
|
| 141 |
+
output_list = sorted(glob.glob(os.path.join(result_folder, '*')))
|
| 142 |
+
|
| 143 |
+
if not input_list or not output_list:
|
| 144 |
+
st.warning("No faces detected in the image.")
|
| 145 |
+
else:
|
| 146 |
+
for input_path, output_path in zip(input_list, output_list):
|
| 147 |
+
img_input = imread(input_path)
|
| 148 |
+
img_output = imread(output_path)
|
| 149 |
+
if img_input is not None and img_output is not None:
|
| 150 |
+
display(img_input, img_output)
|
| 151 |
+
|
| 152 |
+
progress_bar.progress(100)
|
| 153 |
+
|
| 154 |
else:
|
| 155 |
+
status.error("Failed to process image.")
|
| 156 |
+
|
| 157 |
+
except Exception as e:
|
| 158 |
+
st.error(f"Error: {str(e)}")
|
|
|
|
| 159 |
|
| 160 |
+
finally:
|
| 161 |
# Cleanup
|
| 162 |
+
cleanup_directories()
|
| 163 |
+
# Clear status and progress
|
| 164 |
+
time.sleep(2)
|
| 165 |
+
status.empty()
|
| 166 |
+
progress_bar.empty()
|
| 167 |
+
check_memory()
|
| 168 |
+
|
| 169 |
+
if __name__ == "__main__":
|
| 170 |
+
try:
|
| 171 |
+
main()
|
| 172 |
except Exception as e:
|
| 173 |
+
st.error(f"Application error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|