Skip to content

Commit c99479d

Browse files
committed
Added a machine learning project that detects handwritten texts
1 parent 7e3d099 commit c99479d

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
# Handwritten Text Recognition Model
3+
4+
This code creates a Handwritten Text Recognition app using Machine Learning.
5+
6+
The app provides two main functionalities
7+
8+
1. Draw: Users can draw on a canvas, and the app will detect and display any text from the drawing.
9+
2. Upload Image: Users can upload an image, and the app will detect and display any text present in the uploaded image.
10+
11+
12+
## Screenshots
13+
14+
![App Screenshot](results/OP5.png)
15+
![App Screenshot](results/OP7.png)
16+
![App Screenshot](results/OP2.png)
17+
18+
19+
20+
21+
## Tech Stack
22+
23+
**Languages:** Python
24+
25+
**Libraries:** easyocr, opencv, numpy, scipy etc
26+
27+
**Framework:** Streamlit
28+
29+
30+
## Run Locally
31+
32+
Install dependencies
33+
34+
```bash
35+
pip install -r requirements.txt
36+
```
37+
38+
Start the server
39+
40+
```bash
41+
streamlit run app.py
42+
```
43+
44+
+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
easyocr==1.7.1
2+
googletrans==4.0.0rc1
3+
imutils==0.5.4
4+
langdetect==1.0.9
5+
numpy==2.1.2
6+
opencv_contrib_python==4.10.0.84
7+
opencv_python==4.10.0.82
8+
opencv_python_headless==4.10.0.84
9+
Pillow==11.0.0
10+
scipy==1.11.4
11+
streamlit==1.30.0
12+
streamlit==1.36.0
13+
streamlit_drawable_canvas==0.9.3

0 commit comments

Comments
 (0)