Skip to content

Commit

Permalink
Added Sliders
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaibhav-Hariani committed Apr 17, 2024
1 parent 4910074 commit 84e057a
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 50 deletions.
103 changes: 53 additions & 50 deletions ros2/UI/pages/🛣 Lane_Detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,41 @@
import time
from std_msgs.msg import String

## This class currently subscribes to some topics in cam_publisher, which I used to test. Please rewrite for lane Detection.
# This class currently subscribes to some topics in cam_publisher, which I used to test. Please rewrite for lane Detection.


class Image_Handler(Node):
def __init__(self):
self._tabs = None
self._img = None
self._frame_containers = None

def __init__(self, containers):
self.topics = ["/raw_left", "/raw_right", "/tf_left",
"/tf_right", "/sliding_left", "/sliding_right"]
super().__init__('Streamlit_Image_Handler')
self._bridge = CvBridge()
self._left_subscriber = self.create_subscription(Image, self.topics[0], lambda msg: self.camData_callback(msg,containers[0]),qos_profile_sensor_data)
self._right_subscriber = self.create_subscription(Image, self.topics[1], lambda msg: self.camData_callback(msg,containers[1]),qos_profile_sensor_data)
self._tf_left_subscriber = self.create_subscription(Image, self.topics[2], lambda msg: self.camData_callback(msg,containers[2]),qos_profile_sensor_data)
self._tf_right_subscriber = self.create_subscription(Image, self.topics[3], lambda msg: self.camData_callback(msg,containers[3]),qos_profile_sensor_data)
self._sliding_left_subscriber = self.create_subscription(Image, self.topics[4], lambda msg: self.camData_callback(msg,containers[4]),qos_profile_sensor_data)
self._sliding_right_subscriber = self.create_subscription(Image, self.topics[5], lambda msg: self.camData_callback(msg,containers[5]),qos_profile_sensor_data)

self.imgData_subscriber = self.create_subscription(
Image,
'/camera/image',
lambda msg: self.camData_callback(msg,(self._frame_containers[0][0], self._frame_containers[0][1])),
qos_profile_sensor_data)

self.hsvData_subscriber = self.create_subscription(
Image,
'/camera/image_hsv',
lambda msg: self.camData_callback(msg,(self._frame_containers[1][0], self._frame_containers[1][1])),
qos_profile_sensor_data)

def tabs(self, tabs):
self._tabs = tabs
self._frame_containers = [(tab.empty(),tab.empty()) for tab in self._tabs]

#self._frame_containers[] is a container corresponding to one of the tabs: You can create
def camData_callback(self, msg_in,args):
# self._frame_containers[] is a container corresponding to one of the tabs: You can create
def camData_callback(self, msg_in, img_location):
raw = msg_in
img = self._bridge.imgmsg_to_cv2(raw)
args[0].image(img)
args[1].image(img)
img = self._bridge.imgmsg_to_cv2(raw, desired_encoding="passthrough")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
with img_location:
st.image(img)


#This class is your publisher: Flesh it out and integrate accordingly
# This class is your publisher: Flesh it out and integrate accordingly
class Publisher(Node):
def __init__(self):
super().__init__('Streamlit_Button_Publisher')
self.logger = self.get_logger()
self.button_pub = self.create_publisher(String, '/streamlit/button',10)
super().__init__('Streamlit_Button_Publisher')
self.logger = self.get_logger()
self.button_pub = self.create_publisher(
String, '/streamlit/button', 10)


def demo_publish():
msg = String()
Expand All @@ -55,12 +51,10 @@ def demo_publish():
st.success("Message Published!")




# See ../../ros2/src/lanefollowingtest/LaneDetection.py
# This page should render ALL images in there, as well as publish important update data
if __name__ == "__main__":
#How we Configure the page
# How we Configure the page
st.set_page_config(
page_title="Lane Detection",
page_icon="🛣")
Expand All @@ -69,33 +63,42 @@ def demo_publish():
render = st.checkbox("Render Video Feed")
st.write(
"This should render all of the images related to Lane Detection, and relevant parameters.")
tabs = st.tabs(["Original", "HSV", "Sliding Windows"])

#This hunk initializes the ROS2 nodes without breaking anything :)
#Should not need to be tuoched
#Creates the Structure
tabs = st.tabs(["Original", "Birds Eye View", "Sliding Windows"])
containers = []
frame_containers = []
for tab in tabs:
cols = tab.columns(2)
containers.append(cols[0])
containers.append(cols[1])
with cols[0]:
frame_containers.append(st.empty())
with cols[1]:
frame_containers.append(st.empty())

# This hunk initializes the ROS2 nodes without breaking anything :)
# Should not need to be tuoched
if "sub_node" not in st.session_state:
try:
rclpy.init()
except RuntimeError:
st.warning(
"something went wrong performance may be degraded. Try restarting fully.")
finally:
handler = Image_Handler()
handler.tabs(tabs)
handler = Image_Handler(frame_containers)
st.session_state["sub_node"] = handler
if "pub_node" not in st.session_state:
st.session_state["pub_node"] = Publisher()
if "pub_node" not in st.session_state:
st.session_state["pub_node"] = Publisher()

#This should also not need to be modified
# This should also not need to be modified
if render:
while (True):
try:
rclpy.spin_once(st.session_state['sub_node'])
time.sleep(0.01)
except:
st.warning(
"Something went wrong, perhaps tabs were clicked too quickly? Try restarting.")
break
# try:
rclpy.spin_once(st.session_state['sub_node'])
time.sleep(.01)
# except:
# st.warning(
# "Something went wrong, perhaps tabs were clicked too quickly? Try restarting.")
# break
else:
st.button("Ros Topic Publisher Demo!",on_click=demo_publish)

st.button("Ros Topic Publisher Demo!", on_click=demo_publish)
34 changes: 34 additions & 0 deletions ros2/src/lanefollowingtest/LaneDetection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from cv_bridge import CvBridge
import cv2
import numpy as np
import streamlit as st


# Inputs from both cameras
Expand Down Expand Up @@ -147,6 +148,39 @@ def Plot_Line(self, smoothen=False, prevFrameCount=6):
matrix = cv2.getPerspectiveTransform(pts1, pts2)


#sliders for constant values
#lower HSV slider
l_h = st.slider('lower_hue', min_value=0, max_value=255, value=0)
l_s = st.slider('lower_saturation', min_value = 0, max_value=255, value=0)
l_v = st.slider('lower_saturation', min_value = 0, max_value=255, value=200)

#upper HSV slider
l_h = st.slider('upper_hue', min_value=0, max_value=255, value=255)
l_s = st.slider('upper_saturation', min_value = 0, max_value=255, value=50)
l_v = st.slider('upper_saturation', min_value = 0, max_value=255, value=255)

#coordinate input widget
bl_x = st.number_input('bl_x', min_value = 0, None, 12)
bl_y = st.number_input('bl_y', min_value = 0, None, 355)
tl_x = st.number_input('tl_x', min_value = 0, None, 66)
tl_y = st.number_input('tl_y', min_value = 0, None, 304)
br_x = st.number_input('br_x', min_value = 0, None, 635)
br_y = st.number_input('br_y', min_value = 0, None, 344)
tr_x = st.number_input('tr_x', min_value = 0, None,595)
tr_y = st.number_input('tr_y', min_value = 0, None,308)

bl = (bl_x, bl_y)
tl = (tl_x, tl_y)
br = (br_x, br_y)
tr = (tr_x, tr_y)

#applying perspective transformation
pts1 = np.float32([tl, bl, tr, br])
pts2 = np.float32([[0, 0], [0, 480], [640, 0], [640, 480]])
# Matrix to warp the image for birdseye window
matrix = cv2.getPerspectiveTransform(pts1, pts2)


class Lane_Follower(Node):
GUI = True

Expand Down

0 comments on commit 84e057a

Please sign in to comment.