Harini1995 commited on
Commit
c9e3452
·
verified ·
1 Parent(s): 8d915fd

Update utilis/generate_heatmap.py

Browse files
Files changed (1) hide show
  1. utilis/generate_heatmap.py +68 -68
utilis/generate_heatmap.py CHANGED
@@ -1,68 +1,68 @@
1
- import os
2
- import numpy as np
3
- import cv2
4
-
5
-
6
- single_heatmap_path = "motion_transfer/dataset_single/reference_heatmap/00000.npy"
7
- reference_heatmap_dir = "motion_transfer/dataset_single/reference_heatmap" # contains 150 npy files
8
- output_dir = "motion_transfer/dataset_single/test_heatmap"
9
- preview_video = "motion_transfer/dataset_single/simulated_motion.mp4"
10
-
11
- os.makedirs(output_dir, exist_ok=True)
12
-
13
- single_heatmap = np.load(single_heatmap_path)
14
- H, W, C = single_heatmap.shape
15
-
16
- # Extract keypoints
17
- def extract_keypoints(hmap):
18
- kps = []
19
- for i in range(hmap.shape[2]):
20
- y, x = np.where(hmap[:, :, i] > 0)
21
- if len(x) > 0:
22
- kps.append([np.mean(x), np.mean(y)])
23
- else:
24
- kps.append([0, 0])
25
- return np.array(kps, dtype=np.float32)
26
-
27
- single_kp = extract_keypoints(single_heatmap)
28
-
29
- # reference motion
30
- ref_files = sorted([f for f in os.listdir(reference_heatmap_dir) if f.endswith(".npy")])
31
- ref_heatmaps = [np.load(os.path.join(reference_heatmap_dir, f)) for f in ref_files]
32
- ref_kp_list = [extract_keypoints(hm) for hm in ref_heatmaps]
33
-
34
- # Compute motion relative to first reference frame
35
- ref_base_kp = ref_kp_list[0]
36
- motion_vectors = [kp - ref_base_kp for kp in ref_kp_list]
37
-
38
- # Apply motion to single input keypoints
39
- fourcc = cv2.VideoWriter_fourcc(*"mp4v")
40
- video_writer = cv2.VideoWriter(preview_video, fourcc, 30, (W, H))
41
-
42
- def gaussian_heatmaps(points, H, W, sigma=2.0):
43
- N = points.shape[0]
44
- yy, xx = np.mgrid[0:H, 0:W].astype(np.float32)
45
- heat = np.zeros((H, W, N), dtype=np.float32)
46
- s2 = 2 * (sigma ** 2)
47
- for i, (x, y) in enumerate(points):
48
- d2 = (xx - x) ** 2 + (yy - y) ** 2
49
- heat[..., i] = np.exp(-d2 / s2)
50
- return heat
51
-
52
- for frame_idx, displacement in enumerate(motion_vectors):
53
- moved_kp = single_kp + displacement
54
-
55
- # Generate Gaussian heatmap for all points
56
- new_heatmap = gaussian_heatmaps(moved_kp, H, W, sigma=2.0)
57
- np.save(os.path.join(output_dir, f"{frame_idx:05d}.npy"), new_heatmap)
58
-
59
-
60
- frame_vis = np.zeros((H, W, 3), dtype=np.uint8)
61
- for (x, y) in moved_kp.astype(int):
62
- cv2.circle(frame_vis, (x, y), 2, (0, 255, 0), -1)
63
- video_writer.write(frame_vis)
64
-
65
- video_writer.release()
66
-
67
- print(f"Simulated motion heatmaps saved in '{output_dir}'")
68
- print(f"Preview video saved as '{preview_video}'")
 
1
+ import os
2
+ import numpy as np
3
+ import cv2
4
+
5
+
6
+ single_heatmap_path = "motion_transfer/dataset_single/reference_heatmap/00000.npy"
7
+ reference_heatmap_dir = "motion_transfer/dataset_single/reference_heatmap"
8
+ output_dir = "motion_transfer/dataset_single/test_heatmap"
9
+ preview_video = "motion_transfer/dataset_single/simulated_motion.mp4"
10
+
11
+ os.makedirs(output_dir, exist_ok=True)
12
+
13
+ single_heatmap = np.load(single_heatmap_path)
14
+ H, W, C = single_heatmap.shape
15
+
16
+ # Extract keypoints
17
+ def extract_keypoints(hmap):
18
+ kps = []
19
+ for i in range(hmap.shape[2]):
20
+ y, x = np.where(hmap[:, :, i] > 0)
21
+ if len(x) > 0:
22
+ kps.append([np.mean(x), np.mean(y)])
23
+ else:
24
+ kps.append([0, 0])
25
+ return np.array(kps, dtype=np.float32)
26
+
27
+ single_kp = extract_keypoints(single_heatmap)
28
+
29
+ # reference motion
30
+ ref_files = sorted([f for f in os.listdir(reference_heatmap_dir) if f.endswith(".npy")])
31
+ ref_heatmaps = [np.load(os.path.join(reference_heatmap_dir, f)) for f in ref_files]
32
+ ref_kp_list = [extract_keypoints(hm) for hm in ref_heatmaps]
33
+
34
+ # Compute motion relative to first reference frame
35
+ ref_base_kp = ref_kp_list[0]
36
+ motion_vectors = [kp - ref_base_kp for kp in ref_kp_list]
37
+
38
+ # Apply motion to single input keypoints
39
+ fourcc = cv2.VideoWriter_fourcc(*"mp4v")
40
+ video_writer = cv2.VideoWriter(preview_video, fourcc, 30, (W, H))
41
+
42
+ def gaussian_heatmaps(points, H, W, sigma=2.0):
43
+ N = points.shape[0]
44
+ yy, xx = np.mgrid[0:H, 0:W].astype(np.float32)
45
+ heat = np.zeros((H, W, N), dtype=np.float32)
46
+ s2 = 2 * (sigma ** 2)
47
+ for i, (x, y) in enumerate(points):
48
+ d2 = (xx - x) ** 2 + (yy - y) ** 2
49
+ heat[..., i] = np.exp(-d2 / s2)
50
+ return heat
51
+
52
+ for frame_idx, displacement in enumerate(motion_vectors):
53
+ moved_kp = single_kp + displacement
54
+
55
+ # Generate Gaussian heatmap for all points
56
+ new_heatmap = gaussian_heatmaps(moved_kp, H, W, sigma=2.0)
57
+ np.save(os.path.join(output_dir, f"{frame_idx:05d}.npy"), new_heatmap)
58
+
59
+
60
+ frame_vis = np.zeros((H, W, 3), dtype=np.uint8)
61
+ for (x, y) in moved_kp.astype(int):
62
+ cv2.circle(frame_vis, (x, y), 2, (0, 255, 0), -1)
63
+ video_writer.write(frame_vis)
64
+
65
+ video_writer.release()
66
+
67
+ print(f"Simulated motion heatmaps saved in '{output_dir}'")
68
+ print(f"Preview video saved as '{preview_video}'")