首页 > 解决方案 > 这是创建帧张量以使用 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)的最佳方法是什么?

请有人帮助我!

感谢关注!

标签: pythonopencvkerasdeep-learninglstm

解决方案


    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)

推荐阅读