|
| 1 | +import streamlit as st |
| 2 | +from streamlit_drawable_canvas import st_canvas |
| 3 | +import cv2 |
| 4 | +import easyocr |
| 5 | +import numpy as np |
| 6 | +from PIL import Image |
| 7 | +import io |
| 8 | + |
| 9 | +def detect_text(image): |
| 10 | + reader = easyocr.Reader(['en'], gpu=False) |
| 11 | + text_ = reader.readtext(np.array(image)) |
| 12 | + |
| 13 | + img = np.array(image) |
| 14 | + |
| 15 | + for t_, t in enumerate(text_): |
| 16 | + bbox, text, score = t |
| 17 | + cv2.rectangle(img, tuple(map(int, bbox[0])), tuple(map(int, bbox[2])), (0, 255, 0), 2) |
| 18 | + cv2.putText(img, text, tuple(map(int, bbox[0])), cv2.FONT_HERSHEY_TRIPLEX, 0.75, (255, 0, 0), 1) |
| 19 | + |
| 20 | + return img, text_ |
| 21 | + |
| 22 | +st.title("HandWritten Text Recognition") |
| 23 | + |
| 24 | +tab1, tab2 = st.tabs(["Draw", "Upload Image"]) |
| 25 | + |
| 26 | +with tab1: |
| 27 | + # Create a canvas component |
| 28 | + canvas_result = st_canvas( |
| 29 | + stroke_width=3, |
| 30 | + stroke_color="#000000", |
| 31 | + background_color="#ffffff", |
| 32 | + height=400, |
| 33 | + width=600, |
| 34 | + drawing_mode="freedraw", |
| 35 | + key="canvas", |
| 36 | + ) |
| 37 | + |
| 38 | + # Detect text on drawn image |
| 39 | + if st.button("Detect"): |
| 40 | + if canvas_result.image_data is not None: |
| 41 | + img = Image.fromarray(canvas_result.image_data.astype('uint8'), 'RGBA') |
| 42 | + img = img.convert('RGB') |
| 43 | + st.image(img, caption='Drawn Image.', use_column_width=True) |
| 44 | + |
| 45 | + st.write("Processing the image...") |
| 46 | + with st.spinner('Detecting text...'): |
| 47 | + processed_image, text_ = detect_text(np.array(img)) |
| 48 | + |
| 49 | + st.image(processed_image, caption='Processed Image.', use_column_width=True) |
| 50 | + |
| 51 | + st.write("Detected Text:") |
| 52 | + for _, text, score in text_: |
| 53 | + st.write(f"Text: **{text}** ") |
| 54 | + processed_image_pil = Image.fromarray(processed_image) |
| 55 | + buf = io.BytesIO() |
| 56 | + processed_image_pil.save(buf, format="PNG") |
| 57 | + |
| 58 | + else: |
| 59 | + st.write("Please draw something on the canvas first.") |
| 60 | + |
| 61 | +with tab2: |
| 62 | + uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) |
| 63 | + if uploaded_file is not None: |
| 64 | + image = Image.open(uploaded_file) |
| 65 | + st.image(image, caption='Uploaded Image.', use_column_width=True) |
| 66 | + |
| 67 | + if st.button("Detect Text"): |
| 68 | + st.write("Processing the image...") |
| 69 | + with st.spinner('Detecting text...'): |
| 70 | + processed_image, text_ = detect_text(np.array(image)) |
| 71 | + |
| 72 | + st.image(processed_image, caption='Processed Image.', use_column_width=True) |
| 73 | + |
| 74 | + st.write("Detected Text:") |
| 75 | + for _, text, score in text_: |
| 76 | + st.write(f"Text: **{text}** ") |
| 77 | + |
| 78 | + processed_image_pil = Image.fromarray(processed_image) |
| 79 | + buf = io.BytesIO() |
| 80 | + processed_image_pil.save(buf, format="PNG") |
| 81 | + |
0 commit comments