I have a video URL list I want to open / process using OPenCV. Example list below
cam_name,address
Cam_01,vid1.mp4
Cam_02,vid1.mp4
Cam_03,vid1.mp4
I want to open the first camera/address, stream for 60 seconds, release and start the second, release and so on. When get to the last stream in the list e.g. Cam_03, start the list again, back to Cam_01.
I dont need to Thread, a I just need to start a stream and stop after a period of time etc.
Code so far:
# this calls the OpenCV stream
def main(cam_name, camID):
main_bret(cam_name, camID)
df = 'cams_list.txt'
df = pd.read_csv(df)
for index, row in df.iterrows():
main(row['cam_name'], row['address'])
########################################
main_bret(cam_name, camID):
vid = cv2.VideoCapture(camID)
while(True):
ret, frame = vid.read()
cv2.imshow(cam_name, frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
vid.release()
cv2.destroyAllWindows()
How to I construct the loop to stream for 60 seconds, release and move to the next in the list.. and repeat when finished the list?
What is happing now, e.g. if just streaming a mp4, will finish the video, then start the next. Just need it to stop streaming after 60 seconds.
I have tried adding sleep() etc around cv2.waitKey(1) but it runs everything for that period, not individual streams. I guess I am not putting the sleep in the correct place.
tks
CodePudding user response:
I'm not sure I understood every detail of your program, but this is what I have:
df = 'cams_list.txt'
df = pd.read_csv(df)
def main_bret(cam_name, camID):
vid = cv2.VideoCapture(camID)
ret, frame = vid.read()
cv2.imshow(cam_name, frame)
cv2.waitKey(60_000)
vid.release()
cv2.destroyAllWindows()
index = 0
upper_index = df.shape[0]
while True:
main_bret(df.iloc[index]["cam_name"], df.iloc[index]["address"])
index = index 1
if index >= upper_index:
index = 0
- It's easier to iterate over and over through a list with a while so I get rid of the for loop.
- The "waitKey" function waits 60 seconds (60000 ms) and then it deletes the window.
I hope I was helpful.