Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Angular UI camera backup #164

Merged
merged 2 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions angular_ui_app/src/app/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class AppConstants {
// public static get HOST_IP(): string { return '192.168.1.69'; }
public static get HOST_IP(): string { return '192.168.1.69'; }
// public static get HOST_IP(): string { return '10.122.8.160'; }
public static get HOST_IP(): string { return 'localhost'; }
// public static get HOST_IP(): string { return 'localhost'; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<div style="display: flex; justify-content: space-between; margin-bottom: 5px;">
<div>
<app-camera camera_size="large"/>
<!-- <app-backup-camera cameraTopic="/camera_frames" /> -->
<app-drive-component/>
</div>

Expand Down
82 changes: 25 additions & 57 deletions camera_data/src/camera_publisher.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,29 @@
#!/usr/bin/env python3
import os, sys
from pydoc_data.topics import topics
#!/usr/bin/env python

currentdir = os.path.dirname(os.path.realpath(__file__))
sys.path.append(currentdir)
import rospy
import cv2
import time
from cv_bridge import CvBridge
from sensor_msgs.msg import Image
from std_msgs.msg import Int16
from PyQt5 import QtGui
import sys

stop = False

class Node_CameraFramePub():

def __init__(self):
self.frames = None
self.video_capture = cv2.VideoCapture(0, cv2.CAP_V4L2)
self.video_capture.open(0)

rospy.init_node('camera_frame_publisher')
self.camera_select_subscriber = rospy.Subscriber("camera_selection", Int16, self.select_camera)
self.timer = rospy.Timer(rospy.Duration(0.03), self.timer_callback)
self.camera_frame_publisher = rospy.Publisher('/camera_frames', Image, queue_size=10)

def timer_callback(self, event):
try:
ret, frame = self.video_capture.read()
# these code cause error
encode_params = [int(cv2.IMWRITE_JPEG_QUALITY), 90]
result, frame = cv2.imencode(".jpg", frame, encode_params)
self.frames = self.openCV_to_ros_image(frame)
if ret:
print("Publishing frame")
self.camera_frame_publisher.publish(self.frames)
except:
print("No camera found")
finally:
pass

def openCV_to_ros_image(self, cv_image):
try:
bridge = CvBridge()
ros_image = bridge.cv2_to_imgmsg(cv_image, encoding="passthrough")
return ros_image
except:
print("Cannot translate image correctly")

def select_camera(self, x):
self.video_capture.release()
self.video_capture.open(x.data * 2)
print(x.data)

from cv_bridge import CvBridge
import cv2

if __name__ == "__main__":
driver = Node_CameraFramePub()
rospy.spin()
def webcam_publisher():
rospy.init_node('webcam_publisher', anonymous=True)
pub = rospy.Publisher('camera_frames', Image, queue_size=10)
rate = rospy.Rate(30) # 10hz

cap = cv2.VideoCapture(0) # Open the first webcam
bridge = CvBridge()

while not rospy.is_shutdown():
ret, frame = cap.read()
if ret:
# Convert the frame to ROS format
ros_image = bridge.cv2_to_imgmsg(frame, "bgr8")
# Publish the frame
pub.publish(ros_image)
rate.sleep()

if __name__ == '__main__':
try:
webcam_publisher()
except rospy.ROSInterruptException:
pass
1 change: 0 additions & 1 deletion camera_data/src/camera_subscriber.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def __init__(self):

def display_frames(self, frame):
self.frames = self.ros_to_openCV_image(frame)
self.frames = cv2.imdecode(self.frames, 1)
print(self.frames.shape)
cv2.imshow("Live Feed", self.frames)
cv2.waitKey(10) # Look into to reduce latency
Expand Down
Loading