|
| 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 | + |
0 commit comments