File size: 2,258 Bytes
8f25cfa
 
 
 
10e586f
6cd04a8
8f25cfa
3ba81aa
 
 
 
 
 
 
 
 
 
 
 
8f25cfa
 
 
 
ad3d306
8f25cfa
 
3ba81aa
8f25cfa
 
 
 
 
 
 
e13b7e2
8f25cfa
 
04b3685
 
 
 
 
 
 
 
 
c57e88a
 
 
 
61f9858
04b3685
 
8f25cfa
 
 
 
 
 
 
f58179a
6352de6
8f25cfa
 
3ba81aa
 
 
4755e97
 
8f25cfa
 
 
6352de6
 
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
import pandas as pd
import numpy as np
import streamlit as st
import easyocr
import PIL
from PIL import Image, ImageDraw

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)


# main title
st.title("Get text from image with EasyOCR")

# subtitle
st.markdown("## EasyOCR with Streamlit")

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

#read the csv file and display the dataframe
if file is not None:
    image = Image.open(file) # read image with PIL library
    st.image(image) #display

    # it will only detect the English and Turkish part of the image as text
    reader = easyocr.Reader(['tr','en','ja'], gpu=False) 
    result = reader.readtext(np.array(image))  # turn image to numpy array

    # Add a placeholder
    # latest_iteration = st.empty()
    # bar = st.progress(0)
    
    # for i in range(100):
      # Update the progress bar with each iteration.
      # latest_iteration.text(f'Iteration {i+1}')
      # bar.progress(i + 1)
      # time.sleep(0.1)

    # print all predicted text:
    for idx in range(len(result)):
        pred_text = result[idx][1]
        st.write(pred_text)
    
    # collect the results in the dictionary:
    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 data frame which shows the predicted text and prediction confidence
    df = pd.DataFrame.from_dict(textdic_easyocr).T
    st.table(df)

    # get boxes on the image 
    rectangle(image, result)

    st.spinner(text="In progress...")
    
else:
    st.write("Upload your image")