Skip to content

Commit 4eadcf6

Browse files
committed
Update the examples for the B2W and B2 cameras.
1 parent 04a4b03 commit 4eadcf6

10 files changed

Lines changed: 282 additions & 39 deletions

File tree

example/b2/camera/camera_opencv.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from unitree_sdk2py.core.channel import ChannelFactoryInitialize
2+
from unitree_sdk2py.b2.front_video.front_video_client import FrontVideoClient
3+
from unitree_sdk2py.b2.back_video.back_video_client import BackVideoClient
4+
import cv2
5+
import numpy as np
6+
import sys
7+
8+
def display_image(window_name, data):
9+
# If data is a list, we need to convert it to a bytes object
10+
if isinstance(data, list):
11+
data = bytes(data)
12+
13+
# Now convert to numpy image
14+
image_data = np.frombuffer(data, dtype=np.uint8)
15+
image = cv2.imdecode(image_data, cv2.IMREAD_COLOR)
16+
if image is not None:
17+
cv2.imshow(window_name, image)
18+
19+
if __name__ == "__main__":
20+
if len(sys.argv) > 1:
21+
ChannelFactoryInitialize(0, sys.argv[1])
22+
else:
23+
ChannelFactoryInitialize(0)
24+
25+
frontCameraClient = FrontVideoClient() # Create a front camera video client
26+
frontCameraClient.SetTimeout(3.0)
27+
frontCameraClient.Init()
28+
29+
backCameraClient = BackVideoClient() # Create a back camera video client
30+
backCameraClient.SetTimeout(3.0)
31+
backCameraClient.Init()
32+
33+
# Loop to continuously fetch images
34+
while True:
35+
# Get front camera image
36+
front_code, front_data = frontCameraClient.GetImageSample()
37+
if front_code == 0:
38+
display_image("Front Camera", front_data)
39+
40+
# Get back camera image
41+
back_code, back_data = backCameraClient.GetImageSample()
42+
if back_code == 0:
43+
display_image("Back Camera", back_data)
44+
45+
# Press ESC to stop
46+
if cv2.waitKey(20) == 27:
47+
break
48+
49+
# Clean up windows
50+
cv2.destroyAllWindows()
51+

example/b2/camera/capture_image.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import time
2+
import os
3+
import sys
4+
5+
from unitree_sdk2py.core.channel import ChannelFactoryInitialize
6+
from unitree_sdk2py.b2.front_video.front_video_client import FrontVideoClient
7+
from unitree_sdk2py.b2.back_video.back_video_client import BackVideoClient
8+
9+
if __name__ == "__main__":
10+
if len(sys.argv) > 1:
11+
ChannelFactoryInitialize(0, sys.argv[1])
12+
else:
13+
ChannelFactoryInitialize(0)
14+
15+
# 创建前置相机客户端
16+
front_client = FrontVideoClient()
17+
front_client.SetTimeout(3.0)
18+
front_client.Init()
19+
20+
# 创建后置相机客户端
21+
back_client = BackVideoClient()
22+
back_client.SetTimeout(3.0)
23+
back_client.Init()
24+
25+
print("##################Get Front Camera Image###################")
26+
# 获取前置相机图像
27+
front_code, front_data = front_client.GetImageSample()
28+
29+
if front_code != 0:
30+
print("Get front camera image error. Code:", front_code)
31+
else:
32+
front_image_name = "./front_img.jpg"
33+
print("Front Image Saved as:", front_image_name)
34+
35+
with open(front_image_name, "+wb") as f:
36+
f.write(bytes(front_data))
37+
38+
print("##################Get Back Camera Image###################")
39+
# 获取后置相机图像
40+
back_code, back_data = back_client.GetImageSample()
41+
42+
if back_code != 0:
43+
print("Get back camera image error. Code:", back_code)
44+
else:
45+
back_image_name = "./back_img.jpg"
46+
print("Back Image Saved as:", back_image_name)
47+
48+
with open(back_image_name, "+wb") as f:
49+
f.write(bytes(back_data))
50+
51+
time.sleep(1)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from unitree_sdk2py.core.channel import ChannelFactoryInitialize
2+
from unitree_sdk2py.b2.front_video.front_video_client import FrontVideoClient
3+
from unitree_sdk2py.b2.back_video.back_video_client import BackVideoClient
4+
import cv2
5+
import numpy as np
6+
import sys
7+
8+
def display_image(window_name, data):
9+
# If data is a list, we need to convert it to a bytes object
10+
if isinstance(data, list):
11+
data = bytes(data)
12+
13+
# Now convert to numpy image
14+
image_data = np.frombuffer(data, dtype=np.uint8)
15+
image = cv2.imdecode(image_data, cv2.IMREAD_COLOR)
16+
if image is not None:
17+
cv2.imshow(window_name, image)
18+
19+
if __name__ == "__main__":
20+
if len(sys.argv) > 1:
21+
ChannelFactoryInitialize(0, sys.argv[1])
22+
else:
23+
ChannelFactoryInitialize(0)
24+
25+
frontCameraClient = FrontVideoClient() # Create a front camera video client
26+
frontCameraClient.SetTimeout(3.0)
27+
frontCameraClient.Init()
28+
29+
backCameraClient = BackVideoClient() # Create a back camera video client
30+
backCameraClient.SetTimeout(3.0)
31+
backCameraClient.Init()
32+
33+
# Loop to continuously fetch images
34+
while True:
35+
# Get front camera image
36+
front_code, front_data = frontCameraClient.GetImageSample()
37+
if front_code == 0:
38+
display_image("Front Camera", front_data)
39+
40+
# Get back camera image
41+
back_code, back_data = backCameraClient.GetImageSample()
42+
if back_code == 0:
43+
display_image("Back Camera", back_data)
44+
45+
# Press ESC to stop
46+
if cv2.waitKey(20) == 27:
47+
break
48+
49+
# Clean up windows
50+
cv2.destroyAllWindows()
51+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import time
2+
import os
3+
import sys
4+
5+
from unitree_sdk2py.core.channel import ChannelFactoryInitialize
6+
from unitree_sdk2py.b2.front_video.front_video_client import FrontVideoClient
7+
from unitree_sdk2py.b2.back_video.back_video_client import BackVideoClient
8+
9+
if __name__ == "__main__":
10+
if len(sys.argv) > 1:
11+
ChannelFactoryInitialize(0, sys.argv[1])
12+
else:
13+
ChannelFactoryInitialize(0)
14+
15+
# 创建前置相机客户端
16+
front_client = FrontVideoClient()
17+
front_client.SetTimeout(3.0)
18+
front_client.Init()
19+
20+
# 创建后置相机客户端
21+
back_client = BackVideoClient()
22+
back_client.SetTimeout(3.0)
23+
back_client.Init()
24+
25+
print("##################Get Front Camera Image###################")
26+
# 获取前置相机图像
27+
front_code, front_data = front_client.GetImageSample()
28+
29+
if front_code != 0:
30+
print("Get front camera image error. Code:", front_code)
31+
else:
32+
front_image_name = "./front_img.jpg"
33+
print("Front Image Saved as:", front_image_name)
34+
35+
with open(front_image_name, "+wb") as f:
36+
f.write(bytes(front_data))
37+
38+
print("##################Get Back Camera Image###################")
39+
# 获取后置相机图像
40+
back_code, back_data = back_client.GetImageSample()
41+
42+
if back_code != 0:
43+
print("Get back camera image error. Code:", back_code)
44+
else:
45+
back_image_name = "./back_img.jpg"
46+
print("Back Image Saved as:", back_image_name)
47+
48+
with open(back_image_name, "+wb") as f:
49+
f.write(bytes(back_data))
50+
51+
time.sleep(1)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
" service name
3+
"""
4+
ROBOT_BACK_VIDEO_SERVICE_NAME = "back_videohub"
5+
6+
7+
"""
8+
" service api version
9+
"""
10+
ROBOT_BACK_VIDEO_API_VERSION = "1.0.0.0"
11+
12+
13+
"""
14+
" api id
15+
"""
16+
ROBOT_BACK_VIDEO_API_ID_GETIMAGESAMPLE = 1001
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import json
2+
3+
from ...rpc.client import Client
4+
from .back_video_api import *
5+
6+
7+
"""
8+
" class FrontVideoClient
9+
"""
10+
class BackVideoClient(Client):
11+
def __init__(self):
12+
super().__init__(ROBOT_BACK_VIDEO_SERVICE_NAME, False)
13+
14+
15+
def Init(self):
16+
# set api version
17+
self._SetApiVerson(ROBOT_BACK_VIDEO_API_VERSION)
18+
# regist api
19+
self._RegistApi(ROBOT_BACK_VIDEO_API_ID_GETIMAGESAMPLE, 0)
20+
21+
# 1001
22+
def GetImageSample(self):
23+
return self._CallBinary(ROBOT_BACK_VIDEO_API_ID_GETIMAGESAMPLE, [])
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
" service name
3+
"""
4+
ROBOT_FRONT_VIDEO_SERVICE_NAME = "front_videohub"
5+
6+
7+
"""
8+
" service api version
9+
"""
10+
ROBOT_FRONT_VIDEO_API_VERSION = "1.0.0.0"
11+
12+
13+
"""
14+
" api id
15+
"""
16+
ROBOT_FRONT_VIDEO_API_ID_GETIMAGESAMPLE = 1001
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import json
2+
3+
from ...rpc.client import Client
4+
from .front_video_api import *
5+
6+
7+
"""
8+
" class FrontVideoClient
9+
"""
10+
class FrontVideoClient(Client):
11+
def __init__(self):
12+
super().__init__(ROBOT_FRONT_VIDEO_SERVICE_NAME, False)
13+
14+
15+
def Init(self):
16+
# set api version
17+
self._SetApiVerson(ROBOT_FRONT_VIDEO_API_VERSION)
18+
# regist api
19+
self._RegistApi(ROBOT_FRONT_VIDEO_API_ID_GETIMAGESAMPLE, 0)
20+
21+
# 1001
22+
def GetImageSample(self):
23+
return self._CallBinary(ROBOT_FRONT_VIDEO_API_ID_GETIMAGESAMPLE, [])

unitree_sdk2py/b2/video/video_api.py

Lines changed: 0 additions & 16 deletions
This file was deleted.

unitree_sdk2py/b2/video/video_client.py

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)