【mp4的视频转换为ros2的压缩数据发出去】

将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()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值