Nav27 commited on
Commit
1b8c5e9
·
verified ·
1 Parent(s): 9376b1b

Upload slitapp.py

Browse files
Files changed (1) hide show
  1. slitapp.py +86 -0
slitapp.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ import subprocess
4
+ import cv2
5
+ import matplotlib.pyplot as plt
6
+ import glob
7
+
8
+ # Function to display images side by side
9
+ def display(img1, img2):
10
+ fig = plt.figure(figsize=(25, 10))
11
+ ax1 = fig.add_subplot(1, 2, 1)
12
+ plt.title('Input image', fontsize=16)
13
+ ax1.axis('off')
14
+ ax2 = fig.add_subplot(1, 2, 2)
15
+ plt.title('Image-Blitz output', fontsize=16)
16
+ ax2.axis('off')
17
+ ax1.imshow(img1)
18
+ ax2.imshow(img2)
19
+ st.pyplot(fig)
20
+
21
+ # Function to read an image
22
+ def imread(img_path):
23
+ img = cv2.imread(img_path)
24
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
25
+ return img
26
+
27
+ # Function to run shell commands
28
+ def run_shell_commands():
29
+ directories = [
30
+ "results/cropped_faces",
31
+ "results/restored_faces",
32
+ "results/restored_imgs",
33
+ "results/cmp"
34
+ ]
35
+
36
+ # Create directories using os.makedirs()
37
+ for directory in directories:
38
+ os.makedirs(directory, exist_ok=True)
39
+
40
+ # Run inference command
41
+ command = "python inference_gfpgan.py -i inputs/upload -o results -v 1.3 -s 2 --bg_upsampler realesrgan"
42
+ subprocess.run(command, shell=True, check=True)
43
+
44
+ # List the contents of the results/cmp directory
45
+ cmp_contents = subprocess.run("dir results\\cmp", shell=True, capture_output=True, text=True)
46
+ if cmp_contents.returncode != 0:
47
+ st.error(f"Error listing contents of results/cmp: {cmp_contents.stderr}")
48
+ return ""
49
+ return cmp_contents.stdout
50
+
51
+ # Start Streamlit app
52
+ st.title('Image Enhancer')
53
+ st.write('This is a simple web app to enhance the quality of images')
54
+
55
+ # Ask the user to input a file
56
+ uploaded_file = st.file_uploader("Choose an image...", type="jpg")
57
+
58
+ if uploaded_file is not None:
59
+ # Save the uploaded file to the inputs/upload directory
60
+ input_path = os.path.join('inputs', 'upload')
61
+ if not os.path.exists(input_path):
62
+ os.makedirs(input_path)
63
+
64
+ file_path = os.path.join(input_path, uploaded_file.name)
65
+ with open(file_path, 'wb') as f:
66
+ f.write(uploaded_file.getbuffer())
67
+
68
+ # Run shell commands
69
+ try:
70
+ output = run_shell_commands()
71
+ # Display the results
72
+ st.write('Contents of results/cmp:')
73
+ st.text(output)
74
+
75
+ # Display images
76
+ input_folder = 'results/cropped_faces'
77
+ result_folder = 'results/restored_faces'
78
+ input_list = sorted(glob.glob(os.path.join(input_folder, '*')))
79
+ output_list = sorted(glob.glob(os.path.join(result_folder, '*')))
80
+
81
+ for input_path, output_path in zip(input_list, output_list):
82
+ img_input = imread(input_path)
83
+ img_output = imread(output_path)
84
+ display(img_input, img_output)
85
+ except Exception as e:
86
+ st.error(f"An error occurred: {str(e)}")