diff --git a/Machine_Learning/Handwritten Text Recognition Model/README.md b/Machine_Learning/Handwritten Text Recognition Model/README.md new file mode 100644 index 00000000..77843930 --- /dev/null +++ b/Machine_Learning/Handwritten Text Recognition Model/README.md @@ -0,0 +1,44 @@ + +# Handwritten Text Recognition Model + +This code creates a Handwritten Text Recognition app using Machine Learning. + +The app provides two main functionalities + +1. Draw: Users can draw on a canvas, and the app will detect and display any text from the drawing. +2. Upload Image: Users can upload an image, and the app will detect and display any text present in the uploaded image. + + +## Screenshots + +![App Screenshot](results/OP5.png) +![App Screenshot](results/OP7.png) +![App Screenshot](results/OP2.png) + + + + +## Tech Stack + +**Languages:** Python + +**Libraries:** easyocr, opencv, numpy, scipy etc + +**Framework:** Streamlit + + +## Run Locally + +Install dependencies + +```bash + pip install -r requirements.txt +``` + +Start the server + +```bash + streamlit run app.py +``` + + diff --git a/Machine_Learning/Handwritten Text Recognition Model/app.py b/Machine_Learning/Handwritten Text Recognition Model/app.py new file mode 100644 index 00000000..c4f961c3 --- /dev/null +++ b/Machine_Learning/Handwritten Text Recognition Model/app.py @@ -0,0 +1,81 @@ +import streamlit as st +from streamlit_drawable_canvas import st_canvas +import cv2 +import easyocr +import numpy as np +from PIL import Image +import io + +def detect_text(image): + reader = easyocr.Reader(['en'], gpu=False) + text_ = reader.readtext(np.array(image)) + + img = np.array(image) + + for t_, t in enumerate(text_): + bbox, text, score = t + cv2.rectangle(img, tuple(map(int, bbox[0])), tuple(map(int, bbox[2])), (0, 255, 0), 2) + cv2.putText(img, text, tuple(map(int, bbox[0])), cv2.FONT_HERSHEY_TRIPLEX, 0.75, (255, 0, 0), 1) + + return img, text_ + +st.title("HandWritten Text Recognition") + +tab1, tab2 = st.tabs(["Draw", "Upload Image"]) + +with tab1: + # Create a canvas component + canvas_result = st_canvas( + stroke_width=3, + stroke_color="#000000", + background_color="#ffffff", + height=400, + width=600, + drawing_mode="freedraw", + key="canvas", + ) + + # Detect text on drawn image + if st.button("Detect"): + if canvas_result.image_data is not None: + img = Image.fromarray(canvas_result.image_data.astype('uint8'), 'RGBA') + img = img.convert('RGB') + st.image(img, caption='Drawn Image.', use_column_width=True) + + st.write("Processing the image...") + with st.spinner('Detecting text...'): + processed_image, text_ = detect_text(np.array(img)) + + st.image(processed_image, caption='Processed Image.', use_column_width=True) + + st.write("Detected Text:") + for _, text, score in text_: + st.write(f"Text: **{text}** ") + processed_image_pil = Image.fromarray(processed_image) + buf = io.BytesIO() + processed_image_pil.save(buf, format="PNG") + + else: + st.write("Please draw something on the canvas first.") + +with tab2: + uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) + if uploaded_file is not None: + image = Image.open(uploaded_file) + st.image(image, caption='Uploaded Image.', use_column_width=True) + + if st.button("Detect Text"): + st.write("Processing the image...") + with st.spinner('Detecting text...'): + processed_image, text_ = detect_text(np.array(image)) + + st.image(processed_image, caption='Processed Image.', use_column_width=True) + + st.write("Detected Text:") + for _, text, score in text_: + st.write(f"Text: **{text}** ") + + processed_image_pil = Image.fromarray(processed_image) + buf = io.BytesIO() + processed_image_pil.save(buf, format="PNG") + diff --git a/Machine_Learning/Handwritten Text Recognition Model/requirements.txt b/Machine_Learning/Handwritten Text Recognition Model/requirements.txt new file mode 100644 index 00000000..a050392a --- /dev/null +++ b/Machine_Learning/Handwritten Text Recognition Model/requirements.txt @@ -0,0 +1,12 @@ +easyocr==1.7.1 +googletrans==4.0.0rc1 +imutils==0.5.4 +langdetect==1.0.9 +numpy==1.24.4 +opencv_contrib_python==4.10.0.84 +opencv_python==4.10.0.82 +opencv_python_headless==4.10.0.84 +Pillow==10.4.0 +scipy==1.11.4 +streamlit==1.36.0 +streamlit_drawable_canvas==0.9.3 diff --git a/Machine_Learning/Handwritten Text Recognition Model/results/OP1.png b/Machine_Learning/Handwritten Text Recognition Model/results/OP1.png new file mode 100644 index 00000000..8faffb24 Binary files /dev/null and b/Machine_Learning/Handwritten Text Recognition Model/results/OP1.png differ diff --git a/Machine_Learning/Handwritten Text Recognition Model/results/OP2.png b/Machine_Learning/Handwritten Text Recognition Model/results/OP2.png new file mode 100644 index 00000000..89ba90b4 Binary files /dev/null and b/Machine_Learning/Handwritten Text Recognition Model/results/OP2.png differ diff --git a/Machine_Learning/Handwritten Text Recognition Model/results/OP3.png b/Machine_Learning/Handwritten Text Recognition Model/results/OP3.png new file mode 100644 index 00000000..c5a48ba6 Binary files /dev/null and b/Machine_Learning/Handwritten Text Recognition Model/results/OP3.png differ diff --git a/Machine_Learning/Handwritten Text Recognition Model/results/OP4.png b/Machine_Learning/Handwritten Text Recognition Model/results/OP4.png new file mode 100644 index 00000000..9c6d0f0e Binary files /dev/null and b/Machine_Learning/Handwritten Text Recognition Model/results/OP4.png differ diff --git a/Machine_Learning/Handwritten Text Recognition Model/results/OP5.png b/Machine_Learning/Handwritten Text Recognition Model/results/OP5.png new file mode 100644 index 00000000..e187b5f1 Binary files /dev/null and b/Machine_Learning/Handwritten Text Recognition Model/results/OP5.png differ diff --git a/Machine_Learning/Handwritten Text Recognition Model/results/OP6.png b/Machine_Learning/Handwritten Text Recognition Model/results/OP6.png new file mode 100644 index 00000000..d663546b Binary files /dev/null and b/Machine_Learning/Handwritten Text Recognition Model/results/OP6.png differ diff --git a/Machine_Learning/Handwritten Text Recognition Model/results/OP7.png b/Machine_Learning/Handwritten Text Recognition Model/results/OP7.png new file mode 100644 index 00000000..9a373903 Binary files /dev/null and b/Machine_Learning/Handwritten Text Recognition Model/results/OP7.png differ