我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用scipy.misc()。
def nparr_to_full_jpeg(nparr, out_fname, outsizex=770, outsizey=770, scale=True, scale_func=clipped_linscale_img, scale_func_params={'cap':255.0, 'lomult':2, 'himult':2.5}): ''' This just writes a numpy array to a JPEG. ''' if scale: scaled_img = scale_func(nparr,**scale_func_params) else: scaled_img = nparr resized_img = scipy.misc.imresize(scaled_img, (outsizex,outsizey)) if out_fname is None: out_fname = fits_image + '.jpeg' scipy.misc.imsave(out_fname,resized_img)
def load_np_image_uint8(image_file): """Loads an image as a numpy array. Args: image_file: str. Image file. Returns: A 3-D numpy array of shape [image_size, image_size, 3] and dtype uint8, with values in [0, 255]. """ with tempfile.NamedTemporaryFile() as f: f.write(tf.gfile.GFile(image_file, 'rb').read()) f.flush() image = scipy.misc.imread(f.name) # Workaround for black-and-white images if image.ndim == 2: image = np.tile(image[:, :, None], (1, 1, 3)) return image
def save_np_image(image, output_file, save_format='jpeg'): """Saves an image to disk. Args: image: 3-D numpy array of shape [image_size, image_size, 3] and dtype float32, with values in [0, 1]. output_file: str, output file. save_format: format for saving image (eg. jpeg). """ image = np.uint8(image * 255.0) buf = io.BytesIO() scipy.misc.imsave(buf, np.squeeze(image, 0), format=save_format) buf.seek(0) f = tf.gfile.GFile(output_file, 'w') f.write(buf.getvalue()) f.close()
def imsave(image, path): label_colours = [ (0,0,0), # 0=background (128,0,0),(0,128,0),(128,128,0),(0,0,128),(128,0,128), # 1=aeroplane, 2=bicycle, 3=bird, 4=boat, 5=bottle (0,128,128),(128,128,128),(64,0,0),(192,0,0),(64,128,0), # 6=bus, 7=car, 8=cat, 9=chair, 10=cow (192,128,0),(64,0,128),(192,0,128),(64,128,128),(192,128,128), # 11=diningtable, 12=dog, 13=horse, 14=motorbike, 15=person (0,64,0),(128,64,0),(0,192,0),(128,192,0),(0,64,128)] # 16=potted plant, 17=sheep, 18=sofa, 19=train, 20=tv/monitor images = np.ones(list(image.shape)+[3]) for j_, j in enumerate(image): for k_, k in enumerate(j): if k < 21: images[j_, k_] = label_colours[int(k)] scipy.misc.imsave(path, images)
def find_a_dominant_color(image): # K-mean clustering to find the k most dominant color, from: # http://stackoverflow.com/questions/3241929/python-find-dominant-most-common-color-in-an-image n_clusters = 5 # Get image into a workable form im = image.copy() im = im.resize((150, 150)) # optional, to reduce time ar = scipy.misc.fromimage(im) im_shape = ar.shape ar = ar.reshape(scipy.product(im_shape[:2]), im_shape[2]) ar = np.float_(ar) # Compute clusters codes, dist = scipy.cluster.vq.kmeans(ar, n_clusters) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences # Get the indexes of the most frequent, 2nd most frequent, 3rd, ... sorted_idxs = np.argsort(counts) # Get the color peak = codes[sorted_idxs[1]] # get second most frequent color return [int(i) for i in peak.tolist()] # list comprehension to quickly cast everything to int
def getUnlinkedStationary(self, popSize, theta): one_loc_probs = one_locus_probs(popSize=popSize, theta=theta, n=self.n) assertValidProbs(one_loc_probs) n = self.n leftOnes, rightOnes, bothOnes = self.numOnes(0), self.numOnes(1), self.hapCount((1,1)) joint = one_loc_probs[leftOnes] * one_loc_probs[rightOnes] if self.exact: joint[self.numC > 0] = 0 else: joint = joint * scipy.misc.comb(rightOnes, bothOnes) * scipy.misc.comb(n-rightOnes, leftOnes-bothOnes) / scipy.misc.comb(n, leftOnes) joint = joint * self.n_unfolded_versions assertValidProbs(joint) return joint
def test(): test_filename = sys.argv[2] test_image = scipy.misc.imread(test_filename, flatten=True) test_image = scipy.misc.imresize(test_image, [kanji_height, kanji_width]) test_image = skimage.img_as_float(test_image).astype(np.float32) #test_image = 1.0 - test_image #test_image /= np.linalg.norm(test_image) #test_image = 1.0 - test_image scipy.misc.imsave("test.png", test_image) model.load("kanji.tflearn") Y = model.predict(test_image.reshape([-1, kanji_height, kanji_width]))[0] Y_indices = np.argsort(Y)[::-1] num_test = 5 for i in range(num_test): print("Kanji: " + str(Y_indices[i]) + ", score=" + str(Y[Y_indices[i]]))
def test(): test_filename = sys.argv[2] test_image = scipy.misc.imread(test_filename, flatten=True) test_image = scipy.misc.imresize(test_image, [background_height, background_width]) test_image = skimage.img_as_float(test_image).astype(np.float32) model.load("find_kanji.tflearn") Y = model.predict(test_image.reshape([-1, background_height, background_width]))[0] print(Y) masked = np.square(Y) masked = scipy.misc.imresize(masked, [background_height, background_width]) masked = test_image * masked scipy.misc.imsave("y.png", masked) #Y_indices = np.argsort(Y)[::-1]
def next(self): sz = self.target_size output = np.ones([1, sz, sz, 1]).astype(np.float32) img = scipy.misc.imread( self._image_paths[self._index], mode='L').astype(np.float32) original_size = img.shape bigger_size = max(original_size[0], original_size[1]) mult = 1 if bigger_size > self.target_size: mult = self.target_size / float(bigger_size) resized_size = (int(original_size[0] * mult), int(original_size[1]*mult)) img = scipy.misc.imresize(img, resized_size) img = (img - 128.0) / 128.0 output[0, 0:resized_size[0], 0:resized_size[1], 0] = img self._index += 1 return output, original_size, resized_size
def _random_preprocessing(self, image, size): # rotate image rand_degree = np.random.randint(0, 90) rand_flip = np.random.randint(0, 2) if rand_flip == 1: image = np.flip(image, 1) image = scipy.ndimage.interpolation.rotate(image, rand_degree, cval=255) # Select cropping range between (target_size/2 ~ original_size) original_h, original_w = image.shape #crop_width = np.random.randint(self.target_size/3, min(self.target_size, original_w)) #crop_height = np.random.randint(self.target_size/3, min(self.target_size, original_h)) crop_width = self.target_size crop_height = self.target_size topleft_x = np.random.randint(0, original_w - crop_width) topleft_y = np.random.randint(0, original_h - crop_height) cropped_img = image[topleft_y:topleft_y+crop_height, topleft_x:topleft_x+crop_width] #output = scipy.misc.imresize(cropped_img, [self.target_size, self.target_size]) output = cropped_img output = (output - 128.0) / 128.0 return output
def _enqueue_op(self, queue, msg_queue): while msg_queue.qsize() == 0: # randomly select index indexes = np.random.randint(0, self._total_num, self.batch_size) sz = self.target_size output = np.zeros([self.batch_size, sz, sz, 1]) for i in range(len(indexes)): index = indexes[i] output[i] = self._random_preprocessing(scipy.misc.imread( self._image_paths[index], mode='L').astype(np.float), self.target_size).reshape([sz, sz, 1]) while np.amin(output[i]) == np.amax(output[i]): # some data are strange.. output[i] = self._random_preprocessing(scipy.misc.imread( self._image_paths[index], mode='L').astype(np.float32), self.target_size).reshape([sz, sz, 1]) queue.put(output)
def _random_preprocessing(self, image, size): # rotate image rand_degree = np.random.randint(0, 180) rand_flip = np.random.randint(0, 2) image = scipy.ndimage.interpolation.rotate(image, rand_degree, cval=255) if rand_flip == 1: image = np.flip(image, 1) # Select cropping range between (target_size/2 ~ original_size) original_h, original_w = image.shape crop_width = np.random.randint(self.target_size/2, min(self.target_size*2, original_w)) crop_height = np.random.randint(self.target_size/2, min(self.target_size*2, original_h)) topleft_x = np.random.randint(0, original_w - crop_width) topleft_y = np.random.randint(0, original_h - crop_height) cropped_img = image[topleft_y:topleft_y+crop_height, topleft_x:topleft_x+crop_width] output = scipy.misc.imresize(cropped_img, [self.target_size, self.target_size]) # threshold output_thres = np.where(output < 150, -1.0, 1.0) return output_thres
def _enqueue_op(self, queue, msg_queue): while msg_queue.qsize() == 0: # randomly select index indexes = np.random.randint(0, self._total_num, self.batch_size) sz = self.target_size output = np.ones([self.batch_size, sz, sz, 1]) for i in range(len(indexes)): index = indexes[i] output[i] = self._random_preprocessing(scipy.misc.imread( self._image_paths[index], mode='L').astype(np.float32), self.target_size).reshape([sz, sz, 1]) while np.amin(output[i]) == np.amax(output[i]): # some data are strange.. output[i] = self._random_preprocessing(scipy.misc.imread( self._image_paths[index], mode='L').astype(np.float32), self.target_size).reshape([sz, sz, 1]) queue.put(output)
def save_images(images, size, image_path, color=True): h, w = images.shape[1], images.shape[2] if color is True: img = np.zeros((h * size[0], w * size[1], 3)) else: img = np.zeros((h * size[0], w * size[1])) for idx, image in enumerate(images): i = idx % size[1] j = math.floor(idx / size[1]) if color is True: img[j*h:j*h+h, i*w:i*w+w, :] = image else: img[j*h:j*h+h, i*w:i*w+w] = image if color is True: scipy.misc.toimage(rescale_image(img), cmin=0, cmax=255).save(image_path) else: scipy.misc.toimage(rescale_dm(img), cmin=0, cmax=65535, low=0, high=65535, mode='I').save(image_path)
def imread(path, is_grayscale=False): if (is_grayscale): return scipy.misc.imread(path, flatten=True).astype(np.float) else: return scipy.misc.imread(path).astype(np.float)
def imsave(images, size, path): return scipy.misc.imsave(path, merge(images, size))
def fetchdatalabel(path, postfix='roienhance.mat', flag='train'): # 'enhance.mat' 'roienhance.jpeg' data = np.zeros((58, 40, 40)) label = np.zeros((58, 40, 40)) if flag == 'train': data = np.zeros((58*4, 40, 40)) label = np.zeros((58*4, 40, 40)) datacount = 0 fname = [] for file in os.listdir(path): if file.endswith(postfix): if postfix[-4:] == '.mat': im = sio.loadmat(path+file) im = im['im'] elif postfix[-5:] == '.jpeg': im = scipy.misc.imread(path+file) im = im*1.0 / 255.0 imlabel = sio.loadmat(path+file[:-len(postfix)]+'massgt.mat') imlabel = imlabel['im'] data[datacount, :, :] = im label[datacount, :, :] = imlabel datacount += 1 if flag == 'train': data[datacount, :, :] = im[:, ::-1] label[datacount, :, :] = imlabel[:, ::-1] data[datacount+1, :, :] = im[::-1, :] label[datacount+1, :, :] = imlabel[::-1, :] im1 = im[::-1, :] # vertical flip, then horizontal flip imlabel1 = imlabel[::-1, :] data[datacount+2, :, :] = im1[:, ::-1] label[datacount+2, :, :] = imlabel1[:, ::-1] datacount += 3 fname.append(file) if flag == 'train': assert(datacount==58*4) else: assert(datacount==58) return data , label, fname
def make_generator(files, batch_size, n_classes): if batch_size % n_classes != 0: raise ValueError("batch size must be divisible by num classes") class_batch = batch_size // n_classes generators = [] def get_epoch(): while True: images = np.zeros((batch_size, 3, DIM, DIM), dtype='int32') labels = np.zeros((batch_size, n_classes)) n=0 for style in styles: styleLabel = styleNum[style] curr = curPos[style] for i in range(class_batch): if curr == styles[style]: curr = 0 random.shuffle(list(files[style])) t0=time.time() image = scipy.misc.imread("{}/{}/{}.png".format(path, style, str(curr)),mode='RGB') #image = scipy.misc.imresize(image,(DIM,DIM)) images[n % batch_size] = image.transpose(2,0,1) labels[n % batch_size, int(styleLabel)] = 1 n+=1 curr += 1 curPos[style]=curr #randomize things but keep relationship between a conditioning vector and its associated image rng_state = np.random.get_state() np.random.shuffle(images) np.random.set_state(rng_state) np.random.shuffle(labels) yield (images, labels) return get_epoch
def read_depth(self, filename): depth_mat = sio.loadmat(filename) depthtmp=depth_mat["depth"] ds = depthtmp.shape if self.is_crop: depth = scipy.misc.imresize(depthtmp,(self.output_height,self.output_width),mode='F') depth = np.array(depth).astype(np.float32) depth = np.multiply(self.max_depth,np.divide(depth,depth.max())) return depth
def read_img(self, filename): imgtmp = scipy.misc.imread(filename) ds = imgtmp.shape if self.is_crop: img = scipy.misc.imresize(imgtmp,(self.output_height,self.output_width,3)) img = np.array(img).astype(np.float32) return img
def read_depth_small(self, filename): depth_mat = sio.loadmat(filename) depthtmp=depth_mat["depth"] ds = depthtmp.shape if self.is_crop: depth = scipy.misc.imresize(depthtmp,(self.output_height,self.output_width),mode='F') depth = np.array(depth).astype(np.float32) depth = np.multiply(self.max_depth,np.divide(depth,depth.max())) return depth
def read_depth_sample(self, filename): depth_mat = sio.loadmat(filename) depthtmp=depth_mat["depth"] ds = depthtmp.shape if self.is_crop: depth = scipy.misc.imresize(depthtmp,(self.sh,self.sw),mode='F') depth = np.array(depth).astype(np.float32) depth = np.multiply(self.max_depth,np.divide(depth,depth.max())) return depth
def read_img_sample(self, filename): imgtmp = scipy.misc.imread(filename) ds = imgtmp.shape if self.is_crop: img = scipy.misc.imresize(imgtmp,(self.sh,self.sw,3)) img = np.array(img).astype(np.float32) return img
def process_file(params): index, data, base_filename, db_name, C, aug_data = params label = index % NUM_CLASSES if C==1: orig_im = data[0,:,:] im = ndimage.interpolation.zoom(orig_im, DOWNSCALE_FACTOR) elif C==2: im = np.zeros((int(MAT_SHAPE[2]*DOWNSCALE_FACTOR),int(MAT_SHAPE[3]*DOWNSCALE_FACTOR),3)) orig_im = np.zeros((MAT_SHAPE[2],MAT_SHAPE[3],3)) im[:,:,0] = ndimage.interpolation.zoom(data[0,:,:], DOWNSCALE_FACTOR) im[:,:,1] = ndimage.interpolation.zoom(data[1,:,:], DOWNSCALE_FACTOR) orig_im[:,:,0] = data[0,:,:] orig_im[:,:,1] = data[1,:,:] else: print "Error in reading data to db- number of channels must be 1 or 2" im_name = '%s_%d%s' % (base_filename, index,IM_FORMAT) scipy.misc.toimage(im, cmin=0.0, cmax=255.0).save(os.path.join(db_name,im_name)) im_names = [im_name] if aug_data: degrees = [-20, -10, 10, 20] crop_dims = [2, 4, 6, 8] for i, degree in enumerate(degrees): im_name = '%s_%d_%d%s' % (base_filename,index,degree,IM_FORMAT) im_names.append(im_name) rot_im = rotate_im(orig_im, degree) scipy.misc.toimage(rot_im, cmin=0.0, cmax=255.0).save(os.path.join(db_name,im_name)) for i, crop_dim in enumerate(crop_dims): im_name = '%s_%d_%d%s' % (base_filename,index,crop_dim,IM_FORMAT) im_names.append(im_name) cr_im = crop_and_rescale(orig_im, crop_dim) scipy.misc.toimage(cr_im, cmin=0.0, cmax=255.0).save(os.path.join(db_name,im_name)) return label, im_names
def center_crop(image, pre_height, pre_width, height, width): h, w = image.shape[:2] j, i = int((h - pre_height)/2.), int((w - pre_width)/2.) return scipy.misc.imresize( image[j:j+pre_height, i:i+pre_width], [height, width])
def transform(image, pre_height, pre_width, height, width, is_crop): if is_crop: new_image = center_crop(image, pre_height, pre_width, height, width) else: new_image = scipy.misc.imresize(image, [height, width]) return np.array(new_image)/127.5 - 1.
def imread(path, is_grayscale=False): if is_grayscale: return scipy.misc.imread(path, flatten=True).astype(np.float) return scipy.misc.imread(path).astype(np.float)
def save_data(path, image_folder='./images/', label_folder='./labels/'): if not os.path.exists(image_folder): os.makedirs(image_folder) if not os.path.exists(label_folder): os.makedirs(label_folder) data_file = h5py.File(path, 'r') for index in range(data_file['X'].shape[0]): scipy.misc.imsave(image_folder+str(index)+'.png', data_file['X'][index]) imsave(data_file['Y'][index], label_folder+str(index)+'.png')
def _auto_color(self, url:str, ranks): phrases = ["Calculating colors..."] # in case I want more #try: await self.bot.say("**{}**".format(random.choice(phrases))) clusters = 10 async with self.session.get(url) as r: image = await r.content.read() with open('data/leveler/temp_auto.png','wb') as f: f.write(image) im = Image.open('data/leveler/temp_auto.png').convert('RGBA') im = im.resize((290, 290)) # resized to reduce time ar = scipy.misc.fromimage(im) shape = ar.shape ar = ar.reshape(scipy.product(shape[:2]), shape[2]) codes, dist = scipy.cluster.vq.kmeans(ar.astype(float), clusters) vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences # sort counts freq_index = [] index = 0 for count in counts: freq_index.append((index, count)) index += 1 sorted_list = sorted(freq_index, key=operator.itemgetter(1), reverse=True) colors = [] for rank in ranks: color_index = min(rank, len(codes)) peak = codes[sorted_list[color_index][0]] # gets the original index peak = peak.astype(int) colors.append(''.join(format(c, '02x') for c in peak)) return colors # returns array #except: #await self.bot.say("```Error or no scipy. Install scipy doing 'pip3 install numpy' and 'pip3 install scipy' or read here: https://github.com/AznStevy/Maybe-Useful-Cogs/blob/master/README.md```") # converts hex to rgb
def createSpectrogramFile(x, Fs, fileName, stWin, stStep): specgramOr, TimeAxis, FreqAxis = aF.stSpectogram(x, Fs, round(Fs * stWin), round(Fs * stStep), False) print specgramOr.shape if inputs[2]=='full': print specgramOr numpy.save(fileName.replace('.png','')+'_spectrogram', specgramOr) else: #specgram = scipy.misc.imresize(specgramOr, float(227.0) / float(specgramOr.shape[0]), interp='bilinear') specgram = cv2.resize(specgramOr,(227, 227), interpolation = cv2.INTER_LINEAR) im1 = Image.fromarray(numpy.uint8(matplotlib.cm.jet(specgram)*255)) scipy.misc.imsave(fileName, im1)
def Zoomed(data): datazoomed = scipy.misc.imresize(data,(60,60)) datazoomed = datazoomed[5:53,5:53] datazoomed = datazoomed.reshape(2304).tolist() return datazoomed
def outputImage(pixels,number): data = pixels name = str(number)+"output.jpg" scipy.misc.imsave(name, data)
def __init__(self, title): self.title = title if hasattr(data, title): self.data = getattr(data, title) else : self.data = getattr(misc, title)
def process(image): # apply gaussian filter to image to make text wider image = gaussian_filter(image, sigma=BLUR_AMOUNT) # invert black and white because most of the image is white image = 255 - image # resize image to make it smaller image = scipy.misc.imresize(arr=image, size=(FINAL_SIZE, FINAL_SIZE)) # scale down the value of each pixel image = image / 255.0 # flatten the image array to a list return [item for sublist in image for item in sublist]
def convert(inputs): imname = inputs['original_filename'] image = inputs['image'] labels = inputs['labels'] label_vis = inputs['label_vis'] results = [] segmentation = labels[:, :, 0] norm_factor = float(crop) / max(image.shape[:2]) image = scipy.misc.imresize(image, norm_factor, interp='bilinear') segmentation = scipy.misc.imresize(segmentation, norm_factor, interp='nearest') if image.shape[0] < crop: # Pad height. image = pad_height(image, crop) segmentation = pad_height(segmentation, crop) if image.shape[1] < crop: image = pad_width(image, crop) segmentation = pad_width(segmentation, crop) labels = np.dstack([segmentation] * 3) label_vis = apply_colormap(segmentation, vmax=21, vmin=0, cmap=CMAP)[:, :, :3] results.append([imname, image * (labels != 0), labels, label_vis]) # Swapped version. imname = path.splitext(imname)[0] + '_swapped' + path.splitext(imname)[1] image = image[:, ::-1] segmentation = segmentation[:, ::-1] segmentation = lrswap_regions(segmentation) labels = np.dstack([segmentation] * 3) label_vis = apply_colormap(segmentation, vmax=21, vmin=0, cmap=CMAP)[:, :, :3] results.append([imname, image * (labels != 0), labels, label_vis]) return results
def fix_image(image_filename): image = scipy.misc.imread(test_filenames[i], flatten=False) image = scipy.misc.imresize(image, [image_size, image_size]) image = skimage.img_as_float(image) image = np.swapaxes(image, 0, 2) image = np.swapaxes(image, 1, 2) return image
def test(): print("testing...") predictor_model = sys.argv[3] predictor.load_state_dict(torch.load(predictor_model)) img_outputs = predict_test_sequence().data.cpu().numpy() for i in range(num_outputs): img = img_outputs[i].reshape(num_components, image_size, image_size) img = np.swapaxes(img, 0, 1) img = np.swapaxes(img, 1, 2) print(img.shape) scipy.misc.imsave("output_" + str(i).zfill(3) + ".png", img) #train()
def test(): print("testing...") generator_model = "gen_epoch_39.pth" discriminator_model = "disc_epoch_39.pth" generator.load_state_dict(torch.load(generator_model)) discriminator.load_state_dict(torch.load(discriminator_model)) dump_sheet = True if (dump_sheet): fake = generator(fixed_noise) out_file = "sheet.png" print("saving to: " + out_file) vutils.save_image(fake.data, out_file) make_video = True if (make_video): video_noise = Variable(torch.FloatTensor(1, nz, 1, 1)).cuda() video_noise_cpu = fixed_noise[0].data.cpu().numpy()#np.random.normal(loc=0.0, scale=1.0, size=[1, nz, 1, 1]) video_noise.data.copy_(torch.from_numpy(video_noise_cpu)) noise_vel_speed = 0.05 video_noise_vel = np.random.uniform(low=-noise_vel_speed, high=noise_vel_speed, size=[1, nz, 1, 1]) num_frames = 300 for frame_idx in range(num_frames): print(frame_idx) video_frame = generator(video_noise).data.cpu().numpy() video_frame = video_frame.reshape([nc, image_size, image_size]).transpose() scipy.misc.imsave("frame_" + str(frame_idx).zfill(5) + ".png", video_frame.reshape([image_size, image_size])) video_noise_cpu = np.mod(video_noise_cpu + video_noise_vel, 1.0) video_noise.data.copy_(torch.from_numpy(video_noise_cpu))
def augment_kanji(kanji, augmentation_idx): angle = np.random.randint(0,360) * (np.pi / 180.0) dist = np.random.randint(0,aug_translation_max_dist) x = int(math.cos(angle) * dist) y = int(math.sin(angle) * dist) augmented = np.roll(kanji, [y, x], axis=[0, 1]) #angle_step = (np.pi * 2.0) / float(num_augmentations+1) #angle = angle_step + (angle_step * float(augmentation_idx)) #angle *= (180.0 / np.pi) # degrees rot_angle = np.random.randint(-2, 2) augmented = scipy.misc.imrotate(augmented, rot_angle, interp="bilinear") pad_max = 12 pad_w = np.random.randint(0, pad_max) pad_h = pad_w augmented = np.pad(augmented, ((pad_h, pad_h), (pad_w, pad_w)), mode="constant") augmented = scipy.misc.imresize(augmented, [kanji_height, kanji_width]) augmented = skimage.img_as_float(augmented).astype(np.float32) noise = np.random.uniform(low=0.1, high=0.5) augmented += np.random.uniform(low=-noise, high=noise, size=augmented.shape) augmented = np.maximum(0.0, np.minimum(augmented, 1.0)) return augmented
def rasterize_all_kanji(): df = pd.read_csv("kanji.csv", sep="\t", header=None) kanji_strings = df[1].dropna().values num_kanji = kanji_strings.size print("Kanji: " + str(num_kanji)) weights = ["normal"]#"normal", "light", "bold"] num_weights = len(weights) images = np.zeros([num_kanji*num_weights*(num_augmentations+1), kanji_height, kanji_width]) kanjis = np.zeros([num_kanji*num_weights*(num_augmentations+1)], dtype=np.uint32) image_idx = 0 for kanji_idx in range(num_kanji): print("Kanji " + str(kanji_idx)) kanji = kanji_strings[kanji_idx] for weight_idx in range(num_weights): weight = weights[weight_idx] image = rasterize_kanji(kanji, weights[weight_idx], "images/" + str(kanji_idx).zfill(5) + "_" + weight + ".png") images[image_idx] = image kanjis[image_idx] = kanji_idx image_idx += 1 for augmentation_idx in range(num_augmentations): augmented_img = augment_kanji(image, augmentation_idx) #scipy.misc.imsave("aug_" + str(kanji_idx).zfill(4) + "_" + str(weight_idx) + "_" + str(augmentation_idx).zfill(2) + ".png", augmented_img) images[image_idx] = augmented_img kanjis[image_idx] = kanji_idx image_idx += 1 return images, kanjis