def test_init_start_stop(self): """Test camera's thread init/start/stop methods.""" camera = Camera() camera.start() time.sleep(1) image = bgr8_to_jpeg(camera.value) camera.stop() # Check if image changed after stopping assert image == bgr8_to_jpeg(camera.value)
class Observer: def __init__(self, camera_width, camera_height): self.camera = Camera(width=camera_width, height=camera_height) self.image = None def start(self): self.camera.observe(self._callback, names='value') self.camera.start() def stop(self): self.camera.stop() def _callback(self, change): img = change['new'] # Change BGR TO RGB HWC self.image = img[:, :, ::-1] def observation(self): while self.image is None: pass return self.image
class JetbotCamera: bridge = CvBridge() def __init__(self, calib_file=''): self.publisher = rospy.Publisher(IMAGE_TOPIC, Image, queue_size=1) rospy.init_node(NODE_NAME) width = rospy.get_param('~width', 640) height = rospy.get_param('~height', 480) rospy.logwarn(width) self.calib_file = calib_file self.camera = Camera(width=width,height=height) def start(self): self.camera.observe(self.image_proc, names='value') self.camera.start() rospy.spin() def image_proc(self, change): image_value = change['new'] cv2_image = self.bridge.cv2_to_imgmsg(image_value, 'bgr8') self.publisher.publish(cv2_image) def _load_camera_info(self): with open(self.calib_file, "r") as file_handle: calib_data = yaml.load(file_handle) # Parse camera_info_msg = CameraInfo() camera_info_msg.width = calib_data["image_width"] camera_info_msg.height = calib_data["image_height"] camera_info_msg.K = calib_data["camera_matrix"]["data"] camera_info_msg.D = calib_data["distortion_coefficients"]["data"] camera_info_msg.R = calib_data["rectification_matrix"]["data"] camera_info_msg.P = calib_data["projection_matrix"]["data"] camera_info_msg.distortion_model = calib_data["distortion_model"] return camera_info_msg
from jetbot import Robot from jetbot import Camera import cv2 import time import math import os, struct, array from fcntl import ioctl robot = Robot() camera = Camera() camera.start() # Iterate over the joystick devices. print('Available devices:') for fn in os.listdir('/dev/input'): if fn.startswith('js'): print(' /dev/input/%s' % (fn)) #这句显示手柄在硬件中的端口位置: /dev/input/js0 # We'll store the states here. axis_states = {} button_states = {} # These constants were borrowed from linux/input.h axis_names = { 0x00: 'x', 0x01: 'y', 0x02: 'rx', 0x05: 'ry',