Example #1
0
def main():
    detector = Detector(net_id=NET_ARCH,
                        labels_net_arch=LABELS_NET,
                        labels_output=LABELS_OUT)
    rgb_files = _read_rgb_filenames(DATASET_DIR)
    rgb_files.sort()
    label_map = load_labels(LABELS_OUT)

    for idx, img_path in enumerate(rgb_files):
        print("\r[ %d / %d ] Processing %s" %
              (idx, len(rgb_files), os.path.basename(img_path)),
              end='',
              flush=True)
        img_rgb = np.array(Image.open(img_path))
        # run inference
        obj_detected = detector.run_inference_on_img(img_rgb)
        classes_remapped, scores_remapped, boxes_remapped = detector.remap_labels(
            obj_detected.classes, obj_detected.scores, obj_detected.boxes)

        if len(boxes_remapped) > 0:
            classes_remapped = np.squeeze(classes_remapped, axis=0)
            boxes_remapped = np.squeeze(boxes_remapped, axis=0)
            scores_remapped = np.squeeze(scores_remapped, axis=0)

        _create_xml_pascal(img_path, img_rgb, classes_remapped, boxes_remapped,
                           label_map)
Example #2
0
    def __init__(self, flags):
        # Set all flags
        self.flags = flags

        # Load all file names
        self.files = read_filenames(flags.dataset_dir, flags.filter_keywords,
                                    flags.main_sensor, flags.aux_sensor, 'png')

        # Load frozen rgb detector to create annotations
        self._detector = Detector(net_id=flags.net_arch,
                                  arch_config=flags.arch_config,
                                  labels_net_arch=flags.labels_net,
                                  labels_output=flags.labels_out,
                                  retrieval_thresh=flags.retrieval_thresh)

        # Encoder for tfrecords
        self.labels = load_labels(flags.labels_out)
        self._encoder = EncoderTFrecGoogleApi()
        self.output = os.path.join(flags.output_dir,
                                   flags.tfrecord_name_prefix + ".tfrecord")

        # Initialize filter
        self._learning_filter = LearningFilter(
            score_threshold=flags.lf_score_thresh,
            min_img_perimeter=flags.min_obj_size,
            logstats=True,
            mode=self.flags.lf_mode,
            verbose=self.flags.verbose)

        # Analyze the dataset
        self._analysis = self._analyze_dataset(flags, self.files)

        # Image Stats
        self._encoded_mean = []
        self._encoded_std = []
Example #3
0
def visualize():
    """
    Takes in images and xml labels in PASCAL VOC format. They must have the same filenames.
    Visualizes the bounding boxes on the images and saves them in the output folder.
    :return:
    """
    image_files = [
        f for f in os.listdir(IMG_INPUT_DIR)
        if f.endswith(".png") and all(key in f for key in FILTER_KEYWORDS)
    ]
    image_files.sort()
    xml_files = [f for f in os.listdir(XML_INPUT_DIR) if f.endswith(".xml")]
    xml_files.sort()
    classes = load_labels(LABEL_MAP)
    class_names = {
        classes[i]['name']: i
        for i in list(range(1,
                            len(classes) + 1))
    }
    if not os.path.isdir(OUTPUT_DIR):
        os.makedirs(OUTPUT_DIR)

    for idx, (img_name, xml_name) in enumerate(zip(image_files, xml_files)):
        # Parse XML Files
        xml_file = os.path.join(XML_INPUT_DIR, xml_name)
        xml_tree = ET.parse(xml_file).getroot()
        xml_data = _recursive_parse_xml_to_dict(xml_tree)

        # Create tfRecord dict instance
        img_file = os.path.join(IMG_INPUT_DIR, img_name)
        instance = dict_to_tf_instance(xml_data['annotation'], img_file,
                                       class_names)
        visualize_boxes_and_labels_on_image_array(
            instance['image'],
            instance['boxes'],
            instance['labels'],
            None,
            classes,
            use_normalized_coordinates=True,
            line_thickness=2)

        # Show Output
        if VERBOSE:
            cv2.imshow('Visualization',
                       cv2.cvtColor(instance['image'], cv2.COLOR_BGR2RGB))
            cv2.waitKey(5)
        print("\r[ %d / %d ] Processing %s" %
              (idx + 1, len(image_files), os.path.basename(img_file)),
              end='')

        # Save image as png
        output_file = os.path.join(OUTPUT_DIR, os.path.basename(img_file))
        img = Image.fromarray(instance['image'], 'RGB')
        img.save(output_file)

    print("\nDone!")
Example #4
0
 def __init__(self,
              tl_score_threshold,
              norm_wrt=(SILICON_EYE.height, SILICON_EYE.width),
              labels_file='zauron_label_map.json'):
     self._labels = load_labels(labels_file)
     self._n_instances = 0
     self._objstats = []
     self._norm_wrt = norm_wrt
     self._tl_score_thresh = tl_score_threshold
     self._keep_score_label_map = {
         'None': 'All',
         'False': 'Difficult',
         'True': 'Approved'
     }
Example #5
0
def plot_from_corestats(corestats, files, comparison_plot=False):
    """
    Iterates through all corestats and generates filenames and plots
    Uploads to google sheets if specified
    :param corestats:
    :param files:
    :param comparison_plot:
    :return:
    """
    if UPLOAD_TO_SHEETS:
        sheets = GoogleSheetsInterface()
    labels = load_labels(LABELS)
    AP, mAP = compute_scores_from_corestats(corestats)
    if comparison_plot:
        testnames = [os.path.splitext(os.path.basename(file))[0] for file in files]
        networks = []
        for testname in testnames:
            network, _ = split_testname(testname)
            networks.append(network)
        testname = ' & '
        testname = testname.join(networks)
        plots = plot_performance_metrics(corestats, AP, mAP, labels, testname,
                                        relative_bar_chart=True, label_filter=LABEL_FILTER, plot_bars=False)
        for idx in range(len(corestats)):
            print_stats(corestats[idx], testnames[idx], AP[idx], mAP[idx])
        pdf_file = os.path.join(OUTPUT_DIR, testname)
        save_plot(plots, pdf_file)
    else:
        for idx in range(len(corestats)):
            testname = os.path.splitext(os.path.basename(files[idx]))[0]
            network, testset = split_testname(testname)
            plots = plot_performance_metrics([corestats[idx]], [AP[idx]], [mAP[idx]], labels, network,
                                            relative_bar_chart=True, label_filter=LABEL_FILTER, plot_bars=False)
            print_stats(corestats[idx], testname, AP[idx], mAP[idx])
            pdf_file = os.path.join(OUTPUT_DIR, testname)
            save_plot(plots, pdf_file)
            if UPLOAD_TO_SHEETS:
                upload_results(sheets, network, testset, AP[idx], mAP[idx])
Example #6
0
def main():
    """
    Read images, run inference on them and exports them
    :return:
    """
    detector = Detector(net_id=NET_ARCH,
                        arch_config='zurich_networks',
                        labels_net_arch=LABELS_NET,
                        labels_output=LABELS_OUT,
                        retrieval_thresh=RET_THRESH)
    filenames = read_filenames(INPUT_DIR, FILTER_KEYWORDS, SENSOR_NAME, None)
    label_map = load_labels(LABELS_OUT)
    if not os.path.isdir(OUTPUT_DIR):
        os.makedirs(OUTPUT_DIR)

    for count, img_path in enumerate(filenames):
        print('\r[ %i ] Processing %s' % (count, os.path.basename(img_path)),
              end='',
              flush=True)
        img = np.array(Image.open(img_path))

        # Run Object Detection
        obj_detected = detector.run_inference_on_img(img)
        classes, scores, boxes = detector.remap_labels(obj_detected.classes,
                                                       obj_detected.scores,
                                                       obj_detected.boxes)

        if len(classes) > 0:
            classes = np.squeeze(classes, axis=0)
            scores = np.squeeze(scores, axis=0)
            boxes = np.squeeze(boxes, axis=0)

        # Add Visualizations
        visualize_boxes_and_labels_on_image_array(
            img,
            boxes,
            classes,
            scores,
            label_map,
            use_normalized_coordinates=True,
            alpha=scores,
            min_score_thresh=RET_THRESH)
        if TESTNAME is None:
            step = int(abs(NET_ARCH) / 10)
            net = ARCH_DICT[NET_ARCH].upper()
            sensor = SENSOR_NAME
            time = 'DAY' if 'day' in img_path else 'NIGHT' if 'night' in img_path else 'N/A'
            overlay_string = 'step:%s    network:%s    sensor:%s    time:%s' % (
                step, net, sensor, time)
        else:
            overlay_string = TESTNAME
        img = add_text_overlay(img, overlay_string, overlay=False, fontsize=14)

        if VERBOSE:
            cv2.imshow('Actual Frame', cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
            cv2.waitKey(1)

        # Save image as png
        output_file = os.path.join(OUTPUT_DIR, '%05d.png' % count)
        img = Image.fromarray(img, 'RGB')
        img.save(output_file)
def run_evaluation(flags):
    """
    Evaluates datasets for objects detected and uploads them to Google Sheet
    :param flags:
    :return:
    """
    stats = TLStatistician(tl_score_threshold=0)
    labels = load_labels(flags.labels_output)
    sheets = GoogleSheetsInterface()

    with tf.Session() as sess:
        # "Dataset"
        filenames_placeholder = tf.placeholder(tf.string, shape=[None])
        testfiles = [os.path.join(flags.input_dir, flags.filename)]
        dataset = tf.data.TFRecordDataset(filenames_placeholder)

        # Parsing input function takes care of reading and formatting the TFrecords
        dataset = dataset.map(_input_parser)
        dataset = dataset.repeat(1)
        dataset = dataset.batch(flags.batch_size)

        # Initialize input readers
        iterator = dataset.make_initializable_iterator()
        next_batch = iterator.get_next()
        sess.run(iterator.initializer,
                 feed_dict={filenames_placeholder: testfiles})
        batch_count = 0
        while True:
            try:
                # Read in next batch
                print('\r[ %i ] Analysing batches...' % batch_count,
                      end='',
                      flush=True)
                batch_in = sess.run(next_batch)
                img_in = batch_in['frame'][0, :]
                gt_labels = batch_in['gt_labels'][0, :]
                gt_boxes = batch_in['gt_boxes'][0, :]
                difficult_flag = batch_in['difficult_flag'][0, :]

                if len(difficult_flag) == 0 and len(gt_labels) > 0:
                    difficult_flag = [0] * len(gt_labels)

                # Add objects to stats
                for idx, class_id in enumerate(gt_labels):
                    stats.append_obj_stats(
                        label=class_id,
                        ymin=gt_boxes[idx][0],
                        xmin=gt_boxes[idx][1],
                        h=gt_boxes[idx][2] - gt_boxes[idx][0],
                        w=gt_boxes[idx][3] - gt_boxes[idx][1],
                        tl_score=0,
                        tl_difficult=abs(difficult_flag[idx] - 1))

                # Show example if verbose == True
                if flags.verbose:
                    img = np.copy(img_in)
                    visualize_boxes_and_labels_on_image_array(
                        img,
                        gt_boxes,
                        gt_labels,
                        None,
                        labels=labels,
                        use_normalized_coordinates=True,
                        difficult=difficult_flag)
                    plt.imshow(img)
                    plt.show()

                batch_count += 1
                stats.n_instances += 1

            except tf.errors.OutOfRangeError:
                break

        # Create plots to be stored in output folder
        output_name = os.path.splitext(flags.filename)[0]
        if flags.make_plots:
            if not os.path.isdir(flags.output_dir):
                os.makedirs(flags.output_dir)
            stats.make_plots(save_plots=True,
                             output_dir=flags.output_dir,
                             filename=output_name,
                             show_plots=flags.verbose,
                             labels_dict=labels)

        # Prepare data for upload to google sheets result page
        values = []
        for idx in range(len(labels)):
            number = len(stats.get_tlscores(label_filt=idx + 1,
                                            tl_keep_filt=1))
            diff = len(stats.get_tlscores(label_filt=idx + 1, tl_keep_filt=0))
            values.append('%d (%d)' % (number, diff))
        values.append(len(stats.get_tlscores()))
        sheets.upload_data(flags.google_sheet, 'B', 'I', output_name, values)
    return
    def __init__(self):
        self._labels = load_labels('zauron_label_map.json')

        # Only initialize in the instance that really needs it
        self._img_blender = None
def create_tfrecords_fromhandlabels(flags):
    """
    Recursively creates an tfrecord for each image that has been labeled
    Can be filtered by tags set in the flags
    :param flags:
    :return:
    """
    if flags.learning_filter:
        # Initialize filter
        _learning_filter = LearningFilter(
            score_threshold=flags.lf_score_thresh,
            min_img_perimeter=flags.min_obj_size,
            logstats=True,
            mode=flags.lf_mode,
            verbose=False)
        lf_base_images = [
            os.path.join(flags.image_src, f)
            for f in os.listdir(flags.image_src)
            if f.endswith(".png") and flags.filter_lf_base_keyword in f
        ]
        lf_base_images.sort()
    images = [
        os.path.join(flags.image_src, f) for f in os.listdir(flags.image_src)
        if f.endswith(".png") and flags.filter_keyword in f
    ]
    labels_xml = [
        os.path.join(flags.image_src_labels, f)
        for f in os.listdir(flags.image_src_labels) if f.endswith(".xml")
    ]
    images.sort()
    labels_xml.sort()
    assert len(images) == len(labels_xml), \
        "Uneven amounts of labels (%s) and images (%s) found!" % (len(labels_xml), len(images))
    print('Found %i labeled objects' % len(labels_xml))
    encoder = EncoderTFrecGoogleApi()

    # Check if object satisfies filter features
    # if flags.filter_keyword is not None:
    #     labels_xml = _filter_input(labels_xml, flags.filter_keyword)
    classes = load_labels(flags.labels_map)
    class_names = {
        classes[i]['name']: i
        for i in list(range(1,
                            len(classes) + 1))
    }
    print('Found %i objects satisfying condition: %s' %
          (len(labels_xml), flags.data_filter))

    if flags.output_file is not None:
        output = os.path.join(flags.output_dir, flags.output_file)
    else:
        file = '_'.join(flags.data_filter)
        extension = '_%s.tfrecord' % flags.image_type
        output = os.path.join(flags.output_dir, file + extension)

    # Parse labels and create tfrecord example
    try:
        with tf.python_io.TFRecordWriter(output) as writer:
            # Loop through objects and create tfrecords
            for idx, (img_file, xml_file) in enumerate(zip(images,
                                                           labels_xml)):
                xml_tree = ET.parse(xml_file).getroot()
                xml_data = _recursive_parse_xml_to_dict(xml_tree)

                instance = dict_to_tf_instance(xml_data['annotation'],
                                               img_file, class_names)

                # Apply learning filter if defined
                if flags.learning_filter:
                    img_main = np.array(Image.open(lf_base_images[idx]))
                    classes_filtered, boxes_filtered = _learning_filter.apply(
                        img_main, instance['image'], instance['labels'],
                        instance['boxes'])
                    instance['labels'] = classes_filtered
                    instance['boxes'] = boxes_filtered

                example = encoder.encode(instance, flags.difficult_flag)
                writer.write(example.SerializeToString())
                print('\r[ %i / %i ] %s' %
                      (idx + 1, len(labels_xml), os.path.basename(img_file)),
                      end='',
                      flush=True)
    except IOError as e:
        e.args += ('Failed attempting to serialize tfRecord file: %s' %
                   img_file)
    return