def render_fonts_image(x, path, img_per_row, unit_scale=True):
if unit_scale:
# scale 0-1 matrix back to gray scale bitmaps
bitmaps = (x * 255.).astype(dtype=np.int16) % 256
else:
bitmaps = x
num_imgs, w, h = x.shape
assert w == h
side = int(w)
width = img_per_row * side
height = int(np.ceil(float(num_imgs) / img_per_row)) * side
canvas = np.zeros(shape=(height, width), dtype=np.int16)
# make the canvas all white
canvas.fill(255)
for idx, bm in enumerate(bitmaps):
x = side * int(idx / img_per_row)
y = side * int(idx % img_per_row)
canvas[x: x + side, y: y + side] = bm
misc.toimage(canvas).save(path)
return path
python类toimage()的实例源码
1_logistic_regression.py 文件源码
项目:Machine-Learning-Coursera-Tensorflow
作者: themech
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def plot_100_images(X):
"""Plot 100 randomly picked digits."""
width, height = 20, 20
nrows, ncols = 10, 10
indices_to_display = np.random.choice(range(X.shape[0]), nrows * ncols)
big_picture = np.zeros((height * nrows, width * ncols))
irow, icol = 0, 0
for idx in indices_to_display:
if icol == ncols:
irow += 1
icol = 0
iimg = X[idx].reshape(width, height).T # transpose the data set
big_picture[irow * height:irow * height + iimg.shape[0], icol * width:icol * width + iimg.shape[1]] = iimg
icol += 1
fig = plt.figure(figsize=(6, 6))
img = misc.toimage(big_picture)
plt.imshow(img, cmap=matplotlib.cm.Greys_r)
plt.show()
1_nn_training_example.py 文件源码
项目:Machine-Learning-Coursera-Tensorflow
作者: themech
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def plot_100_images(X, indices=None):
"""Plot 100 randomly picked digits."""
width, height = IMAGE_WIDTH, IMAGE_HEIGHT
nrows, ncols = 10, 10
if indices is None:
indices = range(X.shape[0])
indices_to_display = np.random.choice(indices, nrows * ncols)
big_picture = np.zeros((height * nrows, width * ncols))
irow, icol = 0, 0
for idx in indices_to_display:
if icol == ncols:
irow += 1
icol = 0
iimg = X[idx].reshape(width, height).T # transpose the data set
big_picture[irow * height:irow * height + iimg.shape[0], icol * width:icol * width + iimg.shape[1]] = iimg
icol += 1
img = misc.toimage(big_picture)
plt.imshow(img, cmap=matplotlib.cm.Greys_r)
plt.show()
def setup(self, pre_encode=False):
sbd_path = get_data_path('sbd')
voc_path = get_data_path('pascal')
target_path = self.root + '/SegmentationClass/pre_encoded/'
if not os.path.exists(target_path):
os.makedirs(target_path)
sbd_train_list = tuple(open(sbd_path + 'dataset/train.txt', 'r'))
sbd_train_list = [id_.rstrip() for id_ in sbd_train_list]
self.files['train_aug'] = self.files['train'] + sbd_train_list
if pre_encode:
print("Pre-encoding segmentation masks...")
for i in tqdm(sbd_train_list):
lbl_path = sbd_path + 'dataset/cls/' + i + '.mat'
lbl = io.loadmat(lbl_path)['GTcls'][0]['Segmentation'][0].astype(np.int32)
lbl = m.toimage(lbl, high=lbl.max(), low=lbl.min())
m.imsave(target_path + i + '.png', lbl)
for i in tqdm(self.files['trainval']):
lbl_path = self.root + '/SegmentationClass/' + i + '.png'
lbl = self.encode_segmap(m.imread(lbl_path))
lbl = m.toimage(lbl, high=lbl.max(), low=lbl.min())
m.imsave(target_path + i + '.png', lbl)
evaluate_face_new_data_oclusion.py 文件源码
项目:structured-output-ae
作者: sbelharbi
项目源码
文件源码
阅读 16
收藏 0
点赞 0
评论 0
def hide_part(x, part):
"""x shape: 50x50"""
print x.shape
if part is "left":
x[:, :20] = 0.5
elif part is "right":
x[:, 30:] = 0.5
elif part is "up":
x[:20, :] = 0.5
elif part is "down":
x[30:, :] = 0.5
elif part is "middle":
x[15:35, :] = 0.5
# sm.toimage(x).save(str(part)+'.jpg')
return x
def load_gnt_file(filename):
"""
Load characters and images from a given GNT file.
:param filename: The file path to load.
:return: (image: Pillow.Image.Image, character) tuples
"""
# Thanks to nhatch for the code to read the GNT file, available at https://github.com/nhatch/casia
with open(filename, "rb") as f:
while True:
packed_length = f.read(4)
if packed_length == b'':
break
length = struct.unpack("<I", packed_length)[0]
raw_label = struct.unpack(">cc", f.read(2))
width = struct.unpack("<H", f.read(2))[0]
height = struct.unpack("<H", f.read(2))[0]
photo_bytes = struct.unpack("{}B".format(height * width), f.read(height * width))
# Comes out as a tuple of chars. Need to be combined. Encoded as gb2312, gotta convert to unicode.
label = decode(raw_label[0] + raw_label[1], encoding="gb2312")
# Create an array of bytes for the image, match it to the proper dimensions, and turn it into an image.
image = toimage(np.array(photo_bytes).reshape(height, width))
yield image, label
def histEqual(srcpath, dstpath):
im_source = Image.open(srcpath)
if True:
arr_im_gray = np.array(im_source)
arr_im_gray_hist = beautyImage(arr_im_gray)
# figure()
im_conver = toimage(arr_im_gray_hist, 255, 0, None, None, None, 'L')
im_conver.save(dstpath, 'jpeg')
# 2.???
def original_color_transform(content, generated, mask=None, hist_match=0, mode='YCbCr'):
generated = fromimage(toimage(generated, mode='RGB'), mode=mode) # Convert to YCbCr color space
if mask is None:
if hist_match == 1:
for channel in range(3):
generated[:, :, channel] = match_histograms(generated[:, :, channel], content[:, :, channel])
else:
generated[:, :, 1:] = content[:, :, 1:]
else:
width, height, channels = generated.shape
for i in range(width):
for j in range(height):
if mask[i, j] == 1:
if hist_match == 1:
for channel in range(3):
generated[i, j, channel] = match_histograms(generated[i, j, channel], content[i, j, channel])
else:
generated[i, j, 1:] = content[i, j, 1:]
generated = fromimage(toimage(generated, mode=mode), mode='RGB') # Convert to RGB color space
return generated
# util function to load masks
def original_color_transform(content, generated, mask=None):
generated = fromimage(toimage(generated, mode='RGB'), mode='YCbCr') # Convert to YCbCr color space
if mask is None:
generated[:, :, 1:] = content[:, :, 1:] # Generated CbCr = Content CbCr
else:
width, height, channels = generated.shape
for i in range(width):
for j in range(height):
if mask[i, j] == 1:
generated[i, j, 1:] = content[i, j, 1:]
generated = fromimage(toimage(generated, mode='YCbCr'), mode='RGB') # Convert to RGB color space
return generated
improved_neural_doodle.py 文件源码
项目:Neural-Style-Transfer-Windows
作者: titu1994
项目源码
文件源码
阅读 34
收藏 0
点赞 0
评论 0
def original_color_transform(content, generated):
generated = fromimage(toimage(generated, mode='RGB'), mode='YCbCr') # Convert to YCbCr color space
generated[:, :, 1:] = content[:, :, 1:] # Generated CbCr = Content CbCr
generated = fromimage(toimage(generated, mode='YCbCr'), mode='RGB') # Convert to RGB color space
return generated
def original_color_transform(content, generated):
generated = fromimage(toimage(generated, mode='RGB'), mode='YCbCr') # Convert to YCbCr color space
generated[:, :, 1:] = content[:, :, 1:] # Generated CbCr = Content CbCr
generated = fromimage(toimage(generated, mode='YCbCr'), mode='RGB') # Convert to RGB color space
return generated
def original_color_transform(content, generated, mask=None):
generated = fromimage(toimage(generated, mode='RGB'), mode='YCbCr') # Convert to YCbCr color space
if mask is None:
generated[:, :, 1:] = content[:, :, 1:] # Generated CbCr = Content CbCr
else:
width, height, channels = generated.shape
for i in range(width):
for j in range(height):
if mask[i, j] == 1:
generated[i, j, 1:] = content[i, j, 1:]
generated = fromimage(toimage(generated, mode='YCbCr'), mode='RGB') # Convert to RGB color space
return generated
def write_img(img, fpath, cmax=None):
from scipy import misc
if cmax is None:
cmax = 255
if np.max(img) <= 1.0 and img.dtype == float:
cmax = 1.0
img_conv = misc.toimage(img, cmin=0, cmax=cmax)
img_conv.save(fpath)
# json
def sample_hard_negatives(img, roi_mask, out_dir, img_id, abn,
patch_size=256, neg_cutoff=.35, nb_bkg=100,
start_sample_nb=0,
bkg_dir='background', verbose=False):
'''WARNING: the definition of hns may be problematic.
There has been study showing that the context of an ROI is also useful
for classification.
'''
bkg_out = os.path.join(out_dir, bkg_dir)
basename = '_'.join([img_id, str(abn)])
img = add_img_margins(img, patch_size/2)
roi_mask = add_img_margins(roi_mask, patch_size/2)
# Get ROI bounding box.
roi_mask_8u = roi_mask.astype('uint8')
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3:
contours,_ = cv2.findContours(
roi_mask_8u.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
else:
_,contours,_ = cv2.findContours(
roi_mask_8u.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cont_areas = [ cv2.contourArea(cont) for cont in contours ]
idx = np.argmax(cont_areas) # find the largest contour.
rx,ry,rw,rh = cv2.boundingRect(contours[idx])
if verbose:
M = cv2.moments(contours[idx])
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
print "ROI centroid=", (cx,cy); sys.stdout.flush()
rng = np.random.RandomState(12345)
# Sample hard negative samples.
sampled_bkg = start_sample_nb
while sampled_bkg < start_sample_nb + nb_bkg:
x1,x2 = (rx - patch_size/2, rx + rw + patch_size/2)
y1,y2 = (ry - patch_size/2, ry + rh + patch_size/2)
x1 = crop_val(x1, patch_size/2, img.shape[1] - patch_size/2)
x2 = crop_val(x2, patch_size/2, img.shape[1] - patch_size/2)
y1 = crop_val(y1, patch_size/2, img.shape[0] - patch_size/2)
y2 = crop_val(y2, patch_size/2, img.shape[0] - patch_size/2)
x = rng.randint(x1, x2)
y = rng.randint(y1, y2)
if not overlap_patch_roi((x,y), patch_size, roi_mask, cutoff=neg_cutoff):
patch = img[y - patch_size/2:y + patch_size/2,
x - patch_size/2:x + patch_size/2]
patch = patch.astype('int32')
patch_img = toimage(patch, high=patch.max(), low=patch.min(),
mode='I')
filename = basename + "_%04d" % (sampled_bkg) + ".png"
fullname = os.path.join(bkg_out, filename)
patch_img.save(fullname)
sampled_bkg += 1
if verbose:
print "sampled a hns patch at (x,y) center=", (x,y)
sys.stdout.flush()
def sample_blob_negatives(img, roi_mask, out_dir, img_id, abn, blob_detector,
patch_size=256, neg_cutoff=.35, nb_bkg=100,
start_sample_nb=0,
bkg_dir='background', verbose=False):
bkg_out = os.path.join(out_dir, bkg_dir)
basename = '_'.join([img_id, str(abn)])
img = add_img_margins(img, patch_size/2)
roi_mask = add_img_margins(roi_mask, patch_size/2)
# Get ROI bounding box.
roi_mask_8u = roi_mask.astype('uint8')
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3:
contours,_ = cv2.findContours(
roi_mask_8u.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
else:
_,contours,_ = cv2.findContours(
roi_mask_8u.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cont_areas = [ cv2.contourArea(cont) for cont in contours ]
idx = np.argmax(cont_areas) # find the largest contour.
rx,ry,rw,rh = cv2.boundingRect(contours[idx])
if verbose:
M = cv2.moments(contours[idx])
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
print "ROI centroid=", (cx,cy); sys.stdout.flush()
# Sample blob negative samples.
key_pts = blob_detector.detect((img/img.max()*255).astype('uint8'))
rng = np.random.RandomState(12345)
key_pts = rng.permutation(key_pts)
sampled_bkg = 0
for kp in key_pts:
if sampled_bkg >= nb_bkg:
break
x,y = int(kp.pt[0]), int(kp.pt[1])
if not overlap_patch_roi((x,y), patch_size, roi_mask, cutoff=neg_cutoff):
patch = img[y - patch_size/2:y + patch_size/2,
x - patch_size/2:x + patch_size/2]
patch = patch.astype('int32')
patch_img = toimage(patch, high=patch.max(), low=patch.min(),
mode='I')
filename = basename + "_%04d" % (start_sample_nb + sampled_bkg) + ".png"
fullname = os.path.join(bkg_out, filename)
patch_img.save(fullname)
if verbose:
print "sampled a blob patch at (x,y) center=", (x,y)
sys.stdout.flush()
sampled_bkg += 1
return sampled_bkg
#### End of function definition ####
def original_color_transform(content, generated, mask=None):
generated = fromimage(toimage(generated, mode='RGB'), mode='YCbCr') # Convert to YCbCr color space
if mask is None:
generated[:, :, 1:] = content[:, :, 1:] # Generated CbCr = Content CbCr
else:
width, height, channels = generated.shape
for i in range(width):
for j in range(height):
if mask[i, j] == 1:
generated[i, j, 1:] = content[i, j, 1:]
generated = fromimage(toimage(generated, mode='YCbCr'), mode='RGB') # Convert to RGB color space
return generated
def compress_img(img, method):
img_compressed_buffer = StringIO.StringIO()
im = misc.toimage(img)
im.save(img_compressed_buffer, format=method)
img_compressed = img_compressed_buffer.getvalue()
img_compressed_buffer.close()
return img_compressed
def display_image(screen):
image = smp.toimage(screen)
image.save('output.png')
image.show()
def original_color_transform(content, generated, mask=None):
generated = fromimage(toimage(generated, mode='RGB'), mode='YCbCr') # Convert to YCbCr color space
if mask is None:
generated[:, :, 1:] = content[:, :, 1:] # Generated CbCr = Content CbCr
else:
width, height, channels = generated.shape
for i in range(width):
for j in range(height):
if mask[i, j] == 1:
generated[i, j, 1:] = content[i, j, 1:]
generated = fromimage(toimage(generated, mode='YCbCr'), mode='RGB') # Convert to RGB color space
return generated
def compress_img(img, method):
"""Compress an image (numpy array) using the provided image compression
method."""
img_compressed_buffer = StringIO.StringIO()
im = misc.toimage(img)
im.save(img_compressed_buffer, format=method)
img_compressed = img_compressed_buffer.getvalue()
img_compressed_buffer.close()
return img_compressed
def detect_face_task(img):
"""Detect faces from image
@input: image
@output:
- all faces information
"""
# paramter for detect
# image_size = 160
# margin = 44
minsize = 20 # minimum size of face
threshold = [0.6, 0.7, 0.7] # three steps's threshold
factor = 0.709 # scale factor
# caffe model
pnet = caffe_model.get_pnet()
rnet = caffe_model.get_rnet()
onet = caffe_model.get_onet()
bounding_boxes, _ = detect_face.detect_face(img, minsize, pnet, rnet, onet, threshold, factor)
print('detect bounding: ', bounding_boxes)
print('Find faces: ', bounding_boxes.shape[0])
# all_faces is faces information list, include face bytes, face position
all_faces = []
for face_position in bounding_boxes:
face_position = face_position.astype(int)
print('face position: ', face_position)
# each face information, include position, face image
head_rect = face_position[:4].tolist() # numpy array to python list
head_img = misc.toimage(img).crop(head_rect)
head_img_io = StringIO.StringIO()
head_img.save(head_img_io, format='JPEG')
head_img_b64 = base64.b64encode(head_img_io.getvalue())
# construct response
face_info = {}
face_info['rect'] = head_rect
face_info['image'] = head_img_b64
all_faces.append(face_info)
return all_faces