码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • ros 接收相机数据数据并发布


    完整这个任务不需要用到python3

    效果如下所示

    环境 

    ROS1  python2.7

    1. 原始环境 无conda  或者conda deactivate 无conda 状态

    2. pip install rospkg

    3. pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python==4.2.0.3

    4. 代码 放在工程目录中

    1. #!/usr/bin/env python
    2. #!coding=utf-8
    3. #write by leo at 2018.04.26
    4. #function:
    5. #1, Get live_video from the webcam.
    6. #2, With ROS publish Image_info (to yolo and so on).
    7. #3, Convert directly, don't need to save pic at local.
    8. import rospy
    9. from sensor_msgs.msg import Image
    10. import cv2
    11. import numpy as np
    12. from cv_bridge import CvBridge, CvBridgeError
    13. import sys
    14. def webcamImagePub():
    15. # init ros_node
    16. rospy.init_node('webcam_puber', anonymous=True)
    17. # queue_size should be small in order to make it 'real_time'
    18. # or the node will pub the past_frame
    19. img_pub = rospy.Publisher('webcam/image_raw', Image, queue_size=2)
    20. rate = rospy.Rate(25) # 5hz
    21. # make a video_object and init the video object
    22. cap = cv2.VideoCapture(0)
    23. # define picture to_down' coefficient of ratio
    24. scaling_factor = 0.5
    25. # the 'CVBridge' is a python_class, must have a instance.
    26. # That means "cv2_to_imgmsg() must be called with CvBridge instance"
    27. bridge = CvBridge()
    28. if not cap.isOpened():
    29. sys.stdout.write("Webcam is not available !")
    30. return -1
    31. count = 0
    32. # loop until press 'esc' or 'q'
    33. while not rospy.is_shutdown():
    34. ret, frame = cap.read()
    35. # resize the frame
    36. if ret:
    37. count = count + 1
    38. else:
    39. rospy.loginfo("Capturing image failed.")
    40. if count == 2:
    41. count = 0
    42. frame = cv2.resize(frame,None,fx=scaling_factor,fy=scaling_factor,interpolation=cv2.INTER_AREA)
    43. msg = bridge.cv2_to_imgmsg(frame, encoding="bgr8")
    44. img_pub.publish(msg)
    45. print '** publishing webcam_frame ***'
    46. rate.sleep()
    47. if __name__ == '__main__':
    48. try:
    49. webcamImagePub()
    50. except rospy.ROSInterruptException:
    51. pass

    运行代码 

  • 相关阅读:
    【mysql】--记一次delete删除语句使用别名的坑
    打通手车生态壁垒 vivo智能车载打造智慧出行新体验
    多条件分页查询
    关于HTML5画布canvas的功能
    HTML5七夕情人节表白网页制作【新年倒计时白色雪花飘落】HTML+CSS+JavaScript CSS特效
    01 飞行器设计 —— 一门独立的学科
    使用HTML制作静态网站作业——我的校园运动会(HTML+CSS)
    MySQL表的内外连接
    java技术:knife4j实现后端swagger文档
    【Verilog教程】2.4Verilog表达式
  • 原文地址:https://blog.csdn.net/W25679/article/details/133179023
  • 最新文章
  • 【JVM】编译执行与解释执行的区别是什么?JVM 使用哪种方式?
    用 Hashids 优雅解决 C 端自增 ID 暴露问题
    V8引擎 精品漫游指南--Ignition篇(上) 指令 栈帧 槽位 调用约定 内存布局 基础内容
    LLVM Pass快速入门(四):代码插桩
    milkup:桌面端 markdown AI续写和即时渲染
    基于项目工程构建SBOM(软件物料清单)的研究
    鸿蒙应用开发UI基础第二节:鸿蒙应用程序框架核心解析与实操
    .NET 中如何快速实现 List 集合去重?
    扣子Coze实战:从0到1打造抖音+小红书热点监控智能体
    浅谈数据访问层
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号