将mp4的视频使用yolov5识别后转换为ros2的压缩数据发出去
以下是详细代码
import time
import cv2
import numpy as np
import rclpy
from cvu.detector import Detector
from rclpy.node import Node
from sensor_msgs.msg import CompressedImage, CameraInfo
# read image, initiate detector
# img = cv2.imread("/home/hello/xols.jpg")
detector = Detector(classes="coco", weight='yolov5s', backend="onnx", device="auto")
jpeg_quality = 50
class Sender(Node):
def __init__(self):
super().__init__('detect_node')
self.debug_pub = self.create_publisher(CompressedImage, '/camera_1/image_raw/compressed', 1)
self.encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), jpeg_quality]
self.main()
def detect_(self, img):
# inference
predictions = detector(img)
# predictions info
print(predictions)
# draw on frame (inplace + returns img)
predictions.draw(img)
# class-wise counter object
print(predictions.count())
# loop through
for prediction in predictions:
# print info
print(prediction)
# access specific things
print(prediction.bbox, prediction.confidence)
print(prediction.class_id, prediction.class_name)
# draw specific prediction (only inplace, doesn't return img)
prediction.draw(img)
def main(self):
# 打开视频文件
while (1):
video = cv2.VideoCapture('/home/hello/128x1路测看到雪.mp4')
# 循环读取视频的每一帧
while True:
# 读取一帧
ret, frame = video.read()
# self.detect_(frame)
# 如果读取失败(例如到达视频末尾),则退出循环
if not ret:
break
frame = cv2.resize(frame, (1920, 1080))
compressed_img = CompressedImage()
# compressed_img.header = msg.header
compressed_img.header.stamp = self.get_clock().now().to_msg()
compressed_img.header.frame_id = 'camera_1'
compressed_img.format = "jpeg"
compressed_img.data = np.array(cv2.imencode('.jpg', frame, self.encode_param)[1]).tobytes()
self.debug_pub.publish(compressed_img)
# 在这里处理每一帧,例如显示或保存
# time.sleep(0.01)
cv2.imshow('frame', frame)
# 按下'q'键退出循环
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放资源并关闭窗口
video.release()
# # save img
cv2.destroyAllWindows()
if __name__ == '__main__':
rclpy.init()
node = Sender()
rclpy.spin(node)
node.destroy_node()
rclpy.shutdown()