Update app.py
Browse files
app.py
CHANGED
@@ -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 |
+
# subtitle
|
12 |
+
st.markdown("## EasyOCRR with Streamlit")
|
13 |
+
|
14 |
+
# upload image file
|
15 |
+
file = st.file_uploader(label = "Upload your image", type=['png', 'jpg', 'jpeg'])
|
16 |
+
|
17 |
+
image = Image.open(file) # read image with PIL library
|
18 |
+
st.image(image) #display
|
19 |
+
|
20 |
+
# it will only detect the English and Turkish part of the image as text
|
21 |
+
reader = easyocr.Reader(['tr','en'], gpu=False)
|
22 |
+
result = reader.readtext(np.array(image)) # turn image to numpy array
|
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 |
+
def rectangle(image, result):
|
37 |
+
# https://www.blog.pythonlibrary.org/2021/02/23/drawing-shapes-on-images-with-python-and-pillow/
|
38 |
+
""" draw rectangles on image based on predicted coordinates"""
|
39 |
+
draw = ImageDraw.Draw(image)
|
40 |
+
for res in result:
|
41 |
+
top_left = tuple(res[0][0]) # top left coordinates as tuple
|
42 |
+
bottom_right = tuple(res[0][2]) # bottom right coordinates as tuple
|
43 |
+
draw.rectangle((top_left, bottom_right), outline="blue", width=2)
|
44 |
+
#display image on streamlit
|
45 |
+
st.image(image)
|
46 |
+
|