EZOCR / app.py
Mattral's picture
Update app.py
e4c3e4a verified
raw
history blame
2.37 kB
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.")