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