-
[Python OpenCV ROS] cv.cap으로 딴 이미지 compressedImage msg로 publish하기Robot, Autonomous Vehicle 2021. 3. 2. 15:47
-> Thread에서 opencv cap 함수로 webcam 혹은 usbcam 이미지 프레임별로 캡쳐해서
-> ROS topic 중에서 compressedimage msg로 publish하기
-> Thread Keyboard Interrupt 넣어서 안전하게 끄기
#!/usr/bin/env python import rospy import roslib import sys, time import cv2 import threading from sensor_msgs.msg import CompressedImage def TVStart(data) : img_thread = DroneTV() try: img_thread.daemon=True img_thread.start() except KeyboardInterrupt : img_thread.stop() print("Shut down...") class DroneTV(threading.Thread) : def __init__(self) : threading.Thread.__init__(self) global thread_running self.cap = cv2.VideoCapture(0) #now web cam thread_running = True def run(self) : global thread_running msg = CompressedImage() while thread_running : b, frame = self.cap.read() if b: msg.header.stamp = rospy.Time.now() msg.format = "jpeg" encode_param=[int(cv2.IMWRITE_JPEG_QUALITY),10] msg.data = np.array(cv2.imencode('.jpg', frame, encode_param)[1]).tostring() pub_compressed_img.publish(msg) else : pass def stop(self) : thread_running = False self.quit() self.wait(5000) TVStart() pub_compressed_img = rospy.Publisher('/tello_img/image_raw/compressed', CompressedImage, queue_size = 1)
'Robot, Autonomous Vehicle' 카테고리의 다른 글
[GPS]NMEA Output Message (0) 2022.11.07 [Jetson Nano] Jetson Nano 에서 Object Detection ( DetectNet, SSD-MobileNetv2, Pytorch ) (0) 2021.03.24 [ROS USB_CAM] 한 머신에서 multi usb_cam node 켜기 (0) 2021.03.02 [Darknet ROS] darknet_ros yolov3 코드 이것 저것 수정 (2) 2021.03.02 [bash file] 이것저것 (0) 2021.02.13