为什么我的视频文件在转换为 np.array 后变大了?
Why are my video files getting bigger after converting to np.array?
我有大约 700x200 的视频文件,我正在使用 cv2 执行预处理。实际视频是 .mp4 格式,范围从几 mb 取决于长度,但在缩小分辨率后,使彩色视频变灰,当我尝试通过 Pickle
或 np.save
.
预处理代码为:
def save_video(link):
frames = []
# Creating a VideoCapture object to read the video
# cap = cv2.VideoCapture('video.mp4')
cap = cv2.VideoCapture(link)
frameCount = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frameWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frameHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
buf_c = np.empty((frameCount, frameHeight, frameWidth, 3), np.dtype('uint8'))
buf = np.empty((frameCount, frameHeight, frameWidth), np.dtype('uint8'))
fc = 0
ret = True
# Resolution reduction
scale_percent = 30 # percent of original size
width = int(buf.shape[2] * scale_percent / 100)
height = int(buf.shape[1] * scale_percent / 100)
dim = (width, height)
while (fc < frameCount and ret):
ret, buf_c[fc] = cap.read()
buf[fc] = cv2.cvtColor(buf_c[fc], cv2.COLOR_BGR2GRAY)
resized = cv2.resize(buf[fc], dim, interpolation = cv2.INTER_AREA)
frames.append(resized)
fc += 1
cap.release()
cv2.destroyAllWindows()
return frames // or np.asarray(frames)
视频文件的尺寸示例(处理后):
(844, 216, 121)
(缩小后 216x121 的 844 帧)
(788, 216, 121)
实际视频文件在任何预处理之前为 1-2MB,生成的 pkl 或 npy 大小为 10x+。这是应该发生的事情吗?
一个压缩的视频流被OpenCV解压缩并保存为原始数据。要减小大小,您需要对视频流进行编码。例如:
def opencv_replay(video_file: str, video_file_out: str):
import cv2
video_in = cv2.VideoCapture(video_file)
video_out = cv2.VideoWriter()
assert (video_out.open(
video_file_out,
cv2.VideoWriter_fourcc('a', 'v', 'c', '1'),
video_in.get(cv2.CAP_PROP_FPS),
(int(video_in.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video_in.get(cv2.CAP_PROP_FRAME_HEIGHT)))
))
while True:
res, frame = video_in.read()
if not res:
break
video_out.write(frame)
avc1
用于Linux中的h264编码。
我有大约 700x200 的视频文件,我正在使用 cv2 执行预处理。实际视频是 .mp4 格式,范围从几 mb 取决于长度,但在缩小分辨率后,使彩色视频变灰,当我尝试通过 Pickle
或 np.save
.
预处理代码为:
def save_video(link):
frames = []
# Creating a VideoCapture object to read the video
# cap = cv2.VideoCapture('video.mp4')
cap = cv2.VideoCapture(link)
frameCount = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
frameWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frameHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
buf_c = np.empty((frameCount, frameHeight, frameWidth, 3), np.dtype('uint8'))
buf = np.empty((frameCount, frameHeight, frameWidth), np.dtype('uint8'))
fc = 0
ret = True
# Resolution reduction
scale_percent = 30 # percent of original size
width = int(buf.shape[2] * scale_percent / 100)
height = int(buf.shape[1] * scale_percent / 100)
dim = (width, height)
while (fc < frameCount and ret):
ret, buf_c[fc] = cap.read()
buf[fc] = cv2.cvtColor(buf_c[fc], cv2.COLOR_BGR2GRAY)
resized = cv2.resize(buf[fc], dim, interpolation = cv2.INTER_AREA)
frames.append(resized)
fc += 1
cap.release()
cv2.destroyAllWindows()
return frames // or np.asarray(frames)
视频文件的尺寸示例(处理后):
(844, 216, 121)
(缩小后 216x121 的 844 帧)
(788, 216, 121)
实际视频文件在任何预处理之前为 1-2MB,生成的 pkl 或 npy 大小为 10x+。这是应该发生的事情吗?
一个压缩的视频流被OpenCV解压缩并保存为原始数据。要减小大小,您需要对视频流进行编码。例如:
def opencv_replay(video_file: str, video_file_out: str):
import cv2
video_in = cv2.VideoCapture(video_file)
video_out = cv2.VideoWriter()
assert (video_out.open(
video_file_out,
cv2.VideoWriter_fourcc('a', 'v', 'c', '1'),
video_in.get(cv2.CAP_PROP_FPS),
(int(video_in.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video_in.get(cv2.CAP_PROP_FRAME_HEIGHT)))
))
while True:
res, frame = video_in.read()
if not res:
break
video_out.write(frame)
avc1
用于Linux中的h264编码。