def main(): if len(sys.argv) < 3: raise RuntimeError( 'Command Line Argument Must be (sketch file, style file)') style_f = './data/styles/%s' % sys.argv[2] test_f = './data/test/%s' % sys.argv[1] filename = sys.argv[1][:-4] + sys.argv[2][:-4] + '.png' style = Image.open(style_f).convert('RGB') style = transforms.Resize((512, 512))(style) style_pil = style test = Image.open(test_f).convert('RGB') test_pil = transforms.Resize((512, 512))(test) transform = transforms.Compose( [transforms.Resize((512, 512)), transforms.ToTensor()]) test = transform(test) test = scale(test) test = test.unsqueeze(0).to(device) to_pil = transforms.ToPILImage() try: images = list(crop_region(style)) result = {} for i, img in enumerate(images, 1): colors = cgm.extract(img, topk + 1) result[str(i)] = { '%d' % i: get_rgb(colors[i]) for i in range(1, topk + 1) } color_tensor = make_colorgram_tensor(result) color_tensor = color_tensor.unsqueeze(0).to(device) fakeB, _ = model(test, color_tensor) fakeB = fakeB.squeeze(0) fakeB = re_scale(fakeB.detach().cpu()) fakeB = to_pil(fakeB) result_image = Image.new('RGB', (512 * 3, 512)) result_image.paste(test_pil, (512 * 0, 0, 512 * 1, 512)) result_image.paste(style_pil, (512 * 1, 0, 512 * 2, 512)) result_image.paste(fakeB, (512 * 2, 0, 512 * 3, 512)) save_image(result_image, os.path.join(out_root, filename)) except IndexError: exit(1)
def actbycolor(stylelist, testfile): print(stylelist) print(stylelist[0]) if len(sys.argv) < 3: raise RuntimeError( 'Command Line Argument Must be (sketch file, style file)') result = {} j = 0 for i in range(1, 5): temp = {} for k in range(1, 5): if j >= len(stylelist): j = 0 temp[str(k)] = stylelist[j] j = j + 1 result[str(i)] = temp print(result) test_f = testfile # filename = sys.argv[1][:-4] + sys.argv[2][:-4] + '.png' test = Image.open(test_f).convert('RGB') test_pil = transforms.Resize((256, 256))(test) style = Image.open('media/immm.jpg').convert('RGB') style = transforms.Resize((256, 256))(style) print(style) style_pil = style transform = transforms.Compose( [transforms.Resize((256, 256)), transforms.ToTensor()]) test = transform(test) test = scale(test) test = test.unsqueeze(0).to(device) to_pil = transforms.ToPILImage() nowtime = time.time() try: nowtime2 = time.time() print(result) print(nowtime2 - nowtime) color_tensor = make_colorgram_tensor(result) color_tensor = color_tensor.unsqueeze(0).to(device) fakeB, _ = model(test, color_tensor) fakeB = fakeB.squeeze(0) fakeB = re_scale(fakeB.detach().cpu()) fakeB = to_pil(fakeB) fakeB.save(os.path.expanduser('media/result.jpg')) result_image = Image.new('RGB', (256 * 3, 256)) result_image.paste(test_pil, (256 * 0, 0, 256 * 1, 256)) result_image.paste(style_pil, (256 * 1, 0, 256 * 2, 256)) result_image.paste(fakeB, (256 * 2, 0, 256 * 3, 256)) result_image.save(os.path.expanduser('media/compareresult.jpg')) # save_image(result_image, os.path.join(out_root, filename)) except IndexError: exit(1) nowtime2 = time.time() print(nowtime2 - nowtime) # if __name__ == "__main__": # main()
def __getitem__(self, index): """ Niko Dataset Get Item @param index: index Returns: if self.color_histogram tuple: (imageA == original, imageB == sketch, colors) else: tuple: (imageA == original, imageB == sketch) if self.resize resized image will be appended end of the above tuple """ filename = self.image_files[index] file_id = filename.split('/')[-1][:-4] if self.color_histogram: # build colorgram tensor color_info = self.color_cache.get(file_id, None) if color_info is None: with open( os.path.join('./data/colorgram', '%s.json' % file_id).replace('\\', '/'), 'r') as json_file: # load color info dictionary from json file color_info = json.loads(json_file.read()) self.color_cache[file_id] = color_info colors = make_colorgram_tensor(color_info) image = Image.open(filename) image_width, image_height = image.size imageA = image.crop((0, 0, image_width // 2, image_height)) imageB = image.crop((image_width // 2, 0, image_width, image_height)) # default transforms, pad if needed and center crop 256 width_pad = self.size - image_width // 2 if width_pad < 0: # do not pad width_pad = 0 height_pad = self.size - image_height if height_pad < 0: height_pad = 0 # padding as white padding = transforms.Pad((width_pad // 2, height_pad // 2 + 1, width_pad // 2 + 1, height_pad // 2), (255, 255, 255)) # use center crop crop = transforms.CenterCrop(self.size) imageA = padding(imageA) imageA = crop(imageA) imageB = padding(imageB) imageB = crop(imageB) if self.transform is not None: imageA = self.transform(imageA) imageB = self.transform(imageB) # scale image into range [-1, 1] imageA = scale(imageA) imageB = scale(imageB) if not self.color_histogram: return imageA, imageB else: return imageA, imageB, colors