Skip to content

Commit

Permalink
Merge pull request #1565 from Sameeratweb/handwritten-text-detection#…
Browse files Browse the repository at this point in the history
…1560

Added a machine learning project that detects handwritten texts
  • Loading branch information
sanjay-kv authored Oct 30, 2024
2 parents a180377 + c99479d commit 1ce6852
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
44 changes: 44 additions & 0 deletions 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 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")

13 changes: 13 additions & 0 deletions Handwritten Text Recognition Model/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
easyocr==1.7.1
googletrans==4.0.0rc1
imutils==0.5.4
langdetect==1.0.9
numpy==2.1.2
opencv_contrib_python==4.10.0.84
opencv_python==4.10.0.82
opencv_python_headless==4.10.0.84
Pillow==11.0.0
scipy==1.11.4
streamlit==1.30.0
streamlit==1.36.0
streamlit_drawable_canvas==0.9.3

0 comments on commit 1ce6852

Please sign in to comment.