Upload 4 LIVE OCR image_app.py
Browse files- 4 LIVE OCR image_app.py +82 -0
4 LIVE OCR image_app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
@author: idoia lerchundi
|
3 |
+
"""
|
4 |
+
import urllib.request
|
5 |
+
from PIL import Image,ImageFile
|
6 |
+
import streamlit as st
|
7 |
+
import numpy as np
|
8 |
+
import requests
|
9 |
+
from io import BytesIO
|
10 |
+
import easyocr as ocr
|
11 |
+
|
12 |
+
def local_css(file_name):
|
13 |
+
with open(file_name) as f:
|
14 |
+
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
|
15 |
+
|
16 |
+
st.set_page_config(
|
17 |
+
page_title="Streamlit iCodeIdoia - OCR an IMAGE - Extract text from an image",
|
18 |
+
page_icon="images/ilpicon1.png",layout="wide",initial_sidebar_state="expanded"
|
19 |
+
)
|
20 |
+
|
21 |
+
st.image("images/banner.jpg")
|
22 |
+
|
23 |
+
# ---- LOAD
|
24 |
+
local_css("styles/style.css")
|
25 |
+
|
26 |
+
@st.cache_resource
|
27 |
+
def load_model():
|
28 |
+
reader = ocr.Reader(['en'],model_storage_directory='.')
|
29 |
+
return reader
|
30 |
+
|
31 |
+
reader = load_model() #load model
|
32 |
+
|
33 |
+
# ---- TABS
|
34 |
+
tab1, tab2 = st.tabs(["Demo","Application"])
|
35 |
+
|
36 |
+
with tab1:
|
37 |
+
# Handle first image
|
38 |
+
|
39 |
+
url = "https://https://raw.githubusercontent.com/webdevserv/images_video/main/ocr_sample.jpg"
|
40 |
+
|
41 |
+
st.subheader("OCR an image demo")
|
42 |
+
img_description = st.text('Image text will extracted using OCR.')
|
43 |
+
|
44 |
+
if st.button('OCR Demo'):
|
45 |
+
response = requests.get(url)
|
46 |
+
img = Image.open(BytesIO(response.content))
|
47 |
+
st.image(input_image) #display image
|
48 |
+
img.load()
|
49 |
+
|
50 |
+
with st.spinner("🔄 OCR in process."):
|
51 |
+
result = reader.readtext(np.array(img))
|
52 |
+
result_text = [] #empty list
|
53 |
+
for text in result:
|
54 |
+
result_text.append(text[1])
|
55 |
+
|
56 |
+
st.write(result_text)
|
57 |
+
st.balloons()
|
58 |
+
else:
|
59 |
+
st.write("Upload an image to extract text using OCR.")
|
60 |
+
|
61 |
+
|
62 |
+
with tab2:
|
63 |
+
st.subheader("OCR an image app")
|
64 |
+
img_description = st.text('Image text will be extracted using OCR.')
|
65 |
+
uploaded_file = st.file_uploader("Upload a image to OCR.", type=['jpg'])
|
66 |
+
|
67 |
+
if uploaded_file is not None:
|
68 |
+
img = Image.open(uploaded_file)
|
69 |
+
img.load()
|
70 |
+
|
71 |
+
with st.spinner("🔄 OCR in process."):
|
72 |
+
result = reader.readtext(np.array(input_image))
|
73 |
+
|
74 |
+
result_text = [] #empty list for results
|
75 |
+
|
76 |
+
for text in result:
|
77 |
+
result_text.append(text[1])
|
78 |
+
|
79 |
+
st.write(result_text)
|
80 |
+
st.balloons()
|
81 |
+
else:
|
82 |
+
st.write("Upload an image to extract text using OCR.")
|