-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvideo_processor.py
More file actions
129 lines (97 loc) · 4.27 KB
/
Copy pathvideo_processor.py
File metadata and controls
129 lines (97 loc) · 4.27 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
import logging
from tqdm import tqdm
import cv2
import random
import numpy as np
import os
import tensorflow as tf
def get_videos(path_list, label, num_videos):
# Initialize variables.
video_paths = []
video_labels = []
all_videos = []
# Iterate over the paths.
for path in path_list:
# Get a list of video paths in the directory.
videos = [os.path.join(path, f)
for f in os.listdir(path)
if f.endswith('.avi') and os.path.isfile(os.path.join(path, f))]
# Add the video paths to the list of all videos.
all_videos.extend(videos)
# Shuffle the list of all videos.
random.shuffle(all_videos)
# Get the first `num_videos` videos from the list of all videos.
selected_videos = all_videos[:num_videos]
# Iterate over the selected videos.
for video in selected_videos:
# Add the video path and label to the lists of video paths and labels.
video_paths.append(video)
video_labels.append(label)
# Return the lists of video paths and labels.
return video_paths, video_labels
def save_video_labels_to_file(filename, video_paths, labels):
with open(filename, "w") as file:
for video_path, label in zip(video_paths, labels):
file.write(f"{video_path},{label}\n")
def process_dataset(native_videos, modified_videos, native_labels, modified_labels):
# Process the native videos.
processed_native_videos, native_videos_paths = process_videos(native_videos)
processed_modified_videos, modified_videos_paths = process_videos(modified_videos)
# Concatenate the native and modified data.
processed_videos = np.concatenate([processed_native_videos, processed_modified_videos], axis=0)
processed_videos = processed_videos.astype(np.float32) / 255.0
processed_videos = tf.data.Dataset.from_tensor_slices(processed_videos)
# Use the provided labels.
labels = np.concatenate([native_labels, modified_labels], axis=0)
labels = labels.astype(np.int16)
labels = tf.data.Dataset.from_tensor_slices(labels)
all_video_paths = native_videos_paths + modified_videos_paths
# Return the processed data, labels, and video paths.
return processed_videos, labels, all_video_paths
def process_videos(videos):
# Initialize variables.
processed_videos = []
video_paths = []
videos_id_list = []
bgSub = cv2.createBackgroundSubtractorMOG2(history=100, varThreshold=10)
# Iterate over the video paths.
for video_path in tqdm(videos, desc='Processing videos', position=0, leave=True):
# Open the video file.
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
logging.warning(f"Could not open video file: {video_path}")
continue
# Get the number of frames in the video.
num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# Initialize the video frames list.
video_frames = []
# Iterate over the frames in the video.
for frame_count in range(num_frames):
ret, frame = cap.read()
# Skip the frame if it could not be read.
if not ret or frame.size == 0:
break
# Apply background subtraction to the frame.
fgMask = bgSub.apply(frame)
# Convert the foreground mask to RGB.
mask = cv2.cvtColor(fgMask, cv2.COLOR_GRAY2BGR)
# Apply the mask to the frame.
processed_frame = cv2.bitwise_and(frame, mask)
# Skip the first and last 10 frames.
if 50 < frame_count <= 150:
# Skip every other frame.
if frame_count % 10 == 0:
video_frames.append(processed_frame)
# Close the video file.
cap.release()
# If there are no frames in the video, skip it.
if len(video_frames) == 0:
continue
# Store the video path and label
video_paths.append(video_path)
# Stack the frames into a NumPy array.
video_frames = np.array(video_frames)[..., [2, 1, 0]]
video_frames = np.maximum(video_frames, 0)
processed_videos.append(np.stack(video_frames, axis=0))
# Return the processed videos, video paths, and their labels.
return processed_videos, video_paths