简体   繁体   中英

Reading video files one by one from a directory in python

I am using Ubuntu 16.04 with opencv 3.1.0 and python 3.5. I installed it by using a tutorial in this link https://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/ . Then I tried to use this code to read video files and it worked once but then I did some changes which now I am not able to figure out. Because it worked once I don't think it will be a problem of FFmpeg installation which is suggested in most of the results I found. What can be the problem or is there any other way to read and then process video files one by one.

import numpy as np
import cv2
from imutils.video import FileVideoStream
from glob import glob

img_mask = '/home/esrt/Desktop/images/*.mov'
img_names = glob(img_mask)
for fn in img_names:
    start = '/home/esrt/Desktop/images/'
    s = fn
    print((s.split(start))[1])
    filename_1 = (s.split(start))[1]
    cap = cv2.VideoCapture(filename_1)
    while (True):
        ret, frame = cap.read()
        if ret== False:
            print("False")
            break
        else:
            cv2.imshow('frame',frame)
            key = cv2.waitKey(25)
            cap.release()
cv2.destroyAllWindows() 

Posting the error you received would help me determine the issue in your code. Though the loop below will allow you to access the video files you want to.

Put video_dir of where the files are located.

for entry in os.listdir(video_dir):
    if os.path.isfile(os.path.join(video_dir, entry)):
    print(entry)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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