def evaluate(model, config, data, execinfo, metrics, metrics_CV2, results,
             results_CV2, recompute):
    """Wave evaluation"""
    predict(model, config, data, execinfo, results, results_CV2, recompute)

    # IF IT'S NOT THE OVERALL EVALUATION
    leads = np.concatenate((pandas.Index(execinfo.test) + '_0',
                            pandas.Index(execinfo.test) + '_1'))

    retrieve_fiducials(results, leads, recompute)
    retrieve_fiducials(results_CV2, leads, recompute)

    ### COMPUTE METRICS ###
    metric_computation(config, data, metrics, results, execinfo.test,
                       recompute)
    metric_computation(config, data, metrics_CV2, results_CV2, execinfo.test,
                       recompute)

    ### SAVE RESULTS ###
    path_CV2 = os.path.splitext(
        execinfo.results)[0] + '_CV2' + os.path.splitext(execinfo.results)[1]
    save_results(metrics, config, execinfo.test, execinfo.results)
    save_results(metrics_CV2, config, execinfo.test, path_CV2)
    def start_video(self, model):
        camera = cv2.VideoCapture(0)
        while True:
            frame = camera.read()[1]
            if frame is None:
                continue
            image_array = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            selected_boxes = predict(model, image_array, prior_boxes,
                                     frame.shape[0:2], self.num_classes,
                                     self.lower_probability_threshold,
                                     self.iou_threshold, self.background_index,
                                     self.box_scale_factors)
            if selected_boxes is None:
                continue
            draw_video_boxes(selected_boxes, frame, self.arg_to_class,
                             self.colors, self.font)

            cv2.imshow('webcam', frame)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        camera.release()
        cv2.destroyAllWindows()
    num_classes = len(class_names)
    data_manager = DataManager(dataset_name, selected_classes, data_prefix,
                               image_prefix)
    ground_truth_data = data_manager.load_data()
    difficult_data_flags = data_manager.parser.difficult_objects

    image_names = sorted(list(ground_truth_data.keys()))
    # print('Number of images found:', len(image_names))
    for image_name in image_names:
        ground_truth_sample = ground_truth_data[image_name]
        image_prefix = data_manager.image_prefix
        image_path = image_prefix + image_name
        image_array, original_image_size = load_image(image_path, input_shape)
        image_array = preprocess_images(image_array)
        predicted_data = predict(model, image_array, prior_boxes,
                                 original_image_size, 21, class_threshold,
                                 iou_threshold)
        ground_truth_sample = denormalize_box(ground_truth_sample,
                                              original_image_size)
        ground_truth_boxes_in_image = len(ground_truth_sample)
        difficult_objects = difficult_data_flags[image_name]
        difficult_objects = np.asarray(difficult_objects, dtype=bool)
        num_ground_truth_boxes += np.sum(np.logical_not(difficult_objects))
        if predicted_data is None:
            # print('Zero predictions given for image:', image_name)
            continue
        #plt.imshow(original_image_array.astype('uint8'))
        #plt.show()
        #draw_image_boxes(predicted_data, original_image_array, class_decoder, normalized=False)
        #print(predicted_data.shape)
        num_predictions = len(predicted_data)
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")
Exemple #5
0
    ground_truth_sample = ground_truth_data[image_name]
    image_path = image_prefix + image_name

    rgb_image, image_size = load_image(image_path, target_size=(300, 300))
    pytorch_image = preprocess_pytorch_input(rgb_image)
    pytorch_output = pytorch_ssd(pytorch_image)
    p1 = pytorch_output[0].data.numpy()  # bounding boxes
    p2 = softmax(np.squeeze(pytorch_output[1].data.numpy()))  # classes
    p3 = pytorch_output[2].data.numpy()  # prior boxes
    # pytorch_detections = pytorch_output.data

    keras_image = preprocess_images(rgb_image)
    keras_image_input = np.expand_dims(keras_image, axis=0)
    keras_output = model.predict(keras_image_input)
    keras_detection = predict(model, keras_image, prior_boxes, image_size,
                              num_classes, lower_probability_threshold,
                              iou_threshold, background_index)

    keras_output = np.squeeze(keras_output)
    k1 = keras_output[:, :4]
    k2 = keras_output[:, 4:]
    k3 = prior_boxes

    diff = np.abs(p1 - k1)
    diff_mask = .0001 > diff
    all_good = np.all(diff_mask)
    print(all_good)
    if not all_good:
        print('*' * 30)
        print(image_name)
        print(all_good)