add app.py to git
Browse files- app.py +74 -0
- requirements.txt +7 -0
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import numpy as np
|
3 |
+
import streamlit as st
|
4 |
+
import easyocr
|
5 |
+
import PIL
|
6 |
+
from PIL import Image, ImageDraw
|
7 |
+
|
8 |
+
def rectangle(image, result):
|
9 |
+
# https://www.blog.pythonlibrary.org/2021/02/23/drawing-shapes-on-images-with-python-and-pillow/
|
10 |
+
""" draw rectangles on image based on predicted coordinates"""
|
11 |
+
draw = ImageDraw.Draw(image)
|
12 |
+
for res in result:
|
13 |
+
top_left = tuple(res[0][0]) # top left coordinates as tuple
|
14 |
+
bottom_right = tuple(res[0][2]) # bottom right coordinates as tuple
|
15 |
+
draw.rectangle((top_left, bottom_right), outline="blue", width=2)
|
16 |
+
#display image on streamlit
|
17 |
+
st.image(image)
|
18 |
+
|
19 |
+
|
20 |
+
# main title
|
21 |
+
st.title("Get text from image with EasyOCR")
|
22 |
+
|
23 |
+
# subtitle
|
24 |
+
st.markdown("## EasyOCR with Streamlit")
|
25 |
+
|
26 |
+
# upload image file
|
27 |
+
file = st.file_uploader(label = "Upload Here", type=['png', 'jpg', 'jpeg'])
|
28 |
+
|
29 |
+
#read the csv file and display the dataframe
|
30 |
+
if file is not None:
|
31 |
+
image = Image.open(file) # read image with PIL library
|
32 |
+
st.image(image) #display
|
33 |
+
|
34 |
+
# it will only detect the English and Turkish part of the image as text
|
35 |
+
reader = easyocr.Reader(['fa','ar'], gpu=False)
|
36 |
+
result = reader.readtext(np.array(image)) # turn image to numpy array
|
37 |
+
|
38 |
+
# Add a placeholder
|
39 |
+
# latest_iteration = st.empty()
|
40 |
+
# bar = st.progress(0)
|
41 |
+
|
42 |
+
# for i in range(100):
|
43 |
+
# Update the progress bar with each iteration.
|
44 |
+
# latest_iteration.text(f'Iteration {i+1}')
|
45 |
+
# bar.progress(i + 1)
|
46 |
+
# time.sleep(0.1)
|
47 |
+
|
48 |
+
# print all predicted text:
|
49 |
+
for idx in range(len(result)):
|
50 |
+
pred_text = result[idx][1]
|
51 |
+
st.write(pred_text)
|
52 |
+
|
53 |
+
# collect the results in the dictionary:
|
54 |
+
textdic_easyocr = {}
|
55 |
+
for idx in range(len(result)):
|
56 |
+
pred_coor = result[idx][0]
|
57 |
+
pred_text = result[idx][1]
|
58 |
+
pred_confidence = result[idx][2]
|
59 |
+
textdic_easyocr[pred_text] = {}
|
60 |
+
textdic_easyocr[pred_text]['pred_confidence'] = pred_confidence
|
61 |
+
|
62 |
+
# create a data frame which shows the predicted text and prediction confidence
|
63 |
+
df = pd.DataFrame.from_dict(textdic_easyocr).T
|
64 |
+
st.table(df)
|
65 |
+
|
66 |
+
# get boxes on the image
|
67 |
+
rectangle(image, result)
|
68 |
+
|
69 |
+
st.spinner(text="In progress...")
|
70 |
+
|
71 |
+
else:
|
72 |
+
st.write("Upload your image")
|
73 |
+
|
74 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Pillow==9.4.0
|
2 |
+
streamlit==1.20.0
|
3 |
+
easyocr==1.6.2
|
4 |
+
pandas
|
5 |
+
numpy==1.24.2
|
6 |
+
matplotlib==3.7.1
|
7 |
+
altair<5
|