Huertas97 commited on
Commit
a771624
1 Parent(s): 6b70ec8

Add v1 app

Browse files
Examples/meme_1.jpg ADDED
Examples/meme_2.jpg ADDED
Examples/meme_3.jpg ADDED
aida_logo.png ADDED
app_inpaint.py ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import spacy
3
+ from streamlit_echarts import st_echarts
4
+ from annotated_text import annotated_text
5
+ import time
6
+ import easyocr
7
+ import math
8
+ from pathlib import Path
9
+ from PIL import Image, ImageDraw
10
+ import PIL
11
+ import io
12
+ import os
13
+ import random
14
+ import matplotlib.pyplot as plt
15
+ import cv2
16
+ # from google.colab.patches import cv2_imshow
17
+ import numpy as np
18
+ from tqdm.auto import tqdm
19
+ import shutil
20
+ import base64
21
+ import logging
22
+ logging.basicConfig(format='%(asctime)s - %(message)s',
23
+ datefmt='%Y-%m-%d %H:%M:%S',
24
+ level=logging.INFO,
25
+ )
26
+
27
+
28
+ @st.cache(show_spinner=False, allow_output_mutation=True, suppress_st_warning=True)
29
+ def load_models():
30
+ #specify shortform of language you want to extract,
31
+ # I am using Spanish(es) and English(en) here by list of language ids
32
+ reader = easyocr.Reader(['en'],)
33
+ return reader
34
+
35
+ reader = load_models()
36
+
37
+ def midpoint(x1, y1, x2, y2):
38
+ x_mid = int((x1 + x2)/2)
39
+ y_mid = int((y1 + y2)/2)
40
+ return (x_mid, y_mid)
41
+
42
+ def inpaint_text(img, text_coordinates):
43
+ # read image
44
+
45
+ # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
46
+ # generate (word, box) tuples
47
+ mask = np.zeros(img.shape[:2], dtype="uint8")
48
+ for box in text_coordinates:
49
+ x0, y0 = box[0]
50
+ x1, y1 = box[1]
51
+ x2, y2 = box[2]
52
+ x3, y3 = box[3]
53
+
54
+ x_mid0, y_mid0 = midpoint(x1, y1, x2, y2)
55
+ x_mid1, y_mi1 = midpoint(x0, y0, x3, y3)
56
+
57
+ thickness = int(math.sqrt( (x2 - x1)**2 + (y2 - y1)**2 ))
58
+
59
+ cv2.line(mask, (x_mid0, y_mid0), (x_mid1, y_mi1), 255,
60
+ thickness)
61
+ img = cv2.inpaint(img, mask, 7, cv2.INPAINT_NS)
62
+
63
+ return(img)
64
+
65
+
66
+ def file_selector(folder_path='.'):
67
+ filenames = os.listdir(folder_path)
68
+ selected_filename = st.selectbox('Select a file', filenames)
69
+ return os.path.join(folder_path, selected_filename)
70
+
71
+
72
+
73
+
74
+
75
+
76
+
77
+
78
+ st.set_page_config(
79
+ page_title="Inpaint Me",
80
+ page_icon=":art:",
81
+ layout="wide",
82
+ initial_sidebar_state="expanded",
83
+ menu_items={
84
+ 'Get Help': 'https://www.extremelycoolapp.com/help',
85
+ 'Report a bug': "https://www.extremelycoolapp.com/bug",
86
+ 'About': "# This is a header. This is an *extremely* cool app!"
87
+ }
88
+ )
89
+
90
+
91
+ st.markdown(
92
+ """
93
+ <style>
94
+ .logo-img {
95
+
96
+
97
+ margin-top: auto;
98
+ margin-left: 30%;
99
+ width: 50%;
100
+
101
+ }
102
+ .logo-img-2 {
103
+ margin-top: auto;
104
+ margin-left: 20%;
105
+ width: 35%;
106
+
107
+ }
108
+ </style>
109
+ """,
110
+ unsafe_allow_html=True
111
+ )
112
+
113
+ LOGO_IMAGE = "inpaint_me_logo.png"
114
+
115
+ col1, col2= st.columns([2, 2])
116
+ with col1:
117
+ # st.image('./aida_logo.png')
118
+ st.markdown(
119
+ f"""
120
+ <img class="logo-img" src="data:image/png;base64,{base64.b64encode(open(LOGO_IMAGE, "rb").read()).decode()}">
121
+ """,
122
+ unsafe_allow_html=True
123
+ )
124
+
125
+ with col2:
126
+ # st.image('./aida_logo.png')
127
+ st.markdown(
128
+ f"""
129
+ <img class="logo-img-2" src="data:image/png;base64,{base64.b64encode(open("aida_logo.png", "rb").read()).decode()}">
130
+ """,
131
+ unsafe_allow_html=True
132
+ )
133
+
134
+
135
+ st.header("")
136
+ with st.expander("Project Description", expanded=False):
137
+ st.write("""
138
+ Developed in Applied Intelligence and Data Analysis ([AI+DA](http://aida.etsisi.upm.es/)) group at Polytech University of Madrid (UPM).
139
+
140
+ To rule out the possibility of text misleading image Deep Learning models (e.g., CNNs) it is useful to remove text from images. Hence,
141
+ this tool uses [EasyOCR](https://github.com/JaidedAI/EasyOCR) and [OpenCV](https://pypi.org/project/opencv-python/) for detecting texts and inpainting them. Currently, only `JPG` files are supported. This tools has been tested on memes, feel free to try some examples or upload your own images.
142
+ """)
143
+
144
+
145
+
146
+ filename_example = None
147
+ if st.checkbox('Select a example'):
148
+ folder_path = './Examples/'
149
+ # if st.checkbox('Change directory'):
150
+ # folder_path = st.text_input('Enter folder path', '.')
151
+ filename_example = file_selector(folder_path=folder_path)
152
+ st.write('You selected `%s`' % filename_example)
153
+
154
+ uploaded_file = st.file_uploader(label="Upload image",
155
+ type="jpg",
156
+ accept_multiple_files=False,
157
+ key=None,
158
+ help=None,
159
+ on_change=None,
160
+ args=None,
161
+ kwargs=None,
162
+ )
163
+
164
+
165
+
166
+ col1, col2, col3 = st.columns([2, 0.5, 2])
167
+
168
+
169
+
170
+ if filename_example:
171
+ with col1:
172
+ st.header("Original")
173
+ img = Image.open( filename_example )
174
+ st.image(img, caption=None, width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
175
+
176
+ with col3:
177
+ st.header("Inpainted")
178
+ with st.spinner('Wait for it...'):
179
+ img_array = np.array(Image.open( filename_example ))
180
+ # detect text
181
+ bounds = reader.readtext(img_array, detail=1) #detail=1 # [(coordinates, detected text, confidence threshold)]
182
+ text_coordinates = [ bound[0] for bound in bounds]
183
+ # inpaint text coordinates
184
+ inpaint_image = inpaint_text(img_array, text_coordinates)
185
+ st.image(inpaint_image, caption=None, width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
186
+
187
+ if uploaded_file:
188
+ with col1:
189
+ st.header("Original")
190
+ st.image(uploaded_file, caption=None, width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
191
+
192
+ with col3:
193
+ st.header("Inpainted")
194
+ with st.spinner('Wait for it...'):
195
+ # Transform loaded file to bytes
196
+ bytes_data = uploaded_file.getvalue()
197
+ # bytes to numpy array
198
+ img_array = np.array(Image.open(io.BytesIO(bytes_data)))
199
+ # detect text
200
+ bounds = reader.readtext(img_array, detail=1) #detail=1 # [(coordinates, detected text, confidence threshold)]
201
+ text_coordinates = [ bound[0] for bound in bounds]
202
+ # inpaint text coordinates
203
+ inpaint_image = inpaint_text(img_array, text_coordinates)
204
+ st.image(inpaint_image, caption=None, width=None, use_column_width=None, clamp=False, channels="RGB", output_format="auto")
inpaint_me_logo.png ADDED