def transformData(self, data):
if(self.opts.has_key('newdims')):
(H, W) = self.opts['newdims']
data = misc.imresize(data, (H, W), interp='bilinear')
if(self.opts.has_key('zeromean') and self.opts['zeromean']):
mean = self.opts['dataset_mean'] # provided by bmanager
data = data - mean
if(self.opts.has_key('rangescale') and self.opts['rangescale']):
min_ = self.opts['dataset_min'] # provided by bmanager
min_ = np.abs(min_.min())
max_ = self.opts['dataset_max'] # provided by bmanager
max_ = np.abs(max_.max())
data = 127 * data / max(min_, max_)
else:
data = data - 127.0
if(self.opts.has_key('randomflip') and self.opts['randomflip']):
if(np.random.rand() <= self.opts['randomflip_prob']):
data = np.flipud(data)
self.dataflip_state = True
return data
python类imresize()的实例源码
def predict():
# get data from drawing canvas and save as image
parseImage(request.get_data())
# read parsed image back in 8-bit, black and white mode (L)
x = imread('output.png', mode='L')
x = np.invert(x)
x = imresize(x,(28,28))
# reshape image data for use in neural network
x = x.reshape(1,28,28,1)
with graph.as_default():
out = model.predict(x)
print(out)
print(np.argmax(out, axis=1))
response = np.array_str(np.argmax(out, axis=1))
return response
def process_mot(path):
'''
1920 x 1080 -> 384 x 216
640 x 480 -> 320 x 240
'''
images = []
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
if filename[-4:] == ".jpg" and "_ds" not in filename:
full_path = os.path.join(dirpath, filename)
img = misc.imread(full_path,mode='RGB')
if img.shape == LARGE_IMAGE_SIZE:
img = misc.imresize(img, size=LARGE_IMAGE_RESCALE)
img = pad_image(img, FINAL_IMAGE_SIZE)
elif img.shape == MEDIUM_IMAGE_SIZE:
img = misc.imresize(img, size=MEDIUM_IMAGE_RESCALE)
img = pad_image(img, FINAL_IMAGE_SIZE)
else:
print("Unexpected shape " + str(img.shape))
continue
output_filename = os.path.join(dirpath, filename[:-4] + "_ds.jpg")
misc.imsave(output_filename, img)
images.append(output_filename)
return images
def process_vot(path, min_height, min_width):
images = []
for dirpath, dirnames, filenames in os.walk(path):
img_shape = None
pad_height = 0
pad_width = 0
for filename in filenames:
if filename[-4:] == ".jpg" and "_ds" not in filename:
full_path = os.path.join(dirpath, filename)
img = misc.imread(full_path,mode='RGB')
img_shape = img.shape
ratio = min(float(min_width)/img.shape[1], float(min_height)/img.shape[0])
img = misc.imresize(img, size=ratio)
img, pad_height, pad_width = pad_image(img, (min_height, min_width))
output_filename = os.path.join(dirpath, filename[:-4] + "_ds.jpg")
misc.imsave(output_filename, img)
images.append(output_filename)
if img_shape:
gt_path = os.path.join(dirpath, "groundtruth.txt")
preprocess_label(gt_path, ratio, img_shape, min_height, min_width, pad_height, pad_width)
return images
def do_roc(self):
if self.gan_mode and self.dmodel2 is not None:
dmodel_cur = self.dmodel2
scale_factor = 2
elif self.dmodel is not None:
dmodel_cur = self.dmodel
scale_factor = self.scale_factor
else:
theApp.cur_hist_tex = theApp.standard_hist_tex
theApp.cur_roc_tex = theApp.standard_roc_tex
return
encoded_vector_source = self.get_encoded(dmodel_cur, self.cur_vector_source, scale_factor)
encoded_vector_dest = self.get_encoded(dmodel_cur, self.cur_vector_dest, scale_factor)
attribute_vector = encoded_vector_dest - encoded_vector_source
threshold = None
outfile = "{}/{}".format(roc_dir, get_date_str())
do_roc(attribute_vector, encoded, attribs, attribute_index, threshold, outfile)
hist_img = imread("{}_hist_both.png".format(outfile), mode='RGB')
roc_img = imread("{}_roc.png".format(outfile), mode='RGB')
hist_img = imresize(hist_img, roc_image_resize)
roc_img = imresize(roc_img, roc_image_resize)
theApp.cur_hist_tex = image_to_texture(hist_img)
theApp.cur_roc_tex = image_to_texture(roc_img)
def create_dictionary_dl(lmbd, K=100, N=10000, dir_mnist='save_exp/mnist'):
import os.path as osp
fname = osp.join(dir_mnist, "D_mnist_K{}_lmbd{}.npy".format(K, lmbd))
if osp.exists(fname):
D = np.load(fname)
else:
from sklearn.decomposition import DictionaryLearning
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
im = mnist.train.next_batch(N)[0]
im = im.reshape(N, 28, 28)
im = [imresize(a, (17, 17), interp='bilinear', mode='L')-.5
for a in im]
X = np.array(im).reshape(N, -1)
print(X.shape)
dl = DictionaryLearning(K, alpha=lmbd*N, fit_algorithm='cd',
n_jobs=-1, verbose=1)
dl.fit(X)
D = dl.components_.reshape(K, -1)
np.save(fname, D)
return D
def main(input_pic):
img = cv.imread(input_pic,cv.CV_LOAD_IMAGE_GRAYSCALE)
img=sp.gaussian_filter(img,sigma=3)
img= imresize(img,((len(img)/10),(len(img[0])/10)))
img_arr=np.asarray(img,dtype="int32")
LoG_arr=LoG_Filter(img_arr)
cv.imwrite('LoG_image.jpg',LoG_arr)
LoG_arr=cv.imread('LoG_image.jpg',cv.CV_LOAD_IMAGE_GRAYSCALE)
Hist=genHistogram(LoG_arr)
#print(Hist)
for i in range(0,len(LoG_arr)):
for j in range(0,len(LoG_arr[0])):
if LoG_arr[i][j]<200:
LoG_arr[i][j]=0
else:
LoG_arr[i][j]=255
cv.imwrite('LoG_image.jpg',LoG_arr)
#img_new=cv.imread('LoG_image.jpg',cv.CV_LOAD_IMAGE_GRAYSCALE)
def preprocess(self, screen, new_game=False):
"""Converts to grayscale, resizes and stacks input screen.
:param screen: array image in [0; 255] range with shape=[H, W, C]
:param new_game: if True - repeats passed screen `memlen` times
otherwise - stacks with previous screens"
:type screen: nd.array
:type new_game: bool
:return: image in [0.0; 1.0] stacked with last `memlen-1` screens;
shape=[1, h, w, memlen]
:rtype: nd.array"""
gray = screen.astype('float32').mean(2) # no need in true grayscale, just take mean
# convert values into [0.0; 1.0] range
s = imresize(gray, (self.W, self.H)).astype('float32') * (1. / 255)
s = s.reshape(1, s.shape[0], s.shape[1], 1)
if new_game or self.stacked_s is None:
self.stacked_s = np.repeat(s, self.memlen, axis=3)
else:
self.stacked_s = np.append(s, self.stacked_s[:, :, :, :self.memlen - 1], axis=3)
return self.stacked_s
def preprocess(self, screen, new_game=False):
luminance = screen.astype('float32').mean(2) # no need in true grayscale, just take mean
# crop top/bottom Atari specific borders
if self.env.spec.id == 'SpaceInvaders-v0':
# crop only bottom in SpaceInvaders, due to flying object at the top of the screen
luminance = luminance[:-15, :]
else:
luminance = luminance[36:-15, :]
# convert into [0.0; 1.0]
s = imresize(luminance, (self.W, self.H)).astype('float32') * (1. / 255)
s = s.reshape(1, s.shape[0], s.shape[1], 1)
if new_game or self.stacked_s is None:
self.stacked_s = np.repeat(s, self.memlen, axis=3)
else:
self.stacked_s = np.append(s, self.stacked_s[:, :, :, :self.memlen - 1], axis=3)
return self.stacked_s
def resize_images(x, shape):
from scipy.misc import imresize
reszie_func = lambda x, shape: imresize(x, shape, interp='bilinear')
if x.ndim == 4:
def reszie_func(x, shape):
# x: 3D
# The color channel is the first dimension
tmp = []
for i in x:
tmp.append(imresize(i, shape).reshape((-1,) + shape))
return np.swapaxes(np.vstack(tmp).T, 0, 1)
imgs = []
for i in x:
imgs.append(reszie_func(i, shape))
return imgs
def __caffe_predict(self, net, height, width, url):
# logger = logging.getLogger(__name__)
#
# logger.info("caffe_predict has been called")
input_layer = net.inputs[0]
output_layer = net.outputs[0]
r = requests.get(url, allow_redirects=False)
arr = numpy.asarray(bytearray(r.content), dtype=numpy.uint8)
img = cv2.imdecode(arr, -1)
resized_img = imresize(img, (height,width), 'bilinear')
transposed_resized_img = numpy.transpose(resized_img, (2,0,1))
reqd_shape = (1,) + transposed_resized_img.shape
#net.blobs["data_q"].reshape(*reqd_shape)
#net.blobs["data_q"].data[...] = transposed_resized_img
net.blobs[input_layer].reshape(*reqd_shape)
net.blobs[input_layer].data[...] = transposed_resized_img
net.forward()
#result = net.blobs['latent_q_encode'].data[0].tolist()
result = net.blobs[output_layer].data[0].tolist()
return result
def agent_start(self, observation):
# Preprocess
tmp = np.bitwise_and(np.asarray(observation.intArray[128:]).reshape([210, 160]), 0b0001111) # Get Intensity from the observation
obs_array = (spm.imresize(tmp, (110, 84)))[110-84-8:110-8, :] # Scaling
# Initialize State
self.state = np.zeros((4, 84, 84), dtype=np.uint8)
self.state[0] = obs_array
state_ = cuda.to_gpu(np.asanyarray(self.state.reshape(1, 4, 84, 84), dtype=np.float32))
# Generate an Action e-greedy
returnAction = Action()
action, Q_now = self.DDQN.e_greedy(state_, self.epsilon)
returnAction.intArray = [action]
# Update for next step
self.lastAction = copy.deepcopy(returnAction)
self.last_state = self.state.copy()
self.last_observation = obs_array
return returnAction
def preprocess_faces(images, n_components='default', resize_shape='default'):
""" Notes: Images are all square, but not the same size. We resize all the
images to be the same sized square, thereby preserving the
aspect ratios.
"""
for img in images:
assert img.shape[0] == img.shape[1]
if resize_shape == 'default':
resize_shape = get_smallest_shape(images)
preprocessed_images = []
for img in images:
prepped_img = imresize(img, resize_shape).astype(float)
prepped_img = prepped_img.flatten()
preprocessed_images.append(prepped_img)
preprocessed = get_principal_components(preprocessed_images, n_components)
return preprocessed
def find_faces(self, image):
faces = []
bounding_boxes, _ = align.detect_face.detect_face(image, self.minsize,
self.pnet, self.rnet, self.onet,
self.threshold, self.factor)
for bb in bounding_boxes:
face = Face()
face.container_image = image
face.bounding_box = np.zeros(4, dtype=np.int32)
img_size = np.asarray(image.shape)[0:2]
face.bounding_box[0] = np.maximum(bb[0] - self.face_crop_margin / 2, 0)
face.bounding_box[1] = np.maximum(bb[1] - self.face_crop_margin / 2, 0)
face.bounding_box[2] = np.minimum(bb[2] + self.face_crop_margin / 2, img_size[1])
face.bounding_box[3] = np.minimum(bb[3] + self.face_crop_margin / 2, img_size[0])
cropped = image[face.bounding_box[1]:face.bounding_box[3], face.bounding_box[0]:face.bounding_box[2], :]
face.image = misc.imresize(cropped, (self.face_crop_size, self.face_crop_size), interp='bilinear')
faces.append(face)
return faces
def resize_to_optimal(infile, scale_ratio, rect, outfile):
image_array = imread(infile, mode='RGB')
im_shape = image_array.shape
h, w, _ = im_shape
width = float(rect.right()-rect.left())
scale_amount = (optimal_extent * scale_ratio) / width
new_w = int(scale_amount * w)
new_h = int(scale_amount * h)
new_w = new_w - (new_w % 4)
new_h = new_h - (new_h % 4)
print("optimal resize of width {} and ratio {} went from {},{} to {},{}".format(width, scale_ratio, w, h, new_w, new_h))
new_shape = (new_h, new_w)
image_array_resized = imresize(image_array, new_shape)
imsave(outfile, image_array_resized)
return new_shape
def input_wrapper(f):
image = misc.imread(f)
sx,sy = image.shape
diff = np.abs(sx-sy)
sx,sy = image.shape
image = np.pad(image,((sx//8,sx//8),(sy//8,sy//8)),'constant')
if sx > sy:
image = np.pad(image,((0,0),(diff//2,diff//2)),'constant')
else:
image = np.pad(image,((diff//2,diff//2),(0,0)),'constant')
image = dilation(image,disk(max(sx,sy)/32))
image = misc.imresize(image,(32,32))
if np.max(image) > 1:
image = image/255.
return image
def input_wrapper(f):
image = misc.imread(f)
# image[image>50]=255
# image[image<=50]=0
sx,sy = image.shape
diff = np.abs(sx-sy)
sx,sy = image.shape
image = np.pad(image,((sx//8,sx//8),(sy//8,sy//8)),'constant')
if sx > sy:
image = np.pad(image,((0,0),(diff//2,diff//2)),'constant')
else:
image = np.pad(image,((diff//2,diff//2),(0,0)),'constant')
image = dilation(image,disk(max(sx,sy)/32))
image = misc.imresize(image,(32,32))
if np.max(image) > 1:
image = image/255.
return image
def main():
x = tf.placeholder(tf.float32, [None, 224, 224, 3])
network, probs = build_vgg(x)
# network2, probs2 = build_vgg(x)
sess = tf.InteractiveSession()
tl.layers.initialize_global_variables(sess)
network.print_params()
network.print_layers()
npz = np.load('vgg16_weights.npz')
params = []
for val in sorted( npz.items() ):
print(" Loading %s" % str(val[1].shape))
params.append(val[1])
tl.files.assign_params(sess, params, network)
img1 = imread('laska.png', mode='RGB')
img1 = imresize(img1, (224, 224))
prob = sess.run(probs, feed_dict={x: [img1]})[0]
print(prob)
def _merge(self, images, size):
h, w = images.shape[1], images.shape[2]
h_ = int(h * self.resize_factor)
w_ = int(w * self.resize_factor)
img = np.zeros((h_ * size[0], w_ * size[1]))
for idx, image in enumerate(images):
i = int(idx % size[1])
j = int(idx / size[1])
image_ = imresize(image, size=(w_,h_), interp='bicubic')
img[j*h_:j*h_+h_, i*w_:i*w_+w_] = image_
return img
def _merge(self, images, size):
h, w = images.shape[1], images.shape[2]
h_ = int(h * self.resize_factor)
w_ = int(w * self.resize_factor)
img = np.zeros((h_ * size[0], w_ * size[1]))
for idx, image in enumerate(images):
i = int(idx % size[1])
j = int(idx / size[1])
image_ = imresize(image, size=(w_, h_), interp='bicubic')
img[j * h_:j * h_ + h_, i * w_:i * w_ + w_] = image_
return img
# borrowed from https://github.com/ykwon0407/variational_autoencoder/blob/master/variational_bayes.ipynb