Skip to content

Commit

Permalink
update streamlit_app.py
Browse files Browse the repository at this point in the history
  • Loading branch information
geezacoleman committed Sep 26, 2023
1 parent 20c044c commit 52374a4
Showing 1 changed file with 61 additions and 40 deletions.
101 changes: 61 additions & 40 deletions demos/streamlit_app.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import time

import streamlit as st
from owl.utils.io import get_weed_detector, setup_and_run_detector, load_config
from owl.utils import FrameReader
Expand All @@ -16,6 +18,13 @@ def get_file_type(file):
else:
raise ValueError("Unsupported file type")

def update_value():
config = st.session_state.config
config_list = ["conf", "iou", "exgMin", "exgMax", "hueMin", "hueMax", "saturationMin",
"saturationMax", "brightnessMin", "brightnessMax", "minArea"]
for slider in config_list:
config.update({slider: st.session_state[slider]})


def main():
st.set_page_config(layout="wide", page_title="Interactive owl-tools")
Expand All @@ -29,7 +38,8 @@ def main():
st.write("This WebApp is part of the OpenWeedLocator (OWL) project. "
"To build your own OWL device find the code, guide and 3D model files [here](https://github.com/geezacoleman/OpenWeedLocator).")

# Select Algorithm

# select algo
algorithm_key = st.sidebar.selectbox("Select Algorithm", ["ExG", "ExG + HSV", "HSV", "Green-On-Green"])

algorithm_dict = {
Expand Down Expand Up @@ -62,6 +72,7 @@ def main():
submitted = st.form_submit_button("Upload")

st_frame = col_main.empty()
message_box = col_config.empty()

if "logo" not in st.session_state:
response = requests.get(
Expand All @@ -81,45 +92,42 @@ def main():
logo = Image.open(BytesIO(response.content))
st.session_state['logo'] = st.session_state['last_frame'] = logo

st_frame.image(st.session_state.last_frame, width=500)

st_frame.image(st.session_state.last_frame, width=800)
with col_config:
st.write("## Configuration Panel")
with st.form(key='config_form'):
submitted = st.form_submit_button("Update Config")
CONFIG_NAME = st.selectbox("Select Config", ["CONFIG_DAY_SENSITIVITY_1"])
CONFIG_NAME = st.selectbox("Select Config", ["CONFIG_DAY_SENSITIVITY_1"])
if 'config' not in st.session_state or CONFIG_NAME != st.session_state.config_name:
config = load_config(CONFIG_NAME)
st.session_state['config'] = config
st.session_state['config_name'] = CONFIG_NAME

with st.expander("### Green on Green Config"):
conf = st.slider('Confidence', min_value=0.0, max_value=1.0, value=config.get('conf', 0.6))
iou = st.slider('IOU', min_value=0.0, max_value=1.0, value=config.get('iou', 0.7))

with st.expander("### Green on Brown Config"):
exg_min = st.slider('ExG Min', min_value=0, max_value=255, value=config.get('exgMin', 25))
exg_max = st.slider('ExG Max', min_value=0, max_value=255, value=config.get('exgMax', 200))
hue_min = st.slider('Hue Min', min_value=0, max_value=128, value=config.get('hueMin', 39))
hue_max = st.slider('Hue Max', min_value=0, max_value=128, value=config.get('hueMax', 83))
saturation_min = st.slider('Saturation Min', min_value=0, max_value=255, value=config.get('saturationMin', 50))
saturation_max = st.slider('Saturation Max', min_value=0, max_value=255, value=config.get('saturationMax', 220))
brightness_min = st.slider('Brightness Min', min_value=0, max_value=255, value=config.get('brightnessMin', 60))
brightness_max = st.slider('Brightness Max', min_value=0, max_value=255, value=config.get('brightnessMax', 190))
min_area = st.slider('Min Area', min_value=0, max_value=255, value=config.get('minArea', 10))

if submitted:
updated_config = {
"conf": conf,
"iou": iou,
"exgMin": exg_min,
"exgMax": exg_max,
"hueMin": hue_min,
"hueMax": hue_max,
"saturationMin": saturation_min,
"saturationMax": saturation_max,
"brightnessMin": brightness_min,
"brightnessMax": brightness_max,
"minArea": min_area
}
config.update(updated_config)
else:
config = st.session_state.config
with st.expander("### Green on Green Config"):
st.slider('Confidence', min_value=0.0, max_value=1.0, value=config.get('conf', 0.6), key='conf',
on_change=update_value)
st.slider('IOU', min_value=0.0, max_value=1.0, value=config.get('iou', 0.7), key='iou',
on_change=update_value)

with st.expander("### Green on Brown Config"):
st.slider('ExG Min', min_value=0, max_value=255, value=config.get('exgMin', 25), key='exgMin',
on_change=update_value)
st.slider('ExG Max', min_value=0, max_value=255, value=config.get('exgMax', 200), key='exgMax',
on_change=update_value)
st.slider('Hue Min', min_value=0, max_value=128, value=config.get('hueMin', 39), key='hueMin',
on_change=update_value)
st.slider('Hue Max', min_value=0, max_value=128, value=config.get('hueMax', 83), key='hueMax',
on_change=update_value)
st.slider('Saturation Min', min_value=0, max_value=255, value=config.get('saturationMin', 50), key='saturationMin',
on_change=update_value)
st.slider('Saturation Max', min_value=0, max_value=255, value=config.get('saturationMax', 220), key='saturationMax',
on_change=update_value)
st.slider('Brightness Min', min_value=0, max_value=255, value=config.get('brightnessMin', 60), key='brightnessMin',
on_change=update_value)
st.slider('Brightness Max', min_value=0, max_value=255, value=config.get('brightnessMax', 190), key='brightnessMax',
on_change=update_value)
st.slider('Min Area', min_value=0, max_value=255, value=config.get('minArea', 10), key='minArea',
on_change=update_value)

if current_file_idx >= len(uploaded_files) and current_file_idx != 0:
st.session_state.current_file_idx = (len(uploaded_files) - 1)
Expand All @@ -134,8 +142,10 @@ def main():

if 'weed_detector' not in st.session_state or 'model_used' in st.session_state and st.session_state.model_used != model_path:
try:
message_box.info('Loading algorithm...')
weed_detector = get_weed_detector(algorithm=algorithm_dict[algorithm_key], model_path=model_path)
st.session_state.weed_detector = weed_detector
message_box.success('Algorithm ready.')

st.session_state.model_used = model_path
except Exception:
Expand All @@ -144,8 +154,10 @@ def main():
else:
if config.get('algorithm') != algorithm_dict[algorithm_key]:
try:
message_box.info('Loading algorithm...')
weed_detector = get_weed_detector(algorithm=algorithm_dict[algorithm_key], model_path=st.session_state.model_path)
st.session_state.weed_detector = weed_detector
message_box.success('Algorithm ready.')

except Exception:
st.warning('Please upload model or change algorithm')
Expand All @@ -168,13 +180,13 @@ def main():
is_playing = False

if 'last_frame' in st.session_state:
st_frame.image(st.session_state.last_frame, caption='Processed Frame', use_column_width=False, width=500)
st_frame.image(st.session_state.last_frame, caption='Processed Frame', use_column_width=False, width=800)

while True:
if play:
is_playing = True
if stop or not is_playing:
st_frame.image(st.session_state.last_frame, caption='Processed Frame', use_column_width=False, width=500)
st_frame.image(st.session_state.last_frame, caption='Processed Frame', use_column_width=False, width=800)
break

frame = frame_reader.read()
Expand All @@ -184,8 +196,9 @@ def main():
try:
_, _, _, image = setup_and_run_detector(weed_detector=weed_detector, frame=frame.copy(), config=config)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
st_frame.image(rgb, caption='Processed Frame', use_column_width=False, width=500)
st_frame.image(rgb, caption='Processed Frame', use_column_width=False, width=800)
st.session_state.last_frame = rgb
time.sleep(0.03)
except Exception:
st.error('Please upload model or change algorithm.')

Expand All @@ -198,7 +211,8 @@ def main():
_, _, _, image = setup_and_run_detector(weed_detector=weed_detector, frame=image.copy(), config=config)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

st_frame.image(rgb)
st_frame.image(rgb, width=800)
st.session_state['last_frame'] = rgb
except Exception:
st.error('Please upload model or change algorithm.')

Expand All @@ -216,6 +230,13 @@ def main():
st.session_state.current_file_idx = current_file_idx
except IndexError:
st.warning('There is an issue with the file index. Please re-upload the files.')
col_main.write('## Try it yourself!')
col_main.code('''from owl.viz import webcam, images_and_video
# run using your webcam
webcam(algorithm='gog', model_path='models/yolov8n.pt) # add your own model path as required
# run on images, videos or directories
images_and_video(media_path='path/to/your/media_files''')

if __name__ == "__main__":
main()

0 comments on commit 52374a4

Please sign in to comment.