File size: 2,079 Bytes
6ca2703 cc3aad6 849cfbb 3ebf562 6dd2ec6 6ca2703 3ebf562 849cfbb d706d88 6ca2703 2ba33db 6ca2703 8973c03 e4c3e4a 8973c03 e4c3e4a 6ca2703 cc3aad6 e4c3e4a 6ca2703 e4c3e4a 6ca2703 e4c3e4a 6ca2703 e4c3e4a 6ca2703 e4c3e4a 6ca2703 849cfbb 6ca2703 849cfbb 6ca2703 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 |
from PIL import Image, ImageDraw
import numpy as np
import streamlit as st
from streamlit_drawable_canvas import st_canvas
import easyocr
import pandas as pd
def rectangle(image, result):
"""Draw rectangles on the image based on predicted coordinates and display the image."""
draw_image = image.copy() # Work on a copy of the image
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="blue", width=2)
st.image(draw_image, caption="Processed Image with Detected Text Highlighted")
# Main title and markdowns
st.title("Get text from an image with EasyOCR")
st.markdown("## EasyOCR with Streamlit")
st.markdown("## Upload an Image or Draw")
# Column layout for uploader and canvas
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",
)
if image is not None:
st.image(image, caption="Uploaded/Drawn Image")
# Optional: Indicate that processing is happening
with st.spinner('Processing...'):
reader = easyocr.Reader(['en', 'ja'], gpu=False) # Consider moving this outside the loop if performance is a concern
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, orient='index', columns=['pred_confidence'])
st.table(df)
rectangle(image, result)
else:
st.write("Please upload an image or use the canvas to draw.")
|