def getAndSaveOutputs(filepath, network_path=None, amount=100): imagelist = [] code = network_path[:3] with open(filepath, newline='') as csvfile: reader = csv.reader(csvfile, delimiter=',') for row in reader: name = str(row[0]) box = (str(row[1])[1:-1]).split(",") bbox = [int(b) for b in box] imagelist.append([name, bbox]) dataset = WhaleDataset(imagelist[:amount], 512, augment=False) encoding_ids = None encodings = np.array([]) if network_path: model = torch.load(network_path) if amount > len(dataset): amount = len(dataset) for i in range(amount): img, img_name, _, _ = dataset.getImageAndAll(i) output = model.forward(img.float().cuda()) if len(output) > 1: output = output[0] imagename = img_name.split("/")[-1] image = output[0, 0].cpu().detach() io.imsave("./outputs/" + code + imagename, (color.grey2rgb(image) * 255).astype(np.uint8)) io.imsave("./outputs/" + imagename, (color.grey2rgb(img[0, 0]) * 255).astype(np.uint8))
def getTile(self, tile_xy, EM_case): 'read files' if EM_case == True: tile_name = "{}_{}_{}.png".format(tile_xy[0], tile_xy[1], self.zoom_lvl) filename = self.EM_dir + tile_name im_tile = img_as_float(io.imread(filename)) if im_tile is None: rgba = np.zeros((1024, 1024, 4)) * 255 else: inverted = util.invert(im_tile) rgba = color.grey2rgb(inverted, alpha=True) if EM_case == 'FM_hoechst': tile_name = "{}_{}_{}.png".format(tile_xy[0], tile_xy[1], self.zoom_lvl) filename = self.FM_dir_hoechst + tile_name im_tile = img_as_float(io.imread(filename)) if im_tile is None: rgba = np.zeros((1024, 1024, 4)) * 255 else: rgba = color.grey2rgb(im_tile, alpha=True) if EM_case == 'FM_insulin': tile_name = "{}_{}_{}.png".format(tile_xy[0], tile_xy[1], self.zoom_lvl) filename = self.FM_dir_insulin + tile_name im_tile = img_as_float(io.imread(filename)) if im_tile is None: rgba = np.zeros((1024, 1024, 4)) * 255 else: rgba = color.grey2rgb(im_tile, alpha=True) return rgba
def prepare_dataset(X): len_ = X.shape[0] shape_ = X.shape d = int(np.sqrt(X.flatten().reshape(X.shape[0], -1).shape[1])) if len(shape_) == 4: X = np.reshape(X, [-1, d, d, 3]) elif d == shape_[1] and len(shape_) == 3: X = np.reshape(X, [-1, d, d]) X = np.array(list(map(lambda x: grey2rgb(x), X)), dtype=np.float32) else: r = d**2 - X.shape[1] train_padding = np.zeros((shape_[0], r)) X = np.vstack([X, train_padding]) X = np.reshape(X, [-1, d, d]) X = np.array(list(map(lambda x: grey2rgb(x), X)), dtype=np.float32) print('Scaling dataset') if scalar is not None: X = scaler.transform(X.flatten().reshape(-1, 1).astype( np.float32)).reshape(X.shape) else: scaler = MinMaxScaler() X = scaler.fit_transform(X.flatten().reshape(-1, 1).astype( np.float32)).reshape(X.shape) return X
def read_image(img, as_gray): if as_gray: if isinstance(img, str): image = imread(img, as_gray=True) elif img.ndim == 2: image = img.copy() elif img.ndim <= 4: image = rgb2grey(img) else: assert True, "wrong number of image dimensions for image with shape " + str( image.shape) image = image[:, :, np.newaxis] else: if isinstance(img, str): image = imread(img, as_gray=False) if image.ndim == 4: image = rgba2rgb(image) elif img.ndim == 2: image = grey2rgb(img) elif img.ndim == 3: image = img.copy() elif img.ndim == 4: image = rgba2rgb(image) else: assert True, "wrong number of image dimensions for image with shape " + str( image.shape) return img_as_float(image).astype(_im_dtype)
def make_dframe(t): i = int(t * fps) #img = schler(imSeq[i]) img = dimSeq[i] imgSize = img.shape print(imgSize) barRatio = 5 #font = ImageFont.truetype("/usr/share/fonts/truetype/freefont/FreeSansBold.ttf",int(imgSize[0]/barRatio/3)) font = ImageFont.truetype( "/usr/share/fonts/truetype/freefont/FreeSansBold.ttf", int(imgSize[0] / barRatio / 2)) reSize = tuple(np.add(imgSize, (int(imgSize[0] / barRatio), 0))) print(reSize) img2 = np.ones(reSize) img2[int(imgSize[0] / barRatio):, :] = np.copy(img) im = Image.fromarray( img_as_ubyte( (exposure.rescale_intensity(img2, in_range=(img.min(), img.max()))))) draw = ImageDraw.Draw(im) draw.rectangle(((0, 0), (imgSize[0], int(imgSize[0] / barRatio))), fill='black') draw.text((0, 0), 'g: {:03.2f}'.format(params[0]), font=font, fill=255) draw.text((int(imgSize[0] / 2), 0), u'\u03b2: {:04.3f}'.format(params[1]), font=font, fill=255) draw.text((0, int(imgSize[1] / barRatio / 2)), u't: {:04.3f}'.format(t), font=font, fill=255) # draw.text((0,int(imgSize[1]/barRatio/2)),u'\u03b2: 0.4'.format(i),font=font,fill=255) return color.grey2rgb(np.asarray(im))
def process_im(tag=None): """ Exposure correct and map rectangular object ~ median region Arbitrary shape size - suboptimal Extract Text """ # Correct im = io.imread(tag, as_gray=True) local_im = exp_corr(crop_med_v(im)) # Map object local_im = map_obj(local_im) # Keras-OCR pipeline = keras_ocr.pipeline.Pipeline() prediction_groups = pipeline.recognize( [grey2rgb(img_as_ubyte(exp_corr(local_im)))]) #fig, axs = plt.subplots(1, 1, figsize=(6, 6)) #keras_ocr.tools.drawAnnotations(image=grey2rgb(img_as_ubyte(exp_corr(local_im))), predictions=prediction_groups[0], ax=axs) temp_coord = filt_groups(prediction_groups[0]) local_im = get_reg(im=local_im, coor=temp_coord) # Tesseract text_res = get_text(local_im) return text_res
def get_images(parent_path, age_thresh=(6, 18, 25, 35, 60), valid_percent=0.2, resize_shape=(32, 32)): img_files = sorted(glob(os.path.join(parent_path, '*.chip.jpg'))) imgs, ages = [], [] age_thresh = [-1, *age_thresh, 200] for img_file in tqdm(img_files): img = io.imread(img_file) if img.shape[-1] == 1: img = color.grey2rgb(img) age = int( os.path.splitext(os.path.basename(img_file))[0].split('_') [0]) # get age from file name prefix img = transform.resize(img, resize_shape, anti_aliasing=True, preserve_range=True)[..., :3] imgs.append(img.astype(np.uint8)) # assign class label for cnt, (lb, ub) in enumerate(zip(age_thresh[:-1], age_thresh[1:])): if lb < age <= ub: ages.append(cnt) break # shuffle data rand_idx = np.random.permutation(np.arange(len(img_files))) imgs = [imgs[a] for a in rand_idx] ages = [ages[a] for a in rand_idx] valid_num = int(np.floor(len(imgs) * valid_percent)) return imgs[valid_num:], ages[ valid_num:], imgs[:valid_num], ages[:valid_num]
def create_staged_labels(file_name, field, boxes, labels, size_labels, count_elements, unique_elements, RGB_tuples=None): if RGB_tuples is None: RGB_tuples = np.array([(0, 0, 255), (0, 255, 0), (255, 0, 0)]) for i in range(1, 5): output_field = grey2rgb(field.copy()) print(int(labels.shape[0] * (i / 4))) for (x1, y1, x2, y2), label in list(zip(boxes, labels))[:int(labels.shape[0] * (i / 4))]: # use the label to index into the size ordering, to index into the colors. set_color( output_field, circle(abs(x2 + x1) / 2.0, abs(y2 + y1) / 2.0, radius=abs(y2 - y1) / 2.0 + 1.0), RGB_tuples[size_labels[label]]) print(count_elements[size_labels[unique_elements]])
def read_generator(y_csv, train_img_dir, batch_size, permutations=False, shuffle=True, grey=True): channels = 1 if grey else 3 batch_features = np.zeros( (batch_size, IMAGE_HEIGHT, IMAGE_WIDTH, channels)) batch_labels = np.zeros((batch_size, CLASSES_NUM)) names = sorted(list(y_csv.keys())) while True: if shuffle: np.random.shuffle(names) for ind, name in enumerate(names): i = ind % batch_size img = imread(join(train_img_dir, name), as_grey=grey) if len(img.shape) == 2 and not grey: img = grey2rgb(img) if permutations: img = choice_permutation(img) else: img = pad_and_scale(img)[0] batch_labels[i] = np.zeros(CLASSES_NUM) batch_features[i, ...], batch_labels[i][y_csv[name]] = img.reshape( (IMAGE_HEIGHT, IMAGE_WIDTH, channels)), 1 if ind % batch_size == batch_size - 1 or ind == len(names) - 1: yield batch_features, batch_labels
def __getitem__(self, idx): image_path = self.images[idx] image = io.imread(image_path) image, mask = image[..., :-1], image[..., -1] // 255 mask = 1 - mask identity = self.identifier(self.identifier.preprocess(image)).view( 256, 1, 1) image = img_as_float(image) if len(image.shape) < 3: image = color.grey2rgb(image) if np.random.uniform() > 0.5: image = np.fliplr(image) image = (image * 2) - 1 masked = image * mask[..., np.newaxis] image = np.transpose(image, (2, 0, 1)) masked = np.transpose(masked, (2, 0, 1)) image = torch.Tensor(image) masked = torch.Tensor(masked) # print(f'Image {image.shape} masked {masked.shape}') output = {} output['A'] = masked output['B'] = image output['injection'] = identity output['paths'] = image_path return output
def process(path, mode): if mode == 'train': image_addresses = 'train/*/images/*.JPEG' else: image_addresses = '{}/images/*.JPEG'.format(mode) image_addresses = glob(os.path.join(path, image_addresses), recursive=True) if mode != 'test': np.random.shuffle(image_addresses) expected_len = len(image_addresses) data_path = '../prepared_data/tiny/{}'.format(mode) lr_data = get_carray(data_path, expected_len, 64, is_lr=True) hr_data = get_carray(data_path, expected_len, 64, is_lr=False) bpg_sizes = np.zeros((expected_len, ), dtype=np.uint32) flif_sizes = np.zeros((expected_len, ), dtype=np.uint32) for i in trange(expected_len, desc=mode, dynamic_ncols=True): hr_img = io.imread(image_addresses[i]) if hr_img.ndim == 2: hr_img = grey2rgb(hr_img) bpg_size, bpg_lr = get_bpg(hr_img, i, remove_img=True) lr_data.append(bpg_lr) hr_data.append(hr_img.transpose((2, 0, 1))) bpg_sizes[i] = bpg_size bpg_sizes = bpg_sizes * 8.0 / 64 / 64 / 3 # bpsp np.savez(data_path + '.npz', bpg_size=bpg_sizes) lr_data.flush() hr_data.flush()
def adjust_img_to_fixed_height(np_img, split_positions=None, fixed_h=560, segment_task="book_page", text_type="horizontal"): # rotate 90 degrees rightward. text_type = text_type[0].lower() if (segment_task, text_type) in (("book_page", "h"), ("double_line", "h"), ("text_line", "v"), ("mix_line", "v")): np_img, split_positions = rotate_90_degrees(np_img, split_positions) # to rgb if len(np_img.shape) == 2 or np_img.shape[-1] != 3: np_img = color.grey2rgb(np_img) # scale image to fixed shape raw_h, raw_w = np_img.shape[:2] if segment_task in ("book_page", "mix_line", "text_line"): scale_ratio = fixed_h / raw_h # fixed_h是16的倍数 fixed_w = int(raw_w * scale_ratio) # 等比例缩放, 在打包batch时, 将调整为16的倍数 else: fixed_w = raw_w # double_line情况, 等宽缩放, 在打包batch时, 将调整为16的倍数 scale_ratio = 1.0 np_img = transform.resize(np_img, output_shape=(fixed_h, fixed_w)) # float32 np_img = np_img.astype(np.uint8) if split_positions is not None: split_positions = split_positions * scale_ratio np_img, split_positions = tilt_text_img_py(np_img, split_positions, segment_task) # 加入文本倾斜 return np_img, split_positions, scale_ratio
def convert_16bit_to_RGB(a_img_16bit, clip_not_scale=True, d_save_info=None): if a_img_16bit.dtype != 'int16': a_img_16bit = a_img_16bit.astype('int16') a_img_16bit = a_img_16bit - np.min(a_img_16bit) #subtract mnimimum if clip_not_scale: a_img_8bit = img_as_ubyte(np.where(a_img_16bit > 255, 255, a_img_16bit)) else: a_img_8bit = img_as_ubyte(a_img_16bit) #a_4D_RGB = np.asarray([grey2rgb(i,alpha=None) for i in a_img_8bit]) #scikit1702 a_4D_RGB = np.asarray([grey2rgb(i) for i in a_img_8bit]) if d_save_info: save_data(a_4D_RGB, **d_save_info, file_ext='tif', RGB=True, resolution='uint8', channel_reshape=False) return a_4D_RGB
def create_network_image(image, networks, c_mode=0): """Create image with overlayed fibre networks""" image = _normalise_image(image) colours = list(BASE_COLOURS.keys()) rgb_image = grey2rgb(image) for j, network in enumerate(networks): if c_mode == 0: colour = BASE_COLOURS['r'] else: colour = BASE_COLOURS[colours[j % len(colours)]] node_coord = [network.nodes[i]['xy'] for i in network] node_coord = np.stack(node_coord) mapping = zip(network.nodes, np.arange(network.number_of_nodes())) mapping_dict = dict(mapping) for n, node1 in enumerate(network): for node2 in list(network.adj[node1]): m = mapping_dict[node2] rr, cc, val = draw.line_aa(node_coord[m][0], node_coord[m][1], node_coord[n][0], node_coord[n][1]) for i, c in enumerate(colour): rgb_image[rr, cc, i] = c * val return rgb_image
def create_quadrant_file(output_dir, name, original_filename, latitude=float(0.0), longitude=float(0.0), rotation=float(0.0), region_size=230, pixels_in_meters=0.045): #lat = float(0.0) #long = float(0.0) #rot = float(0.0) #name = 'grey_conversion' src_image = imread(output_dir + name + ".png")#.astype(np.uint8)#[:,:,:3] img_width = src_image.shape[1] img_height = src_image.shape[0] boxes = np.load(output_dir + "boxes.npy").astype("int") labels = np.load(output_dir + "size_labels.npy") #0 is small, 1 is medium and 2 is large. #ensure its a rgb image. print(src_image.shape) if len(src_image.shape) == 2: src_image = grey2rgb(src_image) else: src_image = src_image[:,:,:3] #draw_image(src_image, "original") h = img_height w = img_width dist = pixels_in_meters * region_size / 1000.0 #convetr to kms regions = {} lat_long = {} for index,y in enumerate((range(0, h+region_size, region_size))): lat, long = calculate_new_lat_long(latitude, longitude, bearing=rotation, distance=-index * dist) for index1, x in enumerate(range(0, w+region_size, region_size)): key = str(index) + ":" + str(index1) regions[key] = [] lat_long[key] = (lat,long) lat, long = calculate_new_lat_long(lat, long, bearing=rotation + 90, distance=dist) # go through all the boxes and figure out what quadrant they should be in. for box, label in zip(boxes, labels): x1, y1, x2, y2 = box x = np.mean([x2,x1]) y = np.mean([y2,y1]) regions[str(int(x / region_size)) + ":" + str(int(y / region_size))].append(label) #create csv file. with open(output_dir + original_filename + "_fielddata.csv", "w+") as csv_file: writer = csv.writer(csv_file) writer.writerow(["quadrant", "total_count", "small_count", "medium_count", "large_count", "size type","latitude", "longitude"]) for nme, labs in regions.items(): #get lat long in here. lati, longi = lat_long[nme] size = len(labs) if size == 0: counts = [0,0,0] type = -1 else: counts, _ = np.histogram(np.array(labs), bins=[0,1,2,3]) typ = np.argmax(counts) #print(lati, ",", longi) writer.writerow([nme, str(size), str(counts[0]), str(counts[1]), str(counts[2]), str(typ),str(lati), str(longi)])
def image_save_preprocessor(img, report=True): """Normalize the image Procedure --------- - Convert higher bit images (16, 10, etc) to 8 bit - Set color channel to the last channel - Drop Alpha layer and conver b+w -> RGB TODO ---- Correctly handle images with values [0,1) Args ---- img (np array) : raw image data report (bool) : output a short log on the imported data Returns ------- numpy array of cleaned image data with values [0, 255] """ data = np.asarray(img) if data.ndim == 3: # set the color channel to last if in channel_first format if data.shape[0] <= 4: data = np.rollaxis(data, 0, 3) # remove alpha channel if data.shape[-1] == 4: data = data[..., :3] # Convert to color if B+W if len(data.shape) == 2 or (len(data.shape) == 3 and data.shape[-1] == 1): data = grey2rgb(data) # if > 8 bit, shift to a 255 pixel max bitspersample = int(math.ceil(math.log(data.max(), 2))) if bitspersample > 8: data >>= bitspersample - 8 # if data [0, 1), then set range to [0,255] if bitspersample <= 0: data *= 255 data = data.astype(np.uint8) if report: print("Cleaned To:") print("\tShape: ", data.shape) print("\tdtype: ", data.dtype) # Make sure the data is actually in the correct format data_is_ok(data, raise_exception=True) return data
def patchgan_heatmap(d_activations): """ Create heatmap from patchgan discriminator output """ patch = resize(d_activations, (256, 256, 1), order=0, preserve_range=True, anti_aliasing=False, mode='constant') patch = np.asarray(grey2rgb(patch[:, :, 0])) return patch
def makeThumbnail(imfn, baseheight): testim = io.imread(imfn) hpercent = (baseheight / float(testim.shape[1])) wsize = int((float(testim.shape[0]) * float(hpercent))) smallim = transform.resize(testim, (wsize, baseheight)) if len(testim.shape) == 2: smallim = color.grey2rgb(smallim) io.imsave('./tmpim/' + imfn.split('/')[-1], smallim)
def __init__(self, im, x, y): self.im = img_as_ubyte(im) self.imrgb = grey2rgb(self.im) self.w, self.h = im.shape self.x, self.y = x, y self.cellw = self.w / x self.cellh = self.h / y self._cell_colors()
def make_frame(t): i = int(t * fps_in) img = schler(imSeq[i]) imgSize = img.shape xx, yy = np.indices(imgSize) cx, cy = [i // 2 for i in imgSize] r = np.sqrt((xx - cx)**2 + (yy - cy)**2) return color.grey2rgb(np.asarray(im))
def evaluate_whole_field(output_dir, field, model, l=250, stride=5, prune=True): #run through the image cutting off 1k squres. box_length = 20 h, w = field.shape[:2] ##load the main three variables. start = np.array([0,0]) if os.path.exists(output_dir+"loop_vars.npy"): start = np.load(output_dir+"loop_vars.npy") boxes = None if os.path.exists(output_dir+"boxes.npy"): boxes = np.load(output_dir+"boxes.npy") else: boxes = np.zeros((1, 4)) probs = None if os.path.exists(output_dir+"probs.npy"): probs = np.load(output_dir+"probs.npy") else: probs = np.zeros((1)) #we take off box length in case of an overlap. for x in range(start[0], h, l-box_length): for y in range(start[1], w, l-box_length): print("%d, %d" % (x,y)) # Prevent doing all this work for all black squares if np.max(field[x:x+l,y:y+l]) == 0: continue np.save(output_dir+"loop_vars.npy", np.array([x, y])) box, prob = extract_region(field, model, x, y, l, box_length, stride, threshold=0.90, prune=prune) if len(box) is not 0: boxes = np.vstack((boxes,box)) probs = np.hstack((probs,prob)) #save the values for loading. np.save(output_dir+"boxes.npy", boxes) np.save(output_dir+"probs.npy", probs) start = np.array([x, 0]) np.save(output_dir+"loop_vars.npy", start) #set the loop vars to done. np.save(output_dir + "loop_vars.npy", np.array([h, w])) ##prune the overlapping boxes. if not prune: boxes, probs = non_max_suppression_fast(boxes, probs, 0.18) np.save(output_dir + "pruned_boxes.npy", boxes) np.save(output_dir + "pruned_probs.npy", probs) print(boxes.shape) imsave(name+"_lettuce_count_" + str(boxes.shape[0]) + ".png", draw_boxes(grey2rgb(field), boxes, color=(255,0,0)))
def prune_boxes(name,overlap_coefficient=0.18): boxes = np.load("boxes.npy") print(boxes.shape) probs = np.load("probs.npy") boxes, probs = non_max_suppression_fast(boxes, probs, 0.18) np.save(name + "/pruned_boxes.npy", boxes) np.save(name + "/pruned_probs.npy", probs) print(boxes.shape) boxes = np.save(name + "/pruned_boxes.npy", boxes) imsave(name+"_lettuce_count_" + str(boxes.shape[0]) + ".png", draw_boxes(grey2rgb(whole_field), boxes, color=(255,0,0)))
def read_images(batch_image): batch_x = [] for img in batch_image: img_arr = misc.imread(img) resize_image = misc.imresize(img_arr, [224, 224, 3]) if resize_image.shape != (224, 224, 3): resize_image = color.grey2rgb(resize_image) / 255.0 else: resize_image = resize_image / 255.0 batch_x.append(resize_image) return np.array(batch_x)
def felzenszwalb(im, filename): plt.close('all') # Close all remaining figures im = img_as_float(im) im = color.grey2rgb(im) plt.figure(1) plt.imshow(im, cmap='gray') labels = color.label2rgb( segmentation.felzenszwalb(im, scale=800, sigma=0.5, min_size=250), im) labels = img_as_ubyte(labels) imout = Image.fromarray(labels, mode='RGB') imout.save('Processed/Felzenszwalb/' + filename)
def FloatArrayToRawRGB(im, min_value=0.0, max_value=1.0): """Convert a grayscale image to rgb, no encoding. For proper display try matplotlib's rendering/conversion instead of this version. Please be aware that this does not incorporate a proper color transform. http://pillow.readthedocs.io/en/3.4.x/reference/Image.html#PIL.Image.Image.convert https://en.wikipedia.org/wiki/Rec._601 """ im = img_as_ubyte(im) if im.shape[-1] == 1: im = grey2rgb(im) return im
def display(img_path, mask_path, save_path, model_name, train_phrease): img_file_path = os.path.join(img_path, train_phrease) mask_file_path = os.path.join(mask_path, train_phrease, model_name) save_file_path = os.path.join(save_path, train_phrease, model_name) for file_name in os.listdir(img_file_path): img = imageio.imread(os.path.join(img_file_path, file_name)) mask = imageio.imread(os.path.join(mask_file_path, file_name)) img = color.grey2rgb(img) img[mask == 255, 0] = 0 img[mask == 255, 2] = 0 imageio.imwrite(os.path.join(save_file_path, file_name), img) pass
def make_frame_old(time): i = int(time*fps) im =images[i] #ts = img.metadata['t_s'] ts = i/24. frame = i autoim= Image.fromarray(img_as_ubyte(exposure.rescale_intensity(im,in_range=(im.min(),im.max() ) ))) #get rid of alpha channel, it confuses moviepy, and multiply by 255 as that is how moviepy likes its colors for some reason draw= ImageDraw.Draw(autoim) draw.text((0,0),"time: "+str(datetime.timedelta(seconds=float(ts))),font=font,fill=(255)) draw.text((0,400),"frame: "+str(frame),font=font,fill=(255)) return color.grey2rgb(np.asarray(autoim))[:,:,:3]
def get_images(img_path, anno_path, valid_percent=0.1, resize_shape=(12, 12)): scale = resize_shape[0] / 400 # assuming that original image size is 400x400 file_names = sorted(glob(os.path.join(img_path, '*.jpg'))) anno_names = sorted(glob(os.path.join(anno_path, '*.npy'))) anno_names.extend([''] * (len(file_names) - len(anno_names))) imgs, b_probs, b_boxes, f_probs, f_boxes = [], [], [], [], [] for cur_file, cur_anno in tqdm(zip(file_names, anno_names), total=len(file_names)): img = io.imread(cur_file) if img.shape[-1] == 1: img = color.grey2rgb(img) age = os.path.splitext(os.path.basename(cur_file))[0].split('_')[0] if age == 'n': # no child in image b_probs.append(np.array([0, 1], dtype='float32')) b_boxes.append(np.array([0,0,0,0], dtype='int32')) f_probs.append(np.array([0, 1], dtype='float32')) f_boxes.append(np.array([0,0,0,0], dtype='int32')) else: b_probs.append(np.array([1, 0], dtype='float32')) o = np.load(cur_anno) bb, fb = o[0], o[1] bb = np.array([int(round(k*scale)) for k in bb], dtype='int32') fb = np.array([int(round(k*scale)) for k in fb], dtype='int32') b_boxes.append(bb) if (fb == np.array([0,0,0,0])).all(): # no face f_probs.append(np.array([0, 1], dtype='float32')) else: f_probs.append(np.array([1, 0], dtype='float32')) f_boxes.append(fb) #img = sktrsfm.resize(img, resize_shape, anti_aliasing=True, preserve_range=True)[..., :3] img = torchvision.transforms.ToPILImage()(img.astype(np.uint8)) img = img.resize(resize_shape, resample=Image.LANCZOS) imgs.append(img) rand_idx = np.random.permutation(np.arange(len(file_names))) imgs = [imgs[a] for a in rand_idx] b_probs = [b_probs[a] for a in rand_idx] b_boxes = [b_boxes[a] for a in rand_idx] f_probs = [f_probs[a] for a in rand_idx] f_boxes = [f_boxes[a] for a in rand_idx] vn = int(np.floor(len(imgs) * valid_percent)) return imgs[vn:], b_probs[vn:], b_boxes[vn:], f_probs[vn:], f_boxes[vn:], imgs[:vn], b_probs[:vn], b_boxes[:vn], f_probs[:vn], f_boxes[:vn]
def create_hsb_image(image, hue, saturation=1, brightness=1): """ Add color of the given hue to an greyscale image. By default, set the saturation to 1 so that the colors pop! """ rgb = grey2rgb(image) hsv = rgb2hsv(rgb) hsv[..., 0] = hue hsv[..., 1] = saturation hsv[..., 2] = brightness return hsv2rgb(hsv)
def main(): """Call the main program.""" # i/o model = args.model[0] # pre-trained model inp_data = args.input[0] # frames to be segmented out_data = args.output[0] # output csv file # create output os.makedirs(out_data, exist_ok=True) # load the model M = tf.keras.models.load_model(model) # verify if the input path exists, # if it does, then get the frame names if os.path.isdir(inp_data): images = natsorted(glob(inp_data + "/*")) else: raise IOError("No such file or directory \"{}\"".format(inp_data)) # --- loop over frames --- pbar = tqdm(total=len(images)) for k, image in enumerate(images): # print("-- plotting frame {} of {}".format(k+1, total_frames), end="\r") # load image img = grey2rgb(imread(image)) # predict pred = M.predict(np.expand_dims( img / 255, axis=0)) # very important to normalize your data ! prd = np.squeeze(np.argmax(pred, axis=-1)) # plot fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(12, 6), sharex=True, sharey=True) ax1.imshow(np.squeeze(img)) ax2.imshow(np.squeeze(prd)) fig.tight_layout() plt.savefig(os.path.join(out_data, str(k).zfill(6) + ".png"), pad_inches=0.1, bbox_inches='tight') plt.close() pbar.update()