File size: 2,370 Bytes
19f9ac5
 
 
 
 
 
4cb29e5
19f9ac5
 
4cb29e5
19f9ac5
 
4cb29e5
 
19f9ac5
4cb29e5
19f9ac5
 
4cb29e5
8973c03
 
4cb29e5
8973c03
 
4cb29e5
 
 
 
 
 
8973c03
4cb29e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8973c03
4cb29e5
 
 
 
 
 
8973c03
4cb29e5
 
8973c03
4cb29e5
 
 
8973c03
4cb29e5
 
 
8973c03
 
4cb29e5
 
8973c03
4cb29e5
8973c03
 
 
4cb29e5
8973c03
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
76
77
import pandas as pd
import numpy as np
import streamlit as st
import easyocr
import PIL
from PIL import Image, ImageDraw
from streamlit_drawable_canvas import st_canvas

def rectangle(image, result):
    """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 or draw
st.markdown("## Upload an Image or Draw")
col1, col2 = st.columns(2)

with col1:
    file = st.file_uploader("Upload Here", type=['png', 'jpg', 'jpeg'])

with col2:
    # Drawable canvas
    canvas_result = st_canvas(
        fill_color="rgba(255, 165, 0, 0.3)", 
        stroke_width=3,
        stroke_color="#ffffff",
        background_color="#000000",
        background_image=None if file else st.session_state.get("background", None),
        update_streamlit=True,
        width=400,
        height=400,
        drawing_mode="freedraw",
        key="canvas",
    )

# Process uploaded image or drawing
if file is not None:
    image = Image.open(file)  # Read image with PIL library
elif canvas_result.image_data is not None:
    image = Image.fromarray(canvas_result.image_data.astype('uint8'), 'RGBA').convert('RGB')
else:
    st.write("Please upload an image or use the canvas to draw.")
    image = None

if image is not None:
    st.image(image)  # Display

    # Only detect the English and Turkish part of the image as text
    reader = easyocr.Reader(['en','my'], gpu=False) 
    result = reader.readtext(np.array(image))  # Turn image to numpy array

    # Print all predicted text:
    for idx, res in enumerate(result):
        pred_text = res[1]
        st.write(pred_text)
    
    # Collect the results in the dictionary:
    textdic_easyocr = {res[1]: {'pred_confidence': res[2]} for res in result}

    # 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)