Home > Software design >  How do I call a function "x" amount of times in python? Using a for loop?
How do I call a function "x" amount of times in python? Using a for loop?

Time:12-07

I have a cap.read() function where I am reading in frames from a video. The first call of the function is the zeroth frame, the second call is the 1st frame, etc... I am trying to call the function 1200 because I need to start my read-in at the 1200th frame.

Right now this is what I have, but I know it is incorrect.

CodePudding user response:

The direct answer to your question was answered by @Shmack in the comments. The code is simply

for i in range(1200):
    cap.read()

Given that your using the variable cap, I suspect that your using the OpenCV module. If that is the case, then you can simply set the frame you want to start at by using cap.set(1, 1200)

The .set function takes in its first parameter as the identifier, and for your case, you can simply leave that as 1. The second parameter is the frame you wish to start at. You can read more about cap.set() here

  • Related