File size: 2,198 Bytes
378b1dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
@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 - OCR an IMAGE - Extract text from an image",
   page_icon="images/ilpicon1.png",layout="wide",initial_sidebar_state="expanded"
)

st.image("images/banner.jpg")

# ---- LOAD
local_css("styles/style.css")

@st.cache_resource
def load_model(): 
    reader = ocr.Reader(['en'],model_storage_directory='.')
    return reader 

reader = load_model() #load model

# ---- TABS
tab1, tab2 = st.tabs(["Demo","Application"])

with tab1:   
   # Handle first image
   
   url = "https://https://raw.githubusercontent.com/webdevserv/images_video/main/ocr_sample.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))
    st.image(input_image) #display image
    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.')
  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.")