|
import pandas as pd |
|
import numpy as np |
|
import streamlit as st |
|
import easyocr |
|
from PIL import Image, ImageDraw |
|
from streamlit_drawable_canvas import st_canvas |
|
|
|
def rectangle(image, result): |
|
"""Draw rectangles on the image based on predicted coordinates.""" |
|
draw = ImageDraw.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="blue", width=2) |
|
|
|
st.image(image) |
|
|
|
|
|
st.title("Get text from an image with EasyOCR") |
|
|
|
|
|
st.markdown("## EasyOCR with Streamlit") |
|
|
|
|
|
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: |
|
|
|
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", |
|
) |
|
|
|
image = None |
|
|
|
|
|
if file is not None: |
|
image = Image.open(file) |
|
elif canvas_result.image_data is not None: |
|
image = Image.fromarray(np.array(canvas_result.image_data, dtype=np.uint8)).convert('RGB') |
|
|
|
if image is not None: |
|
st.image(image) |
|
|
|
|
|
reader = easyocr.Reader(['en'], gpu=False) |
|
result = reader.readtext(np.array(image)) |
|
|
|
|
|
for idx, res in enumerate(result): |
|
pred_text = res[1] |
|
st.write(pred_text) |
|
|
|
|
|
textdic_easyocr = {res[1]: {'pred_confidence': res[2]} for res in result} |
|
|
|
|
|
df = pd.DataFrame.from_dict(textdic_easyocr).T |
|
st.table(df) |
|
|
|
|
|
rectangle(image, result) |
|
else: |
|
st.write("Please upload an image or use the canvas to draw.") |
|
|