nurindahpratiwi
commited on
Commit
·
eccc6dd
1
Parent(s):
a651b9d
create first app
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import easyocr
|
| 5 |
+
import PIL
|
| 6 |
+
from PIL import Image, ImageDraw
|
| 7 |
+
from matplotlib import pyplot as plt
|
| 8 |
+
|
| 9 |
+
#Main Title
|
| 10 |
+
st.title("Get text from Image with EASYOCR")
|
| 11 |
+
|
| 12 |
+
#Subtitle
|
| 13 |
+
st.markdown('## Easy OCR with streamlit')
|
| 14 |
+
|
| 15 |
+
#Upload image file
|
| 16 |
+
file = st.file_uploader(label = "Upload your image", type=['png', 'jpg', 'jpeg'])
|
| 17 |
+
|
| 18 |
+
image = Image.open(file)
|
| 19 |
+
st.image(image) #display
|
| 20 |
+
|
| 21 |
+
reader = easyocr.Reader(['en'], gpu=False)
|
| 22 |
+
result = reader.readtext(np.array(image))
|
| 23 |
+
|
| 24 |
+
textdic_easyocr = {}
|
| 25 |
+
for idx in range(len(result)):
|
| 26 |
+
pred_coor = result[idx][0]
|
| 27 |
+
pred_text = result[idx][1]
|
| 28 |
+
pred_confidence = result[idx][2]
|
| 29 |
+
textdic_easyocr[pred_text] = {}
|
| 30 |
+
textdic_easyocr[pred_text]['pred_confidence'] = pred_confidence
|
| 31 |
+
|
| 32 |
+
# create a dataframe which shows the predicted text and prediction confidence
|
| 33 |
+
df = pd.DataFrame.from_dict(textdic_easyocr).T
|
| 34 |
+
st.table(df)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def rectangle(image, result):
|
| 38 |
+
# https://www.blog.pythonlibrary.org/2021/02/23/drawing-shapes-on-images-with-python-and-pillow/
|
| 39 |
+
""" draw rectangles on image based on predicted coordinates"""
|
| 40 |
+
draw = ImageDraw.Draw(image)
|
| 41 |
+
for res in result:
|
| 42 |
+
top_left = tuple(res[0][0]) # top left coordinates as tuple
|
| 43 |
+
bottom_right = tuple(res[0][2]) # bottom right coordinates as tuple
|
| 44 |
+
draw.rectangle((top_left, bottom_right), outline="blue", width=2)
|
| 45 |
+
#display image on streamlit
|
| 46 |
+
st.image(image)
|