Mattral commited on
Commit
4cb29e5
·
verified ·
1 Parent(s): ebbf1e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -43
app.py CHANGED
@@ -4,69 +4,73 @@ 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(['my','en'], 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")
 
4
  import easyocr
5
  import PIL
6
  from PIL import Image, ImageDraw
7
+ from streamlit_drawable_canvas import st_canvas
8
 
9
  def rectangle(image, result):
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
+ # Main title
 
20
  st.title("Get text from image with EasyOCR")
21
 
22
+ # Subtitle
23
  st.markdown("## EasyOCR with Streamlit")
24
 
25
+ # Upload image file or draw
26
+ st.markdown("## Upload an Image or Draw")
27
+ col1, col2 = st.columns(2)
28
+
29
+ with col1:
30
+ file = st.file_uploader("Upload Here", type=['png', 'jpg', 'jpeg'])
31
 
32
+ with col2:
33
+ # Drawable canvas
34
+ canvas_result = st_canvas(
35
+ fill_color="rgba(255, 165, 0, 0.3)",
36
+ stroke_width=3,
37
+ stroke_color="#ffffff",
38
+ background_color="#000000",
39
+ background_image=None if file else st.session_state.get("background", None),
40
+ update_streamlit=True,
41
+ width=400,
42
+ height=400,
43
+ drawing_mode="freedraw",
44
+ key="canvas",
45
+ )
46
+
47
+ # Process uploaded image or drawing
48
  if file is not None:
49
+ image = Image.open(file) # Read image with PIL library
50
+ elif canvas_result.image_data is not None:
51
+ image = Image.fromarray(canvas_result.image_data.astype('uint8'), 'RGBA').convert('RGB')
52
+ else:
53
+ st.write("Please upload an image or use the canvas to draw.")
54
+ image = None
55
 
56
+ if image is not None:
57
+ st.image(image) # Display
 
58
 
59
+ # Only detect the English and Turkish part of the image as text
60
+ reader = easyocr.Reader(['en','my'], gpu=False)
61
+ result = reader.readtext(np.array(image)) # Turn image to numpy array
 
 
 
 
 
 
62
 
63
+ # Print all predicted text:
64
+ for idx, res in enumerate(result):
65
+ pred_text = res[1]
66
  st.write(pred_text)
67
 
68
+ # Collect the results in the dictionary:
69
+ textdic_easyocr = {res[1]: {'pred_confidence': res[2]} for res in result}
 
 
 
 
 
 
70
 
71
+ # Create a data frame which shows the predicted text and prediction confidence
72
  df = pd.DataFrame.from_dict(textdic_easyocr).T
73
  st.table(df)
74
 
75
+ # Get boxes on the image
76
  rectangle(image, result)