File size: 1,419 Bytes
eccc6dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
396d5f5
eccc6dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
import numpy as np
import streamlit as st
import easyocr
import PIL
from PIL import Image, ImageDraw
from matplotlib import pyplot as plt

#Main Title
st.title("Get text from Image with EASYOCR")

#Subtitle
st.markdown('## Easy OCR with streamlit')

#Upload image file
file = st.file_uploader(label = "Upload your image", type=['png', 'jpg', 'jpeg'])

image = Image.open(file)
st.image(image) #display

reader = easyocr.Reader(['en'])
result = reader.readtext(np.array(image))

textdic_easyocr = {} 
for idx in range(len(result)): 
  pred_coor = result[idx][0] 
  pred_text = result[idx][1] 
  pred_confidence = result[idx][2] 
  textdic_easyocr[pred_text] = {} 
  textdic_easyocr[pred_text]['pred_confidence'] = pred_confidence

 # create a dataframe which shows the predicted text and prediction confidence
  df = pd.DataFrame.from_dict(textdic_easyocr).T
  st.table(df)


def rectangle(image, result):
    # https://www.blog.pythonlibrary.org/2021/02/23/drawing-shapes-on-images-with-python-and-pillow/
    """ draw rectangles on image based on predicted coordinates"""
    draw = ImageDraw.Draw(image)
    for res in result:
        top_left = tuple(res[0][0]) # top left coordinates as tuple
        bottom_right = tuple(res[0][2]) # bottom right coordinates as tuple
        draw.rectangle((top_left, bottom_right), outline="blue", width=2)
    #display image on streamlit
    st.image(image)