Mattral commited on
Commit
e4c3e4a
·
verified ·
1 Parent(s): 3ebf562

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -21
app.py CHANGED
@@ -1,38 +1,74 @@
1
- from PIL import Image, ImageDraw
2
- import numpy as np
3
  import streamlit as st
4
  from streamlit_drawable_canvas import st_canvas
 
 
5
  import easyocr
6
  import pandas as pd
7
 
8
- def rectangle(image, result):
9
- """Draw rectangles on the image based on predicted coordinates and display the image."""
10
- draw_image = image.copy() # Work on a copy of the image
 
 
 
 
 
 
 
 
11
  draw = ImageDraw.Draw(draw_image)
12
  for res in result:
13
  top_left = tuple(res[0][0])
14
  bottom_right = tuple(res[0][2])
15
- draw.rectangle((top_left, bottom_right), outline="blue", width=2)
16
- st.image(draw_image, caption="Processed Image with Detected Text Highlighted")
 
 
 
 
 
 
 
17
 
18
- # Rest of your script remains unchanged until the final processing:
 
19
 
20
- if image is not None:
21
- st.image(image, caption="Uploaded/Drawn Image")
22
 
23
- # Optional: Indicate that processing is happening
24
- with st.spinner('Processing...'):
25
- reader = easyocr.Reader(['en'], gpu=False) # Consider moving this outside the loop if performance is a concern
26
- result = reader.readtext(np.array(image))
27
 
28
- for idx, res in enumerate(result):
29
- pred_text = res[1]
30
- st.write(pred_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
- textdic_easyocr = {res[1]: {'pred_confidence': res[2]} for res in result}
33
- df = pd.DataFrame.from_dict(textdic_easyocr, orient='index', columns=['pred_confidence'])
34
- st.table(df)
35
 
36
- rectangle(image, result)
37
  else:
38
  st.write("Please upload an image or use the canvas to draw.")
 
 
 
1
  import streamlit as st
2
  from streamlit_drawable_canvas import st_canvas
3
+ import numpy as np
4
+ from PIL import Image
5
  import easyocr
6
  import pandas as pd
7
 
8
+ def process_image_for_ocr(image):
9
+ """Converts the image for EasyOCR and draws rectangles around detected text."""
10
+ # Convert PIL image to numpy array
11
+ image_np = np.array(image)
12
+
13
+ # Initialize EasyOCR reader
14
+ reader = easyocr.Reader(['en'], gpu=False) # Add more languages or adjust as needed
15
+ result = reader.readtext(image_np)
16
+
17
+ # Draw rectangles on a copy of the image
18
+ draw_image = image.copy()
19
  draw = ImageDraw.Draw(draw_image)
20
  for res in result:
21
  top_left = tuple(res[0][0])
22
  bottom_right = tuple(res[0][2])
23
+ draw.rectangle([top_left, bottom_right], outline="red", width=2)
24
+
25
+ return draw_image, result
26
+
27
+ def display_text_results(result):
28
+ """Displays text results in Streamlit."""
29
+ textdic_easyocr = {idx: {'Text': res[1], 'Confidence': res[2]} for idx, res in enumerate(result)}
30
+ df = pd.DataFrame.from_dict(textdic_easyocr, orient='index')
31
+ st.table(df)
32
 
33
+ st.title("OCR with EasyOCR and Streamlit")
34
+ st.markdown("Upload an Image or Draw Below")
35
 
36
+ # Column layout for uploader and canvas
37
+ col1, col2 = st.columns(2)
38
 
39
+ with col1:
40
+ uploaded_file = st.file_uploader("Upload Here", type=['png', 'jpg', 'jpeg'])
 
 
41
 
42
+ with col2:
43
+ # Use the background_image parameter if you want to draw on an uploaded image.
44
+ canvas_result = st_canvas(
45
+ fill_color="rgba(255, 165, 0, 0.3)", # Use a transparent fill color
46
+ stroke_width=3,
47
+ stroke_color="#FFFFFF",
48
+ background_color="#000000",
49
+ width=400,
50
+ height=400,
51
+ drawing_mode="freedraw",
52
+ key="canvas",
53
+ )
54
+
55
+ image = None
56
+
57
+ if uploaded_file is not None:
58
+ image = Image.open(uploaded_file).convert('RGB')
59
+
60
+ elif canvas_result.image_data is not None:
61
+ # Convert the canvas data to an Image
62
+ canvas_image = Image.fromarray((canvas_result.image_data).astype('uint8'), mode='RGBA')
63
+ image = canvas_image.convert('RGB')
64
+
65
+ if image:
66
+ st.image(image, caption="Uploaded/Drawn Image")
67
+
68
+ processed_image, ocr_result = process_image_for_ocr(image)
69
 
70
+ st.image(processed_image, caption="Processed Image with Detected Text")
71
+ display_text_results(ocr_result)
 
72
 
 
73
  else:
74
  st.write("Please upload an image or use the canvas to draw.")