File size: 2,370 Bytes
cc3aad6
849cfbb
e4c3e4a
 
3ebf562
 
6dd2ec6
e4c3e4a
 
 
 
 
 
 
 
 
 
 
3ebf562
849cfbb
d706d88
 
e4c3e4a
 
 
 
 
 
 
 
 
2ba33db
e4c3e4a
 
8973c03
e4c3e4a
 
8973c03
e4c3e4a
 
cc3aad6
e4c3e4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
849cfbb
e4c3e4a
 
849cfbb
2ba33db
 
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 streamlit as st
from streamlit_drawable_canvas import st_canvas
import numpy as np
from PIL import Image
import easyocr
import pandas as pd

def process_image_for_ocr(image):
    """Converts the image for EasyOCR and draws rectangles around detected text."""
    # Convert PIL image to numpy array
    image_np = np.array(image)
    
    # Initialize EasyOCR reader
    reader = easyocr.Reader(['en'], gpu=False)  # Add more languages or adjust as needed
    result = reader.readtext(image_np)
    
    # Draw rectangles on a copy of the image
    draw_image = image.copy()
    draw = ImageDraw.Draw(draw_image)
    for res in result:
        top_left = tuple(res[0][0])
        bottom_right = tuple(res[0][2])
        draw.rectangle([top_left, bottom_right], outline="red", width=2)
    
    return draw_image, result

def display_text_results(result):
    """Displays text results in Streamlit."""
    textdic_easyocr = {idx: {'Text': res[1], 'Confidence': res[2]} for idx, res in enumerate(result)}
    df = pd.DataFrame.from_dict(textdic_easyocr, orient='index')
    st.table(df)

st.title("OCR with EasyOCR and Streamlit")
st.markdown("Upload an Image or Draw Below")

# Column layout for uploader and canvas
col1, col2 = st.columns(2)

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

with col2:
    # Use the background_image parameter if you want to draw on an uploaded image.
    canvas_result = st_canvas(
        fill_color="rgba(255, 165, 0, 0.3)",  # Use a transparent fill color
        stroke_width=3,
        stroke_color="#FFFFFF",
        background_color="#000000",
        width=400,
        height=400,
        drawing_mode="freedraw",
        key="canvas",
    )

image = None

if uploaded_file is not None:
    image = Image.open(uploaded_file).convert('RGB')

elif canvas_result.image_data is not None:
    # Convert the canvas data to an Image
    canvas_image = Image.fromarray((canvas_result.image_data).astype('uint8'), mode='RGBA')
    image = canvas_image.convert('RGB')

if image:
    st.image(image, caption="Uploaded/Drawn Image")

    processed_image, ocr_result = process_image_for_ocr(image)
    
    st.image(processed_image, caption="Processed Image with Detected Text")
    display_text_results(ocr_result)

else:
    st.write("Please upload an image or use the canvas to draw.")