Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

python - How to create multiple VideoCapture Objects

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. This is my code until now

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()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm not a python programmer, but probably the solution is something like:

frames = []
caps = []
for path in videoList:
    caps.append(cv2.VideoCapture(path))

for cap in caps:
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        frames.append(frame)

# now "frames" holds your captured images.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...