hdeldar commited on
Commit
38cd73f
1 Parent(s): bab94a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -50
app.py CHANGED
@@ -4,6 +4,8 @@ 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/
@@ -17,58 +19,126 @@ def rectangle(image, result):
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import easyocr
5
  import PIL
6
  from PIL import Image, ImageDraw
7
+ from captcha.image import ImageCaptcha
8
+ import random, string
9
 
10
  def rectangle(image, result):
11
  # https://www.blog.pythonlibrary.org/2021/02/23/drawing-shapes-on-images-with-python-and-pillow/
 
19
  st.image(image)
20
 
21
 
22
+
23
+
24
+
25
+ # define the costant
26
+ length_captcha = 4
27
+ width = 200
28
+ height = 150
29
+
30
+ # define the function for the captcha control
31
+ def captcha_control():
32
+ #control if the captcha is correct
33
+ if 'controllo' not in st.session_state or st.session_state['controllo'] == False:
34
+ st.title("Captcha Control on OCR")
35
+
36
+ # define the session state for control if the captcha is correct
37
+ st.session_state['controllo'] = False
38
+ col1, col2 = st.columns(2)
39
+
40
+ # define the session state for the captcha text because it doesn't change during refreshes
41
+ if 'Captcha' not in st.session_state:
42
+ st.session_state['Captcha'] = ''.join(random.choices(string.ascii_uppercase + string.digits, k=length_captcha))
43
+ print("the captcha is: ", st.session_state['Captcha'])
44
+
45
+ #setup the captcha widget
46
+ image = ImageCaptcha(width=width, height=height)
47
+ data = image.generate(st.session_state['Captcha'])
48
+ col1.image(data)
49
+ capta2_text = col2.text_area('Enter captcha text', height=30)
50
+
51
+
52
+ if st.button("Verify the code"):
53
+ print(capta2_text, st.session_state['Captcha'])
54
+ capta2_text = capta2_text.replace(" ", "")
55
+ # if the captcha is correct, the controllo session state is set to True
56
+ if st.session_state['Captcha'].lower() == capta2_text.lower().strip():
57
+ del st.session_state['Captcha']
58
+ col1.empty()
59
+ col2.empty()
60
+ st.session_state['controllo'] = True
61
+ st.experimental_rerun()
62
+ else:
63
+ # if the captcha is wrong, the controllo session state is set to False and the captcha is regenerated
64
+ st.error("🚨 Error on Captcha...")
65
+ del st.session_state['Captcha']
66
+ del st.session_state['controllo']
67
+ st.experimental_rerun()
68
+ else:
69
+ #wait for the button click
70
+ st.stop()
71
+
72
+
73
+
74
+
75
  # main title
76
+ st.title("Get text from image with Persian and Arabic OCR")
77
 
78
  # subtitle
79
+ st.markdown("## Persian and Arabic OCR :")
80
+ #try_again = 0
81
+
82
+
83
+ def main():
84
+ # upload image file
85
+ file = st.file_uploader(label = "Upload Here", type=['png', 'jpg', 'jpeg'])
86
+ # global try_again
87
+ # if try_again == 1:
88
+ # del st.session_state['controllo']
89
+ # st.experimental_rerun()
90
+ # try_again = 1
91
+ #read the csv file and display the dataframe
92
+ if file is not None:
93
+ image = Image.open(file) # read image with PIL library
94
+ st.image(image) #display
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
+ # it will only detect the English and Turkish part of the image as text
97
+ reader = easyocr.Reader(['fa','ar'], gpu=False) #, model_storage_directory='temp/',user_network_directory='temp/net'
98
+ result = reader.readtext(np.array(image)) # turn image to numpy array
99
 
100
+ # Add a placeholder
101
+ # latest_iteration = st.empty()
102
+ # bar = st.progress(0)
103
+
104
+ # for i in range(100):
105
+ # Update the progress bar with each iteration.
106
+ # latest_iteration.text(f'Iteration {i+1}')
107
+ # bar.progress(i + 1)
108
+ # time.sleep(0.1)
109
+
110
+ # print all predicted text:
111
+ for idx in range(len(result)):
112
+ pred_text = result[idx][1]
113
+ st.write(pred_text)
114
+
115
+ # collect the results in the dictionary:
116
+ textdic_easyocr = {}
117
+ for idx in range(len(result)):
118
+ pred_coor = result[idx][0]
119
+ pred_text = result[idx][1]
120
+ pred_confidence = result[idx][2]
121
+ textdic_easyocr[pred_text] = {}
122
+ textdic_easyocr[pred_text]['pred_confidence'] = pred_confidence
123
+
124
+ # create a data frame which shows the predicted text and prediction confidence
125
+ df = pd.DataFrame.from_dict(textdic_easyocr).T
126
+ st.table(df)
127
+
128
+ # get boxes on the image
129
+ rectangle(image, result)
130
+
131
+ st.spinner(text="In progress...")
132
+
133
+ else:
134
+ st.write("Upload your image")
135
+
136
+
137
+
138
+
139
+
140
+ # WORK LIKE MULTIPAGE APP
141
+ if 'controllo' not in st.session_state or st.session_state['controllo'] == False:
142
+ captcha_control()
143
+ else:
144
+ main()