Nav27 commited on
Commit
621ca38
·
verified ·
1 Parent(s): 60e772b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -86
app.py CHANGED
@@ -1,86 +1,106 @@
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)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
9
+ def modify_degradations_py():
10
+ file_path = '/usr/local/lib/python3.10/site-packages/basicsr/data/degradations.py'
11
+ with open(file_path, 'r') as f:
12
+ lines = f.readlines()
13
+
14
+ # Find the line containing 'from torchvision.transforms.functional_tensor import rgb_to_grayscale'
15
+ for i, line in enumerate(lines):
16
+ if 'from torchvision.transforms.functional_tensor import rgb_to_grayscale' in line:
17
+ # Replace it with 'from torchvision.transforms.functional import rgb_to_grayscale'
18
+ lines[i] = 'from torchvision.transforms.functional import rgb_to_grayscale\n'
19
+ break
20
+
21
+ with open(file_path, 'w') as f:
22
+ f.writelines(lines)
23
+
24
+ # Call the function to modify the file
25
+ modify_degradations_py()
26
+
27
+
28
+ # Function to display images side by side
29
+ def display(img1, img2):
30
+ fig = plt.figure(figsize=(25, 10))
31
+ ax1 = fig.add_subplot(1, 2, 1)
32
+ plt.title('Input image', fontsize=16)
33
+ ax1.axis('off')
34
+ ax2 = fig.add_subplot(1, 2, 2)
35
+ plt.title('Image-Blitz output', fontsize=16)
36
+ ax2.axis('off')
37
+ ax1.imshow(img1)
38
+ ax2.imshow(img2)
39
+ st.pyplot(fig)
40
+
41
+ # Function to read an image
42
+ def imread(img_path):
43
+ img = cv2.imread(img_path)
44
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
45
+ return img
46
+
47
+ # Function to run shell commands
48
+ def run_shell_commands():
49
+ directories = [
50
+ "results/cropped_faces",
51
+ "results/restored_faces",
52
+ "results/restored_imgs",
53
+ "results/cmp"
54
+ ]
55
+
56
+ # Create directories using os.makedirs()
57
+ for directory in directories:
58
+ os.makedirs(directory, exist_ok=True)
59
+
60
+ # Run inference command
61
+ command = "python inference_gfpgan.py -i inputs/upload -o results -v 1.3 -s 2 --bg_upsampler realesrgan"
62
+ subprocess.run(command, shell=True, check=True)
63
+
64
+ # List the contents of the results/cmp directory
65
+ cmp_contents = subprocess.run("dir results\\cmp", shell=True, capture_output=True, text=True)
66
+ if cmp_contents.returncode != 0:
67
+ st.error(f"Error listing contents of results/cmp: {cmp_contents.stderr}")
68
+ return ""
69
+ return cmp_contents.stdout
70
+
71
+ # Start Streamlit app
72
+ st.title('Image Enhancer')
73
+ st.write('This is a simple web app to enhance the quality of images')
74
+
75
+ # Ask the user to input a file
76
+ uploaded_file = st.file_uploader("Choose an image...", type="jpg")
77
+
78
+ if uploaded_file is not None:
79
+ # Save the uploaded file to the inputs/upload directory
80
+ input_path = os.path.join('inputs', 'upload')
81
+ if not os.path.exists(input_path):
82
+ os.makedirs(input_path)
83
+
84
+ file_path = os.path.join(input_path, uploaded_file.name)
85
+ with open(file_path, 'wb') as f:
86
+ f.write(uploaded_file.getbuffer())
87
+
88
+ # Run shell commands
89
+ try:
90
+ output = run_shell_commands()
91
+ # Display the results
92
+ st.write('Contents of results/cmp:')
93
+ st.text(output)
94
+
95
+ # Display images
96
+ input_folder = 'results/cropped_faces'
97
+ result_folder = 'results/restored_faces'
98
+ input_list = sorted(glob.glob(os.path.join(input_folder, '*')))
99
+ output_list = sorted(glob.glob(os.path.join(result_folder, '*')))
100
+
101
+ for input_path, output_path in zip(input_list, output_list):
102
+ img_input = imread(input_path)
103
+ img_output = imread(output_path)
104
+ display(img_input, img_output)
105
+ except Exception as e:
106
+ st.error(f"An error occurred: {str(e)}")