Ejemplo n.º 1
0
def get_servo_dataset(filename, start_index=0, end_index=None):
    data = pd.DataFrame.from_csv(filename)

    # Outputs
    x = []

    # Servo ranges from 40-150
    servo = []

    for i in data.index[start_index:end_index]:
        # Don't want noisy data
        if data['servo'][i] < -100 or data['servo'][i] > 100 :
            continue

        # Append
        image = deserialize_image(data['image'][i])

        minRGB = np.array([0, 0, 41])
        maxRGB = np.array([88, 88, 255])
        maskRGB = cv2.inRange(image,minRGB,maxRGB)
        image = cv2.bitwise_and(image, image, mask = maskRGB)

        x.append(image)
        servo.append(raw_to_cnn(data['servo'][i]))

    return x, servo
Ejemplo n.º 2
0
def visualize_data(filename, width=72, height=48, depth=3, cnn_model=None):
    """
    When cnn_model is specified it'll show what the cnn_model predicts (red)
    as opposed to what inputs it actually received (green)
    """
    data = pd.DataFrame.from_csv(filename)

    for i in data.index:
        cur_img = data['image'][i]
        cur_throttle = int(data['servo'][i])
        cur_motor = int(data['motor'][i])

        # [1:-1] is used to remove '[' and ']' from string
        cur_img_array = deserialize_image(cur_img)
        y_input = cur_img_array.copy()  # NN input

        # And then rescale it so we can more easily preview it
        cur_img_array = cv2.resize(cur_img_array, (480, 320),
                                   interpolation=cv2.INTER_CUBIC)

        # Extra debugging info (e.g. steering etc)
        cv2.putText(cur_img_array, "frame: %s" % str(i), (5, 35),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, 255)
        cv2.line(cur_img_array, (240, 300), (240 - (90 - cur_throttle), 200),
                 (0, 255, 0), 3)

        # Motor values
        # RGB
        cv2.line(cur_img_array, (50, 160), (50, 160 - (90 - cur_motor)),
                 raw_motor_to_rgb(cur_motor), 3)

        # If we wanna visualize our cnn_model
        if cnn_model:
            y = cnn_model.predict([y_input])
            servo_out = cnn_to_raw(y[0])
            cv2.line(cur_img_array, (240, 300),
                     (240 - (90 - int(servo_out)), 200), (0, 0, 255), 3)

            # Can determine the motor our with a simple exponential equation
            # x = abs(servo_out-90)
            # motor_out = (7.64*e^(-0.096*x)) - 1
            # motor_out = 90 - motor_out
            x_ = abs(servo_out - 90)
            motor_out = (7.64 * np.e**(-0.096 * x_)) - 1
            motor_out = int(80 - motor_out)  # Only wanna go forwards
            cv2.line(cur_img_array, (100, 160), (100, 160 - (90 - motor_out)),
                     raw_motor_to_rgb(motor_out), 3)
            print(motor_out, cur_motor)

        # Show frame
        # Convert to BGR cause thats how OpenCV likes it
        cv2.imshow('frame', cv2.cvtColor(cur_img_array, cv2.COLOR_RGB2BGR))
        if cv2.waitKey(0) & 0xFF == ord('q'):
            break
Ejemplo n.º 3
0
def visualize_data(filename, width=72, height=48, depth=3, cnn_model=None):
    """
    When cnn_model is specified it'll show what the cnn_model predicts (red)
    as opposed to what inputs it actually received (green)
    """
    data = pd.DataFrame.from_csv(filename)     

    for i in data.index:
        cur_img = data['image'][i]
        cur_throttle = int(data['servo'][i])
        cur_motor = int(data['motor'][i])        
        
        # [1:-1] is used to remove '[' and ']' from string 
        cur_img_array = deserialize_image(cur_img)        
        y_input = cur_img_array.copy() # NN input

        # And then rescale it so we can more easily preview it
        cur_img_array = cv2.resize(cur_img_array, (480, 320), interpolation=cv2.INTER_CUBIC)

        # Extra debugging info (e.g. steering etc)
        cv2.putText(cur_img_array, "frame: %s" % str(i), (5,35), cv2.FONT_HERSHEY_SIMPLEX, 1, 255)
        cv2.line(cur_img_array, (240, 300), (240-(90-cur_throttle), 200), (0, 255, 0), 3)

        # Motor values
        # RGB
        cv2.line(cur_img_array, (50, 160), (50, 160-(90-cur_motor)), raw_motor_to_rgb(cur_motor), 3)

        # If we wanna visualize our cnn_model
        if cnn_model:
            y = cnn_model.predict([y_input])
            servo_out = cnn_to_raw(y[0])         
            cv2.line(cur_img_array, (240, 300), (240-(90-int(servo_out)), 200), (0, 0, 255), 3)

            # Can determine the motor our with a simple exponential equation
            # x = abs(servo_out-90)
            # motor_out = (7.64*e^(-0.096*x)) - 1
            # motor_out = 90 - motor_out
            x_ = abs(servo_out - 90)
            motor_out = (7.64*np.e**(-0.096*x_)) - 1
            motor_out = int(80 - motor_out) # Only wanna go forwards
            cv2.line(cur_img_array, (100, 160), (100, 160-(90-motor_out)), raw_motor_to_rgb(motor_out), 3)
            print(motor_out, cur_motor)

        # Show frame
        # Convert to BGR cause thats how OpenCV likes it
        cv2.imshow('frame', cv2.cvtColor(cur_img_array, cv2.COLOR_RGB2BGR))
        if cv2.waitKey(0) & 0xFF == ord('q'):
            break
Ejemplo n.º 4
0
    def update_driving(self):
        drive_dir = (self.drive_forwards - self.drive_back)
        #print('drive_dir = '+str(drive_dir))

        if (drive_dir > 0.1) and self.cozmo.is_on_charger:
            # cozmo is stuck on the charger, and user is trying to drive off - issue an explicit drive off action
            try:
                self.cozmo.drive_off_charger_contacts().wait_for_completed()
            except cozmo.exceptions.RobotBusy:
                # Robot is busy doing another action - try again next time we get a drive impulse
                pass

        turn_dir = (self.turn_right - self.turn_left) + self.mouse_dir
        #print('turn_dir = '+str(turn_dir))

        if drive_dir < 0:
            # It feels more natural to turn the opposite way when reversing
            turn_dir = -turn_dir

        forward_speed = self.pick_speed(150, 75, 50)
        turn_speed = self.pick_speed(100, 50, 30)
        #print(turn_speed)

        if not _is_autodrive:
            self.cur_motor = drive_dir * forward_speed
            self.cur_servo = turn_speed * turn_dir
        else:
            cur_img_array = deserialize_image(np.asarray(self.logImages[-1]),
                                              SETTINGS['width'],
                                              SETTINGS['height'],
                                              SETTINGS['depth'])
            y_input = cur_img_array.copy()  # NN input
            y = cnn_model.predict([y_input])
            print('y[0] = ' + str(y[0]))

            self.cur_servo = int(cnn_to_raw([y[0][0]]))
            self.cur_motor = int(cnn_to_raw([y[0][1]]))
        print('_is_autodrive = ' + str(_is_autodrive))
        print('cur_servo = ' + str(self.cur_servo))
        print('cur_motor = ' + str(self.cur_motor))

        l_wheel_speed = self.cur_motor + self.cur_servo
        r_wheel_speed = self.cur_motor - self.cur_servo

        self.cozmo.drive_wheels(l_wheel_speed, r_wheel_speed,
                                l_wheel_speed * 4, r_wheel_speed * 4)
Ejemplo n.º 5
0
def visualize_data(filename, width=72, height=48, depth=3, cnn_model=None):
    """
    When cnn_model is specified it'll show what the cnn_model predicts (red)
    as opposed to what inputs it actually received (green)
    """
    data = pd.DataFrame.from_csv(filename)     

    for i in range(30):
        cur_img = data['image'][i]
        cur_steer = int(data['servo'][i])
        cur_throttle = int(data['motor'][i])
        
        # [1:-1] is used to remove '[' and ']' from string 
        cur_img_array = deserialize_image(cur_img)
        # cur_img_array = cv2.resize(cur_img_array, (480, 320), interpolation=cv2.INTER_CUBIC)
        image = cv2.cvtColor(cur_img_array, cv2.COLOR_RGB2BGR)
        print(i)
        cv2.imwrite('test'+str(i)+'.jpg', image)
Ejemplo n.º 6
0
def get_servo_dataset(filename, start_index=0, end_index=None):
    data = pd.DataFrame.from_csv(filename)

    # Outputs
    x = []

    # Servo ranges from 40-150
    servo = []

    for i in data.index[start_index:end_index]:
        # Don't want noisy data
        if data['servo'][i] < 40 or data['servo'][i] > 150:
            continue

        # Append
        x.append(deserialize_image(data['image'][i]))
        servo.append(raw_to_cnn(data['servo'][i]))

    return x, servo
Ejemplo n.º 7
0
def get_servo_dataset(filename, start_index=0, end_index=None):
    data = pd.DataFrame.from_csv(filename)

    # Outputs
    x = []

    # Servo ranges from 40-150
    servo = []

    for i in data.index[start_index:end_index]:
        # Don't want noisy data
        if data['servo'][i] < 40 or data['servo'][i] > 150:
            continue

        # Append
        x.append(deserialize_image(data['image'][i]))
        servo.append(raw_to_cnn(data['servo'][i]))

    return x, servo
Ejemplo n.º 8
0
def get_dataset(filename,
                start_index=0,
                end_index=None,
                width=72,
                height=48,
                depth=3):
    data = pd.DataFrame.from_csv(filename)

    # Outputs
    x = []

    # Servo and Motor
    y = []

    for i in data.index[start_index:end_index]:
        # Don't want noisy data
        #if data['servo'][i] < 40 or data['servo'][i] > 150:
        #    continue

        # Append
        x.append(deserialize_image(data['image'][i], width, height, depth))
        y.append([raw_to_cnn(data['servo'][i]), raw_to_cnn(data['motor'][i])])

    return x, y
Ejemplo n.º 9
0
def visualize_data(filename, width=72, height=48, depth=3, cnn_model=None):
    """
    When cnn_model is specified it'll show what the cnn_model predicts (red)
    as opposed to what inputs it actually received (green)
    """
    data = pd.DataFrame.from_csv(filename)
    frames = []

    for i in data.index:
        cur_img = data['image'][i]
        cur_throttle = int(data['servo'][i])
        cur_motor = int(data['motor'][i])

        # [1:-1] is used to remove '[' and ']' from string
        cur_img_array = deserialize_image(cur_img, width, height, depth)
        y_input = cur_img_array.copy()  # NN input

        # And then rescale it so we can more easily preview it
        cur_img_array = cv2.resize(cur_img_array, (480, 320),
                                   interpolation=cv2.INTER_CUBIC)

        # Extra debugging info (e.g. steering etc)
        cv2.putText(cur_img_array, "frame: %s" % str(i), (5, 35),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, 255)
        cv2.putText(cur_img_array, "servo: %s" % str(cur_throttle), (5, 70),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, 255)
        cv2.putText(cur_img_array, "motor: %s" % str(cur_motor), (5, 100),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, 255)

        # Steering values
        cv2.line(cur_img_array, (240, 300), (240 + cur_throttle, 200),
                 (0, 255, 0), 3)

        # Motor values, RGB
        #cv2.line(cur_img_array, (50, 300), (50, 300-cur_motor), raw_motor_to_rgb(cur_motor), 3)
        cv2.line(cur_img_array, (50, 300), (50, 300 - cur_motor), (0, 255, 0),
                 3)

        # If we wanna visualize our cnn_model
        if cnn_model:
            y = cnn_model.predict([y_input])
            print('y[0] = ' + str(y[0]))

            # the conversion expect a list (of size 1) for unknown reason
            # just play along here
            servo_out = int(cnn_to_raw([y[0][0]]))
            cv2.line(cur_img_array, (250, 300), (250 + servo_out, 200),
                     (0, 0, 255), 3)

            # Can determine the motor our with a simple exponential equation
            # x = abs(servo_out-90)
            # motor_out = (7.64*e^(-0.096*x)) - 1
            # motor_out = 90 - motor_out
            #x_ = abs(servo_out - 90)
            #motor_out = (7.64*np.e**(-0.096*x_)) - 1
            #motor_out = int(80 - motor_out) # Only wanna go forwards
            motor_out = int(cnn_to_raw([y[0][1]]))

            cv2.line(cur_img_array, (60, 300), (60, 300 - motor_out),
                     (0, 0, 255), 3)
            #print(motor_out, cur_motor)

            cv2.putText(cur_img_array, "pServo: %s" % str(servo_out),
                        (245, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, 255)
            cv2.putText(cur_img_array, "pMotor: %s" % str(motor_out),
                        (245, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, 255)

        # Show frame
        # Convert to BGR cause thats how OpenCV likes it
        cv2.imshow('frame', cv2.cvtColor(cur_img_array, cv2.COLOR_RGB2BGR))
        frames += [cur_img_array]
        if cv2.waitKey(0) & 0xFF == ord('q'):
            break

    imageio.mimsave(
        os.path.splitext(os.path.basename(filename))[0] + '.gif', frames)
Ejemplo n.º 10
0
"""
fileoutname = get_new_filename(folder='edata',
                               filename='output_',
                               extension='.csv')
outfile = open(fileoutname, 'w')
outfile = open(fileoutname, 'a')

data = pd.DataFrame.from_csv(filename)
frame_results = []
servo_results = []
motorspeed_results = []
for i in data.index:
    cur_img = data['image'][i]
    servo = data['servo'][i]
    motor = data['motor'][i]
    cur_img_array = deserialize_image(cur_img)

    y_input = cur_img_array.copy()  # NN input
    #
    cur_img_array = bw_rgb_filter(cur_img_array, 48, 72)
    cur_img_array = cur_img_array.flatten()
    cur_img_array = cur_img_array.astype('uint8')

    frame_results.append(serialize_image(cur_img_array))
    servo_results.append(servo)
    motorspeed_results.append(motor)
    raw_data = {
        'image': frame_results,
        'servo': servo_results,
        'motor': motorspeed_results
    }