OpenCV replay saved stream and process itImage Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionImage Stitching from a live Video Stream in OpenCvStream processed opencv video to android applicationEfficient way to read and process video files with Qt and OpenCVpython opencv display time countdown using putText in webcam videoRecording Live OpenCV Processing on AndroidOpenCV Cython bridge leaking memoryOpenCV create Output StreamOpenCV: quick access to the columns of the image arrayOpenCV-Python: How to get latest frame from the live video stream or skip old ones.

Can RMSE and MAE have the same value?

How can I reorder triggered abilities in Arena?

How many birds in the bush?

"fF" letter combination seems to be typeset strangely or incorrectly

I don't have the theoretical background in my PhD topic. I can't justify getting the degree

Another solution to create a set with two conditions

Why do banks “park” their money at the European Central Bank?

Why isn't "I've" a proper response?

The Wires Underground

What is a natural problem in theory of computation?

Why is the UK so keen to remove the "backstop" when their leadership seems to think that no border will be needed in Northern Ireland?

Can a giant mushroom be used as a material to build watercraft or sailing ships?

Who was the most successful German spy against Great Britain in WWII, from the contemporary German perspective?

What are the occurences of total war in the Native Americans?

How to respectfully refuse to assist co-workers with IT issues?

Limitations with dynamical systems vs. PDEs?

Where does learning new skills fit into Agile?

European language movie, people enter a church but find they can't leave

Round towards zero

Are there any elected officials in the U.S. who are not legislators, judges, or constitutional officers?

"There were either twelve sexes or none."

How to gently end involvement with an online community?

How to find out the average duration of the peer-review process for a given journal?

Ordering a list of integers



OpenCV replay saved stream and process it


Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionImage Stitching from a live Video Stream in OpenCvStream processed opencv video to android applicationEfficient way to read and process video files with Qt and OpenCVpython opencv display time countdown using putText in webcam videoRecording Live OpenCV Processing on AndroidOpenCV Cython bridge leaking memoryOpenCV create Output StreamOpenCV: quick access to the columns of the image arrayOpenCV-Python: How to get latest frame from the live video stream or skip old ones.






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















My code processes a frame and it takes a couple of seconds to execute. If I'm streaming from a camera, I will naturally drop frames and get a frame every couple of seconds, right? I want to simulate the same thing in replaying a video file.



Normally, when you call vidcap.read(), you get the next frame in the video. This essentially slows down the video and does not miss a frame. This is not like processing a live camera stream. Is there a way to process the video file and drop frames during processing like when processing the camera stream?



The solution that comes to my mind is to keep track of time myself and call vidcap.set(cv2.CAP_PROP_POS_MSEC, currentTime) before each vidcap.read(). Is this how I should do it, or is there a better way?










share|improve this question






























    0















    My code processes a frame and it takes a couple of seconds to execute. If I'm streaming from a camera, I will naturally drop frames and get a frame every couple of seconds, right? I want to simulate the same thing in replaying a video file.



    Normally, when you call vidcap.read(), you get the next frame in the video. This essentially slows down the video and does not miss a frame. This is not like processing a live camera stream. Is there a way to process the video file and drop frames during processing like when processing the camera stream?



    The solution that comes to my mind is to keep track of time myself and call vidcap.set(cv2.CAP_PROP_POS_MSEC, currentTime) before each vidcap.read(). Is this how I should do it, or is there a better way?










    share|improve this question


























      0












      0








      0








      My code processes a frame and it takes a couple of seconds to execute. If I'm streaming from a camera, I will naturally drop frames and get a frame every couple of seconds, right? I want to simulate the same thing in replaying a video file.



      Normally, when you call vidcap.read(), you get the next frame in the video. This essentially slows down the video and does not miss a frame. This is not like processing a live camera stream. Is there a way to process the video file and drop frames during processing like when processing the camera stream?



      The solution that comes to my mind is to keep track of time myself and call vidcap.set(cv2.CAP_PROP_POS_MSEC, currentTime) before each vidcap.read(). Is this how I should do it, or is there a better way?










      share|improve this question














      My code processes a frame and it takes a couple of seconds to execute. If I'm streaming from a camera, I will naturally drop frames and get a frame every couple of seconds, right? I want to simulate the same thing in replaying a video file.



      Normally, when you call vidcap.read(), you get the next frame in the video. This essentially slows down the video and does not miss a frame. This is not like processing a live camera stream. Is there a way to process the video file and drop frames during processing like when processing the camera stream?



      The solution that comes to my mind is to keep track of time myself and call vidcap.set(cv2.CAP_PROP_POS_MSEC, currentTime) before each vidcap.read(). Is this how I should do it, or is there a better way?







      opencv






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 27 at 18:48









      Gazihan AlankusGazihan Alankus

      2,7661 gold badge15 silver badges26 bronze badges




      2,7661 gold badge15 silver badges26 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          1















          One approach is to keep track of the processing time and skip that amount of frames:



          import cv2, time, math
          # Capture saved video that is used as a stand-in for webcam
          cap = cv2.VideoCapture('/home/stephen/Desktop/source_vids/ss(6,6)_id_146.MP4')
          # Get the frames per second of the source video
          fps = 120
          # Iterate through video
          while True:
          # Record your start time for the frame
          start = time.time()
          # Read the frame
          _, img = cap.read()
          # Show the image
          cv2.imshow('img', img)
          # What ever processing that is going to slow things down should go here
          k = cv2.waitKey(0)
          if k == 27: break
          # Calculate the time it took to process this frame
          total = time.time() - start
          # Print out how many frames to skip
          print(total*fps)
          # Skip the frames
          for skip_frame in range(int(total*fps)): _, _ = cap.read()
          cv2.destroyAllWindows()


          This is probably better than nothing, but it does not correctly simulate the way that frames will be dropped. It appears that during processing, the webcam data is written to a buffer (until the buffer fills up). A better approach is to capture the video with a dummy process. This processor intensive dummy process will cause frames to be dropped:



          import cv2, time, math
          # Capture webcam
          cap = cv2.VideoCapture(0)
          # Create video writer
          vid_writer = cv2.VideoWriter('/home/stephen/Desktop/drop_frames.avi',cv2.VideoWriter_fourcc('M','J','P','G'),30, (640,480))
          # Iterate through video
          while True:
          # Read the frame
          _, img = cap.read()
          # Show the image
          cv2.imshow('img', img)
          k = cv2.waitKey(1)
          if k == 27: break
          # Do some processing to simulate your program
          for x in range(400):
          for y in range(40):
          for i in range(2):
          dummy = math.sqrt(i+img[x,y][0])
          # Write the video frame
          vid_writer.write(img)
          cap.release()
          cv2.destroyAllWindows()





          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55384508%2fopencv-replay-saved-stream-and-process-it%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1















            One approach is to keep track of the processing time and skip that amount of frames:



            import cv2, time, math
            # Capture saved video that is used as a stand-in for webcam
            cap = cv2.VideoCapture('/home/stephen/Desktop/source_vids/ss(6,6)_id_146.MP4')
            # Get the frames per second of the source video
            fps = 120
            # Iterate through video
            while True:
            # Record your start time for the frame
            start = time.time()
            # Read the frame
            _, img = cap.read()
            # Show the image
            cv2.imshow('img', img)
            # What ever processing that is going to slow things down should go here
            k = cv2.waitKey(0)
            if k == 27: break
            # Calculate the time it took to process this frame
            total = time.time() - start
            # Print out how many frames to skip
            print(total*fps)
            # Skip the frames
            for skip_frame in range(int(total*fps)): _, _ = cap.read()
            cv2.destroyAllWindows()


            This is probably better than nothing, but it does not correctly simulate the way that frames will be dropped. It appears that during processing, the webcam data is written to a buffer (until the buffer fills up). A better approach is to capture the video with a dummy process. This processor intensive dummy process will cause frames to be dropped:



            import cv2, time, math
            # Capture webcam
            cap = cv2.VideoCapture(0)
            # Create video writer
            vid_writer = cv2.VideoWriter('/home/stephen/Desktop/drop_frames.avi',cv2.VideoWriter_fourcc('M','J','P','G'),30, (640,480))
            # Iterate through video
            while True:
            # Read the frame
            _, img = cap.read()
            # Show the image
            cv2.imshow('img', img)
            k = cv2.waitKey(1)
            if k == 27: break
            # Do some processing to simulate your program
            for x in range(400):
            for y in range(40):
            for i in range(2):
            dummy = math.sqrt(i+img[x,y][0])
            # Write the video frame
            vid_writer.write(img)
            cap.release()
            cv2.destroyAllWindows()





            share|improve this answer





























              1















              One approach is to keep track of the processing time and skip that amount of frames:



              import cv2, time, math
              # Capture saved video that is used as a stand-in for webcam
              cap = cv2.VideoCapture('/home/stephen/Desktop/source_vids/ss(6,6)_id_146.MP4')
              # Get the frames per second of the source video
              fps = 120
              # Iterate through video
              while True:
              # Record your start time for the frame
              start = time.time()
              # Read the frame
              _, img = cap.read()
              # Show the image
              cv2.imshow('img', img)
              # What ever processing that is going to slow things down should go here
              k = cv2.waitKey(0)
              if k == 27: break
              # Calculate the time it took to process this frame
              total = time.time() - start
              # Print out how many frames to skip
              print(total*fps)
              # Skip the frames
              for skip_frame in range(int(total*fps)): _, _ = cap.read()
              cv2.destroyAllWindows()


              This is probably better than nothing, but it does not correctly simulate the way that frames will be dropped. It appears that during processing, the webcam data is written to a buffer (until the buffer fills up). A better approach is to capture the video with a dummy process. This processor intensive dummy process will cause frames to be dropped:



              import cv2, time, math
              # Capture webcam
              cap = cv2.VideoCapture(0)
              # Create video writer
              vid_writer = cv2.VideoWriter('/home/stephen/Desktop/drop_frames.avi',cv2.VideoWriter_fourcc('M','J','P','G'),30, (640,480))
              # Iterate through video
              while True:
              # Read the frame
              _, img = cap.read()
              # Show the image
              cv2.imshow('img', img)
              k = cv2.waitKey(1)
              if k == 27: break
              # Do some processing to simulate your program
              for x in range(400):
              for y in range(40):
              for i in range(2):
              dummy = math.sqrt(i+img[x,y][0])
              # Write the video frame
              vid_writer.write(img)
              cap.release()
              cv2.destroyAllWindows()





              share|improve this answer



























                1














                1










                1









                One approach is to keep track of the processing time and skip that amount of frames:



                import cv2, time, math
                # Capture saved video that is used as a stand-in for webcam
                cap = cv2.VideoCapture('/home/stephen/Desktop/source_vids/ss(6,6)_id_146.MP4')
                # Get the frames per second of the source video
                fps = 120
                # Iterate through video
                while True:
                # Record your start time for the frame
                start = time.time()
                # Read the frame
                _, img = cap.read()
                # Show the image
                cv2.imshow('img', img)
                # What ever processing that is going to slow things down should go here
                k = cv2.waitKey(0)
                if k == 27: break
                # Calculate the time it took to process this frame
                total = time.time() - start
                # Print out how many frames to skip
                print(total*fps)
                # Skip the frames
                for skip_frame in range(int(total*fps)): _, _ = cap.read()
                cv2.destroyAllWindows()


                This is probably better than nothing, but it does not correctly simulate the way that frames will be dropped. It appears that during processing, the webcam data is written to a buffer (until the buffer fills up). A better approach is to capture the video with a dummy process. This processor intensive dummy process will cause frames to be dropped:



                import cv2, time, math
                # Capture webcam
                cap = cv2.VideoCapture(0)
                # Create video writer
                vid_writer = cv2.VideoWriter('/home/stephen/Desktop/drop_frames.avi',cv2.VideoWriter_fourcc('M','J','P','G'),30, (640,480))
                # Iterate through video
                while True:
                # Read the frame
                _, img = cap.read()
                # Show the image
                cv2.imshow('img', img)
                k = cv2.waitKey(1)
                if k == 27: break
                # Do some processing to simulate your program
                for x in range(400):
                for y in range(40):
                for i in range(2):
                dummy = math.sqrt(i+img[x,y][0])
                # Write the video frame
                vid_writer.write(img)
                cap.release()
                cv2.destroyAllWindows()





                share|improve this answer













                One approach is to keep track of the processing time and skip that amount of frames:



                import cv2, time, math
                # Capture saved video that is used as a stand-in for webcam
                cap = cv2.VideoCapture('/home/stephen/Desktop/source_vids/ss(6,6)_id_146.MP4')
                # Get the frames per second of the source video
                fps = 120
                # Iterate through video
                while True:
                # Record your start time for the frame
                start = time.time()
                # Read the frame
                _, img = cap.read()
                # Show the image
                cv2.imshow('img', img)
                # What ever processing that is going to slow things down should go here
                k = cv2.waitKey(0)
                if k == 27: break
                # Calculate the time it took to process this frame
                total = time.time() - start
                # Print out how many frames to skip
                print(total*fps)
                # Skip the frames
                for skip_frame in range(int(total*fps)): _, _ = cap.read()
                cv2.destroyAllWindows()


                This is probably better than nothing, but it does not correctly simulate the way that frames will be dropped. It appears that during processing, the webcam data is written to a buffer (until the buffer fills up). A better approach is to capture the video with a dummy process. This processor intensive dummy process will cause frames to be dropped:



                import cv2, time, math
                # Capture webcam
                cap = cv2.VideoCapture(0)
                # Create video writer
                vid_writer = cv2.VideoWriter('/home/stephen/Desktop/drop_frames.avi',cv2.VideoWriter_fourcc('M','J','P','G'),30, (640,480))
                # Iterate through video
                while True:
                # Read the frame
                _, img = cap.read()
                # Show the image
                cv2.imshow('img', img)
                k = cv2.waitKey(1)
                if k == 27: break
                # Do some processing to simulate your program
                for x in range(400):
                for y in range(40):
                for i in range(2):
                dummy = math.sqrt(i+img[x,y][0])
                # Write the video frame
                vid_writer.write(img)
                cap.release()
                cv2.destroyAllWindows()






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 27 at 22:26









                Stephen MeschkeStephen Meschke

                1,4204 silver badges15 bronze badges




                1,4204 silver badges15 bronze badges





















                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55384508%2fopencv-replay-saved-stream-and-process-it%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript