示例#1
0
    def __getitem__(self, index):

        if self.is_training:
            ele_anno = self.anno[self.train_list[index]]
        else:
            ele_anno = self.anno[self.test_list[index]]

        img_sz = (368, 368)
        img_folder_dir = os.path.join(MPII_FILE_DIR, 'images')
        img_dir = os.path.join(img_folder_dir, ele_anno['img_paths'])
        img = imgutils.load_img(img_dir)

        pts = ele_anno['joint_self']
        cen = ele_anno['objpos'].copy()
        scale = ele_anno['scale_provided']

        # generate crop image
        #print(img)
        img_crop, pts_crop, cen_crop = imgutils.crop(img, ele_anno,
                                                     self.use_rotation,
                                                     self.use_flip)
        pts_crop = np.array(pts_crop)
        cen_crop = np.array(cen_crop)

        height, width, _ = img_crop.shape
        img = np.transpose(img_crop, (2, 0, 1)) / 255.0
        return img
示例#2
0
    def next(self):
        """
        Public function to fetch next batch.

        # Returns
            The next batch of images and labels.
        """
        with self.lock:
            index_array = next(self.index_generator)
            current_batch_size = index_array.shape[0]

        # Image transformation is not under thread lock, so it can be done in
        # parallel
        batch_x = np.zeros((current_batch_size, ) + self.image_shape,
                           dtype=K.floatx())
        batch_steer = np.zeros((
            current_batch_size,
            2,
        ), dtype=K.floatx())
        batch_coll = np.zeros((
            current_batch_size,
            2,
        ), dtype=K.floatx())

        grayscale = self.color_mode == 'grayscale'

        # Build batch of image data
        for i, j in enumerate(index_array):
            fname = self.filenames[j]
            x = img_utils.load_img(os.path.join(self.directory, fname),
                                   grayscale=grayscale,
                                   crop_size=self.crop_size,
                                   target_size=self.target_size)

            x = self.image_data_generator.random_transform(x)
            x = self.image_data_generator.standardize(x)
            batch_x[i] = x

            # Build batch of steering and collision data
            if self.exp_type[index_array[i]] == 1:
                # Steering experiment (t=1)
                batch_steer[i, 0] = 1.0
                batch_steer[i, 1] = self.ground_truth[index_array[i]]
                batch_coll[i] = np.array([1.0, 0.0])
            else:
                # Collision experiment (t=0)
                batch_steer[i] = np.array([0.0, 0.0])
                batch_coll[i, 0] = 0.0
                batch_coll[i, 1] = self.ground_truth[index_array[i]]

        batch_y = [batch_steer, batch_coll]
        return batch_x, batch_y
示例#3
0
    def __getitem__(self, index):

        if self.is_training:
            ele_anno = self.anno[self.train_list[index]]
        else:
            ele_anno = self.anno[self.test_list[index]]

        img_sz = (368, 368)
        img_folder_dir = os.path.join(MPII_FILE_DIR, 'images')
        img_dir = os.path.join(img_folder_dir, ele_anno['img_paths'])
        img = imgutils.load_img(img_dir)

        pts = ele_anno['joint_self']
        cen = ele_anno['objpos'].copy()
        scale = ele_anno['scale_provided']
        """
        dataset :  MPI
        isValidation :  1.0
        img_paths :  005808361.jpg
        img_width :  1280.0
        img_height :  720.0
        objpos :  [966.0, 340.0]
        joint_self :  [[804.0, 711.0, 1.0], [816.0, 510.0, 1.0], [908.0, 438.0, 1.0], [1040.0, 454.0, 1.0], [906.0, 528.0, 1.0], [883.0, 707.0, 1.0], [974.0, 446.0, 1.0], [985.0, 253.0, 1.0], [982.759, 235.969, 1.0], [962.241, 80.031, 1.0], [869.0, 214.0, 1.0], [798.0, 340.0, 1.0], [902.0, 253.0, 1.0], [1067.0, 253.0, 1.0], [1167.0, 353.0, 1.0], [1142.0, 478.0, 1.0]]
        scale_provided :  4.718
        joint_others :  [[667.0, 633.0, 1.0], [675.0, 462.0, 1.0], [567.0, 519.0, 1.0], [375.0, 504.0, 1.0], [543.0, 476.0, 0.0], [532.0, 651.0, 0.0], [471.0, 512.0, 1.0], [463.0, 268.0, 1.0], [472.466, 220.857, 1.0], [503.534, 66.143, 1.0], [702.0, 267.0, 1.0], [721.0, 386.0, 1.0], [584.0, 256.0, 1.0], [341.0, 280.0, 1.0], [310.0, 432.0, 1.0], [372.0, 496.0, 1.0]]
        scale_provided_other :  4.734
        objpos_other :  [489.0, 383.0]
        annolist_index :  7.0
        people_index :  1.0
        numOtherPeople :  1.0
        """

        # generate crop image
        #print(img)
        img_crop, pts_crop, cen_crop = imgutils.crop(img, ele_anno)
        pts_crop = np.array(pts_crop)
        cen_crop = np.array(cen_crop)

        height, width, _ = img_crop.shape
        train_img = np.transpose(img_crop, (2, 0, 1)) / 255.0

        train_heatmaps = imgutils.generate_heatmaps(np.zeros((46, 46)),
                                                    pts_crop / 8)
        train_heatmaps = np.transpose(train_heatmaps, (2, 0, 1))

        train_centermap = imgutils.generate_heatmap(np.zeros((368, 368)),
                                                    cen_crop)
        train_centermap = np.expand_dims(train_centermap, axis=0)

        #return img_crop, pts_crop, cen_crop, img, pts, cen

        return train_img, train_heatmaps, train_centermap
示例#4
0
def _main():
    name = FLAGS.name or "flow_stats"
    folder_in = FLAGS.input_dir
    img_mode = "flow"
    n = 200
    lim = 200
    scale = 1 / 100
    exts = ['*.npy', '*.pfm', '*.flo', '*.bin', '*.png']
    n_im = 0
    clip = False

    # place results in input dir if no output dir is set
    if FLAGS.output_dir is None:
        folder_out = folder_in
    else:
        folder_out = FLAGS.output_dir

    # init
    flow_collector = iu.FlowStatsCollector(folder_out,
                                           n=n,
                                           lim=lim,
                                           name=name,
                                           clip=clip)

    # get filenames
    ipth = []
    fnames = []
    try:
        for ext in exts:
            ipth = os.path.join(folder_in, "**/", ext)
            fnames = sorted(glob.glob(ipth, recursive=True))
            n_im = len(fnames)
            if n_im != 0:
                break

        if n_im == 0:
            raise IOError("No input files found.")
    except IOError as e:
        print(e)
        exit(0)

    print("Evaluating: " + ipth + " (" + str(n_im) + " images)")

    # iterate all imgs
    for i, fname in tqdm(enumerate(fnames)):
        flow = iu.load_img(fname, img_mode=img_mode) * scale
        flow_collector.collect_flow_stats(flow)

    # save data
    flow_collector.print_flow_stats()
    flow_collector.write_flow_stats()
示例#5
0
def generate_rand_bg():
    """
    Generate random cutouts from dataset. For each image create 
    certain number of samples. The number is calculated as 
    desired size of dataset(specified by modul argument --amount)
    devided by number of images that are available.
    """
    args = parse_arguments()

    background_path = args.src
    background_imgs = list_dir_files(background_path)

    new_bg_dir = args.dest
    generate_dir(new_bg_dir)

    gen_count = len(list_dir_files(new_bg_dir))
    gen_count += 1

    new_size_w = args.width
    new_size_h = args.height

    desired_number_of_samples = args.amount
    per_bg = int(desired_number_of_samples / len(background_imgs))

    for idx, bg_path in enumerate(background_imgs):
        # Iterates over dataset of road background images without signs
        # and from each one of them create multiple random crops.
        bg_img = load_img(bg_path)

        #get rid of black part
        bg_img = bg_img[0:bg_img.shape[0] - 1000, 0:bg_img.shape[1]]

        for x in range(per_bg):
            curr = per_bg * (idx) + x
            total = per_bg * len(background_imgs)
            print("Working on", curr, "/", total)
            cropped = crop_random_part(bg_img, new_size_w, new_size_h)

            name = str(gen_count) + ".jpg"
            path = os.path.join(new_bg_dir, name)
            store_img(path, cropped)
            gen_count += 1
示例#6
0
    def __getitem__(self, idx):
        img_path = os.path.join(self.root_dir, self.steering_df.iloc[idx, 0])
        target_size = (FLAGS.img_width, FLAGS.img_height)
        crop_size = (FLAGS.crop_img_width, FLAGS.crop_img_height)
        image = load_img(img_path,
                         grayscale=True,
                         target_size=target_size,
                         crop_size=crop_size)

        steering = self.steering_df.iloc[idx, 1]
        steering = np.array(steering)
        steering = steering.astype('float')

        # Transform image to tensor
        image = self.transform(image)  # apply transforms to images
        image = image / 255.  # rescale between [0,1]
        image = image.type(torch.FloatTensor)
        steering = torch.tensor(steering, dtype=torch.float)

        return (image, steering)
示例#7
0
import numpy as np
import tensorflow as tf
import json

from keras import backend as K
from keras.preprocessing.image import Iterator
from keras.preprocessing.image import ImageDataGenerator
from keras.utils.generic_utils import Progbar
from keras.models import model_from_json
import utils
import img_utils
from common_flags import FLAGS

test_datagen = utils.DroneDataGenerator(rescale=1. / 255)
y = img_utils.load_img("data/testing/HMB_3/images/1479425597710017397.jpg",
                       grayscale=True,
                       crop_size=(200, 200),
                       target_size=(320, 240))

x = test_datagen.random_transform(y, seed=None)
x = test_datagen.standardize(x)
batch_x = np.zeros((1, ) + (200, 200, 1), dtype=K.floatx())
batch_x[0] = x
json_model_path = os.path.join(FLAGS.experiment_rootdir,
                               FLAGS.json_model_fname)
model = utils.jsonToModel(json_model_path)

# Load weights
weights_load_path = os.path.join(FLAGS.experiment_rootdir, FLAGS.weights_fname)
try:
    model.load_weights(weights_load_path)
    print("Loaded model from {}".format(weights_load_path))
示例#8
0
    skt_warmup = context.socket(zmq.PUB)
    skt_warmup.bind("tcp://192.168.1.216:5557")  # 本机ip
    for _ in range(10):
        time.sleep(0.05)
        skt_warmup.send("ss".encode("utf-8"))

    skt_recv_ack = context.socket(zmq.SUB)
    skt_recv_ack.connect("tcp://192.168.1.161:5555")  # 远程ip
    skt_recv_ack.set(zmq.SUBSCRIBE, b"")

    # bottlenet = spp_model_small.MycnnBottlenetDronePart(2).cpu()
    # bottlenet.eval()
    # transform = torchvision.transforms.Compose([torchvision.transforms.ToTensor()])

    for res in [448]:
        print("Res: {}".format(res))
        img = load_img("test_img.jpg",
                       crop_size=(448, 448),
                       target_size=(res, res))
        # img = torch.unsqueeze(transform(img), 0)
        # data = bottlenet(img).detach().numpy()
        # data = zlib.compress(pickle.dumps(data, -1))
        img_encode = cv2.imencode(".jpg", np.array(img))[1]
        data = np.array(img_encode).tostring()
        start = time.time()
        for i in range(1000):
            print("send {} pic".format(i))
            skt_send_img.send(data)
        _ = skt_recv_ack.recv()
        end = time.time()
        print("Latency per img: {:.5f}s".format((end - start) / 100.0))
        print(e)
        exit(0)

    # load labels
    lbl = []
    for fn in ['labels.txt', 'sync_steering.txt']:
        labelfilein = os.path.join(datdir, subdir, fn)
        if os.path.isfile(labelfilein):
            with open(labelfilein, newline='') as f:
                for row in csv.reader(f, delimiter=';'):
                    lbl.append(float(row[0]))

    # show all for making a decisison
    for i, item in enumerate(fnames):
        # load data and convert to rgb
        data = iu.load_img(item, img_mode=mode)
        img = iu.flow_to_img(data) if mode == "flow" else data

        labelbar = iu.pred_as_bar(lbl[i], (img.shape[0], 20), "Ground Truth")
        img_out = np.hstack((img, np.ones((img.shape[0], 10, 3), dtype=np.uint8) * 255, labelbar))

        cv2.imshow('frame', img_out)
        key = cv2.waitKey(50 + int(lbl[i]*200)) & 0xFF
        if key == ord(' '):
            break

    # make a decision
    copy_experiment = False
    cv2.imshow('frame', img_out)
    key = cv2.waitKey(0) & 0xFF
    if key == ord('q'):
示例#10
0
    def _get_batches_of_transformed_samples(self, index_array):
        """
        Public function to fetch next batch.

        # Returns
            The next batch of images and labels.
        """
        current_batch_size = index_array.shape[0]
        # Image transformation is not under thread lock, so it can be done in
        # parallel
        batch_x = np.zeros((current_batch_size, ) + self.image_shape,
                           dtype=K.floatx())
        orient = []
        trans = []

        grayscale = self.color_mode == 'grayscale'

        # Build batch of image data
        for i, j in enumerate(index_array):
            fname = self.filenames[j]
            x = img_utils.load_img(os.path.join(self.directory, fname),
                                   grayscale=grayscale,
                                   crop_size=self.crop_size,
                                   target_size=self.target_size)
            # x = self.image_data_generator.random_transform(x) # keras 自带 data augmentation,应该不用的
            x = self.image_data_generator.standardize(x)  # 应该是效果等同于1./255.
            batch_x[i] = x

            # Build batch of steering and collision data
            # 对于batch_steer[:,0]==1时表示有效,对于batch_coll[:,0]==0时表示有效
            # if self.exp_type[index_array[i]] == 1:
            #     # Steering experiment (t=1)
            #     batch_steer[i,0] =1.0
            #     batch_steer[i,1] = self.ground_truth[index_array[i]]
            #     batch_coll[i] = np.array([1.0, 0.0])
            # else:
            #     # Collision experiment (t=0)
            #     batch_steer[i] = np.array([0.0, 0.0])
            #     batch_coll[i,0] = 0.0
            #     batch_coll[i,1] = self.ground_truth[index_array[i]]
            if self.exp_type[index_array[i]] == 1:

                a = self.ground_truth[index_array[i]]
                if (a < -0.33):
                    orient.append([1, 0])
                elif (a > 0.33):
                    orient.append([1, 2])
                else:
                    orient.append([1, 1])
                trans.append([0, 0])
            else:
                a = self.ground_truth[index_array[i]]
                if (a < -0.1):
                    trans.append([1, 0])
                elif (a > 0.1):
                    trans.append([1, 2])
                else:
                    trans.append([1, 1])
                orient.append([0, 0])
        orient = np.array(orient)
        trans = np.array(trans)
        batch_y = [orient, trans]

        return batch_x, batch_y
def main():

    weights_path = sys.path[0] + '/model/model-Dronet/model_weights_69.h5'
    pics_path = "/home/rikka/uav-project/drone-data-validation/Crossroads2.01/images"

    # Input image dimensions
    img_width, img_height = FLAGS.img_width, FLAGS.img_height
    # Cropped image dimensions
    crop_img_width, crop_img_height = FLAGS.crop_img_width_dronet, FLAGS.crop_img_height_dronet
    target_size = (img_height, img_width)
    crop_size = (crop_img_height, crop_img_width)

    model = cnn_models.resnet8(crop_img_width, crop_img_height, 1, 1)
    plot_model(model, "model.png")
    model.load_weights(weights_path, by_name=True)
    model.compile(loss='mse', optimizer='adam')
    pic_list = os.listdir(pics_path)
    pic_list.sort()
    try:
        for img_name in pic_list:
            current_name = pics_path + '/' + img_name
            img = img_utils.load_img(current_name,
                                     grayscale=True,
                                     target_size=target_size,
                                     crop_size=crop_size)
            img = np.asarray(img, dtype=np.float32) * np.float32(1.0 / 255.0)
            outs = model.predict_on_batch(img[None])
            steer, coll = outs[0][0], outs[1][0]
            steer = np.max(steer)
            coll = 0 * coll
            img_origi = img_utils.load_img(current_name,
                                           target_size=(320, 640))
            out_mu = np.array([[0, 0, steer]])
            out_pi = np.array([[0, 0, 1]])
            # print(out_pi)
            # pi = sum_exp(out_pi, 1)
            pi = np.split(out_pi, 3, axis=1)
            # component_splits = [1, 1, 1]
            mus = np.split(out_mu, 3, axis=1)

            out_sigma = np.array([[0.1, 0.1, 0.1]], dtype='float32')
            sigs = np.split(out_sigma, 3, axis=1)

            x = np.linspace(-1, 1, 100)
            y = np.array([])
            for x_ in x:
                y = np.append(y, gaussian(sigs, mus, pi, x_))

            possible_direct = []
            possible_roll_speed = []

            start = 0
            continue_flag = 0
            sum_y = 0
            sum_x = 0
            for x_, y_ in zip(x, y):
                # print(point)
                if (y_ > 0.3):
                    if (continue_flag == 0):
                        continue_flag = 1
                        start = x_
                    sum_y = sum_y + y_
                    sum_x = sum_x + 1
                    y_ = (img_origi.shape[0] - y_ * 200 - 80).astype(np.int32)
                    x_ = ((x_ + 1) / 2 * img_origi.shape[1]).astype(np.int32)
                    x_ = img_origi.shape[1] - x_
                    #cv2.circle(img_origi, (x_, int(y_ / 2) + 150), 3, (0, 255, 0), 4)
                else:
                    if (continue_flag == 1):
                        continue_flag = 0
                        possible_direct.append((x_ + start) / 2)
                        possible_roll_speed.append((sum_y / sum_x - 1.) / 2)
                        sum_y = 0
                        sum_x = 0
                    y_ = (img_origi.shape[0] - y_ * 200 - 80).astype(np.int32)
                    x_ = ((x_ + 1) / 2 * img_origi.shape[1]).astype(np.int32)
                    x_ = img_origi.shape[1] - x_
                    #cv2.circle(img_origi, (x_, int(y_ / 2) + 150), 1, (255, 0, 255), 4)
            #            print("====Map_direct = {} ====".format(map_direct))
            map_direct = 0
            min_direct_diff = 180
            steer = 0.
            roll_speed_ = 0
            count = 0
            for possible_direct_ in possible_direct:
                # print(possible_direct_)
                cv2.line(
                    img_origi,
                    (int(img_origi.shape[1] / 2), img_origi.shape[0] - 50),
                    (int(img_origi.shape[1] / 2 -
                         math.tan(possible_direct_ * 3.14 / 2) * 100),
                     img_origi.shape[0] - 150), (0, 255, 0), 3)
                diff = abs(-possible_direct_ * 90 - map_direct)
                if (diff < min_direct_diff):
                    min_direct_diff = diff
                    steer = possible_direct_
                    roll_speed_ = possible_roll_speed[count]
                count = count + 1

            cv2.line(img_origi,
                     (int(img_origi.shape[1] / 2), img_origi.shape[0] - 50),
                     (int(img_origi.shape[1] / 2 -
                          math.tan(steer * 3.14 / 2) * 100),
                      img_origi.shape[0] - 150), (0, 255, 255), 3)
            cv2.imshow("img", img_origi / 255)
            cv2.imwrite(pics_path + '_save/dronet_' + img_name, img_origi)
            cv2.waitKey(10)
    except KeyboardInterrupt:
        print("calling to end")
示例#12
0
Licensed under the MIT License (see LICENSE for details)
"""
import os
import cv2
import sys
import numpy as np
localpath = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, localpath + '/../workflow/util/')
import img_utils as iu

path_in_flow0 = '/Users/nfinite/data/tfoptflow/KITTI12_resized/training/flow_noc/000004_10.flo'
path_in_img_1 = '/Users/nfinite/data/tfoptflow/KITTI12_resized/training/colored_0/000004_10.png'
path_in_img_2 = '/Users/nfinite/data/tfoptflow/KITTI12_resized/training/colored_0/000004_11.png'

img_1 = iu.load_img(path_in_img_1)
img_2 = iu.load_img(path_in_img_2)
flowr = iu.load_img(path_in_flow0, img_mode="flow")
go = True
point = (0, 0)


def store_xy(event, x, y):
    # grab references to the global variables
    global point, go

    # if the left mouse button was clicked, record the (x, y) coordinates
    if event == cv2.EVENT_LBUTTONDOWN:
        point = (x, y)
        go = False