Update streamlit_app.py
Browse files- streamlit_app.py +53 -53
streamlit_app.py
CHANGED
@@ -1,53 +1,53 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import cv2
|
3 |
-
import numpy as np
|
4 |
-
import tempfile
|
5 |
-
from PIL import Image
|
6 |
-
from ultralytics import YOLO
|
7 |
-
|
8 |
-
def process_lines(image_path):
|
9 |
-
thickness = 3
|
10 |
-
|
11 |
-
image = cv2.imread(image_path)
|
12 |
-
result = image.copy()
|
13 |
-
|
14 |
-
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
15 |
-
|
16 |
-
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
|
17 |
-
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=80, minLineLength=40, maxLineGap=40)
|
18 |
-
line_mask = np.zeros_like(gray)
|
19 |
-
|
20 |
-
if lines is not None:
|
21 |
-
for line in lines:
|
22 |
-
x1, y1, x2, y2 = line[0]
|
23 |
-
cv2.line(line_mask, (x1, y1), (x2, y2), (255, 255, 255), thickness=3)
|
24 |
-
|
25 |
-
return line_mask
|
26 |
-
|
27 |
-
def detect_text(image_path):
|
28 |
-
model = YOLO(
|
29 |
-
results = model.predict(image_path)
|
30 |
-
annotated_image = results[0].plot()
|
31 |
-
return annotated_image
|
32 |
-
|
33 |
-
st.title("Line and Text Extraction")
|
34 |
-
st.sidebar.header("Upload an Image")
|
35 |
-
|
36 |
-
uploaded_file = st.sidebar.file_uploader("Choose an image file", type=["png", "jpg", "jpeg", "tif"])
|
37 |
-
|
38 |
-
if st.sidebar.button("Process Image"):
|
39 |
-
if uploaded_file is not None:
|
40 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=uploaded_file.name) as temp_file:
|
41 |
-
temp_file.write(uploaded_file.read())
|
42 |
-
temp_file_path = temp_file.name
|
43 |
-
|
44 |
-
line_mask = process_lines(temp_file_path)
|
45 |
-
text_extracted=detect_text(temp_file_path)
|
46 |
-
|
47 |
-
st.subheader("Line Mask")
|
48 |
-
st.image(line_mask, channels="GRAY")
|
49 |
-
|
50 |
-
st.subheader("Text Detection")
|
51 |
-
st.image(cv2.cvtColor(text_extracted, cv2.COLOR_BGR2RGB))
|
52 |
-
else:
|
53 |
-
st.sidebar.error("Please upload an image file before clicking 'Process Image'.")
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import tempfile
|
5 |
+
from PIL import Image
|
6 |
+
from ultralytics import YOLO
|
7 |
+
|
8 |
+
def process_lines(image_path):
|
9 |
+
thickness = 3
|
10 |
+
|
11 |
+
image = cv2.imread(image_path)
|
12 |
+
result = image.copy()
|
13 |
+
|
14 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
15 |
+
|
16 |
+
edges = cv2.Canny(gray, 50, 150, apertureSize=3)
|
17 |
+
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, threshold=80, minLineLength=40, maxLineGap=40)
|
18 |
+
line_mask = np.zeros_like(gray)
|
19 |
+
|
20 |
+
if lines is not None:
|
21 |
+
for line in lines:
|
22 |
+
x1, y1, x2, y2 = line[0]
|
23 |
+
cv2.line(line_mask, (x1, y1), (x2, y2), (255, 255, 255), thickness=3)
|
24 |
+
|
25 |
+
return line_mask
|
26 |
+
|
27 |
+
def detect_text(image_path):
|
28 |
+
model = YOLO("best 3.pt")
|
29 |
+
results = model.predict(image_path)
|
30 |
+
annotated_image = results[0].plot()
|
31 |
+
return annotated_image
|
32 |
+
|
33 |
+
st.title("Line and Text Extraction")
|
34 |
+
st.sidebar.header("Upload an Image")
|
35 |
+
|
36 |
+
uploaded_file = st.sidebar.file_uploader("Choose an image file", type=["png", "jpg", "jpeg", "tif"])
|
37 |
+
|
38 |
+
if st.sidebar.button("Process Image"):
|
39 |
+
if uploaded_file is not None:
|
40 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=uploaded_file.name) as temp_file:
|
41 |
+
temp_file.write(uploaded_file.read())
|
42 |
+
temp_file_path = temp_file.name
|
43 |
+
|
44 |
+
line_mask = process_lines(temp_file_path)
|
45 |
+
text_extracted=detect_text(temp_file_path)
|
46 |
+
|
47 |
+
st.subheader("Line Mask")
|
48 |
+
st.image(line_mask, channels="GRAY")
|
49 |
+
|
50 |
+
st.subheader("Text Detection")
|
51 |
+
st.image(cv2.cvtColor(text_extracted, cv2.COLOR_BGR2RGB))
|
52 |
+
else:
|
53 |
+
st.sidebar.error("Please upload an image file before clicking 'Process Image'.")
|