app and reqs
Browse files- app.py +57 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import easyocr
|
2 |
+
import numpy as np
|
3 |
+
import streamlit as st
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
st.set_page_config(layout="wide")
|
7 |
+
|
8 |
+
def write_to_txt(file_path, data):
|
9 |
+
all_text = ""
|
10 |
+
with open(file_path, 'w') as file:
|
11 |
+
for entry in data:
|
12 |
+
_, text, _ = entry
|
13 |
+
# st.write(bounding_box)
|
14 |
+
# x_min, y_min, x_max, y_max = bounding_box
|
15 |
+
|
16 |
+
# Adjust the formatting to align text with bounding box coordinates
|
17 |
+
# file.write(f"Bounding Box: ({x_min}, {y_min}, {x_max}, {y_max})\n")
|
18 |
+
|
19 |
+
# Add spaces to align the text with the bounding box
|
20 |
+
# file.write(f"{' ' * x_min}Text: {text}\n")
|
21 |
+
|
22 |
+
# file.write(f"Text: {text}\n")
|
23 |
+
file.write(f"{text}\n")
|
24 |
+
all_text += text + "\n"
|
25 |
+
return all_text
|
26 |
+
|
27 |
+
def main():
|
28 |
+
st.write("Hello world!")
|
29 |
+
with st.sidebar:
|
30 |
+
st.write("Hello sidebar!")
|
31 |
+
f = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
32 |
+
# st.write(img)
|
33 |
+
if f is not None:
|
34 |
+
# img = np.asarray(bytearray(f.read()), dtype=np.uint8)
|
35 |
+
img = np.array(Image.open(f))
|
36 |
+
# reader = easyocr.Reader(["en", "hi", "mr"])
|
37 |
+
reader = easyocr.Reader(["en"])
|
38 |
+
out = reader.readtext(img)
|
39 |
+
col1, col2 = st.columns(2)
|
40 |
+
with col1:
|
41 |
+
st.image(img, width=300)
|
42 |
+
|
43 |
+
txt = write_to_txt("out.txt", out)
|
44 |
+
with col2:
|
45 |
+
st.write("Found text:")
|
46 |
+
st.markdown(txt)
|
47 |
+
|
48 |
+
# write contents of out to a txt file which matches the format of the input file
|
49 |
+
# out = [(bbox, text, prob), ...]
|
50 |
+
# bbox = [x1, y1, x2, y2, x3, y3, x4, y4]
|
51 |
+
# text = "string"
|
52 |
+
# prob = float
|
53 |
+
# out = [(bbox, text, prob), ...]
|
54 |
+
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
main()
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
easyocr==1.7.1
|
2 |
+
numpy==1.24.4
|
3 |
+
pillow==10.1.0
|
4 |
+
|