Upload 2 files
Browse files- app.py +157 -0
- requirements.txt +5 -0
app.py
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Application to demo inpainting, Median and Bilateral Blur using streamlit.
|
2 |
+
|
3 |
+
Run using: streamlit run 10_04_image_restoration_app.py
|
4 |
+
"""
|
5 |
+
|
6 |
+
import streamlit as st
|
7 |
+
import pathlib
|
8 |
+
from streamlit_drawable_canvas import st_canvas
|
9 |
+
import cv2
|
10 |
+
import numpy as np
|
11 |
+
import io
|
12 |
+
import base64
|
13 |
+
from PIL import Image
|
14 |
+
|
15 |
+
|
16 |
+
# # Function to create a download link for output image
|
17 |
+
# def get_image_download_link(img, filename, text):
|
18 |
+
# """Generates a link to download a particular image file."""
|
19 |
+
# buffered = io.BytesIO()
|
20 |
+
# img.save(buffered, format='JPEG')
|
21 |
+
# img_str = base64.b64encode(buffered.getvalue()).decode()
|
22 |
+
# href = f'<a href="data:file/txt;base64,{img_str}" download="{filename}">{text}</a>'
|
23 |
+
# return href
|
24 |
+
|
25 |
+
|
26 |
+
# Set title.
|
27 |
+
st.sidebar.title('Image Restoration App with OpenCV')
|
28 |
+
|
29 |
+
# Description
|
30 |
+
st.sidebar.text('Upload an image and apply various restoration techniques.')
|
31 |
+
|
32 |
+
|
33 |
+
# Specify canvas parameters in application
|
34 |
+
uploaded_file = st.sidebar.file_uploader("Upload Image to restore:", type=["png", "jpg"])
|
35 |
+
image = None
|
36 |
+
res = None
|
37 |
+
|
38 |
+
if uploaded_file is not None:
|
39 |
+
|
40 |
+
# Convert the file to an opencv image.
|
41 |
+
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
42 |
+
image = cv2.imdecode(file_bytes, 1)
|
43 |
+
# Display the uploaded image
|
44 |
+
st.subheader("Original Image")
|
45 |
+
st.image(image[:, :, ::-1])
|
46 |
+
# Display a selection box for choosing the filter to apply.
|
47 |
+
option = st.sidebar.selectbox('Median or Bilateral Blur or Inpaint?', ('None', 'Median Blur', 'Bilateral Blur', 'Image Inpaint'))
|
48 |
+
|
49 |
+
if option == 'Median Blur':
|
50 |
+
ksize = st.sidebar.slider("ksize: ", 3, 15, 5, 2)
|
51 |
+
image = cv2.medianBlur(image, ksize)
|
52 |
+
res=image[:,:,::-1]
|
53 |
+
st.subheader("Median Blurred Image")
|
54 |
+
st.image(res)
|
55 |
+
# Display download button
|
56 |
+
result = Image.fromarray(res)
|
57 |
+
buffered = io.BytesIO()
|
58 |
+
result.save(buffered, format="PNG")
|
59 |
+
img_bytes = buffered.getvalue()
|
60 |
+
st.download_button(label='Download Output', data=img_bytes, file_name='median_blur_output.png', mime='image/png')
|
61 |
+
|
62 |
+
elif option == 'Bilateral Blur':
|
63 |
+
dia = st.sidebar.slider("diameter: ", 1, 50, 20)
|
64 |
+
sigmaColor = st.sidebar.slider("sigmaColor: ", 0, 250, 200, 10)
|
65 |
+
sigmaSpace = st.sidebar.slider("sigmaSpace: ", 0, 250, 100, 10)
|
66 |
+
image = cv2.bilateralFilter(image, dia, sigmaColor, sigmaSpace)
|
67 |
+
res=image[:,:,::-1]
|
68 |
+
st.subheader("Bilateral Blurred Image")
|
69 |
+
st.image(res)
|
70 |
+
# Display download button
|
71 |
+
result = Image.fromarray(res)
|
72 |
+
buffered = io.BytesIO()
|
73 |
+
result.save(buffered, format="PNG")
|
74 |
+
img_bytes = buffered.getvalue()
|
75 |
+
st.download_button(label='Download Output', data=img_bytes, file_name='bilateral_blur_output.png', mime='image/png')
|
76 |
+
|
77 |
+
elif option == 'Image Inpaint':
|
78 |
+
|
79 |
+
stroke_width = st.sidebar.slider("Stroke width: ", 1, 25, 5)
|
80 |
+
h, w = image.shape[:2]
|
81 |
+
if w > 800:
|
82 |
+
h_, w_ = int(h * 800 / w), 800
|
83 |
+
else:
|
84 |
+
h_, w_ = h, w
|
85 |
+
|
86 |
+
# Create a canvas component.
|
87 |
+
st.subheader("Draw over the areas you want to inpaint:")
|
88 |
+
canvas_result = st_canvas(
|
89 |
+
fill_color='white',
|
90 |
+
stroke_width=stroke_width,
|
91 |
+
stroke_color='black',
|
92 |
+
background_image=Image.open(uploaded_file).resize((h_, w_)),
|
93 |
+
update_streamlit=True,
|
94 |
+
height=h_,
|
95 |
+
width=w_,
|
96 |
+
drawing_mode='freedraw',
|
97 |
+
key="canvas",
|
98 |
+
)
|
99 |
+
stroke = canvas_result.image_data
|
100 |
+
|
101 |
+
if stroke is not None:
|
102 |
+
|
103 |
+
if st.sidebar.checkbox('show mask'):
|
104 |
+
st.subheader("Mask")
|
105 |
+
st.image(stroke)
|
106 |
+
|
107 |
+
mask = cv2.split(stroke)[3]
|
108 |
+
mask = np.uint8(mask)
|
109 |
+
mask = cv2.resize(mask, (w, h))
|
110 |
+
|
111 |
+
st.sidebar.caption('Happy with the selection?')
|
112 |
+
option = st.sidebar.selectbox('Mode', ['None', 'Telea', 'NS', 'Compare both'])
|
113 |
+
|
114 |
+
if option == 'Telea':
|
115 |
+
st.subheader('Result of Telea')
|
116 |
+
res = cv2.inpaint(src=image, inpaintMask=mask, inpaintRadius=3, flags=cv2.INPAINT_TELEA)[:,:,::-1]
|
117 |
+
st.image(res)
|
118 |
+
# Display download button
|
119 |
+
result = Image.fromarray(res)
|
120 |
+
buffered = io.BytesIO()
|
121 |
+
result.save(buffered, format="PNG")
|
122 |
+
img_bytes = buffered.getvalue()
|
123 |
+
st.download_button(label='Download Output', data=img_bytes, file_name='inpaint_telea_output.png', mime='image/png')
|
124 |
+
elif option == 'Compare both':
|
125 |
+
col1, col2 = st.columns(2)
|
126 |
+
res1 = cv2.inpaint(src=image, inpaintMask=mask, inpaintRadius=3, flags=cv2.INPAINT_TELEA)[:,:,::-1]
|
127 |
+
res2 = cv2.inpaint(src=image, inpaintMask=mask, inpaintRadius=3, flags=cv2.INPAINT_NS)[:,:,::-1]
|
128 |
+
with col1:
|
129 |
+
st.subheader('Result of Telea')
|
130 |
+
st.image(res1)
|
131 |
+
# Display download button
|
132 |
+
result1 = Image.fromarray(res1)
|
133 |
+
buffered1 = io.BytesIO()
|
134 |
+
result1.save(buffered1, format="PNG")
|
135 |
+
img_bytes1 = buffered1.getvalue()
|
136 |
+
st.download_button(label='Download Output', data=img_bytes1, file_name='inpaint_telea_output.png', mime='image/png')
|
137 |
+
with col2:
|
138 |
+
st.subheader('Result of NS')
|
139 |
+
st.image(res2)
|
140 |
+
# Display download button
|
141 |
+
result2 = Image.fromarray(res2)
|
142 |
+
buffered2 = io.BytesIO()
|
143 |
+
result2.save(buffered2, format="PNG")
|
144 |
+
img_bytes2 = buffered2.getvalue()
|
145 |
+
st.download_button(label='Download Output', data=img_bytes2, file_name='inpaint_ns_output.png', mime='image/png')
|
146 |
+
elif option == 'NS':
|
147 |
+
st.subheader('Result of NS')
|
148 |
+
res = cv2.inpaint(src=image, inpaintMask=mask, inpaintRadius=3, flags=cv2.INPAINT_NS)[:,:,::-1]
|
149 |
+
st.image(res)
|
150 |
+
# Display download button
|
151 |
+
result = Image.fromarray(res)
|
152 |
+
buffered = io.BytesIO()
|
153 |
+
result.save(buffered, format="PNG")
|
154 |
+
img_bytes = buffered.getvalue()
|
155 |
+
st.download_button(label='Download Output', data=img_bytes, file_name='inpaint_ns_output.png', mime='image/png')
|
156 |
+
else:
|
157 |
+
pass
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy
|
2 |
+
streamlit
|
3 |
+
streamlit_drawable_canvas
|
4 |
+
opencv-python-headless
|
5 |
+
pillow
|