Exemple #1
0
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        camera.release()
        cv2.destroyAllWindows()


if __name__ == "__main__":

    from models.ssd import SSD300
    from utils.boxes import create_prior_boxes
    from utils.boxes import to_point_form

    dataset_name = 'VOC2007'
    # weights_path = '../trained_models/SSD300_weights.hdf5'
    # weights_path = '../trained_models/weights.07-3.59.hdf5'
    # weights_path = '../trained_models/weights.03-3.37.hdf5'
    # weights_path = '../trained_models/weights.150-3.57.hdf5'
    # weights_path = '../trained_models/weights.12-4.20.hdf5'
    # weights_path = '../trained_models/weights.02-3.44.hdf5'
    # weights_path = '../trained_models/weights.22-5.01.hdf5'
    # weights_path = '../trained_models/weights.79-6.66.hdf5'
    # weights_path = '../trained_models/weights.64-6.52.hdf5'
    # weights_path = '../trained_models/weights.22-3.85.hdf5'
    # weights_path = '../trained_models/weights.50-3.92.hdf5'
    weights_path = '../trained_models/weights.04-3.79.hdf5'
    model = SSD300(weights_path=weights_path)
    prior_boxes = to_point_form(create_prior_boxes())
    # prior_boxes = to_point_form(prior_boxes)
    video = VideoDemo(prior_boxes, dataset_name)
    video.start_video(model)
Exemple #2
0
    # Preprocess precision to be a non-decreasing array
    for i in range(len(precision) - 2, -1, -1):
        precision[i] = np.maximum(precision[i], precision[i + 1])

    indices = np.where(recall[1:] != recall[:-1])[0] + 1
    average_precision = np.sum(
            (recall[indices] - recall[indices - 1]) * precision[indices])
    return average_precision

dataset_name = 'VOC2007'
data_prefix = '../datasets/VOCtest/VOCdevkit/VOC2007/Annotations/'
image_prefix = '../datasets/VOCtest/VOCdevkit/VOC2007/JPEGImages/'
weights_path = '../trained_models/weights_SSD300.hdf5'
model = SSD300(weights_path=weights_path)
prior_boxes = create_prior_boxes(model)
input_shape = model.input_shape[1:3]
class_threshold = .1
iou_threshold = .5


average_precisions = []
for ground_truth_class_arg in range(1, 21):
    labels = []
    scores = []
    class_names = get_class_names(dataset_name)
    #ground_truth_class_arg = class_arg
    selected_classes = [class_names[0]] + [class_names[ground_truth_class_arg]]
    num_ground_truth_boxes = 0
    class_decoder = get_arg_to_class(class_names)
    num_classes = len(class_names)
Exemple #3
0
from preprocessing import get_image_size
from models import SSD300
from metrics import compute_average_precision
from metrics import compute_precision_and_recall

from utils.boxes import create_prior_boxes
from utils.boxes import calculate_intersection_over_union
from utils.boxes import denormalize_boxes
from utils.inference import infer_from_path

dataset_name = 'VOC2007'
image_prefix = '../datasets/VOCdevkit/VOC2007/JPEGImages/'
weights_path = '../trained_models/SSD300_weights.hdf5'
model = SSD300(weights_path=weights_path)

prior_boxes = create_prior_boxes()
input_shape = model.input_shape[1:3]
class_threshold = .1
iou_nms_threshold = .45
iou_threshold = .5
num_classes = 21

image_prefix = '../datasets/VOCdevkit/VOC2007/JPEGImages/'
with_difficult_objects = False
split = 'test'

class_names = get_class_names(dataset_name)
class_names = class_names[1:]
average_precisions = []
for class_name in class_names:
    selected_classes = ['background'] + [class_name]
Exemple #4
0
def _main(args):
    start_time = timer()
    input_path = os.path.expanduser(args.input_path)
    output_path = os.path.expanduser(args.output_path)

    if not os.path.exists(output_path):
        print('Creating output path {}'.format(output_path))
        os.mkdir(output_path)

    logging.basicConfig(filename=output_path + "/tracking.log", level=logging.DEBUG)

    #parse car positions and angles
    print("Parsing timestamps and oxts files...")
    if args.oxts.startswith('..'):
        parse_oxts(input_path + "/" + args.oxts, input_path + "/" + args.time_stamps)
    else:
        parse_oxts(args.oxts, args.time_stamps)
    print("Done. Data acquired.")

    dataset_name = 'VOC2007'
    NUM_CLASSES = 21

    weights_filename = args.model_path
    model = SSD300(num_classes=NUM_CLASSES)
    prior_boxes = create_prior_boxes(model)
    model.load_weights(weights_filename)

    # drawing stuff

    # Generate colors for drawing bounding boxes.
    hsv_tuples = [(x / NUM_CLASSES, 1., 1.)
                  for x in range(NUM_CLASSES)]
    colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
    colors = list(
        map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
            colors))
    random.seed(10101)  # Fixed seed for consistent colors across runs.
    random.shuffle(colors)  # Shuffle colors to decorrelate adjacent classes.
    random.seed(None)  # Reset seed to default.

    class_names = get_class_names(dataset_name)
    box_scale_factors=[.1, .1, .2, .2] # maybe adjust to our image size
    background_index=0
    lower_probability_threshold=.1
    iou_threshold=.2
    num_classes = len(class_names) # sould be equal to NUM_CLASSES
    arg_to_class = dict(zip(list(range(num_classes)), class_names))

    frame_idx = 0

    for image_file in os.listdir(input_path):
        try:
            image_type = imghdr.what(os.path.join(input_path, image_file))
            if not image_type:
                print("frame dropped")
                continue
        except IsADirectoryError:
            print("frame dropped")
            continue
        image = Image.open(os.path.join(input_path, image_file))
        image_data = np.array(image)

        selected_boxes = predict(model, image_data, prior_boxes,
                                image_data.shape[0:2], num_classes,
                                lower_probability_threshold,
                                iou_threshold,
                                background_index,
                                box_scale_factors)
        if selected_boxes is not None:
            x_mins = selected_boxes[:, 0]
            y_mins = selected_boxes[:, 1]
            x_maxs = selected_boxes[:, 2]
            y_maxs = selected_boxes[:, 3]
            classes = selected_boxes[:, 4:]
            num_boxes = len(selected_boxes) 
        else:
            num_boxes = 0
            print("frame dropped, no boxes")

        print('Found {} boxes for {}'.format(num_boxes, image_file))

        font = ImageFont.truetype(
            font='font/FiraMono-Medium.otf',
            size=11) # np.floor(3e-2 * image.size[1] + 0.5).astype('int32')
        thickness = (image_data.shape[0] + image_data.shape[1]) // 300

        logging.info("Img: " + str(image_file))

        boxes_data = []

        for i in range(num_boxes):
            xmin = int(x_mins[i])
            ymin = int(y_mins[i])
            xmax = int(x_maxs[i])
            ymax = int(y_maxs[i])
            box_class_scores = classes[i]
            label_class = np.argmax(box_class_scores)
            score = box_class_scores[label_class]
            predicted_class = arg_to_class[label_class]

            box = [ymin, xmin, ymax, xmax]
            box = [max(0,v) for v in box] # sometimes it's negative.

            # log positions

            obj_coord = np.array([])

            if predicted_class in ["person", "bicycle", "car", "motorbike", "bus", "train", "truck"] and score > 0.2: #object and classes to track
                if predicted_class in ["bus", "bruck"]: #vehicle
                    predicted_class = "car"
                obj_coord = computeCoordinates(box, frame_idx)
                if obj_coord is not None:
                    hist = histogram(image_data, box)
                    #create data and store it
                    boxes_data.append({
                        'predicted_class': predicted_class,
                        'score': float(score),
                        'coord': obj_coord,
                        'hist': hist
                        })
                    logging.info(predicted_class + " :" + str(obj_coord) + " | " + str(np.linalg.norm(obj_coord)))

            # end log positions
            if saveImages:
                if obj_coord is not None:
                    label = '{} {:.2f} {} {:.2f}'.format(predicted_class, score, str(obj_coord), np.linalg.norm(obj_coord))
                else:
                    label = '{} {:.2f}'.format(predicted_class, score)
                draw = ImageDraw.Draw(image)
                label_size = draw.textsize(label, font)

                top, left, bottom, right = box
                top = max(0, np.floor(top + 0.5).astype('int32'))
                left = max(0, np.floor(left + 0.5).astype('int32'))
                bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
                right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
                print(label, (left, top), (right, bottom))

                if top - label_size[1] >= 0:
                    text_origin = np.array([left, top - label_size[1]])
                else:
                    text_origin = np.array([left, top + 1])

                for i in range(thickness):
                    draw.rectangle(
                        [left + i, top + i, right - i, bottom - i],
                        outline=colors[label_class])
                draw.rectangle(
                    [tuple(text_origin), tuple(text_origin + label_size)],
                    fill=colors[label_class])
                draw.text(text_origin, label, fill=(0, 0, 0), font=font)
                del draw

        frame_idx += 1
        global data_frames
        data_frames.append(boxes_data)
        if saveImages:
            image.save(os.path.join(output_path, image_file), quality=80)

    now = timer()
    start_trj_time = timer()
    print("Time elapsed CNN: " + str(now - start_time) + " seconds")
    print("Calculating trajectories...")
    calculate_trajectories()

    now = timer()
    print("Done. Time elapsed: " + str(now - start_trj_time) + " seconds\n\n")
    print("Total time elapsed: " + str(now - start_time) + " seconds")