def __init__(self, donkey_name, mqtt_broker, sensor_size=(120, 160, 3)): self.camera_sub = MQTTValueSub("donkey/%s/camera" % donkey_name, broker=mqtt_broker) self.controller_pub = MQTTValuePub("donkey/%s/controls" % donkey_name, broker=mqtt_broker) self.jpgToImg = JpgToImgArr() self.sensor_size = sensor_size
def __init__(self, car="kari", mqtt_broker="mqtt.eclipse.org"): self.camera_sub = MQTTValueSub("donkey/%s/camera" % car, broker=mqtt_broker) self.state_sub = MQTTValueSub("donkey/%s/state" % car, broker=mqtt_broker) self.controller_pub = MQTTValuePub("donkey/%s/controls" % car, broker=mqtt_broker) self.jpgToImg = JpgToImgArr() self.img = np.zeros((40, 40)) self.state = None self.speed = 0
def update(self): import select from donkeycar.parts.image import JpgToImgArr self.init_video() jpg_conv = JpgToImgArr() while self.running: # Wait for the device to fill the buffer. select.select((self.video, ), (), ()) image_data = self.video.read_and_queue() self.frame = jpg_conv.run(image_data)
class DonkeyRemoteContoller: def __init__(self, donkey_name, mqtt_broker, sensor_size=(120, 160, 3)): self.camera_sub = MQTTValueSub("donkey/%s/camera" % donkey_name, broker=mqtt_broker) self.controller_pub = MQTTValuePub("donkey/%s/controls" % donkey_name, broker=mqtt_broker) self.jpgToImg = JpgToImgArr() self.sensor_size = sensor_size def get_sensor_size(self): return self.sensor_size def wait_until_connected(self): pass def take_action(self, action): self.controller_pub.run(action) def quit(self): self.camera_sub.shutdown() self.controller_pub.shutdown() def get_original_image(self): return self.img def observe(self): jpg = self.camera_sub.run() self.img = self.jpgToImg.run(jpg) return self.img
class DonkeyRemoteContoller: def __init__(self, donkey_name, mqtt_broker, sensor_size=(120, 160, 3)): self.camera_sub = MQTTValueSub("donkey/%s/camera" % donkey_name, broker=mqtt_broker)#, def_value=(np.zeros((40, 40)), {"cte": 0})) self.controller_pub = MQTTValuePub("donkey/%s/controls" % donkey_name, broker=mqtt_broker) self.jpgToImg = JpgToImgArr() self.sensor_size = sensor_size def get_sensor_size(self): return self.sensor_size def wait_until_connected(self): pass def take_action(self, action): self.controller_pub.run(action) def quit(self): self.camera_sub.shutdown() self.controller_pub.shutdown() def get_original_image(self): return self.img def observe(self): jpg = self.camera_sub.run() #data = self.camera_sub.run() # #print("State: {}".format(state)) # #jpg, info = state self.img = self.jpgToImg.run(jpg) # return self.img#, info return self.img
def __init__(self, car_name="Kari", mqtt_broker="mqtt.eclipse.org", realsense=False, env_type="DonkeyCar"): self.camera_sub = MQTTValueSub("donkey/%s/camera" % car_name, broker=mqtt_broker) if realsense: self.state_sub = MQTTValueSub("donkey/%s/state" % car_name, broker=mqtt_broker) self.controller_pub = MQTTValuePub("donkey/%s/controls" % car_name, broker=mqtt_broker) self.jpgToImg = JpgToImgArr() self.img = np.zeros((40, 40)) self.state = None self.speed = 0 self.realsense = realsense self.sim = env_type != "DonkeyCar"
from docopt import docopt import donkeycar as dk import cv2 from donkeycar.parts.cv import CvImageView, ImgBGR2RGB, ImgRGB2BGR, ImageScale, ImgWriter, ArrowKeyboardControls from donkeycar.parts.salient import SalientVis from donkeycar.parts.network import MQTTValuePub, MQTTValueSub from donkeycar.parts.transform import Lambda from donkeycar.parts.image import JpgToImgArr V = dk.vehicle.Vehicle() args = docopt(__doc__) print(args) V.add(MQTTValueSub(name="donkey/%s/camera" % args["--name"], broker=args["--broker"]), outputs=["jpg"]) V.add(JpgToImgArr(), inputs=["jpg"], outputs=["img_arr"]) V.add(ImgBGR2RGB(), inputs=["img_arr"], outputs=["rgb"]) V.add(ImageScale(4.0), inputs=["rgb"], outputs=["lg_img"]) V.add(CvImageView(), inputs=["lg_img"]) V.add(ArrowKeyboardControls(), outputs=["control"]) V.add(MQTTValuePub(name="donkey/%s/controls" % args["--name"]), inputs=["control"]) record_path = args["--record"] if record_path is not None: class ImageSaver: def __init__(self, path): self.index = 0 self.path = path def run(self, img_arr):
class DonkeyCarSpeed: def __init__(self, car="kari", mqtt_broker="mqtt.eclipse.org"): self.camera_sub = MQTTValueSub("donkey/%s/camera" % car, broker=mqtt_broker) self.state_sub = MQTTValueSub("donkey/%s/state" % car, broker=mqtt_broker) self.controller_pub = MQTTValuePub("donkey/%s/controls" % car, broker=mqtt_broker) self.jpgToImg = JpgToImgArr() self.img = np.zeros((40, 40)) self.state = None self.speed = 0 def observe(self): self.img = self.jpgToImg.run(self.camera_sub.run()) self.state = self.state_sub.run() def reset(self): self.controller_pub.run([0, 0]) self.observe() time.sleep(1) return self.img def step(self, control, step_length): self.controller_pub.run(control) time.sleep(step_length) self.observe() v = self.state["v"] self.speed = (v[0]**2 + v[1]**2 + v[2]**2)**0.5 done = self.is_dead() return self.img, done def is_dead(self): crop_height = 20 crop_width = 20 threshold = 70 pixels_percentage = 0.10 pixels_required = (self.img.shape[1] - 2 * crop_width) * crop_height * pixels_percentage #(im[:,:,0] > threshold) & (im[:,:,1] < 150) & (im[:,:,1] < 150) crop = self.img[-crop_height:, crop_width:-crop_width] #gs = np.dot(crop, [0.299, 0.587, 0.114]) r = crop[:,:,0] < threshold g = crop[:,:,1] < threshold b = crop[:,:,2] < threshold pixels = (r & g & b).sum() #im = self.state[-crop_height:, crop_width:-crop_width] #gs = (im[:,:,0] > 150) & (im[:,:,1] < 150) & (im[:,:,1] < 150) #pixels = len(gs[gs]) #pixels = len(gs[gs < threshold]) print("Pixels: {}, Required: {}".format(pixels, pixels_required)) return pixels < pixels_required
def __init__(self, donkey_name, mqtt_broker): self.camera_sub = MQTTValueSub("donkey/%s/camera" % donkey_name, broker=mqtt_broker) self.controller_pub = MQTTValuePub("donkey/%s/controls" % donkey_name, broker=mqtt_broker) self.jpgToImg = JpgToImgArr()
class DonkeyRemoteEnv: def __init__(self, car_name="Kari", mqtt_broker="mqtt.eclipse.org", realsense=False, env_type="DonkeyCar"): self.camera_sub = MQTTValueSub("donkey/%s/camera" % car_name, broker=mqtt_broker) if realsense: self.state_sub = MQTTValueSub("donkey/%s/state" % car_name, broker=mqtt_broker) self.controller_pub = MQTTValuePub("donkey/%s/controls" % car_name, broker=mqtt_broker) self.jpgToImg = JpgToImgArr() self.img = np.zeros((40, 40)) self.state = None self.speed = 0 self.realsense = realsense self.sim = env_type != "DonkeyCar" def observe(self): self.img = self.jpgToImg.run(self.camera_sub.run()) if self.realsense: self.state = self.state_sub.run() v = self.state["v"] self.speed = (v[0]**2 + v[1]**2 + v[2]**2)**0.5 def reset(self): self.controller_pub.run([0, 0]) self.observe() time.sleep(2) return self.img def step(self, control, step_length): print(control) self.controller_pub.run(control) time.sleep(step_length) self.observe() done = self.is_dead_sim() if self.sim else self.is_dead_real() return self.img, done def is_dead_real(self): crop_height = 20 crop_width = 20 threshold = 70 pixels_percentage = 0.10 pixels_required = (self.img.shape[1] - 2 * crop_width) * crop_height * pixels_percentage crop = self.img[-crop_height:, crop_width:-crop_width] r = crop[:, :, 0] < threshold g = crop[:, :, 1] < threshold b = crop[:, :, 2] < threshold pixels = (r & g & b).sum() print("Pixels: {}, Required: {}".format(pixels, pixels_required)) return pixels < pixels_required def is_dead_sim(self): crop_height = 40 required = 0.8 cropped = self.img[-crop_height:] rgb = cropped[:, :, 0] > cropped[:, :, 2] return rgb.sum() / (crop_height * 160) > required
import donkeycar as dk from donkeycar.parts.cv import CvImageView, ImgBGR2RGB, ImgRGB2BGR, ImageScale, ImgWriter from donkeycar.parts.salient import SalientVis from donkeycar.parts.network import ZMQValueSub, UDPValueSub, TCPClientValue from donkeycar.parts.transform import Lambda from donkeycar.parts.image import JpgToImgArr V = dk.vehicle.Vehicle() args = docopt(__doc__) cfg = dk.load_config(args['--config']) model_path = args['--model'] model_type = args['--type'] ip = args['--ip'] if model_type is None: model_type = "categorical" model = dk.utils.get_model_by_type(model_type, cfg) model.load(model_path) V.add(TCPClientValue(name="camera", host=ip), outputs=["packet"]) V.add(JpgToImgArr(), inputs=["packet"], outputs=["img"]) V.add(ImgBGR2RGB(), inputs=["img"], outputs=["img"]) V.add(SalientVis(model), inputs=["img"], outputs=["img"]) V.add(ImageScale(4.0), inputs=["img"], outputs=["lg_img"]) V.add(CvImageView(), inputs=["lg_img"]) V.start(rate_hz=1)