optical character recognition using EasyOCR
Browse files- EasyOCR/model/craft_mlt_25k.pth +3 -0
- EasyOCR/model/english_g2.pth +3 -0
- Images/sample_image_1.jpg +0 -0
- app.py +61 -0
- requirements.txt +75 -0
- utils.py +110 -0
EasyOCR/model/craft_mlt_25k.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4a5efbfb48b4081100544e75e1e2b57f8de3d84f213004b14b85fd4b3748db17
|
| 3 |
+
size 83152330
|
EasyOCR/model/english_g2.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e2272681d9d67a04e2dff396b6e95077bc19001f8f6d3593c307b9852e1c29e8
|
| 3 |
+
size 15143997
|
Images/sample_image_1.jpg
ADDED
|
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import numpy as np
|
| 3 |
+
import streamlit as st
|
| 4 |
+
from PIL import Image, ImageOps
|
| 5 |
+
import cv2
|
| 6 |
+
from utils import OCR
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
alert = False
|
| 10 |
+
ocr = None
|
| 11 |
+
|
| 12 |
+
st.title("Optical Character Recognition")
|
| 13 |
+
|
| 14 |
+
tab_upload, tab_cam = st.tabs(['Upload', 'Camera'])
|
| 15 |
+
|
| 16 |
+
with tab_upload:
|
| 17 |
+
image_upload = st.file_uploader(
|
| 18 |
+
label='Upload the Image', type=['jpg', 'jpeg', 'png'])
|
| 19 |
+
|
| 20 |
+
with tab_cam:
|
| 21 |
+
image_webcam = st.camera_input(
|
| 22 |
+
label="Take a picture 📷")
|
| 23 |
+
if image_upload:
|
| 24 |
+
image = Image.open(image_upload)
|
| 25 |
+
elif image_webcam:
|
| 26 |
+
image = Image.open(image_webcam)
|
| 27 |
+
else:
|
| 28 |
+
image = Image.open('./Images/sample_image_1.jpg')
|
| 29 |
+
|
| 30 |
+
st.image(image=ImageOps.scale(image, factor=0.2))
|
| 31 |
+
|
| 32 |
+
if st.button('Detect'):
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
pass
|
| 36 |
+
ocr = OCR(image=image)
|
| 37 |
+
|
| 38 |
+
except:
|
| 39 |
+
|
| 40 |
+
st.warning(
|
| 41 |
+
" Please use a different image.", icon="⚠")
|
| 42 |
+
alert = True
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
if ocr:
|
| 46 |
+
st.caption("✨Result")
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
st.pyplot(fig=ocr.detection(), use_container_width=True)
|
| 50 |
+
except:
|
| 51 |
+
st.warning(
|
| 52 |
+
" Please use a different image", icon="⚠")
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
else:
|
| 56 |
+
st.caption('Just click the Detect button')
|
| 57 |
+
if alert:
|
| 58 |
+
st.warning(
|
| 59 |
+
" Please use a different image.", icon="⚠")
|
| 60 |
+
|
| 61 |
+
st.image(image)
|
requirements.txt
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
altair==5.0.1
|
| 2 |
+
attrs==23.1.0
|
| 3 |
+
blinker==1.6.2
|
| 4 |
+
cachetools==5.3.1
|
| 5 |
+
certifi==2023.7.22
|
| 6 |
+
charset-normalizer==3.2.0
|
| 7 |
+
click==8.1.6
|
| 8 |
+
colorama==0.4.6
|
| 9 |
+
contourpy==1.1.0
|
| 10 |
+
cycler==0.11.0
|
| 11 |
+
decorator==5.1.1
|
| 12 |
+
easyocr==1.7.0
|
| 13 |
+
filelock==3.12.2
|
| 14 |
+
fonttools==4.42.0
|
| 15 |
+
gitdb==4.0.10
|
| 16 |
+
GitPython==3.1.32
|
| 17 |
+
idna==3.4
|
| 18 |
+
imageio==2.31.1
|
| 19 |
+
importlib-metadata==6.8.0
|
| 20 |
+
Jinja2==3.1.2
|
| 21 |
+
jsonschema==4.19.0
|
| 22 |
+
jsonschema-specifications==2023.7.1
|
| 23 |
+
kiwisolver==1.4.4
|
| 24 |
+
lazy_loader==0.3
|
| 25 |
+
markdown-it-py==3.0.0
|
| 26 |
+
MarkupSafe==2.1.3
|
| 27 |
+
matplotlib==3.7.2
|
| 28 |
+
mdurl==0.1.2
|
| 29 |
+
mpmath==1.3.0
|
| 30 |
+
networkx==3.1
|
| 31 |
+
ninja==1.11.1
|
| 32 |
+
numpy==1.25.2
|
| 33 |
+
opencv-python-headless==4.8.0.74
|
| 34 |
+
packaging==23.1
|
| 35 |
+
pandas==2.0.3
|
| 36 |
+
Pillow==9.5.0
|
| 37 |
+
protobuf==4.23.4
|
| 38 |
+
pyarrow==12.0.1
|
| 39 |
+
pyclipper==1.3.0.post4
|
| 40 |
+
pydeck==0.8.0
|
| 41 |
+
Pygments==2.16.1
|
| 42 |
+
Pympler==1.0.1
|
| 43 |
+
pyparsing==3.0.9
|
| 44 |
+
python-bidi==0.4.2
|
| 45 |
+
python-dateutil==2.8.2
|
| 46 |
+
pytz==2023.3
|
| 47 |
+
pytz-deprecation-shim==0.1.0.post0
|
| 48 |
+
PyWavelets==1.4.1
|
| 49 |
+
PyYAML==6.0.1
|
| 50 |
+
referencing==0.30.2
|
| 51 |
+
requests==2.31.0
|
| 52 |
+
rich==13.5.2
|
| 53 |
+
rpds-py==0.9.2
|
| 54 |
+
scikit-image==0.21.0
|
| 55 |
+
scipy==1.11.1
|
| 56 |
+
shapely==2.0.1
|
| 57 |
+
six==1.16.0
|
| 58 |
+
smmap==5.0.0
|
| 59 |
+
streamlit==1.25.0
|
| 60 |
+
sympy==1.12
|
| 61 |
+
tenacity==8.2.2
|
| 62 |
+
tifffile==2023.7.18
|
| 63 |
+
toml==0.10.2
|
| 64 |
+
toolz==0.12.0
|
| 65 |
+
torch==2.0.1
|
| 66 |
+
torchaudio==2.0.2
|
| 67 |
+
torchvision==0.15.2
|
| 68 |
+
tornado==6.3.2
|
| 69 |
+
typing_extensions==4.7.1
|
| 70 |
+
tzdata==2023.3
|
| 71 |
+
tzlocal==4.3.1
|
| 72 |
+
urllib3==2.0.4
|
| 73 |
+
validators==0.20.0
|
| 74 |
+
watchdog==3.0.0
|
| 75 |
+
zipp==3.16.2
|
utils.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import matplotlib.pyplot as plt
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
import easyocr
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
class OCR:
|
| 8 |
+
def __init__(self, image) -> None:
|
| 9 |
+
self.image = image
|
| 10 |
+
self.reader = easyocr.Reader(
|
| 11 |
+
lang_list=['en'],
|
| 12 |
+
gpu=False,
|
| 13 |
+
model_storage_directory='./EasyOCR/model/',
|
| 14 |
+
download_enabled=False,
|
| 15 |
+
user_network_directory='./EasyOCR/user_network/'
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
def detection(self):
|
| 19 |
+
img_arr = np.array(self.image, dtype=np.uint8)
|
| 20 |
+
response = self.reader.readtext(image=img_arr)
|
| 21 |
+
plot_inputs = []
|
| 22 |
+
|
| 23 |
+
for box, text, conf in response:
|
| 24 |
+
plot_inputs.append((text, np.array(box, dtype=np.float32)))
|
| 25 |
+
|
| 26 |
+
fig, ax = plt.subplots(nrows=1, ncols=1)
|
| 27 |
+
plot = self.drawAnnotations(
|
| 28 |
+
image=img_arr, predictions=plot_inputs, ax=ax)
|
| 29 |
+
return fig
|
| 30 |
+
|
| 31 |
+
def drawAnnotations(self, image, predictions, ax=None):
|
| 32 |
+
"""Draw text annotations onto image.
|
| 33 |
+
|
| 34 |
+
Args:
|
| 35 |
+
image: The image on which to draw
|
| 36 |
+
predictions: The predictions as provided by `pipeline.recognize`.
|
| 37 |
+
ax: A matplotlib axis on which to draw.
|
| 38 |
+
"""
|
| 39 |
+
if ax is None:
|
| 40 |
+
_, ax = plt.subplots()
|
| 41 |
+
ax.imshow(self.drawBoxes(image=image, boxes=predictions,
|
| 42 |
+
boxes_format="predictions"))
|
| 43 |
+
predictions = sorted(predictions, key=lambda p: p[1][:, 1].min())
|
| 44 |
+
left = []
|
| 45 |
+
right = []
|
| 46 |
+
for word, box in predictions:
|
| 47 |
+
if box[:, 0].min() < image.shape[1] / 2:
|
| 48 |
+
left.append((word, box))
|
| 49 |
+
else:
|
| 50 |
+
right.append((word, box))
|
| 51 |
+
ax.set_yticks([])
|
| 52 |
+
ax.set_xticks([])
|
| 53 |
+
for side, group in zip(["left", "right"], [left, right]):
|
| 54 |
+
for index, (text, box) in enumerate(group):
|
| 55 |
+
y = 1 - (index / len(group))
|
| 56 |
+
xy = box[0] / np.array([image.shape[1], image.shape[0]])
|
| 57 |
+
xy[1] = 1 - xy[1]
|
| 58 |
+
ax.annotate(
|
| 59 |
+
text=text,
|
| 60 |
+
xy=xy,
|
| 61 |
+
xytext=(-0.05 if side == "left" else 1.05, y),
|
| 62 |
+
xycoords="axes fraction",
|
| 63 |
+
arrowprops={"arrowstyle": "->", "color": "r"},
|
| 64 |
+
color="r",
|
| 65 |
+
fontsize=14,
|
| 66 |
+
horizontalalignment="right" if side == "left" else "left",
|
| 67 |
+
)
|
| 68 |
+
return ax
|
| 69 |
+
|
| 70 |
+
def drawBoxes(self, image, boxes, color=(255, 0, 0), thickness=1, boxes_format="boxes"):
|
| 71 |
+
"""Draw boxes onto an image.
|
| 72 |
+
|
| 73 |
+
Args:
|
| 74 |
+
image: The image on which to draw the boxes.
|
| 75 |
+
boxes: The boxes to draw.
|
| 76 |
+
color: The color for each box.
|
| 77 |
+
thickness: The thickness for each box.
|
| 78 |
+
boxes_format: The format used for providing the boxes. Options are
|
| 79 |
+
"boxes" which indicates an array with shape(N, 4, 2) where N is the
|
| 80 |
+
number of boxes and each box is a list of four points) as provided
|
| 81 |
+
by `keras_ocr.detection.Detector.detect`, "lines" (a list of
|
| 82 |
+
lines where each line itself is a list of (box, character) tuples) as
|
| 83 |
+
provided by `keras_ocr.data_generation.get_image_generator`,
|
| 84 |
+
or "predictions" where boxes is by itself a list of (word, box) tuples
|
| 85 |
+
as provided by `keras_ocr.pipeline.Pipeline.recognize` or
|
| 86 |
+
`keras_ocr.recognition.Recognizer.recognize_from_boxes`.
|
| 87 |
+
"""
|
| 88 |
+
if len(boxes) == 0:
|
| 89 |
+
return image
|
| 90 |
+
canvas = image.copy()
|
| 91 |
+
if boxes_format == "lines":
|
| 92 |
+
revised_boxes = []
|
| 93 |
+
for line in boxes:
|
| 94 |
+
for box, _ in line:
|
| 95 |
+
revised_boxes.append(box)
|
| 96 |
+
boxes = revised_boxes
|
| 97 |
+
if boxes_format == "predictions":
|
| 98 |
+
revised_boxes = []
|
| 99 |
+
for _, box in boxes:
|
| 100 |
+
revised_boxes.append(box)
|
| 101 |
+
boxes = revised_boxes
|
| 102 |
+
for box in boxes:
|
| 103 |
+
cv2.polylines(
|
| 104 |
+
img=canvas,
|
| 105 |
+
pts=box[np.newaxis].astype("int32"),
|
| 106 |
+
color=color,
|
| 107 |
+
thickness=thickness,
|
| 108 |
+
isClosed=True,
|
| 109 |
+
)
|
| 110 |
+
return canvas
|