简体   繁体   English

这是使用Keras CNN-LSTM创建帧张量以对视频进行分类的最佳方法

[英]Which is the best way to create a tensor of frames to classify videos with Keras CNN-LSTM

everyone, i'm facing the following problem. 大家,我面临以下问题。

I have a CNN-LSTM Keras Model to video classification. 我有一个CNN-LSTM Keras模型可以对视频进行分类。 I'm trying to create a tensor to store frames i've got with OpenCV, as you can see in this snippet of code: 我正在尝试创建一个张量来存储我在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)

But the problem is that i repeat this code for two classes of videos, and the shape of my final X_data is (2, 15, 28, 28). 但是问题是我在两类视频中重复了此代码,而我最终的X_data的形状是(2,15,28,28)。 Should not that have more than 2 samples, once i have 145 videos in each folder? 一旦每个文件夹中有145个视频,那该不应该有2个以上的样本吗?

My idea is to add another column to this X_data with the targets, 1 for the Class 1 of videos, and 0 for the Class 2 of videos, but with this shape i don't know what i have to do. 我的想法是向此X_data添加另一列目标,其中1代表视频的第1类,0代表视频的第2类,但是具有这种形状,我不知道我该怎么做。 :/ :/

It is the best way to store 15 frames of each video in a tensor in order to use it to train a classifier (CNN-LSTM)? 最好的方法是将每个视频的15帧存储在张量中,以用于训练分类器(CNN-LSTM)?

Someone help me, please! 请有人帮我!

Thanks for the attention! 感谢您的关注!

    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

You might want to move the X_data into the loop or something along those lines 您可能希望将X_data移入循环或沿这些行的内容

Something along these lines will work: 遵循这些原则的方法将起作用:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM