Ankan Ghosh commited on
Commit
e0ac091
·
verified ·
1 Parent(s): 58dba97

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +119 -0
  2. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Application to demo inpainting using streamlit.
2
+
3
+ Run using: streamlit run 10_03_image_inpaint_streamlit.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
+ STREAMLIT_STATIC_PATH = pathlib.Path(st.__path__[0]) / 'static'
16
+
17
+ # We create a downloads directory within the streamlit static asset directory
18
+ # and we write output files to it.
19
+ DOWNLOADS_PATH = (STREAMLIT_STATIC_PATH / "downloads")
20
+ if not DOWNLOADS_PATH.is_dir():
21
+ DOWNLOADS_PATH.mkdir()
22
+
23
+
24
+ def get_image_download_link(img, filename, text):
25
+ """Generates a link to download a particular image file."""
26
+ buffered = io.BytesIO()
27
+ img.save(buffered, format='JPEG')
28
+ img_str = base64.b64encode(buffered.getvalue()).decode()
29
+ href = f'<a href="data:file/txt;base64,{img_str}" download="{filename}">{text}</a>'
30
+ return href
31
+
32
+
33
+ # Set title.
34
+ st.sidebar.title('Image Inpaint')
35
+
36
+ # Specify canvas parameters in application
37
+ uploaded_file = st.sidebar.file_uploader("Upload Image to restore:", type=["png", "jpg"])
38
+ image = None
39
+ res = None
40
+
41
+ if uploaded_file is not None:
42
+
43
+ # Convert the file to an opencv image.
44
+ file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
45
+ image = cv2.imdecode(file_bytes, 1)
46
+
47
+ stroke_width = st.sidebar.slider("Stroke width: ", 1, 25, 5)
48
+ h, w = image.shape[:2]
49
+ if w > 800:
50
+ h_, w_ = int(h * 800 / w), 800
51
+ else:
52
+ h_, w_ = h, w
53
+
54
+ # Create a canvas component.
55
+ canvas_result = st_canvas(
56
+ fill_color='gray',
57
+ stroke_width=stroke_width,
58
+ stroke_color='green',
59
+ background_image=Image.open(uploaded_file).resize((h_, w_)),
60
+ update_streamlit=True,
61
+ height=h_,
62
+ width=w_,
63
+ drawing_mode='freedraw',
64
+ key="canvas",
65
+ )
66
+ stroke = canvas_result.image_data
67
+
68
+ if stroke is not None:
69
+
70
+ if st.sidebar.checkbox('show mask'):
71
+ st.image(stroke)
72
+
73
+ mask = cv2.split(stroke)[3]
74
+ mask = np.uint8(mask)
75
+ mask = cv2.resize(mask, (w, h))
76
+
77
+ st.sidebar.caption('Happy with the selection?')
78
+ option = st.sidebar.selectbox('Mode', ['None', 'Telea', 'NS', 'Compare both'])
79
+
80
+ if option == 'Telea':
81
+ st.subheader('Result of Telea')
82
+ res = cv2.inpaint(src=image, inpaintMask=mask, inpaintRadius=3, flags=cv2.INPAINT_TELEA)[:, :, ::-1]
83
+ st.image(res)
84
+ elif option == 'Compare both':
85
+ col1, col2 = st.columns(2)
86
+ res1 = cv2.inpaint(src=image, inpaintMask=mask, inpaintRadius=3, flags=cv2.INPAINT_TELEA)[:, :, ::-1]
87
+ res2 = cv2.inpaint(src=image, inpaintMask=mask, inpaintRadius=3, flags=cv2.INPAINT_NS)[:, :, ::-1]
88
+ with col1:
89
+ st.subheader('Result of Telea')
90
+ st.image(res1)
91
+ with col2:
92
+ st.subheader('Result of NS')
93
+ st.image(res2)
94
+ if res1 is not None:
95
+ # Display link.
96
+ result1 = Image.fromarray(res1)
97
+ st.sidebar.markdown(
98
+ get_image_download_link(result1, 'telea.png', 'Download Output of Telea'),
99
+ unsafe_allow_html=True)
100
+ if res2 is not None:
101
+ # Display link.
102
+ result2 = Image.fromarray(res2)
103
+ st.sidebar.markdown(
104
+ get_image_download_link(result2, 'ns.png', 'Download Output of NS'),
105
+ unsafe_allow_html=True)
106
+
107
+ elif option == 'NS':
108
+ st.subheader('Result of NS')
109
+ res = cv2.inpaint(src=image, inpaintMask=mask, inpaintRadius=3, flags=cv2.INPAINT_NS)[:, :, ::-1]
110
+ st.image(res)
111
+ else:
112
+ pass
113
+
114
+ if res is not None:
115
+ # Display link.
116
+ result = Image.fromarray(res)
117
+ st.sidebar.markdown(
118
+ get_image_download_link(result, 'output.png', 'Download Output'),
119
+ unsafe_allow_html=True)
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ numpy
2
+ streamlit
3
+ streamlit_drawable_canvas
4
+ opencv-python-headless
5
+ pillow