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])
python类misc()的实例源码
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 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
generateSpectrograms.py 文件源码
项目:CNNs-Speech-Music-Discrimination
作者: MikeMpapa
项目源码
文件源码
阅读 35
收藏 0
点赞 0
评论 0
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