Python Forum
Better way to append frames from a video to multiple lists?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Better way to append frames from a video to multiple lists?
#1
Hi, I am trying to split a video based on the index and value stored in a text file. The index represents the frame number and value represents frame class. Then I create multiples lists based on the count of unique values in the text file and split the videos and append the frames to a list based on the frame number. But this operation on a larger video crashes my python because of memory issues. Is there a better way to do this? Here is an example text https://pastebin.com/Shdw2C0d

In the code, I am referring to line 28. Thanks for your time.

import numpy as np
import cv2
import os
from collections import defaultdict

# Create a generator to let you loop over the video in a for loop
def get_frames(vid):
    while True:
        ret, frame = vid.read()
        if not ret:
            return
        yield frame


def split_videos(txt_file, video_file):

    with open(txt_file,'r') as f:
        frame_numbers = list(map(int, f.read().split()))
        size = len(set(frame_numbers))

    offset = min(frame_numbers)
    frame_groups = [[] for _ in range(7)]

    video = cv2.VideoCapture(video_file)
    

    for index, frame in zip(frame_numbers, get_frames(video)):
        frame_groups[index - offset].append(frame)

    video.release()

    frame_groups_final = [x for x in frame_groups if x]
    codec = cv2.VideoWriter_fourcc(*'XVID')

    for index, group in enumerate(frame_groups_final):
    # Arguments are filename, codec, framerate, resolution
        print((len(group[0]), len(group[0][0])))
        writer = cv2.VideoWriter(f'video_{index + offset}.avi', codec, 30.0, (len(group[0][0]), len(group[0])))
        for frame in group:
            writer.write(frame)
        writer.release()
    cv2.destroyAllWindows()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,432 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  What is the fastest way to get all the frames from a video file? glorsh66 3 990 May-26-2023, 04:41 AM
Last Post: Gribouillis
  How to map two data frames based on multiple condition SriRajesh 0 1,448 Oct-27-2021, 02:43 PM
Last Post: SriRajesh
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 2,757 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Create Dict from multiple Lists with duplicate Keys rhat398 10 3,979 Jun-26-2021, 11:12 AM
Last Post: Larz60+
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 2,273 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
Question How to print multiple elements from multiple lists in a FOR loop? Gilush 6 2,841 Dec-02-2020, 07:50 AM
Last Post: Gilush
  Append 2d empty lists NMMST 2 2,792 Oct-19-2020, 09:25 PM
Last Post: NMMST
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Reading Multiple Lists Using SUM function dgrunwal 6 3,283 Jun-03-2020, 08:23 PM
Last Post: dgrunwal

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020