?????? 我計(jì)劃從樹(shù)莓派上的攝像頭采集圖像數(shù)據(jù)到筆記本上,tensorflow處理完畢后再向樹(shù)莓派發(fā)送操作指令结闸,控制電機(jī)和舵機(jī)齿坷。ROS的雙機(jī)連接還是挺簡(jiǎn)單的透硝,首先樹(shù)莓派和筆記本設(shè)置ssh免密碼和用戶名登錄(具體請(qǐng)百度),從樹(shù)莓派和筆記本上都可以互相無(wú)密碼和用戶名登錄成功后宪肖,在樹(shù)莓派上輸入sudo vi ~/.bashrc表制,在最后增加export ROS_HOSTNAME=樹(shù)莓派的ip健爬,export ROS_MASTER_URI=http://筆記本的ip:11311,然后source ~/.bashrc么介;登錄筆記本上sudo vi ~/.bashrc娜遵,最后增加 export ROS_HOSTNAME=筆記本ip,export ROS_MASTER_URI=http://筆記本ip:11311(我用筆記本當(dāng)主機(jī)壤短,如果用樹(shù)莓派當(dāng)主機(jī)设拟,ROS_MASTER_URI地址更換就可以了)。然后樹(shù)莓派久脯、pc上安裝opencv纳胧,樹(shù)莓派上安裝picamera。在筆記本上啟動(dòng)roscore桶现,在樹(shù)莓派用以下代碼測(cè)試雙機(jī)發(fā)送圖像:
# -*- encoding:utf-8 -*-
# import the necessary packages
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import sys
import rospy
from std_msgs.msg import String
from sensor_msgs.msg import Image
from cv_bridge import CvBridge, CvBridgeError
import io
import numpy as np
# initialize the camera and grab a reference to the raw camera capture
def talker():
? ? camera = PiCamera()
? ? camera.resolution = (320,240)
? ? time.sleep(1)
? ? #allow the camera to warmup
? ? image_pub = rospy.Publisher("image_topic_2",Image,queue_size=10)
? ? bridge = CvBridge()
? ? rospy.init_node('image_talker', anonymous=True)
? ? rate = rospy.Rate(10) # 10hz
? ? #以上是ros節(jié)點(diǎn)初始化完成
? ? # capture frames from the camera
? ? stream = io.BytesIO()
? ? for foo in camera.capture_continuous(stream, format='jpeg', use_video_port=True):
? ? ? ? # grab the raw NumPy array representing the image, then initialize the timestamp
? ? ? ? # and occupied/unoccupied text
? ? ? ? data = np.fromstring(stream.getvalue(), dtype=np.uint8)
? ? ? ? # 轉(zhuǎn)換為CV2的mat格式
? ? ? ? image = cv2.imdecode(data, cv2.IMREAD_COLOR)
? ? ? ? (rows,cols,channels) = image.shape
? ? ? ? if cols > 60 and rows > 60 :
? ? ? ? ? ? cv2.circle(image, (50,50), 10, 255)
? ? ? ? image_pub.publish(bridge.cv2_to_imgmsg(image, "bgr8"))
? ? ? ? rate.sleep()
? ? stream.truncate()
? ? stream.seek(0)? ? ? ?
if __name__ == '__main__':
? ? try:
? ? ? ? talker()
? ? except rospy.ROSInterruptException:
? ? ? ? pass
在/catkin_ws/src/beginner_tutorials/src保存為test.py躲雅,chmod +x test.py。運(yùn)行rosrun beginner_tutorials test.py骡和。在筆記本上運(yùn)營(yíng)rqt_image_view查看圖像相赁。