-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
95 lines (85 loc) · 3.38 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import cv2
import streamlit as st
import helper
import numpy as np
import tempfile
from pathlib import Path
import sys
# Get the absolute path of the current file
file_path = Path(__file__).resolve()
# Get the parent directory of the current file
root_path = file_path.parent
# Add the root path to the sys.path list if it is not already there
if root_path not in sys.path:
sys.path.append(str(root_path))
# Get the relative path of the root directory with respect to the current working directory
ROOT = root_path.relative_to(Path.cwd())
st.set_page_config(
page_title="Crack Detection using SSD_MobileNetV2",
page_icon="🤖",
layout="wide",
initial_sidebar_state="expanded"
)
st.title("Crack Detection using SSD_MobileNetV2")
st.sidebar.header("Settings")
source_radio = st.sidebar.radio(
"Select Source", ['Image', 'Video'])
confidence = float(st.sidebar.slider(
"Select Detection Confidence", 25, 100, 50)) / 100
model_path = str(ROOT / 'saved_model')
print('model path', model_path)
# Load Pre-trained ML Model
try:
model = helper.load_model(model_path)
except Exception as ex:
st.error(f"Unable to load model. Check the specified path: {model_path}")
st.error(ex)
if source_radio == 'Image':
source_img = st.sidebar.file_uploader(
"Choose an image...", type=("jpg", "jpeg", "png", 'bmp', 'webp'))
# print('soruce image', source_img)
col1, col2 = st.columns(2)
with col1:
# try:
# if source_img is None:
# default_image_path = str(ROOT / 'test.jpg')
# source_img = cv2.imread(default_image_path)
# st.image(source_img, caption="Default Image",
# use_column_width=True)
# uploaded_image = source_img
if source_img:
file_bytes = np.asarray(bytearray(source_img.read()), dtype=np.uint8)
uploaded_image = cv2.imdecode(file_bytes, 1)
# uploaded_image = cv2.imread(source_img)
st.image(uploaded_image, caption="Uploaded Image",
use_column_width=True)
# except Exception as ex:
# st.error("Error occurred while opening the image.")
# st.error(ex)
with col2:
if st.sidebar.button('Detect Objects'):
result_img = helper.result_image(confidence, model, uploaded_image)
st.image(result_img, caption='Detected Image',
use_column_width=True)
elif source_radio == 'Video':
f = st.sidebar.file_uploader("Upload file",type=("mp4", "avi", "mpg"))
tfile = tempfile.NamedTemporaryFile(delete=False)
if f:
tfile.write(f.read())
# if tfile:
st.video(tfile.name)
if st.sidebar.button('Detect Objects'):
try:
vid_cap = cv2.VideoCapture(tfile.name)
st_frame = st.empty()
while (vid_cap.isOpened()):
success, image = vid_cap.read()
if success:
result_img = helper.result_image(confidence, model, image)
st_frame.image(result_img, caption='Detected Video',
use_column_width=True)
else:
vid_cap.release()
break
except Exception as e:
st.sidebar.error("Error loading video: " + str(e))