import pandas as pd
import numpy as np
import streamlit as st
import easyocr
import PIL
from PIL import Image, ImageDraw
from utils import construct_possible_nickname, text_dict
file = None
col1, col2, col3 = st.columns(3)
with col1:
st.image('ubahn.png', width=100)
with col2:
st.title("WIENER UBAHN / VIENNA SUBWAY - GEOCACHE")
with col3:
st.image('gc_logo_grey.png', width=100)
language = st.selectbox(
'Sprache / Language:',
('Deutsch', 'English'))
# subtitle
st.markdown(f"#### {text_dict['step1'][language]}")
nickname = st.text_input("Nickname", value="")
if nickname is not None and nickname != "":
with st.spinner(f"{text_dict['wait'][language]}"):
stations, possible_nickname = construct_possible_nickname(nickname)
df_list = []
for key in stations:
df_list.append([key, ", ".join(stations[key])])
possible_stations_table = pd.DataFrame(df_list, columns=[text_dict['letter'][language], text_dict['stations'][language]])
st.divider()
st.markdown(f"#### {text_dict['step2'][language]}")
st.markdown(f"
{possible_nickname}3>",
unsafe_allow_html=True)
with st.expander(f"{text_dict['explanation'][language]}"):
st.table(possible_stations_table)
st.divider()
# subtitle
st.markdown(f"#### {text_dict['step3'][language]}")
# upload image file
file = st.file_uploader(label = "Upload", type=['png', 'jpg', 'jpeg'])
#read the csv file and display the dataframe
if file is not None:
with st.spinner(f"{text_dict['wait'][language]}"):
image = Image.open(file) # read image with PIL library
st.image(image) #display
with st.spinner(f"{text_dict['wait'][language]}"):
# it will only detect the English and Turkish part of the image as text
reader = easyocr.Reader(['en','de'], gpu=False)
result = reader.readtext(np.array(image)) # turn image to numpy array
with st.spinner(f"{text_dict['wait'][language]}"):
# collect the results in the dictionary:
textdic_easyocr = {}
for idx in range(len(result)):
pred_coor = result[idx][0]
pred_text = result[idx][1]
pred_confidence = result[idx][2]
textdic_easyocr[pred_text] = {}
textdic_easyocr[pred_text]['Text confidence'] = pred_confidence
# create a data frame which shows the predicted text and prediction confidence
df = pd.DataFrame.from_dict(textdic_easyocr).T
st.markdown(f"### {text_dict['detect'][language]}")
st.table(df)
if nickname is not None and nickname != "" and file is not None:
detected_text = [key.lower() for key in textdic_easyocr.keys()]
if possible_nickname.lower() in detected_text:
st.markdown(f"{text_dict['correct'][language]}
",
unsafe_allow_html=True)
else:
st.markdown(f"{text_dict['wrong'][language]}
",
unsafe_allow_html=True)