Playback option in OpenCV videos

I am trying to create a play loop option for OpenCV video . My program uses Python multiprocessing and has a button for sending loopswitch calls via queue4 to enable or disable the loop option. My particular problem is that my video freezes in the last frame, and I would like to know if the vidFile.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, 1) line of the vidFile.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, 1) method is used cv2.VideoCapture.set() , and, indeed, you need to return the video to frame 1 and play it (as I think it should).

EDIT

After reviewing my code, it now launches a C ++ error at runtime, but no other instructions are given. According to this answer, it seems that using cv2.VideoCapture.set() to jump between frames is an error. Could anyone do this?

Thanks,

My code for the capture process ( queue and queue2 are input and output queues):

 def image_capture(queue, con, queue2, queue4): videopath = con.recv() vidFile = cv2.VideoCapture(videopath) fps = vidFile.get(cv2.cv.CV_CAP_PROP_FPS) waitframe = 1/fps con.send(waitframe)#sending waitkey duration through pipe to update_image() loopswitch = False #init for playing video in a loop while True: if queue4.empty(): pass else: queueval = queue4.get() if queueval=='loop': if loopswitch==False: loopswitch = True elif loopswitch==True: loopswitch = False try: flag, frame=vidFile.read() if flag==0: if loopswitch==False: queue2.put(None) break elif loopswitch==True: vidFile.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, 1) continue else: queue2.put(frame) cv2.waitKey(waitframe) except: continue 
+4
source share
3 answers

I partially solved it by replacing vidFile.set (cv2.cv.CV_CAP_PROP_POS_FRAMES, 1) with vidFile.set(cv2.cv.CV_CAP_PROP_POS_AVI_RATIO, 0) , although this only works for .avi files.

+2
source

I can get loop playback using the if statement when the number of frames reaches cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT) and then reset the number of frames and cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, num) to the same value. In the example below, the video recording loop is for me.

 import cv2 cap = cv2.VideoCapture('path/to/video') frame_counter = 0 while(True): # Capture frame-by-frame ret, frame = cap.read() frame_counter += 1 #If the last frame is reached, reset the capture and the frame_counter if frame_counter == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT): frame_counter = 0 #Or whatever as long as it is the same as next line cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, 0) # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the resulting frame cv2.imshow('frame',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows() 

It also works to return a video instead of resetting the number of frames:

 if frame_counter == cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT): frame_counter = 0 cap = cv2.VideoCapture(video_name) 

So at least for me use cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES, num) to record video. What happens if you reset to frame zero instead of the first (for example, using the avi method)?

+1
source

For python3, opencv3.1.0, raspberry pi 3

 import numpy as np import cv2 cap = cv2.VideoCapture('intro.mp4') while(cap.isOpened()): ret, frame = cap.read() #cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN) #cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN) if ret: cv2.imshow("Image", frame) else: print('no video') cap.set(cv2.CAP_PROP_POS_FRAMES, 0) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() 
0
source

Source: https://habr.com/ru/post/1486726/


All Articles