Mattral commited on
Commit
8973c03
·
verified ·
1 Parent(s): 9965632

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -28
app.py CHANGED
@@ -4,34 +4,6 @@ import streamlit as st
4
  import easyocr
5
  import PIL
6
  from PIL import Image, ImageDraw
7
- from matplotlib import pyplot as plt
8
-
9
- # main title
10
- st.title("Get text from image with EasyOCR")
11
- # subtitle
12
- st.markdown("## EasyOCRR with Streamlit")
13
-
14
- # upload image file
15
- file = st.file_uploader(label = "Upload your image", type=['png', 'jpg', 'jpeg'])
16
-
17
- image = Image.open(file) # read image with PIL library
18
- st.image(image) #display
19
-
20
- # it will only detect the English and Turkish part of the image as text
21
- reader = easyocr.Reader(['my','en'], gpu=False)
22
- result = reader.readtext(np.array(image)) # turn image to numpy array
23
-
24
- textdic_easyocr = {}
25
- for idx in range(len(result)):
26
- pred_coor = result[idx][0]
27
- pred_text = result[idx][1]
28
- pred_confidence = result[idx][2]
29
- textdic_easyocr[pred_text] = {}
30
- textdic_easyocr[pred_text]['pred_confidence'] = pred_confidence
31
-
32
- # create a dataframe which shows the predicted text and prediction confidence
33
- df = pd.DataFrame.from_dict(textdic_easyocr).T
34
- st.table(df)
35
 
36
  def rectangle(image, result):
37
  # https://www.blog.pythonlibrary.org/2021/02/23/drawing-shapes-on-images-with-python-and-pillow/
@@ -44,3 +16,57 @@ def rectangle(image, result):
44
  #display image on streamlit
45
  st.image(image)
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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/
 
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(['en','my'], 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")