Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added machine learning project that detects handwritten texts #843

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Machine_Learning/Handwritten Text Recognition Model/README.md
Original file line number Diff line number Diff line change
@@ -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
```


81 changes: 81 additions & 0 deletions Machine_Learning/Handwritten Text Recognition Model/app.py
Original file line number Diff line number Diff line change
@@ -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")

Original file line number Diff line number Diff line change
@@ -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
Sameeratweb marked this conversation as resolved.
Show resolved Hide resolved
streamlit_drawable_canvas==0.9.3
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.