-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpreprocess.py
More file actions
59 lines (46 loc) · 1.81 KB
/
preprocess.py
File metadata and controls
59 lines (46 loc) · 1.81 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
import os
import cv2
# Function to resize and convert video format and rename files
def resize_and_convert_video(video_path, output_folder, target_width, target_height, index):
# Read video
cap = cv2.VideoCapture(video_path)
if not cap.isOpened():
print(f"Error: Cannot open video '{video_path}'")
return
# Create output folder if it doesn't exist
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Get video properties
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
# Define codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
output_video_path = os.path.join(output_folder, f"{index:04d}nonfight.mp4")
out = cv2.VideoWriter(output_video_path, fourcc, fps, (target_width, target_height))
# Resize frames and write to new video
while True:
ret, frame = cap.read()
if not ret:
break
resized_frame = cv2.resize(frame, (target_width, target_height))
out.write(resized_frame)
# Release video capture and writer objects
cap.release()
out.release()
print(f"Video resized and saved to {output_video_path}")
# Directory containing video files
videos_folder = 'D:\project\dummy'
# Output folder for resized videos
output_folder = 'D:\project\\train\\nonvoilance'
# Target size for resizing
target_width = 640
target_height = 480
# Initialize index for renaming
index =801
# Iterate through video files, resize/convert format, and rename
for filename in os.listdir(videos_folder):
if filename.endswith(".mp4"):
video_path = os.path.join(videos_folder, filename)
resize_and_convert_video(video_path, output_folder, target_width, target_height, index)
index += 1