Spaces:
Runtime error
Runtime error
Commit
·
8f25cfa
1
Parent(s):
66cafe3
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import PIL
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import torch
|
| 7 |
+
import easyocr
|
| 8 |
+
from matplotlib import pyplot as plt
|
| 9 |
+
|
| 10 |
+
# main title
|
| 11 |
+
st.title("Get text from image with EasyOCR")
|
| 12 |
+
|
| 13 |
+
# subtitle
|
| 14 |
+
st.markdown("## EasyOCRR with Streamlit")
|
| 15 |
+
|
| 16 |
+
# upload image file
|
| 17 |
+
image = st.file_uploader(label = "Upload your image", type=['png', 'jpg', 'jpeg'])
|
| 18 |
+
|
| 19 |
+
#read the csv file and display the dataframe
|
| 20 |
+
if file is not None:
|
| 21 |
+
image = Image.open(file) # read image with PIL library
|
| 22 |
+
st.image(image) #display
|
| 23 |
+
|
| 24 |
+
# it will only detect the English and Turkish part of the image as text
|
| 25 |
+
reader = easyocr.Reader(['tr','en'], gpu=False)
|
| 26 |
+
result = reader.readtext(np.array(image)) # turn image to numpy array
|
| 27 |
+
|
| 28 |
+
# collect the results in dictionary:
|
| 29 |
+
textdic_easyocr = {}
|
| 30 |
+
for idx in range(len(result)):
|
| 31 |
+
pred_coor = result[idx][0]
|
| 32 |
+
pred_text = result[idx][1]
|
| 33 |
+
pred_confidence = result[idx][2]
|
| 34 |
+
textdic_easyocr[pred_text] = {}
|
| 35 |
+
textdic_easyocr[pred_text]['pred_confidence'] = pred_confidence
|
| 36 |
+
|
| 37 |
+
df = pd.DataFrame.from_dict(textdic_easyocr).T
|
| 38 |
+
|
| 39 |
+
st.table(df)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
ax1.plot(agg_df.year, agg_df.rating)
|
| 43 |
+
st.pyplot(fig1)
|
| 44 |
+
|
| 45 |
+
for res in result:
|
| 46 |
+
top_left = tuple(res[0][0]) # top left coordinates as tuple
|
| 47 |
+
bottom_right = tuple(res[0][2]) # bottom right coordinates as tuple
|
| 48 |
+
# draw rectangle on image, 2 is thickness
|
| 49 |
+
cv2.rectangle(image, top_left, bottom_right, (0, 255, 0), 2)
|
| 50 |
+
# write recognized text on image (top_left) minus 10 pixel on y
|
| 51 |
+
cv2.putText(image, res[1], (top_left[0], top_left[1]-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
|
| 52 |
+
fig1, ax1 = plt.subplots()
|
| 53 |
+
plt.imshow(image)
|
| 54 |
+
plt.show()
|
| 55 |
+
|
| 56 |
+
else:
|
| 57 |
+
st.write("Upload your image")
|