Jetseon TX2 & IntelRealsense D435i & Python & Socket
一、IntelRealsense Python Wrapper
GitHub
1、Installation
pip install pyrealsense2
2、Building From Source
① Ubuntu 14.04/16.04 LTS
a. Ensure apt-get up to date
sudo apt-get update && sudo apt-get upgrade
b. Install Python and its development files(或尝试用conda虚拟python环境)
sudo apt-get install python python-dev
sudo apt-get install python3 python3-dev
c. Clone source code from github
git clone https://github.com/IntelRealSense/librealsense.git
d. Run top level CMake command
cd librealsense
mkdir build
cd build
cmake ../ -DBUILD_PYTHON_BINDINGS:bool=true
make -j4
sudo make install
e. Update PYTHONPATH environment variable to add the pyrealsense library(或者采用e’)
export PYTHONPATH=$PYTHONPATH:/usr/local/lib
e’. Extract ‘librealsense/build/wrappers/python/*’ files. Under the path, run:
python
import pyrealsense2 as rs
二、 D435i深度相机
参考
import pyrealsense2 as rs
import numpy as np
import cv2
if __name__=="__main__":
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
pipeline.start(config)
try:
while True:
frames = pipeline.wait_for_frames()
depth_frame = frames.get_depth_frame()
color_frame = frames.get_color_frame()
if not depth_frame or not color_frame:
continue
depth_image = np.asanyarray(depth_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
images = np.hstack((color_image, depth_colormap))
cv2.nameWindow('Realsense', cv2.WINDOW_AUTOSIZE)
cv2.imshow('Realsense', images)
key = cv2.waitKey(1)
if key & 0xFF == ord('q') or key == 27:
cv2.destoryAllWindows()
break
finally:
pipeline.stop()
三、Socket TCP数据传输
1、Windows 设置
① 点击右下角网络图标,进入网络和Internet设置
② 设置局域网ip地址
更改适配器选项->右键以太网,点击属性->选择IPv4,属性->勾选使用以下ip地址->设置ip地址->保存完成
③ 关闭Windows防火墙
2、TX2 & Ubuntu 设置
① 进入设置,选择网络设置
② 选中网线->options->IPv4->选择手动设置->Add->设置ip地址(注意与Windows在同一网段)->保存完成
3、联通测试
① Windows中’win + R’ -> 'cmd’打开terminal (输入ipconfig查看本机ip地址)
ping 192.168.10.1
② TX2&Ubuntu中’ctrl+alt+t’打开terminal (输入ifconfig查看本机ip地址)
ping 192.168.10.2
4、深度相机图像数据传输
① client端/TX2读取深度图像并打包发送
import cv2
import time
import socket
import pyrealsense2 as rs
import numpy as np
HOST = '192.168.10.2'
PORT = 8080
Address = (HOST, PORT)
tcpClient = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpClient.connect(Address)
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
pipeline.start(config)
while True:
start = time.perf_counter()
frames = pipeline.wait_for_frames()
depth_frame = frames.get_depth_frame()
color_frame = frames.get_color_frame()
if not depth_frame or not color_frame:
continue
depth_image = np.asanyarray(depth_frame.get_data())
depth_image = np.asanyarray(depth_image, 2).repeat(3, axis=2)
color_image = np.asanyarray(color_frame.get_data())
images = np.hstack((color_image, depth_image))
img_encode = cv2.imencode('.jpg', images)[1]
bytedata = img_encode.tobytes()
flag_data = (str(len(bytedata))).encode() + ",".encode() + " ".encode()
tcpClient.send(flag_data)
data = tcpClient.recv(1024)
if ("ok" == data.decode()):
tcpClient.send(bytedata)
data = tcpClient.recv(1024)
if ("ok" == data.decode()):
print("延时:" + str(int((time.per_counter() - start) * 1000)) + "ms")
② sever端/Windows/Ubuntu接收数据并解压可视化
import socket
import cv2
import numpy as np
HOST = ''
PORT = 8080
ADDRESS = (HOST, PORT)
tcpServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpServer.bind(ADDRESS)
tcpServer.listen(5)
while True:
print("等待连接……")
client_socket, client_address = tcpServer.accept()
print("连接成功!")
try:
while True:
data = client_socket.recv(1024)
if data:
client_socket.send(b"ok")
flag = data.decode().split(",")
total = int(flag[0])
cnt = 0
img_bytes = b""
while cnt < total:
data = client_socket.recv(256000)
img_bytes += data
cnt += len(data)
print("receive:" + str(cnt) + "/" + flag[0])
client_socket.send(b"ok")
img = np.asarray(bytearray(img_bytes), dtype="uint8")
img = cv2.imdecode(img, cv2.IMREAD_COLOR)
cv2.imshow("img", img)
cv2.waitKey(1)
else:
print("已断开!")
break
finally:
client_socket.close()
先运行sever端,等待连接,再运行client端进行连接。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)