import streamlit as st from streamlit_drawable_canvas import st_canvas from PIL import Image import pytesseract import numpy as np # Set the path to the Tesseract executable for Hugging Face Spaces pytesseract.pytesseract.tesseract_cmd = '/usr/bin/tesseract' st.title("Handwritten Text Recognition") # Create a canvas component canvas_result = st_canvas( fill_color="rgba(255, 165, 0, 0.3)", # Fill color with some transparency stroke_width=2, stroke_color="#000000", background_color="#ffffff", update_streamlit=True, height=150, width=600, drawing_mode="freedraw", key="canvas" ) # Process the canvas image if canvas_result.image_data is not None: img = Image.fromarray(canvas_result.image_data.astype('uint8'), 'RGBA').convert('L') img = img.resize((img.width * 2, img.height * 2), Image.LANCZOS) # Resize to improve OCR accuracy text = pytesseract.image_to_string(img, config='--psm 6') st.text_area("Recognized Text", text, height=100)