When Try For One video file . this is working but I need to display the unknown or non-fixed number of video files
I tried os.listdir for loop But I couldnt make it run in a function
I need to send different video paths when First video ends everytime with in order and in infinite loop
cap=VideoCapture(path)
while (cap.IsOpened()):
ret, frame = cap.read()
if ret:
cv2.imshow('frame',frame)
else:
print('Not found')
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
if(cv2.waitKey(1) & 0xFF == ord('q')):
break
cap.release()
cv2.destroyAllWindows()
Use glob.glob() to get the list of files you want to open.
Each time one video ends, you should close the VideoCapture and open a new one.
from glob import glob
videolist = sorted(glob(r"videoexamplefolder\*.mp4")) # modify as needed
for video in videolist:
cap=VideoCapture(video)
# ...
Related
I am trying to make this videocapture opencv2 python script allow me to do multiple video streams from my laptop cam and USB cams and I succeeded (with help of youtube) to do so only every time I add a camera I have to edit the line of code and add another videocapture line and another frame and another cv2.imshow. But I want to edit the video capture code in a way that allows me to stream as many cameras as detected without the need to add a line every time there is a camera using a loop. I'm obviously new here so accept my apologies if the solution is too simple.
This is the code that allows me to stream multiple cameras but with adding a line for each camera.
import urllib.request
import time
import numpy as np
import cv2
# Defining URL for camera
video_capture_0 = cv2.VideoCapture(0)
video_capture_1 = cv2.VideoCapture(1)
while True:
# Capture frame-by-frame
ret0, frame0 = video_capture_0.read()
ret1, frame1 = video_capture_1.read()
if (ret0):
# Display the resulting frame
cv2.imshow('Cam 0', frame0)
if (ret1):
# Display the resulting frame
cv2.imshow('Cam 1', frame1)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
video_capture_0.release()
video_capture_1.release()
cv2.destroyAllWindows()
I tried making a list camlist = [i for i in range(100)] and then adding that to a for loop that keeps adding it to videocapture. But I believe that's a mess so I deleted the code plus that doesn't seem so effective.
If you want to work with many cameras then first you should keep them on list - and then you can use for-loop to get all frame. And frames you should also keep on list so later you can use for-loop to display them. And finally you can use for-loop to release cameras
import cv2
#video_captures = [cv2.VideoCapture(x) for x in range(2)]
video_captures = [
cv2.VideoCapture(0),
#cv2.VideoCapture(1),
cv2.VideoCapture('https://imageserver.webcamera.pl/rec/krupowki-srodek/latest.mp4'),
cv2.VideoCapture('https://imageserver.webcamera.pl/rec/krakow4/latest.mp4'),
cv2.VideoCapture('https://imageserver.webcamera.pl/rec/warszawa/latest.mp4'),
]
while True:
results = []
for cap in video_captures:
ret, frame = cap.read()
results.append( [ret, frame] )
for number, (ret, frame) in enumerate(results):
if ret:
cv2.imshow(f'Cam {number}', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
for cap in video_captures:
cap.release()
cv2.destroyAllWindows()
And now if you even add new camera to list then you don't have to change rest of code.
But for many cameras it is good to run every camera in separated thread - but still keep all on lists.
Few days ago was question: How display multi videos with threading using tkinter in python?
I have to play a video from a particular frame, which i'm able to do with opencv, but how to play an audio of that particular video
I have the frame from where i have to start playing my video, i just want the corresponding audio too.
cap = cv2.VideoCapture('tcs.mp4')
fps = cap.get(cv2.CAP_PROP_FPS)
count = 0
success = True
while success:
success,frame = cap.read()
count+=1
ts = count/fps
if t == ts:
print("time stamp of current frame:",count/fps)
print("Press Q to quit")
f_count = count
cap.set(cv2.CAP_PROP_POS_FRAMES, f_count)
# Check if camera opened successfully
if (cap.isOpened()== False):
print("Error opening video file")
# Read until video is completed
while(cap.isOpened()):
# Capture frame-by-frame
ret, frame = cap.read()
if ret == True:
# Display the resulting frame
cv2.imshow('Frame', frame)
# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break
# Break the loop
else:
break
# When everything done, release
# the video capture object
cap.release()
# Closes all the frames
cv2.destroyAllWindows()
OpenCV is a library for computer vision. To play audio, you will have to use another library (i.e. in C++, one would use FFMPEG).
An alternative in Python is ffpyplayer (https://pypi.org/project/ffpyplayer/), which is based on FFMPEG.
You can use OpenCV to process the frame, and display the frame while concurrently playing the audio frame grabbed by ffpyplayer.
A better option would be to process and write the video file to disk in OpenCV, then play it with python-VLC.
I am using Python 3.5 and Opencv for an interactive video. However I can't figure out how to detect when my video finishes playing. Any ideas how I can detect when the video ends?
Thanks a bunch.
When ret is False, it means that the video is in the last frame.
Here is my code.You can try it.
import cv2
video_capture = cv2.VideoCapture("huge.mp4")
while True:
ret, frame = video_capture.read()
if ret:
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
video_capture.release()
cv2.destroyAllWindows()
check this link out. You can use the identifier CV_CAP_PROP_FRAME_COUNT to get the Number of frames in the video file.
Basically what I did was capture the video using cv2.videocapture in 'frame2' and extracted data from it and then closed the current video capture session using cap.release and then wanted to start a new video capture and use the previously extracted data from the first video capture .But it keeps lagging on the second video capture while pressing 'q'.
Like it should be capturing video after the first cap release as smoothly as it does in a normal video capture of a single session
Here's the code:
import cv2
import numpy as np
cap=cv2.VideoCapture(0)
x=0
while(x==0):
while(1):
#some functions
_,frame2 = cap.read()
cv2.imshow('frame2',frame2)
k=cv2.waitKey(0) & 0xFF
if k == ord('q'):
cv2.destroyWindow('frame2')
x=1
break
cap.release()
cap2=cv2.VideoCapture(0)
while(1):
_,frame = cap2.read()
#again some functions
cv2.imshow('frame',frame)
j=cv2.waitKey(0) & 0xFF
if j == ord('y'):
break
cv2.destroyAllWindows()
Well, it seems like my question had been asked many times before and unfortunately, no one replied. I hope someone will help.
I have an Easycap device that converts the analog images from my analog camera to digital signals through a USB port.
The device is identified to the system in the Device Manager under "Sound, Video and game controllers" category as "SMI Grabber Device".
I use a simple Python code to display the video from this device. I also have an embedded webcam in my laptop.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Display the resulting frame
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if cv2.waitKey(1) & 0xFF == ord('s'):
cv2.imwrite('screenshot.jpg',frame)
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
First, when I unplug the Easycap, CaptureVideo(0) returns the embedded webcam video stream. However, when I plug the Easycap, an error appears:
"Traceback (most recent call last):
File "C:\Users\DELL\Desktop\code\cam.py", line 10, in
cv2.imshow('frame',frame)
error: ......\src\opencv\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0"
Notice that, any number except 0 makes the program display the webcam image. So if I tried cap = cv2.CaptureVideo(1), it will show the webcam, cap = cv2.CaptureVideo(20) is the same.
I also tried to enter "SMI Grabber Device" instead of 0 or 1 in the VideoCaptureconstructor function, but it didn't make any difference.
I'm using Windows 8, and I've installed the accompanying driver for Easycap. The software that comes with the driver (called ULead) works fine and display the CCTV camera video. I tried to display the images while I'm closing that program, and without, the result is the same.
I used before a C# program with Aforge library which had getCamList method or something which allowed me to choose the specific device I want to display from a comboBox. I can't find a similar function is opencv.
I'm using OpenCV 2.4.6. I didn't try the code on prior versions.
I really need to understand why this code doesn't work, knowing that I'm just a very beginner of opencv and image processing.
I hope someone can help.
I am using EasyCAP too.
You must check that ret is True.
I am use below code
while True:
ret, frame = vc.read()
if ret:
break
cv2.waitKey(10)
h, w = frame.shape[:2]
print h, w
while True:
ret, frame = vc.read()
if ret:
cv2.imshow(WID, frame)
if cv2.waitKey(1) == 27:
break
Let there be light!
On serious note, I struggled with the same problem and I hope this helps!
the original thread + answer