def __init__(self, conf, gan): # Default shapes self.g_input_shape = conf.input_crop_size self.d_input_shape = gan.G.output_size # shape entering D downscaled by G self.d_output_shape = self.d_input_shape - gan.D.forward_shave # Read input image self.input_image = read_image(conf.input_image_path) / 255. self.input_lr = self.input_image if not conf.weakly_supervised_path else read_image( conf.weakly_supervised_path) / 255. self.shave_edges(scale_factor=conf.scale_factor, real_image=conf.real_image) # self.in_rows, self.in_cols = self.input_image.shape[0:2] # Create prob map for choosing the crop # print(len(self.input_image) * len(self.input_image[0]), my_prob_map(self.input_image).shape) self.crop_indices_for_g = np.random.choice( a=(len(self.input_image) * len(self.input_image[0])), size=conf.max_iters, p=my_prob_map(self.input_image)) self.crop_indices_for_d = np.random.choice( a=(len(self.input_lr) * len(self.input_lr[0])), size=conf.max_iters, p=my_prob_map( self.input_lr)) # self.make_list_of_crop_indices(conf=conf)
def get_keypoints(image_path, keypoint_path): # get list of image names image_names = os.listdir(image_path) img_size = util.read_image("%s/%s"%(image_path,image_names[0])).shape keypoint_size = io.loadmat("%s/%s"%(keypoint_path, image_names[0]), appendmat=True)['output']['pred'][0][0].shape results = np.zeros(len(image_names), dtype=[('file_name','a30'), ('image', '%s float32'%str(img_size)), ( 'keypoints', '%s float32'%str(keypoint_size))]) for i in xrange(len(image_names)): name = image_names[i] img = util.read_image("%s/%s"%(image_path,image_names[i])) keypoints = io.loadmat("%s/%s"%(keypoint_path, image_names[i]), appendmat=True)['output']['pred'][0][0] results[i] = (name, img, keypoints) return results
def iter_images(filename, index=None, header=None): ''' Read a set of SPIDER images :Parameters: filename : str or file object Filename or open stream for a file index : int, optional Index of image to start, if None, start with the first image (Default: None) header : dict, optional Output dictionary to place header values :Returns: out : array Array with image information from the file ''' f = util.uopen(filename, 'rb') if index is None: index = 0 try: h = read_web_header(f) #if header is not None: util.update_header(header, h, web2ara, 'web') count = count_images(h) offset, ar_args = array_from_header(h) f.seek(int(offset)) if not hasattr(index, '__iter__'): index = xrange(index, count) else: index = index.astype(numpy.int) for i in index: yield util.read_image(f, *ar_args) finally: util.close(filename, f)
def read_image(filename, index=None, header=None, cache=None): ''' Read an image from the specified file in the WEB format :Parameters: filename : str or file object Filename or open stream for a file index : int, optional Index of image to get, if None, first image (Default: None) header : dict, optional Output dictionary to place header values :Returns: out : array Array with image information from the file ''' idx = 0 if index is None else index f = util.uopen(filename, 'rb') try: h = read_web_header(f) #if header is not None: util.update_header(header, h, web2ara, 'web') if idx >= count_images(h): raise IOError, "Index exceeds number of images in stack: %d < %d"%(idx, count_images(h)) offset, ar_args = array_from_header(h) f.seek(offset + idx * ar_args[1] * ar_args[0].itemsize) out = util.read_image(f, *ar_args) finally: util.close(filename, f) return out
def get_example(self, i): id_ = self.ids[i] anno = ET.parse(os.path.join(self.data_dir, 'Annotations', id_+'.xml')) bbox = list() label = list() difficult = list() for obj in anno.findall('object'): if not self.use_difficult and int(obj.find('difficult').text) == 1: continue difficult.append(int(obj.find('difficult').text)) bndbox_anno = obj.find('bndbox') bbox.append([ int(bndbox_anno.find(tag).text) - 1 for tag in ('ymin', 'xmin', 'ymax', 'xmax')]) name = obj.find('name').text.lower().strip() label.append(voc_bbox_label_names.index(name)) bbox = np.stack(bbox).astype(np.float32) label = np.stack(label).astype(np.float32) difficult = np.array(difficult, dtype=np.bool) img_file = os.path.join(self.data_dir, 'JPEGImages', id_ + '.jpg') img = read_image(img_file) if self.return_difficult: return img, bbox, label, difficult return img, bbox, label, difficult
def get_image(img_prefix): """ Returns a the image for the given img_prefix """ # intialize array for images file_path = "./data/cohn-kanade-images/%s/%s/%s.png"%(img_prefix[0], img_prefix[1], '_'.join(img_prefix[0:3])) return util.read_image(file_path, True)
def encode(self, url): try: image = util.read_image(url) except Exception as err: raise exceptions.ImageError(url) # image = self.pre_process(image) res = self.model.encode(image) return res
def encode(self, url): try: image = util.read_image(url) except Exception as err: raise exceptions.ImageError(f'read {url} failed!') image = self.pre_process(image) feat = self.extract_model.forward(image) assert len(feat) > 0, 'No detect object.' return feat[0]
def compression(image_path, save_folder, sample_percentages): # Define vectors to hold metric results. ssim_results = np.zeros(len(sample_percentages)) mse_results = np.zeros(len(sample_percentages)) psnr_results = np.zeros(len(sample_percentages)) # Read in image and calculate dimensions. original_image, ny, nx, n_channels = read_image(image_path, as_gray=False) final_result = np.zeros(original_image.shape, dtype='uint8') masks = np.zeros(original_image.shape, dtype='uint8') # Iterate through each sample percentage value. for i, sample_percentage in enumerate(sample_percentages): print(f'Samples = {100 * sample_percentage}%') start = time() # Get random sample indices so they're the same for all channels ri = generate_random_samples(nx * ny, sample_percentage) # Iterate through each color channel for j in range(n_channels): # Randomly sample from the image with the given percentage. # Retrieve the samples (b) and the masked image. b, masks[:, :, j] = create_mask(original_image[:, :, j], ri) # Compute results using OWL-QN final_result[:, :, j] = owl_qn_cs(original_image[:, :, j], nx, ny, ri, b) # Compute Structural Similarity Index (SSIM) of # reconstructed image versus original image. ssim_results[i] = structural_similarity(original_image, final_result, data_range=final_result.max() - final_result.min(), multichannel=True) mse_results[i] = mean_squared_error(original_image, final_result) psnr_results[i] = peak_signal_noise_ratio( original_image, final_result, data_range=final_result.max() - final_result.min()) # Save images. imageio.imwrite( f'results/{save_folder}mask_{trunc( 100 * sample_percentage )}.png', masks) imageio.imwrite( f'results/{save_folder}recover_{trunc( 100 * sample_percentage )}.png', final_result) print(f'Elapsed Time: {time() - start:.3f} seconds.\n') for i, sample_percentage in enumerate(sample_percentages): print( f'{trunc( 100 * sample_percentage ): 6.2f}%:\n SSIM: {ssim_results[ i ]}\n MSE: {mse_results[ i ]}\n PSNR: {psnr_results[ i ]}\n' )
def loadframes(data_name, data, name, frequency, max_len): ''' load and sample the frames according to frequency, resize the frames Parameters ---------- data : zipfile, the loaded zip of input frames name : str, the name of video to load data_name : dict, match the name of each videos to its frames max_len : int, the maximum length of sampled videos frequency : the frequency used to sample the frames, which decide how many frames used to represent the videos ''' values = [] files = [ f for f in data_name[name] if f.endswith(".jpg") and f.replace(".jpg", "").isnumeric() ] files = sorted(files, key=lambda x: int(x.replace(".jpg", ""))) last = [files[-1]] if len(files) % frequency >= 2 / 3 * frequency else [] start = 0 files = np.concatenate([np.array(files)[start::frequency], last]) l = 0 pad_x = np.zeros((max_len, 3, 48, 48)) for frame in files: try: cur = read_image(data, os.path.join(name, frame)) except: cur = read_image(data, name + "/" + frame) cur = np.swapaxes(np.asarray(cur.resize((48, 48))), 2, 0) values.append(cur) l += 1 x = np.array(values) l = max_len - l pad_x[l:] = x return pad_x, l
def search(self, ip, data): min_score = data.pop("score", 0) filters = data.pop("filter", None) imageurl = data["imageurl"] image = util.read_image(imageurl) image = self.pre_process(data, image) result = self.encode(image) feat = result[0]["feat"] data = {"query": {"sum": [{"feature": feat, "field": "feature", "min_score": min_score, "max_score": 1.0}],"filter":filters}} response_body = requests.post(ip, headers=_HEADERS, data=json.dumps(data)) return response_body.json()
def load_image_set(directory: str) -> Dict[ImageMetadata, np.ndarray]: """Reads all the PNGs in the given directory.""" logging.info('Reading image set in: %s', directory) image_set = {} for f in gfile.ListDirectory(directory): path = os.path.join(directory, f) if gfile.Exists(path) and os.path.splitext( f)[1] == '.png' and 'PREDICTED' not in f: image_set[parse_image_path(path)] = util.read_image(path) return image_set
def __getitem__(self, idx): img = read_image(self.g_images[idx]) _, H, W = img.shape scale = H / H try: img = preprocess(img) img, params = util.random_flip( img, x_random=True, return_param=True) except: print("Exception") img = torch.from_numpy(img)[None] return img,self.g_images[idx],scale
def insert(self, ip, data): results = [] # imageurl = data["imageurl"] try: image = util.read_image(data["imageurl"]) except Exception as err: raise exceptions.ImageError() image = self.pre_process(data, image) result = self.encode(image) for res in result: response_body = self.pkg_insert(ip, data, res) results.append(response_body.json()) return results
def __init__(self, conf, gan): # Default shapes self.g_input_shape = conf.input_crop_size self.d_input_shape = gan.G.output_size # shape entering D downscaled by G self.d_output_shape = self.d_input_shape - gan.D.forward_shave # Read input image self.input_image = read_image(conf.input_image_path) / 255. self.shave_edges(scale_factor=conf.scale_factor, real_image=conf.real_image) self.in_rows, self.in_cols = self.input_image.shape[0:2] # Create prob map for choosing the crop self.crop_indices_for_g, self.crop_indices_for_d = self.make_list_of_crop_indices(conf=conf)
def get_images(img_lst): """ Returns a Nximg.shape np array of each image in img_lst """ max_shape = (0,0) # intialize array for images for i in xrange(len(img_lst)): prefix = img_lst[i] file_path = "./data/cohn-kanade-images/%s/%s/%s.png"%(prefix[0], prefix[1], '_'.join(prefix[0:3])) shape = util.read_image(file_path, True).shape max_shape = map(max, zip(*[max_shape, shape])) images = np.zeros([len(img_lst), max_shape[0], max_shape[1]]) # get np array of each image for i in xrange(len(img_lst)): prefix = img_lst[i] file_path = "./data/cohn-kanade-images/%s/%s/%s.png"%(prefix[0], prefix[1], '_'.join(prefix[0:3])) img = util.read_image(file_path, True) # TODO: Change this later img = np.resize(img, max_shape) images[i,:,:] = img return images
def generate(K_targetImg, K_optiuneRedimensionare, K_metodaSelectareDrum, K_pixels): target = read_image(K_targetImg) if K_optiuneRedimensionare == 'micsoreazaLatime': a = target.copy() (w, h) = target.shape[0:2] r = misc.imresize(target, (w, h - K_pixels)) o = micsoreazaLatime(target, K_metodaSelectareDrum, K_pixels) return (a, r, o) elif K_optiuneRedimensionare == 'miscoreazaInaltime': a = target.copy() (w, h) = target.shape[0:2] r = misc.imresize(target, (w - K_pixels, h)) o = miscoreazaInaltime(target, K_metodaSelectareDrum, K_pixels) return (a, r, o) elif K_optiuneRedimensionare == 'maresteLatime': a = target.copy() (w, h) = target.shape[0:2] r = misc.imresize(target, (w, h + K_pixels)) o = maresteLatime(target, K_metodaSelectareDrum, K_pixels) return (a, r, o) elif K_optiuneRedimensionare == 'maresteInaltime': a = target.copy() (w, h) = target.shape[0:2] r = misc.imresize(target, (w + K_pixels, h)) o = maresteInaltime(target, K_metodaSelectareDrum, K_pixels) return (a, r, o) elif K_optiuneRedimensionare == 'maresteAmbele': a = target.copy() (w, h) = target.shape[0:2] r = misc.imresize(target, (w + K_pixels, h + K_pixels)) o = maresteLatime(target, K_metodaSelectareDrum, K_pixels) o = maresteInaltime(o, K_metodaSelectareDrum, K_pixels) return (a, r, o) elif K_optiuneRedimensionare == 'amplificaContinut': a = target.copy() r = amplificaContinut(target, K_metodaSelectareDrum, 5.0) o = amplificaContinut(target, K_metodaSelectareDrum, 2.0) return (a, r, o) elif K_optiuneRedimensionare == 'eliminaObiect': rect = K_pixels a = target.copy() r = target.copy() r[rect[0]:rect[2], rect[1]:rect[3]].fill(0) o = eliminaObiect(target, rect) return (a, r, o) elif K_optiuneRedimensionare == 'energie': return calculeazaEnergie(target) pass
def get_bounding_boxes(self, index): img_path = self.img_paths[index] img = util.read_image(img_path) # we need to scale bounding boxes since we applied a transformation height, width, _ = np.shape(img) height_scale = self.target_height / height width_offset = (width * height_scale - self.target_width) / 2 bbxs = copy.deepcopy(self.bounding_boxes[img_path]) for bbx in bbxs: bbx[0] = int(bbx[0] * height_scale - width_offset) bbx[1] = int(bbx[1] * height_scale) bbx[2] = int(bbx[2] * height_scale - width_offset) bbx[3] = int(bbx[3] * height_scale) return bbxs
def __getitem__(self, index): file_name = self.bags[index] # Examples of file name : "N_xxx_" ,i.e., # T1 and T2 file names are N_xxx_T1.tif, N_xxx_T2.tif # "N" denotes "Negative bag" and "P" denotes "Positive bag" bag_path = os.path.join(self.data_dir, file_name) if file_name[0] == 'N': # Negative bag label = torch.LongTensor([0]) else: # Positive bag label = torch.LongTensor([1]) t1_path = bag_path + 'T1.tif' t2_path = bag_path + 'T2.tif' t1 = util.read_image(t1_path) t2 = util.read_image(t2_path) t2 = util.hist_match(t2, t1) data1 = t1.transpose((2, 0, 1)) data2 = t2.transpose((2, 0, 1)) return data1, data2, label, bag_path
def run(orig_shape_name, orig_folder, before_mfd_folder, after_mfd_folder, output_folder): log.info("Processing %s" % orig_shape_name) orig_shape_path = join_path(orig_folder, orig_shape_name) before_mfd_path = join_path(before_mfd_folder, util.get_binary_image_name(orig_shape_name)) after_mfd_image_name = util.get_image_name_clean_after_mfd(orig_shape_name) after_mfd_path = join_path(after_mfd_folder, after_mfd_image_name) output_image_name = after_mfd_image_name orig_shape = util.read_image(orig_shape_path) shape_before_mfd = util.read_image(before_mfd_path) shape_after_mfd = util.read_image(after_mfd_path) log.info("Overriding shape frame: red -> white") clean_img = override_frame(shape_before_mfd, shape_after_mfd) log.info("Overriding outside shape red lines: red -> black") clean_img = clean_outside_red_lines(orig_shape, clean_img) log.info("Saving clean image") util.save_on_path(clean_img, output_image_name, output_folder)
def ensure_target_exists(): if Path(target).exists(): return links = google_images_download.get_image_links( search_keywords=[keyword], keywords=['high resolution'], requests_delay=0, limit=LIMIT) for target_link in links: try: print('Trying to retrieve target link {}'.format(target_link)) misc.imsave(target, read_image(target_link)) break except urllib.error.HTTPError: continue except OSError: continue
def process_fn(keyword, target, output, imresize_file, desc_file): def ensure_target_exists(): if Path(target).exists(): return links = google_images_download.get_image_links( search_keywords=[keyword], keywords=['high resolution'], requests_delay=0, limit=LIMIT) for target_link in links: try: print('Trying to retrieve target link {}'.format(target_link)) misc.imsave(target, read_image(target_link)) break except urllib.error.HTTPError: continue except OSError: continue ensure_target_exists() target_img = read_image(target) (n, m) = target_img.shape[0:2] (del_lines, del_columns) = random_modification(n, m) path_algorithm = random_path_algorithm() args = [ '--lines', str(del_lines), '--columns', str(del_columns), '--path-algorithm', path_algorithm, ] with open(desc_file, 'w+t') as f: f.write('{} from google, with {} lines, {} columns and path algorithm {}'.format( keyword, del_lines, del_columns, path_algorithm)) gen_output( target, output, imresize_file, args=args, imresize_fn=delta_imresize(del_lines, del_columns))
def __getitem__(self, index): """ :param index: index of data point :return: img ndarray (3 x w x h) RGB image mask ndarray (w x h) segmentation classification of each pixel index (int) image index """ img_path = self.img_paths[index] bounding_boxes = self.bounding_boxes[img_path] img = util.read_image(img_path) height, width, depth = np.array(img).shape # final mask will have no channels but we need 3 initially to convert it to PIL image to apply transformation mask = np.ones((height, width, 3)) * Label.OTHER.value for bb in bounding_boxes: pt1, pt2, label = np.array(bb[0:2]), np.array(bb[2:4]), bb[4] center = tuple(((pt1 + pt2) / 2).astype(np.int)) size = tuple(((pt2 - pt1) / 2).astype(np.int)) if not size == (0, 0): if label == Label.BALL: mask = cv2.ellipse(mask, center, size, 0, 0, 360, label.value, -1) if label == Label.ROBOT: mask = cv2.rectangle(mask, tuple(pt1), tuple(pt2), label.value, -1) mask = Image.fromarray(mask.astype('uint8')) # Apply transformations to get desired dimensions img = np.array(self.img_transform(img)) mask = np.array(self.mask_transform(mask)) if depth == 4: img = img[:, :, :3] img = img / 255 # flip to channel*W*H - how Pytorch expects it img = np.moveaxis(img, -1, 0) mask = np.moveaxis(mask, -1, 0)[0] # get rid of channel dimension return img, mask, index
def get_example(self, i): ''' Returns the i-th example. Returns a color image and bounding boxex. The image is in CHW format The returned image is RGB. :param i: :return: ''' id_ = self.ids[i] anno = ET.parse( os.path.join(self.data_dir, "Annotations", id_ + ".xml")) bbox = list() label = list() difficult = list() for obj in anno.findall('object'): # when in not using difficult split, and the object is # difficult, skip it if not self.use_difficult and int(obj.find('difficult').text) == 1: continue difficult.append(int(obj.find('difficult').text)) bndbox_anno = obj.find('bndbox') # subtract 1 to make pixel indexes 0-based bbox.append([ int(bndbox_anno.find(tag).text) - 1 for tag in ('ymin', 'xmin', 'ymax', 'xmax') ]) name = obj.find('name').text.lower().strip() label.append(VOC_BBOX_LABEL_NAMES.index(name)) bbox = np.stack(bbox).astype(np.float32) label = np.stack(label).astype(np.int32) # when use_difficult==false, all elements in difficult are false difficult = np.array(difficult, dtype=np.bool).astype(np.int8) # Load a image img_file = os.path.join(self.data_dir, 'JPEGImages', id_ + '.jpg') img = read_image(img_file, color=True) return img, bbox, label, difficult
def generate_testing(): positive = Path('positive_testing') negative = Path('negative_testing') os.makedirs(positive, exist_ok=True) os.makedirs(negative, exist_ok=True) for i in Path('data').iterdir(): if 'Testare' not in str(i): continue if str(i.name).endswith('pozitive'): dest = positive elif str(i.name).endswith('negative'): dest = negative else: sys.exit('Could not determine where to place {}'.format(f)) for f in i.iterdir(): misc.imsave(dest / f.name, resize_image(read_image(f)))
def __init__(self, conf): np.random.seed(0) self.conf = conf print('*' * 60 + '\nPreparing data ...') # Default shapes self.g_input_shape = conf.input_crop_size self.d_input_shape = int(conf.input_crop_size * conf.scale_factor_downsampler) # Read input image self.input_image = read_image(conf.input_image_path) / 255. self.shave_edges(scale_factor=conf.scale_factor_downsampler, real_image=False) self.in_rows, self.in_cols = self.input_image.shape[0:2] # Create prob map for choosing the crop self.crop_indices_for_g, self.crop_indices_for_d = self.make_list_of_crop_indices( conf=conf)
def run_funcs(orig_shape_name, folder_input_path, folder_output_path): shape_input_path = join_path(folder_input_path, orig_shape_name) output_image_name = 'binary_%s' % orig_shape_name log.info("Reading %s" % orig_shape_name) image = util.read_image(shape_input_path) log.info("Improving shape coloring") image = improve_coloring(image) log.info("Calculating boundaries") boundaries = calc_boundaries(image) log.info("Marking boundary") image = mark_boundary(image, boundaries, config.colors_dic['black']) log.info("Building binary") image = whiten(image) image = replace_white_and_black(image) util.save_on_path(image, output_image_name, folder_output_path) log.info("New shape %s saved on %s" % (output_image_name, folder_output_path))
def __init__(self, conf, gan): # Default shapes self.g_input_shape = conf.input_crop_size self.d_input_shape = gan.G.output_size # shape entering D downscaled by G self.d_output_shape = self.d_input_shape - gan.D.forward_shave # Read input image self.input_image = read_image(conf.input_image_path) / 255. self.input_lr = imresize( im=self.input_image, scale_factor=0.5, kernel='cubic' ) # read_image("/content/gdrive/MyDrive/for_ws_kernel_gan/0803ss.png") / 255. # implement self.shave_edges(scale_factor=conf.scale_factor, real_image=conf.real_image) # self.in_rows, self.in_cols = self.input_image.shape[0:2] # Create prob map for choosing the crop # print(len(self.input_image) * len(self.input_image[0]), my_prob_map(self.input_image).shape) # self.crop_indices_for_g = np.random.choice(a=(len(self.input_image) * len(self.input_image[0])), size=conf.max_iters, p=my_prob_map(self.input_image)) # self.crop_indices_for_d = np.random.choice(a=(len(self.input_image) * len(self.input_image[0])), size=conf.max_iters, p=my_prob_map(self.input_image)) # self.make_list_of_crop_indices(conf=conf) self.crop_indices_for_g, self.crop_indices_for_d = self.make_list_of_crop_indices( conf=conf)
def numpixels(image): return image.view(rgb_dtype).size def test(image): return numcolors(image) == numpixels(image) if __name__ == '__main__': import argparse import sys parser = argparse.ArgumentParser(description="Generates an image with every RGB color exactly once") parser.add_argument('command', choices=['generate', 'test'], default='generate') parser.add_argument('-i', '--input', type=argparse.FileType('r'), default=sys.stdin) parser.add_argument('-o', '--output', type=argparse.FileType('w'), default=sys.stdout) args = parser.parse_args() image = read_image(args.input) if args.command == 'generate': write_image(args.output, generate(image)) elif args.command == 'test': imagecolors = numcolors(image) imagepixels = numpixels(image) print("This image has {} colors and {} pixels".format(imagecolors, imagepixels)) sys.exit(imagecolors != imagepixels)
[0, 0, -1], [0, 1, 0], [0, 0, 0] ]) kernel_c3 = np.array([ [0, 0, 2], [0, -1, 0], [-1, 0, 0] ]) _offset = 1 # Original name = "monkey.jpg" _img = util.read_image(name) cv.imshow("Original", _img) # Gray img_gray = color.rgb_to_gray(_img) cv.imshow("Gray", img_gray) util.write_image("gray-" + name, img_gray) # Convolution Kernel c1 util.do_convolution_show_and_write(_img, kernel_c1, "conv-c1-" + name, _offset) # Convolution Gray Kernel c1 util.do_convolution_show_and_write(img_gray, kernel_c1, "conv-c1-gray-" + name, _offset) # 2 util.do_convolution_show_and_write(_img, kernel_c2, "conv-c2-" + name, _offset) util.do_convolution_show_and_write(img_gray, kernel_c2, "conv-c2-gray-" + name, _offset)
def main(): # Parse the command line arguments prog = argparse.ArgumentParser() prog.add_argument('--model', type=str, default='DIPFKP', help='models: DIPFKP, DIPSoftmax, DoubleDIP.') prog.add_argument('--dataset', '-d', type=str, default='Set5', help='dataset, e.g., Set5.') prog.add_argument('--sf', type=str, default='2', help='The wanted SR scale factor') prog.add_argument('--path-nonblind', type=str, default='../data/pretrained_models/usrnet_tiny.pth', help='path for trained nonblind model') prog.add_argument('--SR', action='store_true', default=False, help='when activated - nonblind SR is performed') prog.add_argument('--real', action='store_true', default=False, help='if the input is real image') # to be overwritten automatically prog.add_argument('--path-KP', type=str, default='../data/pretrained_models/FKP_x2.pt', help='path for trained kernel prior') prog.add_argument('--input-dir', '-i', type=str, default='../data/datasets/Set5/DIPFKP_lr_x2', help='path to image input directory.') prog.add_argument('--output-dir', '-o', type=str, default='../data/log_KernelGANFKP/Set5_DIPFKP_lr_x2', help='path to image output directory') args = prog.parse_args() # overwritting paths args.path_KP = '../data/pretrained_models/FKP_x{}.pt'.format(args.sf) args.input_dir = '../data/datasets/{}/DIPFKP_lr_x{}'.format(args.dataset, args.sf) args.output_dir = '../data/log_DIPFKP/{}_{}_lr_x{}'.format(args.dataset, args.model, args.sf) # load nonblind model if args.SR: netG = USRNet(n_iter=6, h_nc=32, in_nc=4, out_nc=3, nc=[16, 32, 64, 64], nb=2, act_mode="R", downsample_mode='strideconv', upsample_mode="convtranspose") netG.load_state_dict(torch.load(args.path_nonblind), strict=True) netG.eval() for key, v in netG.named_parameters(): v.requires_grad = False netG = netG.cuda() filesource = os.listdir(os.path.abspath(args.input_dir)) filesource.sort() for filename in filesource[:]: print(filename) # kernel estimation conf = Config().parse(create_params(filename, args)) lr_image = im2tensor01(read_image(os.path.join(args.input_dir, filename))).unsqueeze(0) # crop the image to 960x960 due to memory limit if 'DIV2K' in args.input_dir: crop = int(960 / 2 / conf.sf) lr_image = lr_image[:, :, lr_image.shape[2] // 2 - crop: lr_image.shape[2] // 2 + crop, lr_image.shape[3] // 2 - crop: lr_image.shape[3] // 2 + crop] kernel, sr_dip = train(conf, lr_image) plt.imsave(os.path.join(conf.output_dir_path, '%s.png' % conf.img_name), tensor2im01(sr_dip), vmin=0, vmax=1., dpi=1) # nonblind SR if args.SR: kernel = map2tensor(kernel) sr = netG(lr_image, torch.flip(kernel, [2, 3]), int(args.sf), (10 if args.real else 0) / 255 * torch.ones([1, 1, 1, 1]).cuda()) plt.imsave(os.path.join(conf.output_dir_path, '%s.png' % conf.img_name), tensor2im01(sr), vmin=0, vmax=1., dpi=1) if not conf.verbose: evaluation_dataset(args.input_dir, conf) prog.exit(0)
""" Custom filters """ import cv2 as cv import filter import util import color name = "skate.jpg" # name = "32bits.png" original = util.read_image(name) cv.imshow('Original', original) # Custom 1 custom1 = filter.custom_filter1(original) cv.imshow('Custom 1', custom1) util.write_image("custom1-filter-" + name, custom1) # Custom 2 custom2 = filter.custom_filter2(original) cv.imshow('Custom 2', custom2) util.write_image("custom2-filter-" + name, custom2) cv.waitKey(0) cv.destroyAllWindows()
def gen_output(target, output, imresize_file, imresize_fn, args): subprocess.check_call(['python3', 'main.py', target, output, *args]) misc.imsave(imresize_file, imresize_fn(read_image(target)))
def add_picture_to_canvas(pic, desc, c): (w, h) = read_image(pic).shape[0:2] c.setPageSize((max(h, 500), w + 100)) c.drawInlineImage(str(pic), 0, 0) c.drawCentredString(h / 2, w + 50, desc) c.showPage()