Skip to content

Instantly share code, notes, and snippets.

@Merwanski
Last active November 23, 2022 21:18
Show Gist options
  • Save Merwanski/bec05d37bdfb2c594e26bcaae77ee63c to your computer and use it in GitHub Desktop.
Save Merwanski/bec05d37bdfb2c594e26bcaae77ee63c to your computer and use it in GitHub Desktop.
streamlit_ros.py webgui
#!/usr/bin/env python3
import streamlit as st
import rospy
import time
import threading
from std_msgs.msg import String, Int32
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
bridge = CvBridge()
textROS_output = None
imageROS_output = None
def callback_text(msg):
global textROS_output
textROS_output = msg.data
def callback_image(msg):
global imageROS_output
imageROS_output = bridge.imgmsg_to_cv2(msg, desired_encoding='passthrough') # "passthrough": destination image encoding will be the same as the image message encoding
def getData():
return textROS_output, imageROS_output
threading.Thread(target=lambda: rospy.init_node('streamlit_webgui_node', disable_signals=True)).start()
rospy.Subscriber("/textROS", String , callback_text)
rospy.Subscriber("/imageROS", Image , callback_image)
def main():
selected_box = st.sidebar.selectbox('Choose one of the following', ('Welcome','demo-1'))
if selected_box == 'Welcome':
welcome()
if selected_box == 'demo-1':
demo1()
def welcome():
st.title('Webgui using Streamlit')
st.subheader('A simple app that shows .... You can choose the options'
+ ' from the left. I have implemented only a few to show how it works on Streamlit. ')
st.image('overview.jpg', use_column_width=True)
def demo1():
st.subheader('demo1')
st.text('Demo1 text(s):')
textLocation = st.empty()
imageLocation = st.empty()
while True:
textROS, imageROS = getData()
with textLocation:
st.text(textROS)
with imageLocation:
if imageROS is not None:
st.image(imageROS, caption='Result -->> Image')
time.sleep(0.01)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment