def create_heatmaps(img, pred):
"""
Uses objectness probability to draw a heatmap on the image and returns it
"""
# find anchors with highest prediction
best_pred = np.max(pred[..., 0], axis=-1)
# convert probabilities to colormap scale
best_pred = np.uint8(best_pred * 255)
# apply color map
# cv2 colormaps create BGR, not RGB
cmap = cv2.cvtColor(cv2.applyColorMap(best_pred, cv2.COLORMAP_JET), cv2.COLOR_BGR2RGB)
# resize the color map to fit image
cmap = cv2.resize(cmap, img.shape[1::-1], interpolation=cv2.INTER_NEAREST)
# overlay cmap with image
return cv2.addWeighted(cmap, 1, img, 0.5, 0)
python类COLORMAP_JET的实例源码
def save(mask, img, blurred):
mask = mask.cpu().data.numpy()[0]
mask = np.transpose(mask, (1, 2, 0))
mask = (mask - np.min(mask)) / np.max(mask)
mask = 1 - mask
heatmap = cv2.applyColorMap(np.uint8(255*mask), cv2.COLORMAP_JET)
heatmap = np.float32(heatmap) / 255
cam = 1.0*heatmap + np.float32(img)/255
cam = cam / np.max(cam)
img = np.float32(img) / 255
perturbated = np.multiply(1 - mask, img) + np.multiply(mask, blurred)
cv2.imwrite("perturbated.png", np.uint8(255*perturbated))
cv2.imwrite("heatmap.png", np.uint8(255*heatmap))
cv2.imwrite("mask.png", np.uint8(255*mask))
cv2.imwrite("cam.png", np.uint8(255*cam))
interaction_updated_global.py 文件源码
项目:Interactive-object-tracking
作者: abhishekarya286
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def likelihood_map(prob_map,image) :
'''This functon generates the likelihood map based on either obj-surr/dist model
input: probability map
output:likelihood map, an image(each pixel value=corresponding probability)'''
global h_img,w_img,bin
sf=256.0/bin
image_10=image/sf
image_10=image_10.astype('uint8')
# creating a likelihood image acc. to obj-surr or obj-distractor model
a=image_10[:,:,0]
a=a.ravel()
b=image_10[:,:,1]
b=b.ravel()
c_=image_10[:,:,2]
c_=c_.ravel()
prob_image=prob_map[a,b,c_]
prob_image=prob_image.reshape((h_img,w_img))
prob_image1=prob_image*255
prob_image1=prob_image1.astype('uint8')
likemap=cv2.applyColorMap(prob_image1, cv2.COLORMAP_JET)
return likemap,prob_image1
lab_global_optimisation.py 文件源码
项目:Interactive-object-tracking
作者: abhishekarya286
项目源码
文件源码
阅读 27
收藏 0
点赞 0
评论 0
def likelihood_map(prob_map,image) :
'''This functon generates the likelihood map based on either obj-surr/dist model
input: probability map
output:likelihood map, an image(each pixel value=corresponding probability)'''
global h_img,w_img,bin
sf=256.0/bin
image_10=image/sf
image_10=image_10.astype('uint8')
# creating a likelihood image acc. to obj-surr or obj-distractor model
a=image_10[:,:,0]
a=a.ravel()
b=image_10[:,:,1]
b=b.ravel()
c_=image_10[:,:,2]
c_=c_.ravel()
prob_image=prob_map[a,b,c_]
prob_image=prob_image.reshape((h_img,w_img))
prob_image1=prob_image*255
prob_image1=prob_image1.astype('uint8')
likemap=cv2.applyColorMap(prob_image1, cv2.COLORMAP_JET)
return likemap,prob_image1
def visualize_hypercolumns(model, original_img):
img = np.float32(cv2.resize(original_img, (200, 66))) / 255.0
layers_extract = [9]
hc = extract_hypercolumns(model, layers_extract, img)
avg = np.product(hc, axis=0)
avg = np.abs(avg)
avg = avg / np.max(np.max(avg))
heatmap = cv2.applyColorMap(np.uint8(255 * avg), cv2.COLORMAP_JET)
heatmap = np.float32(heatmap) / np.max(np.max(heatmap))
heatmap = cv2.resize(heatmap, original_img.shape[0:2][::-1])
both = 255 * heatmap * 0.7 + original_img
both = both / np.max(both)
return both
def recalculate(self):
in_mark = self.g_pool.trim_marks.in_mark
out_mark = self.g_pool.trim_marks.out_mark
section = slice(in_mark,out_mark)
# calc heatmaps
for s in self.surfaces:
if s.defined:
s.generate_heatmap(section)
# calc distirbution accross all surfaces.
results = []
for s in self.surfaces:
gaze_on_srf = s.gaze_on_srf_in_section(section)
results.append(len(gaze_on_srf))
self.metrics_gazecount = len(gaze_on_srf)
if results == []:
logger.warning("No surfaces defined.")
return
max_res = max(results)
results = np.array(results,dtype=np.float32)
if not max_res:
logger.warning("No gaze on any surface for this section!")
else:
results *= 255./max_res
results = np.uint8(results)
results_c_maps = cv2.applyColorMap(results, cv2.COLORMAP_JET)
for s,c_map in zip(self.surfaces,results_c_maps):
heatmap = np.ones((1,1,4),dtype=np.uint8)*125
heatmap[:,:,:3] = c_map
s.metrics_texture = Named_Texture()
s.metrics_texture.update_from_ndarray(heatmap)
def save_cam_image(img, mask, filename):
heatmap = cv2.applyColorMap(np.uint8(255*mask), cv2.COLORMAP_JET)
heatmap = np.float32(heatmap) / 255
cam = heatmap + np.float32(img)
cam = cam / np.max(cam)
cv2.imwrite(filename, np.uint8(255 * cam))
def opencv_plot(des_name):
densmap = np.fromfile(densmap_name, np.float32)
densmap = np.reshape(densmap, (227, 227))
#densmap = norm_image(densmap) * 100
densmap *= 100.0
densmap[densmap >1 ] = 1
densmap = norm_image(densmap) * 255
densmap = densmap.astype(np.uint8)
im_color = cv2.applyColorMap(densmap, cv2.COLORMAP_JET)
cv2.imshow("im", im_color)
cv2.waitKey(0)
def predict_image(flag):
t_start = cv2.getTickCount()
config = tf.ConfigProto()
# config.gpu_options.per_process_gpu_memory_fraction = 0.9
config.gpu_options.allow_growth = True
set_session(tf.Session(config=config))
with open(os.path.join(flag.ckpt_dir, flag.ckpt_name, 'model.json'), 'r') as json_file:
loaded_model_json = json_file.read()
model = model_from_json(loaded_model_json)
weight_list = sorted(glob(os.path.join(flag.ckpt_dir, flag.ckpt_name, "weight*")))
model.load_weights(weight_list[-1])
print "[*] model load : %s"%weight_list[-1]
t_total = (cv2.getTickCount() - t_start) / cv2.getTickFrequency() * 1000
print "[*] model loading Time: %.3f ms"%t_total
imgInput = cv2.imread(flag.test_image_path, 0)
input_data = imgInput.reshape((1,256,256,1))
t_start = cv2.getTickCount()
result = model.predict(input_data, 1)
t_total = (cv2.getTickCount() - t_start) / cv2.getTickFrequency() * 1000
print "Predict Time: %.3f ms"%t_total
imgMask = (result[0]*255).astype(np.uint8)
imgShow = cv2.cvtColor(imgInput, cv2.COLOR_GRAY2BGR)
_, imgMask = cv2.threshold(imgMask, int(255*flag.confidence_value), 255, cv2.THRESH_BINARY)
imgMaskColor = cv2.applyColorMap(imgMask, cv2.COLORMAP_JET)
# imgZero = np.zeros((256,256), np.uint8)
# imgMaskColor = cv2.merge((imgZero, imgMask, imgMask))
imgShow = cv2.addWeighted(imgShow, 0.9, imgMaskColor, 0.3, 0.0)
output_path = os.path.join(flag.output_dir, os.path.basename(flag.test_image_path))
cv2.imwrite(output_path, imgShow)
print "SAVE:[%s]"%output_path
def train_visualization_seg(self, model, epoch):
image_name_list = sorted(glob(os.path.join(self.flag.data_path,'train/IMAGE/*/*.png')))
print image_name_list
image_name = image_name_list[-1]
image_size = self.flag.image_size
imgInput = cv2.imread(image_name, self.flag.color_mode)
output_path = self.flag.output_dir
input_data = imgInput.reshape((1,image_size,image_size,self.flag.color_mode*2+1))
t_start = cv2.getTickCount()
result = model.predict(input_data, 1)
t_total = (cv2.getTickCount() - t_start) / cv2.getTickFrequency() * 1000
print "[*] Predict Time: %.3f ms"%t_total
imgMask = (result[0]*255).astype(np.uint8)
imgShow = cv2.cvtColor(imgInput, cv2.COLOR_GRAY2BGR)
imgMaskColor = cv2.applyColorMap(imgMask, cv2.COLORMAP_JET)
imgShow = cv2.addWeighted(imgShow, 0.9, imgMaskColor, 0.4, 0.0)
output_path = os.path.join(self.flag.output_dir, '%04d_'%epoch+os.path.basename(image_name))
cv2.imwrite(output_path, imgShow)
# print "SAVE:[%s]"%output_path
# cv2.imwrite(os.path.join(output_path, 'img%04d.png'%epoch), imgShow)
# cv2.namedWindow("show", 0)
# cv2.resizeWindow("show", 800, 800)
# cv2.imshow("show", imgShow)
# cv2.waitKey(1)
def grad_cam(input_model, image, weights, feature_maps=None):
#activation size of final convolition layer is 10x10"
cam = np.ones((10, 10), dtype=np.float32)
# Add weighted activation maps
grads_val = weights
for i in range(grads_val.shape[0]):
# Added relu
temp = (weights[i, :] * feature_maps[:, :, i])
np.maximum(temp, 0, temp)
cam += temp
# resize and normalization
del feature_maps
cam = cv2.resize(cam, (299, 299))
# Relu
cam = np.maximum(cam, 0)
cam = cam / np.max(cam)
image = image[0, :]
image -= np.min(image)
image = np.minimum(image, 255)
# print image.shape
cam = cv2.applyColorMap(np.uint8(255 * cam), cv2.COLORMAP_JET)
cam = 0.5*np.float32(cam) + 0.5*np.float32(image)
cam = 255 * cam / np.max(cam)
return np.uint8(cam)
def save(self, filename, gcam, raw_image):
gcam = cv2.applyColorMap(np.uint8(gcam * 255.0), cv2.COLORMAP_JET)
gcam = gcam.astype(np.float) + raw_image.astype(np.float)
gcam = gcam / gcam.max() * 255.0
cv2.imwrite(filename, np.uint8(gcam))
def visualize_grad_cam(input_model, original_img, layer_name = "conv3_1"):
img = np.float32(cv2.resize(original_img, (200, 66))) / 255.0
angle = input_model.predict(np.array([img]))
print("The predicted angle is", 180.0 * angle[0][0] / scipy.pi, "degrees")
model = Sequential()
model.add(input_model)
target_layer = lambda x: grad_cam_loss(x, angle)
model.add(Lambda(target_layer,
output_shape = grad_cam_loss_output_shape))
loss = K.sum(model.layers[-1].output)
conv_output = [l for l in model.layers[0].layers if l.name is layer_name][0].output
grads = normalize(K.gradients(loss, conv_output)[0])
gradient_function = K.function([model.layers[0].input], [conv_output, grads])
output, grads_val = gradient_function([[img]])
output, grads_val = output[0, :], grads_val[0, :, :, :]
weights = np.mean(grads_val, axis = (0, 1))
cam = np.ones(output.shape[0 : 2], dtype = np.float32)
for i, w in enumerate(weights):
cam += w * output[:, :, i]
#ReLU:
cam = np.maximum(cam, 0)
cam = cam / np.max(cam)
cam = cv2.resize(cam, tuple(original_img.shape[0:2][::-1]))
cam = cv2.applyColorMap(np.uint8(255*cam), cv2.COLORMAP_JET)
cam = 1.0 * np.float32(cam) + np.float32(original_img)
cam = cam / np.max(cam)
return cam
def showImg(self,label,img):
if len(img.shape) == 2:
img = cv2.applyColorMap(img, cv2.COLORMAP_JET)
img = cv2.resize(img, (512, 512),cv2.INTER_AREA)
height, width, byteValue = img.shape
byteValue = byteValue * width
timg = img.copy()
image = QtGui.QImage(timg.data, width, height,byteValue, QtGui.QImage.Format_RGB888)
label.setPixmap(QtGui.QPixmap(image).scaled(label.size(),aspectMode=QtCore.Qt.KeepAspectRatio))
""" visualize function from
https://github.com/BVLC/caffe/blob/master/examples/00-classification.ipynb
"""
def vis_square(self, data):
"""Take an array of shape (n, height, width) or (n, height, width, 3)
and visualize each (height, width) thing in a grid of size approx. sqrt(n) by sqrt(n)"""
print "Data Shape : ", data.shape
# normalize data for display
data = (data - data.min()) / (data.max() - data.min())
# force the number of filters to be square
n = int(np.ceil(np.sqrt(data.shape[0])))
padding = (((0, n ** 2 - data.shape[0]),
(0, 1), (0, 1)) # add some space between filters
+ ((0, 0),) * (data.ndim - 3)) # don't pad the last dimension (if there is one)
data = np.pad(data, padding, mode='constant', constant_values=0) # pad with ones (white)
# tile the filters into an image
data = data.reshape((n, n) + data.shape[1:]).transpose((0, 2, 1, 3) + tuple(range(4, data.ndim + 1)))
data = data.reshape((n * data.shape[1], n * data.shape[3]) + data.shape[4:])
# show at display
#print 'Data shape : ', data.shape , len(data.shape)
img = 255 * data
img = cv2.resize(img, (512, 512))
img = np.array(img, dtype='uint8')
img_c = cv2.applyColorMap(img, cv2.COLORMAP_JET)
height, width, byteValue = img_c.shape
byteValue = byteValue * width
self.image = QtGui.QImage(img_c.data, width, height, byteValue, QtGui.QImage.Format_RGB888)
self.ui.labelDisplay.setPixmap(QtGui.QPixmap(self.image))
def show_cam_on_image(img, mask):
heatmap = cv2.applyColorMap(np.uint8(255*mask), cv2.COLORMAP_JET)
heatmap = np.float32(heatmap) / 255
cam = heatmap + np.float32(img)
cam = cam / np.max(cam)
cv2.imwrite("cam.jpg", np.uint8(255 * cam))
def grad_cam(input_model, image, category_index, layer_name):
model = Sequential()
model.add(input_model)
nb_classes = 1000
target_layer = lambda x: target_category_loss(x, category_index, nb_classes)
model.add(Lambda(target_layer,
output_shape = target_category_loss_output_shape))
loss = K.sum(model.layers[-1].output)
conv_output = [l for l in model.layers[0].layers if l.name is layer_name][0].output
grads = normalize(K.gradients(loss, conv_output)[0])
gradient_function = K.function([model.layers[0].input], [conv_output, grads])
output, grads_val = gradient_function([image])
output, grads_val = output[0, :], grads_val[0, :, :, :]
weights = np.mean(grads_val, axis = (0, 1))
cam = np.ones(output.shape[0 : 2], dtype = np.float32)
for i, w in enumerate(weights):
cam += w * output[:, :, i]
cam = cv2.resize(cam, (224, 224))
cam = np.maximum(cam, 0)
heatmap = cam / np.max(cam)
#Return to BGR [0..255] from the preprocessed image
image = image[0, :]
image -= np.min(image)
image = np.minimum(image, 255)
cam = cv2.applyColorMap(np.uint8(255*heatmap), cv2.COLORMAP_JET)
cam = np.float32(cam) + np.float32(image)
cam = 255 * cam / np.max(cam)
return np.uint8(cam), heatmap
def save_cam_image(img, mask, filename):
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
heatmap = cv2.applyColorMap(np.uint8(255.*mask), cv2.COLORMAP_JET)
res = np.concatenate((img, heatmap), axis=1)
cv2.imwrite(filename, res)
def generate_heatmap(self,section):
if self.cache is None:
logger.warning('Surface cache is not build yet.')
return
x,y = self.real_world_size['x'],self.real_world_size['y']
x = max(1,int(x))
y = max(1,int(y))
filter_size = int(int(self.heatmap_detail * x)/2)*2 +1
std_dev = int(filter_size /6.)
self.heatmap = np.ones((y,x,4),dtype=np.uint8)
all_gaze = []
for frame_idx,c_e in enumerate(self.cache[section]):
if c_e:
frame_idx+=section.start
for gp in self.gaze_on_srf_by_frame_idx(frame_idx,c_e['m_from_screen']):
if gp['confidence']>=self.g_pool.min_data_confidence:
all_gaze.append(gp['norm_pos'])
if not all_gaze:
logger.warning("No gaze data on surface for heatmap found.")
all_gaze.append((-1.,-1.))
all_gaze = np.array(all_gaze)
all_gaze *= [self.real_world_size['x'],self.real_world_size['y']]
hist,xedge,yedge = np.histogram2d(all_gaze[:,0], all_gaze[:,1],
bins=[x,y],
range=[[0, self.real_world_size['x']], [0,self.real_world_size['y']]],
normed=False,
weights=None)
hist = np.rot90(hist)
#smoothing..
hist = cv2.GaussianBlur(hist,(filter_size,filter_size),std_dev)
maxval = np.amax(hist)
if maxval:
scale = 255./maxval
else:
scale = 0
hist = np.uint8( hist*(scale) )
#colormapping
c_map = cv2.applyColorMap(hist, cv2.COLORMAP_JET)
self.heatmap[:,:,:3] = c_map
self.heatmap[:,:,3] = 125
self.heatmap_texture = Named_Texture()
self.heatmap_texture.update_from_ndarray(self.heatmap)