| """ |
| @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") |
|
|
| |
| 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() |
|
|
| |
| tab1, tab2 = st.tabs(["Demo","Application"]) |
|
|
| with tab1: |
| |
| |
| 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) |
|
|
| with st.spinner("π OCR in process."): |
| result = reader.readtext(np.array(img)) |
| result_text = [] |
| 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) |
|
|
| with st.spinner("π OCR in process."): |
| result = reader.readtext(np.array(img)) |
|
|
| result_text = [] |
|
|
| 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.") |
| |
| |
| st.markdown( |
| """ |
| <div style="margin: 0.75em 0;"> |
| <a href="https://www.buymeacoffee.com/Artgen" target="_blank"> |
| <img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" |
| alt="Buy Me A Coffee" height="41" width="174"> |
| </a> |
| </div> |
| """, |
| unsafe_allow_html=True |
| ) |
| |
| |
| st.markdown( |
| """ |
| <div style="margin: 0.75em 0;"> |
| But what would really help me is a <strong>PRO subscription</strong> |
| to Google Colab, Kaggle or Hugging Face. Many thanks. |
| </div> |
| """, |
| unsafe_allow_html=True |
| ) |