[英]how can I play multiple videos simultaneously using opencv in python?
Hi guys I am stuck at this point. 大家好,我被困在这一点上。 I want to play four videos on my screen using opencv . 我想使用opencv在屏幕上播放四个视频。 Can anyone help me how to do that? 谁能帮我怎么做? Suppose I want to play simultaneously 假设我想同时玩
I am referring following code. 我指的是以下代码。 It plays very well for single avi file. 对于单个avi文件,它的播放效果非常好。 Is it necessary to concatenate or i can run in four different windows?. 是否有必要串联或我可以在四个不同的窗口中运行? any suggestions are welcome import cv2 import numpy as np 任何建议都可以导入cv2 import numpy as np
# Create a VideoCapture object and read from input file
# If the input is the camera, pass 0 instead of the video file name
cap = cv2.VideoCapture('first.avi')
cap2 =cv2.VideoCapture('second.avi')
if (cap.isOpened()== False):
print("Error opening video stream or file")
if (cap2.isOpened()== False):
print("Error opening video stream or file")
while(cap.isOpened()||cap2.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
ret, frame1 = cap2.read()
if ret == True:
# Display the resulting frame
cv2.imshow('Frame',frame)
cv2.imshow('Frame', frame1)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
cap.release()
cap2.release()
cv2.destroyAllWindows()
For playing multiple videos, we have to use unique window titles for each video. 要播放多个视频,我们必须为每个视频使用唯一的窗口标题。 Here is a sample code demonstrating how it can be achieved. 这是示例代码,演示如何实现。
import numpy as np
import cv2
names = ['first.avi', 'second.avi', 'third.avi', 'fourth.avi'];
window_titles = ['first', 'second', 'third', 'fourth']
cap = [cv2.VideoCapture(i) for i in names]
frames = [None] * len(names);
gray = [None] * len(names);
ret = [None] * len(names);
while True:
for i,c in enumerate(cap):
if c is not None:
ret[i], frames[i] = c.read();
for i,f in enumerate(frames):
if ret[i] is True:
gray[i] = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
cv2.imshow(window_titles[i], gray[i]);
if cv2.waitKey(1) & 0xFF == ord('q'):
break
for c in cap:
if c is not None:
c.release();
cv2.destroyAllWindows()
PS: This code is just a quick and dirty example for demo purpose only. PS:这段代码只是一个简单又肮脏的示例,仅用于演示目的。 Tested with python 2 and OpenCV 3.2 on Ubuntu 14.04. 在Ubuntu 14.04上使用python 2和OpenCV 3.2进行了测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.