Image2OCR / app.py
AItool's picture
Upload app.py
78d51b1 verified
"""
@author: idoia lerchundi
"""
import urllib.request
from PIL import Image,ImageFile
import streamlit as st
import numpy as np
import requests
from io import BytesIO
import easyocr as ocr
def local_css(file_name):
with open(file_name) as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
st.set_page_config(
page_title="Streamlit iCodeIdoia - Image2OCR Extract text from an image"
)
st.image("images/banner.jpg")
# ---- LOAD
local_css("styles/style.css")
@st.cache_resource
def load_model():
reader = ocr.Reader(['en'],model_storage_directory='.',gpu=False)
return reader
reader = load_model() #load model
# ---- TABS
tab1, tab2 = st.tabs(["Demo","Application"])
with tab1:
# Handle first image
url = "https://raw.githubusercontent.com/webdevserv/image2OCR/main/images/ocr.jpg"
st.subheader("OCR an image demo")
img_description = st.text('Image text will extracted using OCR.')
if st.button('OCR Demo'):
response = requests.get(url)
img = Image.open(BytesIO(response.content))
img.load()
st.image(img) #display image
with st.spinner("πŸ”„ OCR in process."):
result = reader.readtext(np.array(img))
result_text = [] #empty list
for text in result:
result_text.append(text[1])
st.write(result_text)
st.balloons()
else:
st.write("Upload an image to extract text using OCR.")
with tab2:
st.subheader("OCR an image app")
img_description = st.text('Image text will be extracted using OCR. Warning: It might take an awful long time it is not GPU enabled.')
uploaded_file = st.file_uploader("Upload a image to OCR.", type=['jpg'])
if uploaded_file is not None:
img = Image.open(uploaded_file)
img.load()
st.image(img) #display image
with st.spinner("πŸ”„ OCR in process."):
result = reader.readtext(np.array(img))
result_text = [] #empty list for results
for text in result:
result_text.append(text[1])
st.write(result_text)
st.balloons()
else:
st.write("Upload an image to extract text using OCR.")