|
14 | 14 | THRESHOLD_NS = 10 * 1000 * 1000
|
15 | 15 |
|
16 | 16 |
|
17 |
| -def extract(input_dir, output_dir): |
18 |
| - # Loop over each directory in the input directory |
19 |
| - for dir_name in os.listdir(input_dir): |
20 |
| - dir_path = os.path.join(input_dir, dir_name) |
21 |
| - if os.path.isdir(dir_path): |
22 |
| - # Find the video file in the directory |
23 |
| - video_filename = None |
24 |
| - for filename in os.listdir(dir_path): |
25 |
| - if filename.endswith(".mp4"): |
26 |
| - video_filename = filename |
27 |
| - break |
28 |
| - if video_filename is None: |
29 |
| - continue |
30 |
| - |
31 |
| - # Define the path to the input video |
32 |
| - input_path = os.path.join(dir_path, video_filename) |
33 |
| - |
34 |
| - # Find the CSV file in the directory |
35 |
| - csv_filename = os.path.splitext(video_filename)[0] + ".csv" |
36 |
| - csv_path = os.path.join(dir_path, csv_filename) |
37 |
| - |
38 |
| - # Read the CSV file with the timestamps |
39 |
| - timestamps = [] |
40 |
| - with open(csv_path, "r") as f: |
41 |
| - reader = csv.reader(f) |
42 |
| - for row in reader: |
43 |
| - timestamps.append(float(row[0])) |
44 |
| - |
45 |
| - # Define the output directory for the frames |
46 |
| - video_name = os.path.splitext(video_filename)[0] |
47 |
| - video_output_dir = os.path.join(output_dir, dir_name) |
48 |
| - if not os.path.exists(video_output_dir): |
49 |
| - os.makedirs(video_output_dir) |
50 |
| - |
51 |
| - # Open the video file |
52 |
| - cap = cv2.VideoCapture(input_path) |
53 |
| - if not cap.isOpened(): |
54 |
| - continue |
55 |
| - |
56 |
| - # Loop over each timestamp in the CSV file |
57 |
| - for timestamp in timestamps: |
58 |
| - # Extract the frame using OpenCV |
59 |
| - cap.set(cv2.CAP_PROP_POS_MSEC, timestamp * 1000) |
60 |
| - ret, frame = cap.read() |
61 |
| - if ret: |
62 |
| - frame_name = f"{video_name}_{timestamp:.3f}.jpg" |
63 |
| - frame_path = os.path.join(video_output_dir, frame_name) |
64 |
| - cv2.imwrite(frame_path, frame) |
65 |
| - |
66 |
| - # Release the video file |
67 |
| - cap.release() |
68 |
| - |
69 |
| -# |
70 |
| -# |
71 | 17 | def scan_session_dir(input_dir: Path) -> Tuple[List[str], List[pd.DataFrame], List[str]]:
|
72 | 18 | #
|
73 | 19 | # Find all CSV files in the directory and read it into a data frame
|
|
0 commit comments