forked from JiawangBian/sc_depth_pl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_valid_frame_index.py
59 lines (40 loc) · 1.35 KB
/
generate_valid_frame_index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# This script is used to filter out static-camera frames.
import numpy as np
import cv2
from path import Path
import argparse
def parse_args():
parser = argparse.ArgumentParser(
description='Selecting video frames for training sc_depth')
parser.add_argument('--dataset_dir', required=True)
args = parser.parse_args()
return args
def compute_movement_ratio(frame1, frame2):
frame1_gray = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY)
frame2_gray = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY)
h, w = frame1_gray.shape
diff = np.abs(frame1_gray - frame2_gray)
ratio = (diff > 10).sum() / (h*w)
return ratio
def generate_index(scene):
images = sorted(scene.files('*.jpg'))
index = [0]
for idx in range(1, len(images)):
frame1 = cv2.imread(images[index[-1]])
frame2 = cv2.imread(images[idx])
move_ratio = compute_movement_ratio(frame1, frame2)
if move_ratio < 0.5:
continue
index.append(idx)
print(len(images), len(index))
return index
def main():
args = parse_args()
DataRoot = Path(args.dataset_dir)
scenes = sorted((DataRoot/'training').dirs())
for scene in scenes:
print(scene)
index = generate_index(scene)
np.savetxt(scene/'frame_index.txt', index, fmt='%d', delimiter='\n')
if __name__ == '__main__':
main()