def generate_generator_mask(generator, path, batch_size = 8, img_height = IMG_HEIGHT, img_width = IMG_WIDTH):

        gen_img = generator.flow_from_directory(path,
                                              classes = ["images"],
                                              target_size = (img_height,img_width),
                                              batch_size = batch_size,
                                              shuffle=True, 
                                              seed=7)

        gen_mask = generator.flow_from_directory(path,
                                              classes = ["masks"],
                                              target_size = (img_height,img_width),
                                              batch_size = batch_size,
                                              color_mode = 'grayscale',
                                              shuffle=True, 
                                              seed=7)
        while True:
                imgs, _ = gen_img.next() #in 255
                if TRAIN_PREPROC == "hsv":
                    imgs = rgb2hsv(imgs)
                elif TRAIN_PREPROC == "hsv_norm":
                    imgs = rgb2hsv(imgs, normalization = True)
                elif TRAIN_PREPROC == "gray":
                    imgs = np.expand_dims(rgb2gray(imgs),axis = -1)
                masks, _ = gen_mask.next()
                masks = masks.squeeze()
  
                yield imgs, masks #Yield both images and their mutual label
Example #2
0
 def histogramHSV(self):
     histogram = []
     for i in range(self.height):
         for j in range(self.width):
             r, g, b = self.getpixel((j, i))
             hsv = rgb2hsv(r, g, b)
             histogram[int(hsv[0])] += 1
     return histogram
Example #3
0
	def test_imageMatch(self):
		img_prot = load('orig/kueche.jpg')
		rgb_prot = img_prot.getdata()
		hsv_prot = rgb2hsv(rgb_prot)
		nc = 16
		filt = Filter(nc)
		hc = clust1d([x[0] for x in hsv_prot], nc, 1000)
		add_palettes(img_prot, hc)
		img_prot.show()
		filt.hues = hc
		filt.update()
Example #4
0
	def setUpClass(cls):
		# store test images in dict
		cls.imgs = {}
		cls.hsv = {}
		print 
		logging.info('Init images')
		# palette
		img = gen_hs(1.0)
		hsv_data = rgb2hsv(img.getdata())
		cls.imgs['palette'] = img
		cls.hsv['palette'] = hsv_data
Example #5
0
args = parser.parse_args()

print("model loaded.")

d_img = path.join(args.target_ds, "images") + "/*"
d_mask = path.join(args.target_ds, "masks") + "/*"
img_paths = sorted(glob.glob(d_img))
mask_paths = sorted(glob.glob(d_mask))

j, wrong_count = 0, 0
y_pred, y_true = [], []
print(img_paths)
for img_path, mask_path in zip(img_paths, mask_paths):

    img = load_test_image(img_path, dsize=(IMG_HEIGHT, IMG_WIDTH))
    img_hsv = rgb2hsv(img, normalization=True)
    mask = pb_predict_mask(sess, img) > CLOUD_THRES
    #mask = pb_predict_window_mask(sess, img_hsv, window_size = SLIDING_WINDOW_SIZE) > CLOUD_THRES
    #mask = np.zeros_like(img_hsv[...,0])
    mask_pct = np.sum(mask) / (mask.shape[0] * mask.shape[1]) * 100
    gt_mask = load_mask(mask_path,
                        dsize=(IMG_HEIGHT, IMG_WIDTH)) / 255.0 > CLOUD_THRES
    gt_mask_pct = np.sum(gt_mask) / (gt_mask.shape[0] * gt_mask.shape[1]) * 100
    y_pred.append(mask_pct)
    y_true.append(gt_mask_pct)

    if pct_to_label(mask_pct) != pct_to_label(gt_mask_pct):
        wrong_count += 1
        print("Wrong Prediction. {} has around {}% of sky area. Predicted:{}%".
              format(img_path, PCT_LVL[pct_to_label(gt_mask_pct)],
                     PCT_LVL[pct_to_label(mask_pct)]))
Example #6
0
plt.imshow(image)

# 把mxn的矩阵像素转为一维像素
image = image.reshape((image.shape[0] * image.shape[1], 3))

# 使用KMeans算法找到图片的主要颜色
clt = KMeans(n_clusters=args["clusters"])
clt.fit(image)

# # 在utils.py中定义,绘制主要颜色条
hist = utils.centroid_histogram(clt)
bar = utils.plot_colors(hist, clt.cluster_centers_)

for (percent, color) in zip(hist, clt.cluster_centers_):
    print(color)
    print(utils.rgb2hsv(color[0], color[1], color[2]))

# print(hist)
# colors = list(set([tuple(t) for t in bar[0]]))
# print(colors)
# for color in colors:
#     hsv = colorsys.rgb_to_hsv(color[0] / 255, color[1] / 255, color[2] / 255)
#     print(int(hsv[0] * 360), int(hsv[1] * 100), int(hsv[1] * 100))
#     result = hsb.findColor(
#         int(hsv[0] * 360), int(hsv[1] * 100), int(hsv[1] * 100))
#     if len(result) > 0:
#         print(result[0]['name'])
#     else:
#         print('深色衣物')

# 显示
Example #7
0
    def process(self,
                images_alpha,
                uvmaps_alpha,
                uvmap_gts,
                vertices,
                coeffs,
                uv_gt=True):

        # zero optimizers
        self.gen_optimizer.zero_grad()
        if self.config.adv_weight > 0:
            self.im_dis_optimizer.zero_grad()
            self.uv_dis_optimizer.zero_grad()

        # process outputs
        images = images_alpha[:, :3].contiguous()
        im_skins = images_alpha[:, 3:4].contiguous()

        gen_uvmaps, renders_alpha, lights = self(images, uvmaps_alpha,
                                                 vertices, coeffs)
        renders = renders_alpha[:, :3]
        renders_mask = renders_alpha[:, 3:]

        gen_loss = torch.tensor(0, dtype=torch.float32, device=self.device)
        im_dis_loss = torch.tensor(0, dtype=torch.float32, device=self.device)
        uv_dis_loss = torch.tensor(0, dtype=torch.float32, device=self.device)
        im_gen_gan_loss = torch.tensor(0,
                                       dtype=torch.float32,
                                       device=self.device)
        uv_gen_gan_loss = torch.tensor(0,
                                       dtype=torch.float32,
                                       device=self.device)
        gen_uv_std_loss = torch.tensor(0,
                                       dtype=torch.float32,
                                       device=self.device)
        gen_uv_sym_loss = torch.tensor(0,
                                       dtype=torch.float32,
                                       device=self.device)
        gen_rd_l1_loss = torch.tensor(0,
                                      dtype=torch.float32,
                                      device=self.device)
        gen_rd_style_loss = torch.tensor(0,
                                         dtype=torch.float32,
                                         device=self.device)
        gen_uv_style_loss = torch.tensor(0,
                                         dtype=torch.float32,
                                         device=self.device)
        gen_uv_content_loss = torch.tensor(0,
                                           dtype=torch.float32,
                                           device=self.device)
        gen_uv_l1_loss = torch.tensor(0,
                                      dtype=torch.float32,
                                      device=self.device)

        double_image = torch.cat([images, torch.flip(images, (3, ))], dim=0)
        double_skins = torch.cat(
            [im_skins, torch.flip(im_skins, (3, ))], dim=0)
        render_mask = renders_mask * double_skins
        uv_merged = double_image * (1 - render_mask) + renders * render_mask
        uv_merged = uv_merged.contiguous()
        self.imsave('tmp/train/full_render.png', uv_merged[0, :3])
        self.imsave('tmp/train/full_render_flip.png',
                    uv_merged[self.batch_size, :3])
        self.imsave('tmp/train/image_mask.png', double_skins[0, :3])
        self.imsave('tmp/train/image_mask_flip.png',
                    double_skins[self.batch_size, :3])
        self.imsave('tmp/train/renders_mask.png', renders_mask[0, 0])
        self.imsave('tmp/train/render_mask.png', render_mask[0, 0])
        self.imsave('tmp/train/image.png', double_image[0, :3])
        self.imsave('tmp/train/image_flip.png',
                    double_image[self.batch_size, :3])

        if self.config.adv_weight > 0:
            # discriminator loss
            im_dis_input_real = images
            im_dis_input_fake = uv_merged[:self.batch_size].detach()
            self.imsave('tmp/train/im_dis_input_real.png',
                        im_dis_input_real[0, :3])
            self.imsave('tmp/train/im_dis_input_fake.png',
                        im_dis_input_fake[0, :3])
            im_dis_real = self.image_disc(im_dis_input_real)
            im_dis_fake = self.image_disc(im_dis_input_fake)

            if self.config.gan_loss == 'wgan':
                im_dis_real_loss = -torch.mean(im_dis_real)
                im_dis_fake_loss = torch.mean(im_dis_fake)
                im_dis_gp = self.calculate_gradient_penalty(
                    self.image_disc, im_dis_input_real, im_dis_input_fake)
                im_dis_loss += (im_dis_real_loss + im_dis_fake_loss +
                                im_dis_gp) * self.config.adv_weight
            else:
                im_dis_real_loss = self.adversarial_loss(
                    im_dis_real, True, True)
                im_dis_fake_loss = self.adversarial_loss(
                    im_dis_fake, False, True)
                im_dis_loss += (im_dis_real_loss + im_dis_fake_loss) / 2

            uv_dis_input_real = uvmap_gts
            if not uv_gt:
                uv_dis_input_real = torch.flip(uv_dis_input_real, (3, ))
            self.imsave('tmp/train/uv_dis_input_real.png',
                        uv_dis_input_real[0, :3])
            uv_dis_real = self.uvmap_disc(uv_dis_input_real)

            uv_dis_input_fake_1 = gen_uvmaps.detach()
            self.imsave('tmp/train/uv_dis_input_fake_1.png',
                        uv_dis_input_fake_1[0, :3])
            uv_dis_fake_1 = self.uvmap_disc(uv_dis_input_fake_1)

            if self.config.gan_loss == 'wgan':
                uv_dis_real_loss = -torch.mean(uv_dis_real)
                uv_dis_fake_loss_1 = torch.mean(uv_dis_fake_1)
                uv_dis_gp = self.calculate_gradient_penalty(
                    self.uvmap_disc, uv_dis_input_real, uv_dis_input_fake_1)
                uv_dis_loss += (uv_dis_real_loss + uv_dis_fake_loss_1 +
                                uv_dis_gp) * self.config.adv_weight
            else:
                uv_dis_real_loss = self.adversarial_loss(
                    uv_dis_real, True, True)
                uv_dis_fake_loss_1 = self.adversarial_loss(
                    uv_dis_fake_1, False, True)
                uv_dis_loss += (uv_dis_real_loss + uv_dis_fake_loss_1) / 2

            # generator adversarial loss
            im_gen_input_fake = uv_merged[:self.batch_size]
            self.imsave('tmp/train/im_gen_input_fake.png',
                        im_gen_input_fake[0, :3])
            im_gen_fake = self.image_disc(im_gen_input_fake)
            if self.config.gan_loss == 'wgan':
                im_gen_gan_loss = -torch.mean(im_gen_fake)
            else:
                im_gen_gan_loss = self.adversarial_loss(
                    im_gen_fake, True, False) * self.config.adv_weight
            gen_loss += im_gen_gan_loss

            uv_gen_input_fake = gen_uvmaps
            self.imsave('tmp/train/uv_gen_input_fake.png',
                        uv_gen_input_fake[0, :3])
            uv_gen_fake = self.uvmap_disc(uv_gen_input_fake)
            if self.config.gan_loss == 'wgan':
                uv_gen_gan_loss = -torch.mean(uv_gen_fake)
            else:
                uv_gen_gan_loss = self.adversarial_loss(
                    uv_gen_fake, True, False) * self.config.adv_weight
            gen_loss += uv_gen_gan_loss

        #* Other Losses
        if self.config.sym_weight > 0 or self.config.std_weight > 0:
            blur_gen_uvs = gaussian_blur(
                gen_uvmaps, (self.uv_size // 8 + 1, self.uv_size // 8 + 1),
                (self.uv_size // 32, self.uv_size // 32))
            self.imsave('tmp/train/blur_gen_uv.png', blur_gen_uvs[0, :3])

            # generator symmetry loss
            if self.config.sym_weight > 0:
                flipped_uv = torch.flip(blur_gen_uvs, dims=(3, ))
                gen_uv_sym_loss = self.l1_loss(blur_gen_uvs, flipped_uv)
                self.imsave('tmp/train/uv_flip.png', flipped_uv[0, :3])
                gen_loss += gen_uv_sym_loss * self.config.sym_weight

            # generator variance loss
            if self.config.std_weight > 0:
                blur_uv_hsv = utils.rgb2hsv(blur_gen_uvs)
                gen_uv_std_loss = torch.mean(
                    torch.std(blur_uv_hsv[:, :,
                                          self.skin_ear_mask.type(torch.bool)],
                              dim=-1))
                blur_gen_uvs_for_lip = gaussian_blur(
                    gen_uvmaps,
                    (self.uv_size // 32 + 1, self.uv_size // 32 + 1),
                    (self.uv_size // 64, self.uv_size // 64))
                gen_uv_std_loss += torch.mean(
                    torch.std(
                        blur_gen_uvs_for_lip[:, :,
                                             self.lip_mask.type(torch.bool)],
                        dim=-1)) * 0.05

                gen_loss += gen_uv_std_loss * self.config.std_weight

        if uv_gt:
            self.imsave('tmp/train/uvmap_gens.png', gen_uvmaps[0, :3])
            self.imsave('tmp/train/uvmap_gts.png', uvmap_gts[0, :3])
            # generator l1 loss uvmap
            if self.config.l1_weight > 0:
                gen_uv_l1_loss = self.l1_loss(gen_uvmaps, uvmap_gts)
                gen_loss += gen_uv_l1_loss * self.config.l1_weight * 3

            # generator perceptual loss
            if self.config.con_weight > 0:
                gen_uv_content_loss = self.perceptual_loss(
                    gen_uvmaps, uvmap_gts)
                gen_loss += gen_uv_content_loss * self.config.con_weight

            # generator style loss
            if self.config.sty_weight > 0:
                gen_uv_style_loss = self.style_loss(gen_uvmaps, uvmap_gts)
                gen_loss += gen_uv_style_loss * self.config.sty_weight

        # rendered L1 loss
        if self.config.l1_weight > 0:
            gen_rd_l1_loss = self.l1_loss(double_image, uv_merged)
            gen_loss += gen_rd_l1_loss * self.config.l1_weight

        if self.config.sty_weight > 0:
            gen_rd_style_loss = self.style_loss(double_image, uv_merged)
            gen_loss += gen_rd_style_loss * self.config.sty_weight

        gen_loss += torch.mean(
            torch.std(lights[:, 0:3], dim=-1) +
            torch.std(lights[:, 3:6], dim=-1) * 0.3)

        # create logs
        logs = {
            'im_d': im_dis_loss.item(),
            'uv_d': uv_dis_loss.item(),
            'im_g': im_gen_gan_loss.item(),
            'uv_g': uv_gen_gan_loss.item(),
            'uv_std': gen_uv_std_loss.item(),
            'uv_sym': gen_uv_sym_loss.item(),
            'rd_l1': gen_rd_l1_loss.item(),
            'rd_sty': gen_rd_style_loss.item()
        }
        if uv_gt:
            logs['uv_sty'] = gen_uv_style_loss.item()
            logs['uv_con'] = gen_uv_content_loss.item()
            logs['uv_l1'] = gen_uv_l1_loss.item()

        return gen_uvmaps, gen_loss, im_dis_loss, uv_dis_loss, logs
Example #8
0
        # If denoise chambolle flag is set
        if F_DENOISE:
            orig_img = denoise_tv_chambolle(orig_img, weight=0.2, multichannel=True)

        # If histogram equalization flag is set
        if F_EQ_HIST:
            orig_img = equalize_hist(orig_img)

        # Get traffic signal patch
        # orig_traf_sign = get_patch(orig_img, d['tlx'], d['tly'], d['brx'], d['bry'])

        # Convert traffic signal image to HSV color space
        # patch_hsv = rgb2hsv(orig_traf_sign)

        # Convert image to HSV color space
        orig_img_hsv = ut.rgb2hsv(orig_img)

        # Get Hue channel
        h = orig_img_hsv[..., 0]

        # Create masks based on color segmentation
        masks = [
            np.logical_and(H_RED_MIN < h, h < H_RED_MAX),
            np.logical_or(H_BLUE_MIN < h, h < H_BLUE_MAX)
        ]

        # Create list for bboxes for this image
        bboxes_in_img = list()

        # If morphology flag is set
        if F_MORPH:
Example #9
0
])
ths_s = np.array([[0.0, 1.0]])
ths_v = np.array([[0.0, 1.0]])

# Get elapsed time
t0 = time.time()
t_frame = 0

# Iterate over test image paths
for img_dir_temp in glob.glob(IMAGE_DIR + "/*.jpg"):
    img_dir = img_dir_temp.split("/")[-1]

    t_frame_0 = time.time()
    # Get numpy array of the image and convert it to HSV
    img = get_img(IMAGE_DIR, img_dir)
    img_hsv = rgb2hsv(img)
    # img_hsv = ndimage.filters.gaussian_filter(img_hsv, sigma=3)

    # Get the mask of the HSV image
    mask = threshold_image(img_hsv, ths_h, channel=0)
    #mask += threshold_image(img_hsv, ths_s, channel=1)
    #mask += threshold_image(img_hsv, ths_v, channel=2)

    # Create a numpy array where mask values are 255
    final = mask.astype(np.uint8) * 255

    # Save mask as image
    fn, d = save_image(final, RESULT_DIR, img_dir)
    logger.info("'{filename}' saved in '{folder}' folder".format(
        filename=fn, folder=os.path.join(ROOT_DIR, d)))
Example #10
0
	def load(cls, key):
		img = load('orig/{}.jpg'.format(key))
		cls.imgs[key] = img
		cls.hsv[key] = rgb2hsv(img.getdata())
Example #11
0
dataset_location = path.expanduser("~/dataset/sky_pixel_swim_test.npz")

if SVM_FORM_DATA:
    img_dir = path.expanduser("~/dataset/swimseg/train/images") + "/*.png"
    mask_dir = path.expanduser("~/dataset/swimseg/train/masks") + "/*.png"
    img_paths = sorted(glob.glob(img_dir))
    mask_paths = sorted(glob.glob(mask_dir))
    data = None
    labels = None
    for img_name, mask_name in zip(img_paths, mask_paths):
        print("Forming data set {} out of 200".format(n))
        img = pltimg.imread(img_name)
        img = cv2.resize(img, dsize=(256, 256))
        mask = pltimg.imread(mask_name)
        mask = cv2.resize(mask, dsize=(256, 256))
        img = rgb2hsv(img)
        img_int = (img[..., 2] * 255).astype(np.uint8)
        img_int = cv2.equalizeHist(img_int)
        img[..., 2] = img_int

        #pixel classification

        patch_data, mask_data = [], []
        i = 0
        while i < img.shape[0]:
            j = 0
            while j < img.shape[1]:
                patch = img[i:i + SLIDING_WINDOW_SIZE,
                            j:j + SLIDING_WINDOW_SIZE, :]
                padded = np.zeros(shape=(SLIDING_WINDOW_SIZE,
                                         SLIDING_WINDOW_SIZE, 3))