示例#1
0
def transform_labels(bbox_labels, sample_bbox):
    proj_bbox = bbox(0, 0, 0, 0)
    sample_labels = []
    for i in range(len(bbox_labels)):
        sample_label = []
        object_bbox = bbox(bbox_labels[i][1], bbox_labels[i][2],
                           bbox_labels[i][3], bbox_labels[i][4])
        if not meet_emit_constraint(object_bbox, sample_bbox):
            continue
        sample_width = sample_bbox.xmax - sample_bbox.xmin
        sample_height = sample_bbox.ymax - sample_bbox.ymin
        proj_bbox.xmin = (object_bbox.xmin - sample_bbox.xmin) / sample_width
        proj_bbox.ymin = (object_bbox.ymin - sample_bbox.ymin) / sample_height
        proj_bbox.xmax = (object_bbox.xmax - sample_bbox.xmin) / sample_width
        proj_bbox.ymax = (object_bbox.ymax - sample_bbox.ymin) / sample_height
        proj_bbox = clip_bbox(proj_bbox)
        if bbox_area(proj_bbox) > 0:
            sample_label.append(bbox_labels[i][0])
            sample_label.append(float(proj_bbox.xmin))
            sample_label.append(float(proj_bbox.ymin))
            sample_label.append(float(proj_bbox.xmax))
            sample_label.append(float(proj_bbox.ymax))
            sample_label.append(bbox_labels[i][5])
            sample_labels.append(sample_label)
    return sample_labels
示例#2
0
def proc_fit(name, k):
    im = cv2.imread(name)
    img = create_fixed_image_shape(bbox(im), out_shape,
                                   random_fill=False, mode='fit')
    new_name = name.replace(in_folder, out_folder)
    cv2.imwrite(new_name, img)
    if k % 10 == 0:
        print "Completed %d." % k
示例#3
0
def get_image_with_bbox(attrs):
    images = parse_file(config.get('deepfashion', 'attributes_file'),
                        val_type=int, key_item_id=None, validate_fields=False)
    attrs = parse_attr(attrs, _PREDEFINED_ATTR)
    filtered = filter_items(images, attrs)

    image_files = append_path(config.get('deepfashion', 'image_dir'), filtered, key='image_name')
    boxes = bbox(filtered)

    return image_files, boxes
示例#4
0
def satisfy_sample_constraint(sampler, sample_bbox, bbox_labels):
    if sampler.min_jaccard_overlap == 0 and sampler.max_jaccard_overlap == 0:
        return True
    for i in range(len(bbox_labels)):
        object_bbox = bbox(bbox_labels[i][1], bbox_labels[i][2],
                           bbox_labels[i][3], bbox_labels[i][4])
        overlap = jaccard_overlap(sample_bbox, object_bbox)
        if sampler.min_jaccard_overlap != 0 and overlap < sampler.min_jaccard_overlap:
            continue
        if sampler.max_jaccard_overlap != 0 and overlap > sampler.max_jaccard_overlap:
            continue
        return True
    return False
示例#5
0
def proc(name, k):
    im = cv2.imread(name)
    imre = create_fixed_image_shape(bbox(im), temp_shape,
                                    random_fill=False, mode='fit')
    imgs = data_augmentation(imre, frame_size=out_shape)
    for i in range(len(imgs)):
        img = imgs[i]
        imgs.append(img[:, ::-1, :])
    for i, img in enumerate(imgs):
        new_name = name.replace(in_folder, out_folder)
        new_name = new_name.replace('.jpeg', '_%d.jpeg' % i)
        cv2.imwrite(new_name, img)
    if k % 10 == 0:
        print "Completed %d." % k
示例#6
0
def generate_sample(sampler):
    scale = np.random.uniform(sampler.min_scale, sampler.max_scale)
    aspect_ratio = np.random.uniform(sampler.min_aspect_ratio,
                                     sampler.max_aspect_ratio)
    aspect_ratio = max(aspect_ratio, (scale**2.0))
    aspect_ratio = min(aspect_ratio, 1 / (scale**2.0))

    bbox_width = scale * (aspect_ratio**0.5)
    bbox_height = scale / (aspect_ratio**0.5)
    xmin_bound = 1 - bbox_width
    ymin_bound = 1 - bbox_height
    xmin = np.random.uniform(0, xmin_bound)
    ymin = np.random.uniform(0, ymin_bound)
    xmax = xmin + bbox_width
    ymax = ymin + bbox_height
    sampled_bbox = bbox(xmin, ymin, xmax, ymax)
    return sampled_bbox
def test_IDT_aux(depth_image, color_image, prev_silhouette, use_bbox=False, draw_contour=True,
                 threshold=0, real_time=True):
    normalized = normalize_depth_image(depth_image, threshold, amplify=True)

    bbox = []
    if use_bbox:
        color_image = np.expand_dims(color_image, axis=0)
        (frames_idx, bbox) = ut.bbox(color_image)
        if 0 not in frames_idx:
            # no person was detected in the frame
            bbox = []
        else:
            bbox = bbox[0]

    silhouette = get_img_silhouette(normalized, prev_silhouette, bbox)
    IDT = ut.getIDT(silhouette)

    if draw_contour:
        contour = ut.createCircleContour(IMG_WIDTH, IMG_HEIGHT)
        IDT_gradient = ut.getImgGradient(IDT)
        normals = ut.calc_normal(contour, normalize=False).astype(int)
        IDT_BGR = draw_contour_on_IDT(IDT, contour, normals, IDT_gradient)

        # convert IDT to int so that we can display it with cv.imshow
        IDT_BGR = cv.convertScaleAbs(IDT_BGR)

        silhouette_BGR = cv.cvtColor(silhouette, cv.COLOR_GRAY2BGR)
        normalized_BGR = cv.cvtColor(normalized, cv.COLOR_GRAY2BGR)
        result = np.hstack((normalized_BGR, silhouette_BGR, IDT_BGR))
    else:
        IDT = cv.convertScaleAbs(IDT)
        result = np.hstack((normalized, silhouette, IDT))

    # display result
    title_result = 'result: normalized, silhouette, IDT with threshold ' + \
        str(threshold)
    ut.imgDisplayCV(result, title_result)

    if not real_time:
        cv.waitKey(0)

    return silhouette
示例#8
0
def process_img(name, crop_shape, scale):
    ferr = open("out_%d.log" % os.getpid(), 'a')
    sys.stdout = ferr
    sys.stderr = ferr
    print "%s [%d] Processing file %s" % (get_time(), os.getpid(), name)
    a = cv2.imread(name)
    a = bbox(scaleRadius(a,scale))
    if a is None:
        ferr.close()
        return
    b = np.zeros(a.shape)
    cv2.circle(b,(a.shape[1]//2,a.shape[0]//2),int(scale*0.9),(1,1,1),-1,8,0)
    aa = cv2.addWeighted(a,4,cv2.GaussianBlur(a,(0,0),scale/30),-4,128)*b+128*(1-b)
    rand_im = random_crops(aa, shape=crop_shape)
    if "train/" in name:
        new_name = name.replace("train/", "%d_train/" % scale)
    elif "validation/" in name:
        new_name = name.replace("validation/", "%d_val/" % scale)
    cv2.imwrite(new_name,rand_im)
    ferr.close()
示例#9
0
def expand_image(img, bbox_labels, img_width, img_height):
    prob = np.random.uniform(0, 1)
    if prob < train_parameters['image_distort_strategy']['expand_prob']:
        expand_max_ratio = train_parameters['image_distort_strategy'][
            'expand_max_ratio']
        if expand_max_ratio - 1 >= 0.01:
            expand_ratio = np.random.uniform(1, expand_max_ratio)
            height = int(img_height * expand_ratio)
            width = int(img_width * expand_ratio)
            h_off = math.floor(np.random.uniform(0, height - img_height))
            w_off = math.floor(np.random.uniform(0, width - img_width))
            expand_bbox = bbox(-w_off / img_width, -h_off / img_height,
                               (width - w_off) / img_width,
                               (height - h_off) / img_height)
            expand_img = np.uint8(
                np.ones((height, width, 3)) * np.array([127.5, 127.5, 127.5]))
            expand_img = Image.fromarray(expand_img)
            expand_img.paste(img, (int(w_off), int(h_off)))
            bbox_labels = transform_labels(bbox_labels, expand_bbox)
            return expand_img, bbox_labels, width, height
    return img, bbox_labels, img_width, img_height
示例#10
0
    def loadImages(self, path):
        #load negative training images
        def loadNegative():
            negative_list = []
            for dirs in self.neg_dirs:
                for img_path in listdir(dirs):
                    base, ext = os.path.splitext(img_path)
                    if ext == '.jpg':
                        img = Image.open(dirs + '/' + img_path)
                        negative_list.append(img)
                    if self.args.verbose:
                        print('appending {} to {} list'.format(
                            img_path, 'negative'))
            return negative_list

        # return array of positive images
        imagesList = listdir(path)
        posImages, faces_bbox = [], []
        left_bbox, right_bbox = [], []

        #bounding boxes
        dict_combo = bbox()
        faces_dict, left_dict, right_dict = dict_combo[0], dict_combo[
            1], dict_combo[2]

        #load serially to ensure labels match
        for image in imagesList:
            img = Image.open(path + image)
            face = faces_dict[image]
            left = left_dict[image]
            right = right_dict[image]

            posImages.append(img)
            faces_bbox.append(face)
            left_bbox.append(left)
            right_bbox.append(right)
        negImages = loadNegative()
        return posImages, negImages, faces_bbox, left_bbox, right_bbox
示例#11
0
def testBbox(n=10):
    cap = cv.VideoCapture(0)
    img_seq = []
    for i in range(n):
        _, frame = cap.read()
        img_seq.append(frame)
    img_seq = np.stack(img_seq)

    (frames_idx, bbox) = ut.bbox(img_seq)

    n = 0
    for i, img in enumerate(img_seq):
        if i not in frames_idx:
            print('could not find any person in frame number ' + str(i) + '!')
            n += 1
            continue
        b = bbox[i - n]
        (y0, y1, x0, x1) = [int(p) for p in b]
        cut = img[y0:y1, x0:x1]

        ut.imgDisplayCV(cut, "cut")
        cv.waitKey(0)
    return
示例#12
0
    def loadImages(self, path):
        # return array of images
        imagesList = listdir(path)

        loadedImages, faces_bbox = [], []
        left_bbox, right_bbox = [], []

        #bounding boxes
        dict_combo = bbox()
        faces_dict, left_dict, right_dict = dict_combo[0], dict_combo[1],  dict_combo[2]

        #load serially to ensure labels match
        for image in imagesList:
            img = Image.open(path + image)
            face = faces_dict[image]
            left = left_dict[image]
            right = right_dict[image]

            loadedImages.append(img)
            faces_bbox.append(face)
            left_bbox.append(left)
            right_bbox.append(right)

        return loadedImages, faces_bbox, left_bbox, right_bbox