Skip to content

Commit 4d82ec1

Browse files
committed
video frame extraction code added
1 parent 4264225 commit 4d82ec1

File tree

2 files changed

+18
-57
lines changed

2 files changed

+18
-57
lines changed

PostProcessing/PostProcessVideos.py

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -14,60 +14,6 @@
1414
THRESHOLD_NS = 10 * 1000 * 1000
1515

1616

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-
#
7117
def scan_session_dir(input_dir: Path) -> Tuple[List[str], List[pd.DataFrame], List[str]]:
7218
#
7319
# Find all CSV files in the directory and read it into a data frame

PostProcessing/video.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
import cv2
2-
# or ffmpeg
2+
import os
33

44
import pandas as pd
55

6-
76
def extract_frames(video_file: str, timestamps: pd.DataFrame, output_dir: str):
8-
pass
7+
# Open the video file
8+
cap = cv2.VideoCapture(video_file)
9+
assert cap.isOpened() == True
10+
video_name = os.path.splitext(video_file)[0]
11+
# Loop over each timestamp in the CSV file
12+
for timestamp in timestamps:
13+
# Extract the frame using OpenCV
14+
cap.set(cv2.CAP_PROP_POS_MSEC, timestamp * 1000)
15+
ret, frame = cap.read()
16+
if ret:
17+
frame_name = f"{video_name}_{timestamp:.3f}.jpg"
18+
frame_path = os.path.join(output_dir, frame_name)
19+
cv2.imwrite(frame_path, frame)
20+
21+
# Release the video file
22+
cap.release()
23+

0 commit comments

Comments
 (0)