|
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.""" |
|
|
|
image_np = np.array(image) |
|
|
|
|
|
reader = easyocr.Reader(['en'], gpu=False) |
|
result = reader.readtext(image_np) |
|
|
|
|
|
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") |
|
|
|
|
|
col1, col2 = st.columns(2) |
|
|
|
with col1: |
|
uploaded_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", |
|
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: |
|
|
|
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.") |
|
|