def heatmap_generator(img_dir, heatmap_dir, histo_datafile): # Enter Directory of all images #img_dir = "./output_frames" data_path = os.path.join(img_dir,'*g') files = glob.glob(data_path) # print(files) # heatmapper = Heatmapper( point_diameter=90, # the size of each point to be drawn point_strength=0.8, # the strength, between 0 and 1, of each point to be drawn opacity=0.65, # the opacity of the heatmap layer colours='default', # 'default' or 'reveal' # OR a matplotlib LinearSegmentedColorMap object # OR the path to a horizontal scale image grey_heatmapper='PIL' # The object responsible for drawing the points # Pillow used by default, 'PySide' option available if installed ) professions_dict = points.getCordinatesDict(histo_datafile) for k, v in professions_dict.items(): # print(k, v) if k == "frame": continue frame = Image.open(img_dir + "/" + k) heatmap = heatmapper.heatmap_on_img(v, frame) dest_path = heatmap_dir + "/" + k + ".png" #dest_path = os.path.join(".\\destination\\", '*g') # print(dest_path) heatmap.save(dest_path)
def buildTimedHeatMap(pointsList, circlesCoords, redCoords, planePath, imgFile_path): mapimg = Image.open(imgFile_path).convert('RGBA') game_events_image = Image.new('RGBA', mapimg.size, (255,255,255,0)) draw = ImageDraw.Draw(game_events_image) if redCoords[1] > 0: draw.ellipse([redCoords[0][0] - redCoords[1], redCoords[0][1] - redCoords[1], redCoords[0][0] + redCoords[1], redCoords[0][1] + redCoords[1]], fill=(255, 0, 0, 50)) # draw line of plane moving if len(planePath) == 2 and planePath[0] is not None and planePath[1] is not None: length = 2000 starty = planePath[0][1] - length * math.sin(planePath[1]) startx = planePath[0][0] - length * math.cos(planePath[1]) endy = planePath[0][1] + length * math.sin(planePath[1]) endx = planePath[0][0] + length * math.cos(planePath[1]) draw.line(xy=[(startx, starty), (endx, endy)], width=3, fill=(0, 255, 255)) draw.ellipse([circlesCoords[0][0] - circlesCoords[1], circlesCoords[0][1] - circlesCoords[1], circlesCoords[0][0] + circlesCoords[1], circlesCoords[0][1] + circlesCoords[1]], outline='white') mapimg = Image.alpha_composite(mapimg, game_events_image) heatmapper = Heatmapper(point_diameter=25, point_strength=0.5, opacity=0.7) heatmapImg = heatmapper.heatmap_on_img(pointsList, mapimg) return heatmapImg
def buildHeatMap(pointsList, circlesCoordsList, planePath, imgFile_path): mapimg = Image.open(imgFile_path) draw = ImageDraw.Draw(mapimg) # draw line of plane moving if len(planePath ) == 2 and planePath[0] is not None and planePath[1] is not None: length = 2000 starty = planePath[0][1] - length * math.sin(planePath[1]) startx = planePath[0][0] - length * math.cos(planePath[1]) endy = planePath[0][1] + length * math.sin(planePath[1]) endx = planePath[0][0] + length * math.cos(planePath[1]) draw.line(xy=[(startx, starty), (endx, endy)], width=3, fill=(0, 255, 255)) for coordsAndRadius in circlesCoordsList: draw.ellipse([ coordsAndRadius[0][0] - coordsAndRadius[1], coordsAndRadius[0][1] - coordsAndRadius[1], coordsAndRadius[0][0] + coordsAndRadius[1], coordsAndRadius[0][1] + coordsAndRadius[1] ], outline='white') heatmapper = Heatmapper(point_diameter=25, point_strength=0.2, opacity=0.7) heatmapImg = heatmapper.heatmap_on_img(pointsList, mapimg) return heatmapImg
def cal_heatmap(icam, frame, startFrame, find_ind): # draw the image for heatmap heatmap_value = [] path = 'D:/Code/DeepCC/DeepCC/src/visualization/data/background' + str( icam) + '.jpg' background_img = Image.open(path) for i in find_ind: center_x = int(data_part[i][3] + (data_part[i][5] / 2)) center_y = int(data_part[i][4] + (data_part[i][6] / 2)) heatmap_value.append((center_x, center_y)) heatmapper = Heatmapper() heatmap = heatmapper.heatmap_on_img(heatmap_value, background_img) img = cv2.cvtColor(np.asarray(heatmap), cv2.COLOR_RGB2BGR) return img
def drawing_heatmap(bucket_name, destination_key, key, point_list) : try: s3.Object(bucket_name=bucket_name, key=key).load() is_image_exists(bucket_name, key) except botocore.exceptions.ClientError: return None obj = s3.Object(bucket_name=bucket_name, key=key) obj_body = obj.get()['Body'].read() #이미지 등록 img = Image.open(BytesIO(obj_body)) heatmapper = Heatmapper() heatmap = heatmapper.heatmap_on_img(point_list, img) buffer = BytesIO() heatmap.save(buffer, 'PNG') buffer.seek(0) obj = s3.Object(bucket_name=bucket_name, key=destination_key) obj.put(Body=buffer, ContentType='image/png') return "https://{bucket}.s3.amazonaws.com/{destination_key}".format(bucket=bucket_name, destination_key=destination_key)
def heatmap_calculator(video_class): print('heatmap one time') plano_ori_heat = Image.open('images/square_plane.jpg') heatmapper = Heatmapper(point_diameter=50, point_strength=0.25, opacity=0.65, colours='default', grey_heatmapper='PIL') while True: points = copy.copy(video_class.points) if len(points) != 0: heatmap_img = heatmapper.heatmap_on_img(points, plano_ori_heat) heatmap_img = np.asarray(heatmap_img) heatmap_img = cv2.cvtColor(heatmap_img, cv2.COLOR_BGR2RGB) video_class.heatmap_img = heatmap_img else: heatmap_img = np.asarray(plano_ori_heat) heatmap_img = cv2.cvtColor(heatmap_img, cv2.COLOR_BGR2RGB) video_class.heatmap_img = heatmap_img if video_class.stopped: return
plt.imshow(frame_bw, cmap="gray") plt.show() process_frame(frame_bw, frame_clr, Pt, area=500) plt.figure() plt.title(run_name) plt.imshow(frame_clr) plt.show() #%% Save final view and heatmap npstack = Pt.get_drawable_tracks_npstack() df = pd.DataFrame(npstack, columns=['x','y','time','track']) paths = df[['x','y']] write_name = run_name + str(frame_idx) + "_" cv2.imwrite(base_dir + 'outputFiles/' + write_name + 'final_img.png', frame_clr) ret, frame_clr = Dataset.get_next_frame(frame_idx) pil_img = Image.fromarray(frame_clr) heatmapper = Heatmapper() heatmap = heatmapper.heatmap_on_img(paths.values.tolist(), pil_img) heatmap.save(base_dir + 'outputFiles/' + write_name + 'heatmap.png')
(337, 112), (92, 253), (92, 253), (92, 253), (92, 253), (92, 253), (92, 253), (92, 253), (92, 253), (92, 253), (92, 253), (459, 191), (459, 191), (459, 162), (92, 270), (459, 285), (213, 112), (337, 96), (92, 85), (459, 347), (213, 112), (213, 112), (92, 253), (92, 253), (92, 180), (92, 180), (92, 180), (92, 118), (337, 67), (337, 67), (337, 67), (337, 67), (92, 118), (213, 112), (213, 112), (92, 302), (459, 130), (337, 96), (337, 112), (213, 96), (459, 118), (459, 118), (459, 118), (459, 146), (459, 118), (459, 118), (459, 118), (92, 191), (459, 375), (459, 375), (459, 285), (459, 162), (459, 162), (459, 180), (459, 180), (337, 112), (92, 302), (92, 302), (92, 302), (92, 302), (92, 118), (92, 180), (92, 118), (459, 118), (459, 118), (459, 60), (92, 302), (92, 302), (92, 69), (459, 191), (459, 207), (459, 253), (459, 253), (92, 118), (92, 69), (459, 191), (459, 253), (459, 191), (459, 253), (459, 253), (92, 69), (337, 112), (459, 60), (459, 60), (92, 101), (92, 101), (92, 101), (92, 101), (92, 101), (92, 101), (92, 101), (92, 101), (459, 180), (459, 180), (459, 180), (92, 101), (459, 180), (459, 180), (459, 180), (92, 101), (459, 180), (459, 180), (459, 180), (92, 101), (459, 130), (459, 130), (459, 130), (92, 331), (92, 331), (337, 67), (92, 331), (337, 67), (337, 67), (459, 130), (459, 130), (459, 330), (92, 163), (92, 163), (459, 330), (459, 146), (92, 163), (92, 223), (459, 330)] example_img_path = '/Users/jimhahn/Desktop/input-map-undergrad_2018-03-26_V1.0.png' example_img = Image.open(example_img_path) heatmapper = Heatmapper() heatmap = heatmapper.heatmap_on_img(wayfinder_points, example_img) heatmap.save('/Users/jimhahn/Desktop/wayfinder-only_2018-03-26_V1.0.png')
map_img = Image.open(map_path) heatmapper_cutout = Heatmapper( point_diameter=50, # the size of each point to be drawn point_strength= 0.005, # the strength, between 0 and 1, of each point to be drawn opacity=1, # the opacity of the heatmap layer colours='reveal', # 'default' or 'reveal' # OR a matplotlib LinearSegmentedColorMap object # OR the path to a horizontal scale image grey_heatmapper='PIL' # The object responsible for drawing the points # Pillow used by default, 'PySide' option available if installed ) heatmapper = Heatmapper( point_diameter=50, # the size of each point to be drawn point_strength= 0.02, # the strength, between 0 and 1, of each point to be drawn opacity=1, # the opacity of the heatmap layer colours='default', # 'default' or 'reveal' # OR a matplotlib LinearSegmentedColorMap object # OR the path to a horizontal scale image grey_heatmapper='PIL' # The object responsible for drawing the points # Pillow used by default, 'PySide' option available if installed ) heatmap = heatmapper.heatmap_on_img(points, map_img) heatmap.save('heatmap_norm.png') heatmap_cutout = heatmapper_cutout.heatmap_on_img(points, map_img) heatmap_cutout.save('heatmap_cutout_norm.png')
# Ensure script is run on python 3.5.2 and above try: assert sys.version_info >= (3, 5, 2) except AssertionError: print('Please use Python 3.6 : https://www.python.org/downloads/ \n') raise # Read original data filename = askopenfilename() with open(filename) as temp: newdata = temp.read() data = json.loads(newdata) coord_img_list = [] coord_vid_list = [] for key, value in data.items(): x_coord = float(value.pop('x_coord', None)) y_coord = float(value.pop('y_coord', None)) coord_pair = (x_coord, y_coord) coord_triple = (x_coord, y_coord, float(value.pop('timestamp', None)) * 1000) coord_img_list.append(coord_pair) example_img_path = 'test.jpg' example_img = Image.open(example_img_path) heatmap.save('heatmapnottest.png') heatmapper = Heatmapper(point_diameter=40, point_strength=0.1) heatmap = heatmapper.heatmap_on_img(coord_img_list, example_img)
(83.2964724, 206.3611842), (83.2964724, 206.3611842), (83.2964724, 206.3611842), (83.2964724, 206.3611842), (83.2964724, 206.3611842), (83.2964724, 206.3611842), (83.2964724, 206.3611842), (83.2964724, 206.3611842), (90.58982342, 208.9370392), (90.58982342, 208.9370392), (90.58982342, 208.9370392), (90.58982342, 208.9370392), (90.58982342, 208.9370392), (90.58982342, 208.9370392), (90.58982342, 208.9370392), (90.58982342, 208.9370392), (90.58982342, 208.9370392), (90.58982342, 208.9370392), (90.58982342, 208.9370392), (90.58982342, 208.9370392), (90.58982342, 208.9370392), (85.85770642, 190.939349), (85.85770642, 190.939349), (85.85770642, 190.939349), (85.85770642, 190.939349), (85.85770642, 190.939349), (85.85770642, 190.939349), (85.85770642, 190.939349), (85.85770642, 190.939349), (85.85770642, 190.939349), (85.85770642, 190.939349), (85.85770642, 190.939349), (85.85770642, 190.939349), (85.85770642, 190.939349), (128.1527387, 198.9594999), (128.1527387, 198.9594999), (128.1527387, 198.9594999), (128.1527387, 198.9594999), (128.1527387, 198.9594999), (128.1527387, 198.9594999), (128.1527387, 198.9594999), (128.1527387, 198.9594999), (128.1527387, 198.9594999), (128.1527387, 198.9594999), (128.1527387, 198.9594999), (128.1527387, 198.9594999), (128.1527387, 198.9594999)] rec_img_path = '/Users/jimhahn/Desktop/wayfinder-only_2018-03-26_V1.0.png' rec_img = Image.open(rec_img_path) heatmapper = Heatmapper(opacity=0.3, colours='reveal') #heatmapper = Heatmapper() heatmap = heatmapper.heatmap_on_img(rec_points, rec_img) heatmap.save( '/Users/jimhahn/Desktop/rec-and-wayfinder-heatmap_2018-03-26_V1.0.png')
def test(self, heatmap=True): """ Test the model on the held-out test data. """ # load the best checkpoint self.load_checkpoint(best=True) self.model.eval() with torch.no_grad(): losses = [] accuracies = [] confusion_matrix = torch.zeros(self.num_classes, self.num_classes) heat_points = [[[] for i in range(self.num_glimpses)] for j in range(self.num_digits + 1)] heat_mean = torch.zeros(self.num_digits, *self.im_size) var_means = torch.zeros(self.num_glimpses, len(self.test_loader)) loss_means = torch.zeros(self.num_glimpses, len(self.test_loader)) count_hits = torch.zeros(self.num_glimpses, 2) var_dist = torch.zeros(len(self.test_loader.dataset), self.num_glimpses, self.im_size[-1], self.im_size[-1]) err_dist = torch.zeros(len(self.test_loader.dataset), self.num_glimpses, self.im_size[-1], self.im_size[-1]) labels = torch.zeros(len(self.test_loader.dataset)) dist = PairwiseDistance(2) dists = torch.zeros(len(self.test_loader.dataset), self.num_glimpses) run_idx = 0 for i, (_, x, y) in enumerate(self.test_loader): if self.use_gpu: x = x.cuda() y = y.cuda() true = y.detach().argmax(1) # initialize location vector and hidden state self.batch_size = x.shape[0] s_t, _, l_t = self.reset() targets = torch.zeros(self.num_glimpses, 3, *x.shape[2:]) glimpses = torch.zeros(self.num_glimpses, 3, *x.shape[2:]) means = torch.zeros(self.num_glimpses, 3, *x.shape[2:]) vars = torch.zeros(self.num_glimpses, 3, *x.shape[2:]) locs = torch.zeros(self.batch_size, self.num_glimpses, 2) acc_loss = 0 sub_dir = self.plot_dir / f"{i:06d}_dir" if not sub_dir.exists() and i < 10: sub_dir.mkdir(parents=True) for t in range(self.num_glimpses): # forward pass through model locs[:, t] = l_t.clone().detach() s_t, l_pred, r_t, out_t, mu, logvar = self.model( x, l_t, s_t, samples=self.samples, classify=self.classify, ) draw = x[0].expand(3, -1, -1) extent = self.patch_size for patch in range(self.num_patches): draw = draw_glimpse( draw, l_t[0], extent=extent, ) extent *= self.glimpse_scale targets[t] = draw glimpses[t] = self.model.sensor.glimpse[0][0].expand( 3, -1, -1) means[t] = r_t[0][0].expand(3, -1, -1) vars[t] = convert_heatmap( r_t.var(0)[0].squeeze().cpu().numpy()) if t == 0: var_first = r_t.var(0).squeeze().cpu().numpy() var_last = r_t.var(0).squeeze().cpu().numpy() loc_prev = loc = denormalize(x.size(-1), l_t) l_t = get_loc_target(r_t.var(0), max=self.use_amax).type(l_t.type()) loc = denormalize(x.size(-1), l_t) dists[run_idx:run_idx + len(x), t] = dist.forward(loc_prev.float(), loc.float()).detach().cpu() var_dist[run_idx:run_idx + len(x), t] = r_t.var(0).detach().cpu().squeeze() temp_loss = torch.zeros(self.batch_size, *err_dist.shape[2:]) for s in range(self.samples): temp_loss += F.binary_cross_entropy( r_t[s], x, reduction='none').detach().cpu( ).squeeze() / self.samples err_dist[run_idx:run_idx + len(x), t] = temp_loss for k, l in enumerate(loc): if x.squeeze()[k][l[0], l[1]] > 0: count_hits[t, 0] += 1 else: count_hits[t, 1] += 1 imgs = [targets[t], glimpses[t], means[t], vars[t]] filename = sub_dir / f"{i:06d}_glimpse{t:01d}.png" if i < 10: torchvision.utils.save_image( imgs, filename, nrow=1, normalize=True, pad_value=1, padding=1, scale_each=True, ) var = r_t.var(0).reshape(self.batch_size, -1) var_means[t, i] = var.max(-1)[0].mean(0) loss_means[t, i] = temp_loss.mean() for j in range(self.num_digits): heat_points[j][t].extend( denormalize(x.size(-1), l_t[true == j]).tolist()) heat_points[-1][t].extend(loc.tolist()) labels[run_idx:run_idx + len(true)] = true run_idx += len(x) for s in range(self.samples): loss = F.mse_loss(r_t[s], x) / self.samples acc_loss += loss.item() # store if self.classify: if self.num_classes == 1: pred = out_t.detach().squeeze().round() target = (y.argmax(1) == self.target_class).float() else: pred = out_t.detach().argmax(1) target = true for p, tr in zip(pred.view(-1), target.view(-1)): confusion_matrix[tr.long(), p.long()] += 1 accuracies.append( torch.sum(pred == target).float().item() / len(y)) for j in range(self.num_digits): if len(x[true == j]): heat_mean[j] += x[true == j].mean(0).cpu() losses.append(acc_loss) imgs = [targets, glimpses, means, vars] for im in imgs: im = (im - im.min()) / (im.max() - im.min()) # store images + reconstructions of largest scale filename = self.plot_dir / f"{i:06d}.png" if i < 10: torchvision.utils.save_image( torch.cat(imgs, 0), filename, nrow=6, normalize=False, pad_value=1, padding=3, scale_each=False, ) pkl.dump(dists, open(self.file_dir / 'dists.pkl', 'wb')) pkl.dump(var_dist, open(self.file_dir / 'var_dist.pkl', 'wb')) pkl.dump(err_dist, open(self.file_dir / 'err_dist.pkl', 'wb')) pkl.dump(heat_points, open(self.file_dir / 'locs.pkl', 'wb')) pkl.dump(labels, open(self.file_dir / 'labels.pkl', 'wb')) pkl.dump(heat_mean, open(self.file_dir / 'mean.pkl', 'wb')) print(count_hits[:, 0] / count_hits.sum(1)) sn.set(font="serif", font_scale=2, context='paper', style='dark', rc={"lines.linewidth": 2.5}) errs = err_dist.view(len(self.test_loader.dataset), self.num_glimpses, -1).cpu().numpy() vars = var_dist.view(len(self.test_loader.dataset), self.num_glimpses, -1).cpu().numpy() f, ax = plt.subplots(2, 1, sharex=True, figsize=(9, 12)) ax[0].plot(range(self.num_glimpses), errs.mean((0, -1)), marker='o', markersize=10, c=PLOT_COLOR) ax[0].set_ylabel('Prediction Error') ax[1].plot(range(self.num_glimpses), vars.max(-1).mean(0), marker='o', markersize=10, c=PLOT_COLOR) ax[1].set_xlabel('Saccade number') ax[1].set_ylabel('Uncertainty') f.tight_layout() f.savefig(self.plot_dir / f"comb_var_err.pdf", bbox_inches='tight', pad_inches=0, dpi=600) print("#######################") if self.classify: print(confusion_matrix) print(confusion_matrix.diag() / confusion_matrix.sum(1)) plot_confusion_matrix(confusion_matrix, self.plot_dir / f"confusion_matrix.png") pkl.dump(confusion_matrix, open(self.file_dir / 'conf_matrix.pkl', 'wb')) #create heatmaps flatten = lambda l, i: [ item for sublist in l for item in sublist[i] ] for i in range(self.num_digits): img = array2img(heat_mean[i]) points = np.array(heat_points[i]) first = points[:3].reshape(-1, 2) heatmapper = Heatmapper( point_diameter=1, # the size of each point to be drawn point_strength= 0.3, # the strength, between 0 and 1, of each point to be drawn opacity=0.85, ) heatmap = heatmapper.heatmap_on_img(first, img) heatmap.save(self.plot_dir / f"heatmap_bef{i}.png") last = points[3:-1].reshape(-1, 2) heatmap = heatmapper.heatmap_on_img(last, img) heatmap.save(self.plot_dir / f"heatmap_aft{i}.png") for j in range(self.num_glimpses): heatmap = heatmapper.heatmap_on_img(heat_points[i][j], img) heatmap.save(self.plot_dir / f"heatmap_class{i}_glimpse{j}.png") self.logger.info( f"[*] Test loss: {np.mean(losses)}, Test accuracy: {np.mean(accuracies)}" ) return np.mean(losses)
from heatmappy import Heatmapper from PIL import Image example_points = [(100, 20), (120, 25), (200, 50), (60, 300), (170, 250)] example_img_path = 'default-cat.png' example_img = Image.open(example_img_path) heatmapper = Heatmapper() heatmap = heatmapper.heatmap_on_img(example_points, example_img) heatmap.save('heatmapbasic.png') heatmapper = Heatmapper(opacity=0.9, colours='reveal') heatmap = heatmapper.heatmap_on_img_path(example_points, example_img_path) heatmap.save('heatmapreveal.png')
opacity=0.65, # the opacity of the heatmap layer colours='default', # 'default' or 'reveal' # OR a matplotlib LinearSegmentedColorMap object # OR the path to a horizontal scale image grey_heatmapper='PIL' # the object responsible for drawing the points # pillow used by default, 'PySide' option available if installed ) assert os.path.isfile(csv), \ 'csv {} does not exist'.format(csv) assert os.path.isfile(video), \ 'video {} does not exist'.format(video) # Capture the first frame of the video videoFeed = cv2.VideoCapture(video) assert videoFeed.isOpened(), \ 'Cannot capture source' _, cv2_image = videoFeed.read() cv2_image = cv2.cvtColor(cv2_image,cv2.COLOR_BGR2RGB) image = Image.fromarray(cv2_image) for i in range(1, len(csvData)): coorList.append((csvData.iloc[i, 2], csvData.iloc[i, 3])) heatmap = heatmapper.heatmap_on_img(coorList, image) heatmap.save('workdir/heatmap.png') videoFeed.release() cv2.destroyAllWindows() exit('Finished')