Update the examples for the B2W and B2 cameras.
This commit is contained in:
parent
04a4b035f1
commit
4eadcf6ba0
|
@ -0,0 +1,51 @@
|
||||||
|
from unitree_sdk2py.core.channel import ChannelFactoryInitialize
|
||||||
|
from unitree_sdk2py.b2.front_video.front_video_client import FrontVideoClient
|
||||||
|
from unitree_sdk2py.b2.back_video.back_video_client import BackVideoClient
|
||||||
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def display_image(window_name, data):
|
||||||
|
# If data is a list, we need to convert it to a bytes object
|
||||||
|
if isinstance(data, list):
|
||||||
|
data = bytes(data)
|
||||||
|
|
||||||
|
# Now convert to numpy image
|
||||||
|
image_data = np.frombuffer(data, dtype=np.uint8)
|
||||||
|
image = cv2.imdecode(image_data, cv2.IMREAD_COLOR)
|
||||||
|
if image is not None:
|
||||||
|
cv2.imshow(window_name, image)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
ChannelFactoryInitialize(0, sys.argv[1])
|
||||||
|
else:
|
||||||
|
ChannelFactoryInitialize(0)
|
||||||
|
|
||||||
|
frontCameraClient = FrontVideoClient() # Create a front camera video client
|
||||||
|
frontCameraClient.SetTimeout(3.0)
|
||||||
|
frontCameraClient.Init()
|
||||||
|
|
||||||
|
backCameraClient = BackVideoClient() # Create a back camera video client
|
||||||
|
backCameraClient.SetTimeout(3.0)
|
||||||
|
backCameraClient.Init()
|
||||||
|
|
||||||
|
# Loop to continuously fetch images
|
||||||
|
while True:
|
||||||
|
# Get front camera image
|
||||||
|
front_code, front_data = frontCameraClient.GetImageSample()
|
||||||
|
if front_code == 0:
|
||||||
|
display_image("Front Camera", front_data)
|
||||||
|
|
||||||
|
# Get back camera image
|
||||||
|
back_code, back_data = backCameraClient.GetImageSample()
|
||||||
|
if back_code == 0:
|
||||||
|
display_image("Back Camera", back_data)
|
||||||
|
|
||||||
|
# Press ESC to stop
|
||||||
|
if cv2.waitKey(20) == 27:
|
||||||
|
break
|
||||||
|
|
||||||
|
# Clean up windows
|
||||||
|
cv2.destroyAllWindows()
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
import time
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from unitree_sdk2py.core.channel import ChannelFactoryInitialize
|
||||||
|
from unitree_sdk2py.b2.front_video.front_video_client import FrontVideoClient
|
||||||
|
from unitree_sdk2py.b2.back_video.back_video_client import BackVideoClient
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
ChannelFactoryInitialize(0, sys.argv[1])
|
||||||
|
else:
|
||||||
|
ChannelFactoryInitialize(0)
|
||||||
|
|
||||||
|
# 创建前置相机客户端
|
||||||
|
front_client = FrontVideoClient()
|
||||||
|
front_client.SetTimeout(3.0)
|
||||||
|
front_client.Init()
|
||||||
|
|
||||||
|
# 创建后置相机客户端
|
||||||
|
back_client = BackVideoClient()
|
||||||
|
back_client.SetTimeout(3.0)
|
||||||
|
back_client.Init()
|
||||||
|
|
||||||
|
print("##################Get Front Camera Image###################")
|
||||||
|
# 获取前置相机图像
|
||||||
|
front_code, front_data = front_client.GetImageSample()
|
||||||
|
|
||||||
|
if front_code != 0:
|
||||||
|
print("Get front camera image error. Code:", front_code)
|
||||||
|
else:
|
||||||
|
front_image_name = "./front_img.jpg"
|
||||||
|
print("Front Image Saved as:", front_image_name)
|
||||||
|
|
||||||
|
with open(front_image_name, "+wb") as f:
|
||||||
|
f.write(bytes(front_data))
|
||||||
|
|
||||||
|
print("##################Get Back Camera Image###################")
|
||||||
|
# 获取后置相机图像
|
||||||
|
back_code, back_data = back_client.GetImageSample()
|
||||||
|
|
||||||
|
if back_code != 0:
|
||||||
|
print("Get back camera image error. Code:", back_code)
|
||||||
|
else:
|
||||||
|
back_image_name = "./back_img.jpg"
|
||||||
|
print("Back Image Saved as:", back_image_name)
|
||||||
|
|
||||||
|
with open(back_image_name, "+wb") as f:
|
||||||
|
f.write(bytes(back_data))
|
||||||
|
|
||||||
|
time.sleep(1)
|
|
@ -0,0 +1,51 @@
|
||||||
|
from unitree_sdk2py.core.channel import ChannelFactoryInitialize
|
||||||
|
from unitree_sdk2py.b2.front_video.front_video_client import FrontVideoClient
|
||||||
|
from unitree_sdk2py.b2.back_video.back_video_client import BackVideoClient
|
||||||
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def display_image(window_name, data):
|
||||||
|
# If data is a list, we need to convert it to a bytes object
|
||||||
|
if isinstance(data, list):
|
||||||
|
data = bytes(data)
|
||||||
|
|
||||||
|
# Now convert to numpy image
|
||||||
|
image_data = np.frombuffer(data, dtype=np.uint8)
|
||||||
|
image = cv2.imdecode(image_data, cv2.IMREAD_COLOR)
|
||||||
|
if image is not None:
|
||||||
|
cv2.imshow(window_name, image)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
ChannelFactoryInitialize(0, sys.argv[1])
|
||||||
|
else:
|
||||||
|
ChannelFactoryInitialize(0)
|
||||||
|
|
||||||
|
frontCameraClient = FrontVideoClient() # Create a front camera video client
|
||||||
|
frontCameraClient.SetTimeout(3.0)
|
||||||
|
frontCameraClient.Init()
|
||||||
|
|
||||||
|
backCameraClient = BackVideoClient() # Create a back camera video client
|
||||||
|
backCameraClient.SetTimeout(3.0)
|
||||||
|
backCameraClient.Init()
|
||||||
|
|
||||||
|
# Loop to continuously fetch images
|
||||||
|
while True:
|
||||||
|
# Get front camera image
|
||||||
|
front_code, front_data = frontCameraClient.GetImageSample()
|
||||||
|
if front_code == 0:
|
||||||
|
display_image("Front Camera", front_data)
|
||||||
|
|
||||||
|
# Get back camera image
|
||||||
|
back_code, back_data = backCameraClient.GetImageSample()
|
||||||
|
if back_code == 0:
|
||||||
|
display_image("Back Camera", back_data)
|
||||||
|
|
||||||
|
# Press ESC to stop
|
||||||
|
if cv2.waitKey(20) == 27:
|
||||||
|
break
|
||||||
|
|
||||||
|
# Clean up windows
|
||||||
|
cv2.destroyAllWindows()
|
||||||
|
|
|
@ -0,0 +1,51 @@
|
||||||
|
import time
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from unitree_sdk2py.core.channel import ChannelFactoryInitialize
|
||||||
|
from unitree_sdk2py.b2.front_video.front_video_client import FrontVideoClient
|
||||||
|
from unitree_sdk2py.b2.back_video.back_video_client import BackVideoClient
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if len(sys.argv) > 1:
|
||||||
|
ChannelFactoryInitialize(0, sys.argv[1])
|
||||||
|
else:
|
||||||
|
ChannelFactoryInitialize(0)
|
||||||
|
|
||||||
|
# 创建前置相机客户端
|
||||||
|
front_client = FrontVideoClient()
|
||||||
|
front_client.SetTimeout(3.0)
|
||||||
|
front_client.Init()
|
||||||
|
|
||||||
|
# 创建后置相机客户端
|
||||||
|
back_client = BackVideoClient()
|
||||||
|
back_client.SetTimeout(3.0)
|
||||||
|
back_client.Init()
|
||||||
|
|
||||||
|
print("##################Get Front Camera Image###################")
|
||||||
|
# 获取前置相机图像
|
||||||
|
front_code, front_data = front_client.GetImageSample()
|
||||||
|
|
||||||
|
if front_code != 0:
|
||||||
|
print("Get front camera image error. Code:", front_code)
|
||||||
|
else:
|
||||||
|
front_image_name = "./front_img.jpg"
|
||||||
|
print("Front Image Saved as:", front_image_name)
|
||||||
|
|
||||||
|
with open(front_image_name, "+wb") as f:
|
||||||
|
f.write(bytes(front_data))
|
||||||
|
|
||||||
|
print("##################Get Back Camera Image###################")
|
||||||
|
# 获取后置相机图像
|
||||||
|
back_code, back_data = back_client.GetImageSample()
|
||||||
|
|
||||||
|
if back_code != 0:
|
||||||
|
print("Get back camera image error. Code:", back_code)
|
||||||
|
else:
|
||||||
|
back_image_name = "./back_img.jpg"
|
||||||
|
print("Back Image Saved as:", back_image_name)
|
||||||
|
|
||||||
|
with open(back_image_name, "+wb") as f:
|
||||||
|
f.write(bytes(back_data))
|
||||||
|
|
||||||
|
time.sleep(1)
|
|
@ -0,0 +1,16 @@
|
||||||
|
"""
|
||||||
|
" service name
|
||||||
|
"""
|
||||||
|
ROBOT_BACK_VIDEO_SERVICE_NAME = "back_videohub"
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
" service api version
|
||||||
|
"""
|
||||||
|
ROBOT_BACK_VIDEO_API_VERSION = "1.0.0.0"
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
" api id
|
||||||
|
"""
|
||||||
|
ROBOT_BACK_VIDEO_API_ID_GETIMAGESAMPLE = 1001
|
|
@ -0,0 +1,23 @@
|
||||||
|
import json
|
||||||
|
|
||||||
|
from ...rpc.client import Client
|
||||||
|
from .back_video_api import *
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
" class FrontVideoClient
|
||||||
|
"""
|
||||||
|
class BackVideoClient(Client):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(ROBOT_BACK_VIDEO_SERVICE_NAME, False)
|
||||||
|
|
||||||
|
|
||||||
|
def Init(self):
|
||||||
|
# set api version
|
||||||
|
self._SetApiVerson(ROBOT_BACK_VIDEO_API_VERSION)
|
||||||
|
# regist api
|
||||||
|
self._RegistApi(ROBOT_BACK_VIDEO_API_ID_GETIMAGESAMPLE, 0)
|
||||||
|
|
||||||
|
# 1001
|
||||||
|
def GetImageSample(self):
|
||||||
|
return self._CallBinary(ROBOT_BACK_VIDEO_API_ID_GETIMAGESAMPLE, [])
|
|
@ -0,0 +1,16 @@
|
||||||
|
"""
|
||||||
|
" service name
|
||||||
|
"""
|
||||||
|
ROBOT_FRONT_VIDEO_SERVICE_NAME = "front_videohub"
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
" service api version
|
||||||
|
"""
|
||||||
|
ROBOT_FRONT_VIDEO_API_VERSION = "1.0.0.0"
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
" api id
|
||||||
|
"""
|
||||||
|
ROBOT_FRONT_VIDEO_API_ID_GETIMAGESAMPLE = 1001
|
|
@ -0,0 +1,23 @@
|
||||||
|
import json
|
||||||
|
|
||||||
|
from ...rpc.client import Client
|
||||||
|
from .front_video_api import *
|
||||||
|
|
||||||
|
|
||||||
|
"""
|
||||||
|
" class FrontVideoClient
|
||||||
|
"""
|
||||||
|
class FrontVideoClient(Client):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__(ROBOT_FRONT_VIDEO_SERVICE_NAME, False)
|
||||||
|
|
||||||
|
|
||||||
|
def Init(self):
|
||||||
|
# set api version
|
||||||
|
self._SetApiVerson(ROBOT_FRONT_VIDEO_API_VERSION)
|
||||||
|
# regist api
|
||||||
|
self._RegistApi(ROBOT_FRONT_VIDEO_API_ID_GETIMAGESAMPLE, 0)
|
||||||
|
|
||||||
|
# 1001
|
||||||
|
def GetImageSample(self):
|
||||||
|
return self._CallBinary(ROBOT_FRONT_VIDEO_API_ID_GETIMAGESAMPLE, [])
|
|
@ -1,16 +0,0 @@
|
||||||
"""
|
|
||||||
" service name
|
|
||||||
"""
|
|
||||||
VIDEO_SERVICE_NAME = "videohub"
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
|
||||||
" service api version
|
|
||||||
"""
|
|
||||||
VIDEO_API_VERSION = "1.0.0.1"
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
|
||||||
" api id
|
|
||||||
"""
|
|
||||||
VIDEO_API_ID_GETIMAGESAMPLE = 1001
|
|
|
@ -1,23 +0,0 @@
|
||||||
import json
|
|
||||||
|
|
||||||
from ...rpc.client import Client
|
|
||||||
from .video_api import *
|
|
||||||
|
|
||||||
|
|
||||||
"""
|
|
||||||
" class VideoClient
|
|
||||||
"""
|
|
||||||
class VideoClient(Client):
|
|
||||||
def __init__(self):
|
|
||||||
super().__init__(VIDEO_SERVICE_NAME, False)
|
|
||||||
|
|
||||||
|
|
||||||
def Init(self):
|
|
||||||
# set api version
|
|
||||||
self._SetApiVerson(VIDEO_API_VERSION)
|
|
||||||
# regist api
|
|
||||||
self._RegistApi(VIDEO_API_ID_GETIMAGESAMPLE, 0)
|
|
||||||
|
|
||||||
# 1001
|
|
||||||
def GetImageSample(self):
|
|
||||||
return self._CallBinary(VIDEO_API_ID_GETIMAGESAMPLE, [])
|
|
Loading…
Reference in New Issue