-1

I have a list of videos (10 sec each) in a folder and I'm trying to loop through each action video to extract keypoints and save them as json files.

path = "path to video folder"
for file in os.listdir(path):
  cap = cv2.VideoCapture(path+file)
  while cap.isOpened():
    try:
      ret, frame = cap.read()

I ran into a problem where the extracted data has some keypoints from other videos, and I just want to run this code, end with the stop time for the video is done, pause, start next video. How can I help correct this?

AloneTogether
  • 25,814
  • 5
  • 20
  • 39
hishan lu
  • 1
  • 1
  • 3
    you stated what you want done. that in itself isn't a problem. what _is_ the problem and _why_ is it a problem? please review [ask] and [help/on-topic] – Christoph Rackwitz Sep 01 '22 at 07:23

1 Answers1

0

If you want to process multiple videos in turn you can check the ret (success) value of cap.read() to detect the end of each file. Here is a basic example you can start with:

import os
import cv2


path = "videos"

for file in os.listdir(path):
    print(file)

    cap = cv2.VideoCapture(path + '/' + file)
    count = 0

    while True:
        ret, frame = cap.read()

        # check for end of file
        if not ret:
            break

        # process frame
        count += 1

    print(f"{count} frames read")

    cap.release()
Markus
  • 5,976
  • 5
  • 6
  • 21