File size: 7,624 Bytes
3cf4fff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import os
import glob
import cv2

from tqdm import tqdm
from torchvision.transforms import (
    Compose,
    Resize,
    CenterCrop,
    ToTensor,
    Normalize,
    InterpolationMode,
)
from PIL import Image
import torch
import numpy as np
import pandas as pd


def convert_avi_to_mp4(input_path, output_path):
    # Check if output_path already exists

    # Convert .avi to .mp4 using OpenCV
    cap = cv2.VideoCapture(input_path)
    if not cap.isOpened():
        raise IOError(f"Cannot open {input_path}")

    fourcc = cv2.VideoWriter_fourcc(*"mp4v")  # Use mp4 codec
    fps = cap.get(cv2.CAP_PROP_FPS)
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))

    while True:
        ret, frame = cap.read()
        if not ret:
            break
        out.write(frame)

    cap.release()
    out.release()


def create_images(video_path, output_dir):
    if os.path.isdir(output_dir):
        pass
    else:
        os.makedirs(output_dir, exist_ok=True)
        cap = cv2.VideoCapture(video_path)
        frame_idx = 0

        while cap.isOpened():
            ret, frame = cap.read()
            if not ret:
                break
            frame_path = os.path.join(output_dir, f"{frame_idx:05d}.jpg")
            cv2.imwrite(frame_path, frame)
            frame_idx += 1


def create_videos(video_path, output_dir):
    # Determine input and output paths for .avi and .webm
    base_path = video_path
    mp4_path = video_path.replace("/Data/", "/Video_data/")
    if mp4_path.endswith(".avi") or mp4_path.endswith(".webm"):
        mp4_path = mp4_path.rsplit(".", 1)[0] + ".mp4"
    else:
        mp4_path = mp4_path.replace(".mp4", ".mp4")  # fallback, should already be .mp4

    os.makedirs(output_dir, exist_ok=True)

    if not os.path.exists(mp4_path):
        # Try .avi first
        avi_path = video_path.replace(".mp4", ".avi")
        if os.path.exists(avi_path):
            convert_avi_to_mp4(avi_path, mp4_path)
        else:
            # Try .webm
            webm_path = video_path.replace(".mp4", ".webm")
            if os.path.exists(webm_path):
                convert_avi_to_mp4(webm_path, mp4_path)
            else:
                raise FileNotFoundError(
                    f"Neither .avi nor .webm found for {video_path}"
                )


# Code to convert one video to few images.
def video2image(video_path, frame_rate=1.0, size=224):
    def preprocess(size, n_px):
        return Compose(
            [
                Resize(size, interpolation=InterpolationMode.BICUBIC),
                CenterCrop(size),
                lambda image: image.convert("RGB"),
                ToTensor(),
                Normalize(
                    (0.48145466, 0.4578275, 0.40821073),
                    (0.26862954, 0.26130258, 0.27577711),
                ),
            ]
        )(n_px)

    cap = cv2.VideoCapture(video_path)
    cap = cv2.VideoCapture(video_path, cv2.CAP_FFMPEG)
    frameCount = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    fps = int(cap.get(cv2.CAP_PROP_FPS))
    if fps < 1:
        images = np.zeros([3, size, size], dtype=np.float32)
        print("ERROR: problem reading video file: ", video_path)
    else:
        total_duration = (frameCount + fps - 1) // fps
        start_sec, end_sec = 0, total_duration
        interval = fps / frame_rate
        frames_idx = np.floor(np.arange(start_sec * fps, end_sec * fps, interval))
        ret = True
        images = np.zeros([len(frames_idx), 3, size, size], dtype=np.float32)

        for i, idx in enumerate(frames_idx):
            cap.set(cv2.CAP_PROP_POS_FRAMES, idx)
            ret, frame = cap.read()
            if not ret:
                break
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            last_frame = i
            images[i, :, :, :] = preprocess(size, Image.fromarray(frame).convert("RGB"))

        images = images[: last_frame + 1]
    cap.release()
    video_frames = torch.tensor(images)
    return video_frames


## Dataset specific


def create_images_breakfast(gobal_path, start_end=15):
    sub_dirs = sorted(glob.glob(gobal_path + "*/"))

    def get_folder_name(local_path):
        return local_path.split("/")[-2].replace("P", "")

    part_dirs = []

    for i in sub_dirs:
        if int(get_folder_name(i)) <= start_end:
            part_dirs.append(i)

    for i in tqdm(part_dirs, desc="Processing avi to images"):
        video_dirs = sorted(glob.glob(i + "*/"))
        for j in video_dirs:
            avi_files = sorted(glob.glob(j + "*.avi"))
            for k in avi_files:
                output_dir = k.replace("/Data/", "/Image_data/").replace(".avi", "")
                create_images(k, output_dir)


def create_videos_breakfast(gobal_path, start_end=15):
    sub_dirs = sorted(glob.glob(gobal_path + "*/"))

    def get_folder_name(local_path):
        return local_path.split("/")[-2].replace("P", "")

    part_dirs = []

    for i in sub_dirs:
        if int(get_folder_name(i)) <= start_end:
            part_dirs.append(i)

    for i in tqdm(part_dirs, desc="Processing avi to mp4"):
        video_dirs = sorted(glob.glob(i + "*/"))
        for j in video_dirs:
            avi_files = sorted(glob.glob(j + "*.avi"))
            for k in avi_files:
                output_dir = j.replace("/Data/", "/Video_data/")
                create_videos(k, output_dir)


def create_images_ucf(global_path, files):

    path_list = pd.read_csv(files, sep=" ", header=None)

    for i in tqdm(path_list.values):
        video_path = os.path.join(global_path, i[0])
        output_dir = video_path.replace("/Data/", "/Image_data/").replace(".avi", "")
        create_images(video_path, output_dir)


def create_videos_ucf(global_path, files):
    path_list = pd.read_csv(files, sep=" ", header=None)

    for i in tqdm(path_list.values):
        video_path = os.path.join(global_path, i[0])
        output_dir = os.path.dirname(
            video_path.replace("/Data/", "/Video_data/").replace(".avi", "")
        )
        create_videos(video_path, output_dir)


def create_images_hmdb(global_path, path_list):
    for i in tqdm(path_list):
        local_name = i.split("/")[1]
        video_path = global_path + i
        output_dir = (
            video_path.replace("/Data/", "/Image_data/")
            .replace(".avi", "")
            .replace("//", "/")
        )
        video_path = global_path + local_name + i
        create_images(video_path, output_dir)


def create_videos_hmdb(global_path, path_list):
    for i in tqdm(path_list):
        local_name = i.split("/")[1]
        video_path = global_path + local_name + i
        output_dir = (
            video_path.replace("/Data/", "/Video_data/")
            .replace(".avi", "")
            .replace("//", "/")
        )
        # remove last folder in outputdir
        output_dir = os.path.dirname(output_dir)
        video_path = global_path + local_name + i
        create_videos(video_path, output_dir)


def create_images_sth2(global_path, files):
    for i in tqdm(files):
        video_path = global_path + str(i) + ".webm"
        output_dir = video_path.replace("/Data/", "/Image_data/").replace(".webm", "")
        create_images(video_path, output_dir)


def create_videos_sth2(global_path, files):
    for i in tqdm(files):
        video_path = global_path + str(i) + ".webm"
        output_dir = os.path.dirname(
            video_path.replace("/Data/", "/Video_data/").replace(".webm", "")
        )
        create_videos(video_path, output_dir)