如何为Keras Conv3D模型处理视频

时间:2019-11-29 06:04:27

标签: python opencv tensorflow keras

我正在将该Github转换为Keras模型

https://github.com/xiaogangLi/tensorflow-C3D

但是在尝试预处理数据时遇到了一个问题。我已经更改了一些代码来尝试解决此问题,但是仍然没有运气。代码会执行什么操作,但不会保存任何图像或视频。

这是原始代码https://github.com/xiaogangLi/tensorflow-C3D/blob/master/C3D/Code/prepare_clips.py

这就是我改变的方式

import os
import random
import cv2
import pandas as pd

labels = pd.read_csv("Label_Map/label.txt")

rate = 0.2
IN_DEPTH = 16
IN_HEIGHT = 128
IN_WIDTH = 128
IN_CHANNEL = 3
STRIDE = 16

fourcc = cv2.VideoWriter_fourcc(*'XVID')



def sample_video(video_path):
    cap = cv2.VideoCapture(video_path)
    while (cap.isOpened()):
        ret, frame = cap.read()
        if ret:
            cv2.imshow("Video", frame)
            if cv2.waitKey(25) & 0xFF == ord('q'):
                break
        else:
            break
    cap.release()
    cv2.destroyAllWindows()


def process_all_videos(fourcc):
    #out = None
    for j in range(len(labels.Class_name)):
        i = 0
        class_name = labels.Class_name[j]
        src_video_path = os.path.join('Raw_Data/', class_name)
        video_names = os.listdir(src_video_path)
        random.shuffle(video_names)

        num_train = int(len(video_names) * (1 - 2 * rate))
        num_val = int(len(video_names) * rate)
        num_test = int(len(video_names) * rate)
        n = 1

        for name in video_names:
            print('Preprocessing:', name)
            cap = cv2.VideoCapture(os.path.join(src_video_path, name))
            num_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
            frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
            frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

            dst_clips_path = "Data/Dump/"

            if n <= num_train:
                dst_clips_path = os.path.join('Data/Train/', class_name)
            elif num_train < n <= (num_train + num_val):
                dst_clips_path = os.path.join('Data/Val/', class_name)
            elif n > (num_train + num_val):
                dst_clips_path = os.path.join('Data/Test/', class_name)

           n = n + 1

            while(cap.isOpened()):
                if num_frames < IN_DEPTH: continue
                frame_list = []
                for j in range(num_frames):
                    ret, frame = cap.read()
                    if ret:
                        frame_list.append(frame)

                if len(frame_list) < IN_DEPTH:
                    continue

                for j in range(int(len(frame_list) / IN_DEPTH) + 1):
                    start = j * STRIDE
                    end = j * STRIDE + IN_DEPTH

                    if (start > len(frame_list)) or (end > len(frame_list)):
                        clips = frame_list[-IN_DEPTH::]
                    else:
                        clips = frame_list[j * STRIDE:j * STRIDE + IN_DEPTH]
                    # write clips
                    i += 1
                    path_of_output = dst_clips_path + '/' + class_name + "_" + str(i) + '.avi'
                    print("Path Written to: ", path_of_output)
                    out = cv2.VideoWriter(path_of_output, fourcc, IN_DEPTH, (frame_width, frame_height))
                    for k in range(IN_DEPTH):
                        cv2.imshow("Video", clips[k])
                        out.write(clips[k])

    out.release()
    cap.release()


#sample_video('Raw_Data/cartwheel/Bodenturnen_im_sportunterricht_cartwheel_f_cm_np1_ri_med_2.avi')
process_all_videos(fourcc)

我认为这段代码所做的是将视频的各个部分分组为较小的片段,然后保存这些片段,但是现在我真的不知道它的作用,因为当我使用'''cv2时,所有视频播放器都会显示.imshow'''是灰屏。

0 个答案:

没有答案