python - 这是创建帧张量以使用 Keras CNN-LSTM 对视频进行分类的最佳方法
问题描述
大家,我面临以下问题。
我有一个用于视频分类的 CNN-LSTM Keras 模型。我正在尝试创建一个张量来存储我使用 OpenCV 获得的帧,正如您在这段代码中看到的那样:
for i in list1:
#Video Path
vid = str(path + i) #path to each video from list1 = os.listdir(path)
#Reading the Video
cap = cv2.VideoCapture(vid)
#To Store Frames
frames = []
for j in range(15): #i want 15 frames from each video
ret, frame = cap.read()
if ret == True:
print('Class 1 - Success! {0}'.format(count))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = cv2.resize(frame,(28,28),interpolation=cv2.INTER_AREA)
frames.append(frame)
X_data.append(frames)
但问题是我对两类视频重复了这段代码,我最终的 X_data 的形状是 (2, 15, 28, 28)。一旦我在每个文件夹中有 145 个视频,那不应该有超过 2 个样本吗?
我的想法是在此 X_data 中添加另一列,其中包含目标,1 用于视频类 1,0 用于视频类 2,但是对于这种形状,我不知道我必须做什么。:/
将每个视频的 15 帧存储在张量中以便用它来训练分类器(CNN-LSTM)的最佳方法是什么?
请有人帮助我!
感谢关注!
解决方案
frames = [] <# You reset frames on each video
loop to construct frames
X_data.append(frames) <#You add the frames into X_data but frames only
has frames from last video in list1
您可能希望将 X_data 移动到循环中或沿着这些线移动
这些方面的东西会起作用:
for class,video_list in enumerate([negative_videos, positive_videos]):
for i in list1:
#Video Path
vid = str(path + i) #path to each video from list1 = os.listdir(path)
#Reading the Video
cap = cv2.VideoCapture(vid)
#To Store Frames
frames = []
for j in range(15): #i want 15 frames from each video
ret, frame = cap.read()
if ret == True:
print('Class 1 - Success! {0}'.format(count))
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = cv2.resize(frame,(28,28),interpolation=cv2.INTER_AREA)
frames.append(frame)
X_data.append(frames)
y_data.append(class)
推荐阅读
- css - 溢出-x 子元素宽度
- open-source - Prestashop 1.7.2 总额含税
- c# - 在数据库级别跟踪进程
- ansible - Ansible,with_item 循环问题
- javascript - 如何在不重新加载页面的情况下在 javascript 中的 json 文件之间切换
- ios - 我是否使用 Apple 的 CallKit 框架进行 Voip 通话,以及当用户使用 callkit 按钮接受呼叫时,它在后台状态下如何工作?
- python - Tensorflow 中的分类和连续交叉特征列
- sapui5 - SAPUI5 odata 绑定到 vbox 项目的深层结构
- php - PHP中的组数组项
- kotlin - 在 Kotlin 中,有没有一种更原生的方式来比较整数,然后是字符串?