forked from amrrs/image-to-text-python-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocr_easyOCR.py
108 lines (78 loc) · 2.71 KB
/
ocr_easyOCR.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# NCVPRIPG-2024 Auto Eval Challenge
import cv2
import numpy as np #Image Processing
import pandas as pd
import easyocr as ocr #OCR
import streamlit as st #Web App
from PIL import Image #Image Processing
#title
st.title("NCVPRIPG'24: Auto-Eval Challenge")
#subtitle
st.markdown("## Optical Character Recognition - Using `easyocr`, `streamlit`")
st.markdown("")
#image uploader
image = st.file_uploader(label = "Please upload your image here",type=['png','jpg','jpeg'])
@st.cache
def load_model():
reader = ocr.Reader(['en'])
return reader
reader = load_model() #load model
if image is not None:
input_image = Image.open(image) #read image
st.image(input_image) #display image
with st.spinner("🤖 AI is at Work! "):
# result = reader.readtext(np.array(input_image))
# result_text = [] #empty list for results
# for text in result:
# result_text.append(text[1])
res = reader.readtext(np.array(input_image))
for t in res:
bbox, text, score = t
l_bbox = bbox[0][0]
l_bbox1 = bbox[0][1]
r_bbox = bbox[2][0]
r_bbox1 = bbox[2][1]
img_out = cv2.rectangle(np.array(input_image),
(int(l_bbox), int(l_bbox1)),
(int(r_bbox), int(r_bbox1)),
(255,0,0),5)
st.image(img_out) #display image
all_text = []
for i in range(len(res)):
text = res[i][1]
all_text.append(text)
# st.write(result_text)
st.write(all_text)
#st.success("Here you go!")
all_text = []
lb = []
lb1 = []
rb = []
rb1 = []
prob = []
for t in res:
bbox, text, score = t
l_bbox = bbox[0][0]
l_bbox1 = bbox[0][1]
r_bbox = bbox[2][0]
r_bbox1 = bbox[2][1]
img_out = cv2.rectangle(img, (int(l_bbox), int(l_bbox1)), (int(r_bbox), int(r_bbox1)), (255,0,0),5)
all_text.append(text)
lb.append(l_bbox)
lb1.append(l_bbox1)
rb.append(r_bbox)
rb1.append(r_bbox1)
prob.append(score)
df = pd.DataFrame({'ocr_word' : all_text,
'lb' : lb,
'lb1':lb1,
'rb':rb,
'rb1':rb1,
'prob':prob})
trsld = np.array(input_image).shape[0] * 0.8
df_sub = df[(df['lb'] >= trsld) | (df['rb'] >= trsld)].reset_index(drop = True)
st.write(df_sub)
st.balloons()
else:
st.write("Upload an Image")
st.caption("Made with ❤️ by Team: IIT Jodhpur")