Ask Your Question
1

How to create multiple VideoCapture Obejcts

asked 2016-09-26 05:09:07 -0600

tinku gravatar image

updated 2018-08-31 16:49:12 -0600

I wanted to create multiple VideoCapture Objects for stitching video from multiple cameras to a single video mashup.

for example: I have path for three videos that I wanted to be read using Video Capture object shown below to get the frames from individual videos,so they can be used for writing.

Expected:For N number of video paths

   cap0=cv2.VideoCapture(path1)
   cap1=cv2.VideoCapture(path2)
   cap2=cv2.VideoCapture(path3)
   .
   . 
   capn=cv2.VideoCapture(path4)

similarly I also wanted to create frame objects to read frames like

ret,frame0=cap0.read()
ret,frame1=cap1.read()
.
.
ret,frameN=capn.read()

I tried using for loop on the lists where the paths are stored but every time only one path is read and frames are stored for that particular video only.I have seen in many forums it is possible to create multiple capture objects in C++ but not in python in dynamic scenario where number of videos are not known before hand.

videoList=glob.glob(r'C:\Users\chaitanya\Desktop\Thesis\*.avi')
        frames=[]
            for path in videoList:
                indices=[]
                cap = cv2.VideoCapture(path)
                while(cap.isOpened()):
                    ret,frame=cap.read()
                    if not ret:
                        break
                    indices.append(cap.get(1))
                frames.append(indices)
    cap.release()
    cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

1

"I tried using for loop on the lists" -- so, if that was the problem, show us the code !

berak gravatar imageberak ( 2016-09-26 05:26:45 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-09-26 09:25:50 -0600

berak gravatar image

you need an array of videocaptures then, not a single one.

#
#step 1, set up the captures:
#
caps = []
videoList=glob.glob(r'C:\Users\chaitanya\Desktop\Thesis\*.avi')
for path in videoList:
    cap = cv2.VideoCapture(path)
    if not cap.isOpened():
        print "error opening ", path
    else:
        caps.append(cap)
#
#step 2, iterate through videoframes, collect images, and stitch them
#    
while True:
    frames = [] # frames for one timestep
    for cap in caps:
        ret,frame=cap.read()
        if not ret:
              return # game over
        frames.append(frame)

    # stitch a new frame from frames
    # append to output video
edit flag offensive delete link more

Comments

btw ,what's mean "glob.glob(r'C:\Users\chaitanya\Desktop\Thesis*.avi')"

xiaoerlaigeid gravatar imagexiaoerlaigeid ( 2016-10-30 02:13:45 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-09-26 05:09:07 -0600

Seen: 4,652 times

Last updated: Sep 26 '16