Exemplo n.º 1
0
def choose_positive_samples(image_dir, sample_size, bbinfo_dir):
    filtered_image_list = utils.list_images_in_directory(image_dir)

    # Filter out images without bounding boxes:
    global_info = training.loadGlobalInfo(bbinfo_dir)
    bbox_filter = lambda img_path: os.path.split(img_path)[1] in global_info
    filtered_image_list = filter(bbox_filter, filtered_image_list)

    # Extract image regions:
    img_regions = []
    for img_path in filtered_image_list:
        key = os.path.split(img_path)[1]
        rects = utils.rectangles_from_cache_string(global_info[key])
        for rect in rects:
            region = utils.ImageRegion(rect, img_path)
            img_regions.append(region)

    # Truncate region list to sample the correct number of images:
    if sample_size is None:
        random.shuffle(img_regions)
    else:
        img_regions = random.sample(img_regions, min(sample_size, len(img_regions)))

    if len(img_regions) < sample_size:
        raise ValueError(
            "Requested {} positive samples, but only {} could be found in '{}'!".format(
                sample_size, len(img_regions), image_dir
            )
        )

    return img_regions
Exemplo n.º 2
0
def choose_positive_samples(image_dir, sample_size, bbinfo_dir):
    filtered_image_list = utils.list_images_in_directory(image_dir)

    # Filter out images without bounding boxes:
    global_info = training.loadGlobalInfo(bbinfo_dir)
    bbox_filter = lambda img_path: os.path.split(img_path)[1] in global_info
    filtered_image_list = filter(bbox_filter, filtered_image_list)

    # Extract image regions:
    img_regions = []
    for img_path in filtered_image_list:
        key = os.path.split(img_path)[1]
        rects = utils.rectangles_from_cache_string(global_info[key])
        for rect in rects:
            region = utils.ImageRegion(rect, img_path)
            img_regions.append(region)

    # Truncate region list to sample the correct number of images:
    if sample_size is None:
        random.shuffle(img_regions)
    else:
        img_regions = random.sample(img_regions,
                                    min(sample_size, len(img_regions)))

    if len(img_regions) < sample_size:
        raise ValueError(
            'Requested {} positive samples, but only {} could be found in \'{}\'!'
            .format(sample_size, len(img_regions), image_dir))

    return img_regions
Exemplo n.º 3
0
def view_positive_samples(classifier_yaml, output_dir):
    bbinfo_dir = classifier_yaml['dataset']['directory']['bbinfo']

    global_info = utils.load_opencv_bounding_box_info_directory(
        bbinfo_dir, suffix='bbinfo')
    # global_info = loadGlobalInfo(bbinfo_dir)

    positive_dir = classifier_yaml['dataset']['directory']['positive']

    # for img_path in glob.glob("{}/*_*.jpg".format(positive_dir)):
    # for img_path in utils.list_images_in_directory(positive_dir):

    pos_samples = sampleTrainingImages(positive_dir, ['.*'],
                                       None,
                                       require_bboxes=True,
                                       bbinfo_dir=bbinfo_dir)

    print 'Selected {} positive samples.'.format(len(pos_samples))

    for img_path in pos_samples:
        img = cv2.imread(img_path)

        key = img_path.split('/')[-1]
        rects_str = global_info[key]
        rects = utils.rectangles_from_cache_string(rects_str)

        print img_path, len(rects)
        for rect in rects:
            lw = max(2, min(img.shape[0], img.shape[1]) / 150)
            cvDrawRectangle(img, rect, (255, 0, 0), lw)
            # cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,255),lw+2)
            # cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),lw)

            # aspectRect = gm.extendBoundingBox(rect, 83/64.0)
            # cvDrawRectangle(img, aspectRect, (0,255,0),2)
            #
            # paddedRect = gm.padBoundingBox(aspectRect, (0.1, 0.1))
            # cvDrawRectangle(img, paddedRect, (0,0,255),2)

            h, w = img.shape[:2]
            max_dim = 1200.0
            # if w > max_dim + 50 or h > max_dim + 50:
            if abs(w - max_dim) > 50 or abs(h - max_dim) > 50:
                sx = max_dim / w
                sy = max_dim / h
                current_scale = min(sx, sy)
                img = cv2.resize(img,
                                 dsize=None,
                                 fx=current_scale,
                                 fy=current_scale)

        cv2.imshow('img', img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
Exemplo n.º 4
0
def view_positive_samples(classifier_yaml, output_dir):
    bbinfo_dir = classifier_yaml['dataset']['directory']['bbinfo']

    global_info = utils.load_opencv_bounding_box_info_directory(bbinfo_dir, suffix='bbinfo')
    # global_info = loadGlobalInfo(bbinfo_dir)

    positive_dir = classifier_yaml['dataset']['directory']['positive']

    # for img_path in glob.glob("{}/*_*.jpg".format(positive_dir)):
    # for img_path in utils.list_images_in_directory(positive_dir):

    pos_samples = sampleTrainingImages(positive_dir, ['.*'], None, require_bboxes=True, bbinfo_dir=bbinfo_dir)

    print 'Selected {} positive samples.'.format(len(pos_samples))

    for img_path in pos_samples:
        img = cv2.imread(img_path)

        key = img_path.split('/')[-1]
        rects_str = global_info[key]
        rects = utils.rectangles_from_cache_string(rects_str)

        print img_path, len(rects)
        for rect in rects:
            lw = max(2, min(img.shape[0], img.shape[1]) / 150)
            cvDrawRectangle(img, rect, (255,0,0), lw)
            # cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,255),lw+2)
            # cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),lw)


            # aspectRect = gm.extendBoundingBox(rect, 83/64.0)
            # cvDrawRectangle(img, aspectRect, (0,255,0),2)
            #
            # paddedRect = gm.padBoundingBox(aspectRect, (0.1, 0.1))
            # cvDrawRectangle(img, paddedRect, (0,0,255),2)

            h, w = img.shape[:2]
            max_dim = 1200.0
            # if w > max_dim + 50 or h > max_dim + 50:
            if abs(w - max_dim) > 50 or abs(h - max_dim) > 50:
                sx = max_dim / w
                sy = max_dim / h
                current_scale = min(sx, sy)
                img = cv2.resize(img, dsize=None, fx=current_scale, fy=current_scale)

        cv2.imshow('img', img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()