-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_ranges.py
More file actions
237 lines (200 loc) · 9.96 KB
/
Copy pathvalidate_ranges.py
File metadata and controls
237 lines (200 loc) · 9.96 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import cv2
import json
import os
import sys
import pickle
from create_processed_folder import create_processed_folder
from save_frames import save_frames
WINDOW_NAME = 'Frame'
# Define arrow key codes (handling standard waitKeyEx and platform specific keysyms)
LEFT_KEYS = [2424832, 0x250000, 65361]
RIGHT_KEYS = [2555904, 0x270000, 65363]
UP_KEYS = [2490368, 0x260000, 65362]
DOWN_KEYS = [2621440, 0x280000, 65364]
# Page up/down and Home/End keys (several platform variants)
PAGE_UP_KEYS = [2162688, 0x210000, 65365]
PAGE_DOWN_KEYS = [2228224, 0x220000, 65366]
HOME_KEYS = [2359296, 0x240000, 65360]
END_KEYS = [2293760, 0x2b0000, 65367]
CLOSED_BRACKETS = [93, 125, 41] # ']', '}', ')'
OPEN_BRACKETS = [91, 123, 40] # '[', '{', '('
def load_progress(save_path):
with open(save_path, 'r') as f:
try:
progress = json.load(f)
return progress.get('frame_ranges'), progress.get('validated_ranges'), progress.get('current_range_index'), progress.get('complet')
except json.JSONDecodeError:
print("Error decoding save file. Starting fresh.")
return 1
def save_progress(save_path, frame_ranges, validated_ranges, current_range_index, complet = False):
progress = {'frame_ranges': frame_ranges, 'validated_ranges': validated_ranges, 'current_range_index': current_range_index, 'complet':complet}
with open(save_path, 'w') as f:
json.dump(progress, f)
print(f"💾 Progress saved to {save_path}")
def validate_ranges(video_path, output_path=None):
cap = cv2.VideoCapture(video_path)
processed_path = create_processed_folder(video_path, output_path=output_path)
save_path = os.path.join(processed_path, "video_review_progress.json")
with open(os.path.join(processed_path, "ranges.pkl"), "rb") as f:
frame_ranges = pickle.load(f)
validated_ranges = []
current_range_index = 0
if os.path.exists(save_path):
answer = input("Load previous progress? (y/n): ").lower()
if answer == 'y':
frame_ranges, validated_ranges, current_range_index, complet = load_progress(save_path)
print("Loaded previous progress.")
if complet == True:
print("Loaded file is already complet!")
return 0
else:
print("Starting fresh.")
else:
print("No previous progress found. Starting fresh.")
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
fps = cap.get(cv2.CAP_PROP_FPS)
speed = int(1000 / fps) # Milliseconds between frames in auto mode
manual_step = 1
manual_step_large = int(fps*2)
review_mode = "auto" # "auto" or "manual"
early_exit = False
while current_range_index < len(frame_ranges) and not early_exit:
start, end = frame_ranges[current_range_index]
# Validate bounds
if start < 0 or end >= total_frames or start >= end:
print(f"Invalid range: [{start}, {end}]")
current_range_index += 1
continue
print(f"Reviewing range: [{start}, {end}] - ", end="")
if review_mode == "auto":
print("Press UP to validate, DOWN to skip, LEFT/RIGHT to step (manual), SPACE to pause (manual), ESC to exit.")
elif review_mode == "manual":
print("Press UP to validate until current frame, DOWN to skip until current frame, LEFT/RIGHT to step, SPACE to resume (auto), ESC to exit.")
cap.set(cv2.CAP_PROP_POS_FRAMES, start)
current_frame = start
range_length = end - start + 1
# Determine adjacent-range clamps for stepping/jumping
# Allow stepping to at most the previous range end (min) and next range start (max)
prev_end = frame_ranges[current_range_index - 1][1] if current_range_index > 0 else 0
next_start = frame_ranges[current_range_index + 1][0] if current_range_index < len(frame_ranges) - 1 else total_frames - 1
# Create a resizable window
cv2.namedWindow(WINDOW_NAME, cv2.WINDOW_NORMAL)
while True:
if current_frame > end and review_mode == "auto":
current_frame = start
cap.set(cv2.CAP_PROP_POS_FRAMES, start)
# elif current_frame < start and review_mode == "manual":
# current_frame = start
# cap.set(cv2.CAP_PROP_POS_FRAMES, start)
ret, frame = cap.read()
if not ret:
break
# Calculate relative frame number
relative_pos = current_frame - start + 1
overlay_text = f"{relative_pos}/{range_length} {current_frame}"
# Put the overlay on the frame
cv2.putText(frame, overlay_text, (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow(WINDOW_NAME, frame)
key_code = cv2.waitKeyEx(speed if review_mode == "auto" else 0)
key_ascii = key_code & 0xFF if key_code != -1 else -1
# print(f"Key pressed: {key_code} (ASCII: {key_ascii})") # Debug print for key codes
if key_code in UP_KEYS:
if review_mode == "manual" and current_frame < end:
actual_start = min(start, current_frame)
actual_end = max(start, current_frame)
validated_ranges.append([actual_start, actual_end])
print(f"✅ Validated split range: [{actual_start}, {actual_end}]")
# Move the next start point safely to the frame right after our max boundary
start = actual_end + 1
frame_ranges[current_range_index] = [start, end]
current_frame = start
cap.set(cv2.CAP_PROP_POS_FRAMES, start)
range_length = end - start + 1
review_mode = "auto"
print("Switched to auto review mode.")
else:
validated_ranges.append([start, end])
print(f"✅ Validated range: [{start}, {end}]")
break
elif key_code in DOWN_KEYS:
if review_mode == "manual" and current_frame < end:
actual_start = min(start, current_frame)
actual_end = max(start, current_frame)
print(f"❌ Skipped split range: [{actual_start}, {actual_end}]")
start = actual_end + 1
frame_ranges[current_range_index] = [start, end]
current_frame = start
cap.set(cv2.CAP_PROP_POS_FRAMES, start)
range_length = end - start + 1
review_mode = "auto"
print("Switched to auto review mode.")
else:
print(f"❌ Skipped range: [{start}, {end}]")
break
elif key_code in LEFT_KEYS:
review_mode = "manual"
# Step backward
current_frame = max(prev_end, current_frame - manual_step)
cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)
elif key_code in RIGHT_KEYS:
review_mode = "manual"
# Step forward
current_frame = min(next_start, current_frame + manual_step)
cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)
elif key_code in PAGE_UP_KEYS:
# Jump forward
review_mode = "manual"
current_frame = min(next_start, current_frame + manual_step_large)
cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)
elif key_code in PAGE_DOWN_KEYS:
# Jump backward
review_mode = "manual"
current_frame = max(prev_end, current_frame - manual_step_large)
cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)
elif key_code in HOME_KEYS:
# Go to start of the current range
review_mode = "manual"
current_frame = start
cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)
elif key_code in END_KEYS:
# Go to end of the current range
review_mode = "manual"
current_frame = end
cap.set(cv2.CAP_PROP_POS_FRAMES, current_frame)
elif key_code in OPEN_BRACKETS:
speed -= 100
print(f"Speed decreased to {speed} ms")
elif key_code in CLOSED_BRACKETS:
speed += 100
print(f"Speed increased to {speed} ms")
elif key_ascii == 27: # ESC key
early_exit = True
save_progress(save_path, frame_ranges, validated_ranges, current_range_index)
break
elif key_ascii == ord(' '): # Space
review_mode = "auto" if review_mode == "manual" else "manual"
print(f"Switched to {review_mode} review mode.")
elif review_mode == "auto":
current_frame += 1
if early_exit:
print("🚪 Exiting early due to ESC key.")
break
current_range_index += 1
cap.release()
cv2.destroyAllWindows()
if current_range_index == len(frame_ranges):
save_progress(save_path, frame_ranges, validated_ranges, current_range_index, True)
print("🎉 All work is done!")
save = input("Save now? y/n: ")
if save == "y":
save_frames(video_path, output_path=output_path)
# print("\nValidated Ranges:", validated_ranges)
# print("Total Ranges:", frame_ranges)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Validate movement ranges for a clip.")
parser.add_argument("video_path", help="Path to input video file")
parser.add_argument("output_path", nargs="?", default=None, help="Optional output path or directory name")
args = parser.parse_args()
validate_ranges(args.video_path, output_path=args.output_path)