Alright all, so I’ve worked on a new piece of code that was originally written in Python, but i had translated to Java. So, the code shows up with zero errors, however… the framerate has still not been printed! I’ve been traversing the internet for a reason as to why this is the case. Could anybody explain to me as to why
import org.opencv.core.Mat;
import org.opencv.videoio.VideoCapture;
import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
public static void main (String[] args) {
//Pass your video capture argument
VideoCapture cap = new VideoCapture(0);
printFps(cap);
}
// Takes an OpenCV VideoCapture as an argument.
public static void printFps(VideoCapture capture) {
// Completely arbitrary number of frames but 120 is a decent sample.
int num_frames = 120;
// Declare variables for timekeeping.
// time1 = where the sample start time will be stored
// time2 = where the sample end time will be stored
OffsetDateTime time1, time2;
// Create a new Matrix (OpenCV's variable for storing a video frame)
Mat frame = new Mat();
// Print out a message indicating frames are
// starting to be captured now.
System.out.println("Capturing frames now.");
// Save the current time in the first variable.
time1 = OffsetDateTime.now();
// Capture a number of frames
for (int index = 0; index < num_frames; ++index) {
// Store the frame in the frame variable
capture.read(frame);
}
// When this operation finishes, store the current time in the second variable.
time2 = OffsetDateTime.now();
// Calculate the time that passed during the captures
long secondsBetween = ChronoUnit.SECONDS.between(time2, time1);
// Calculate frames per second
int fps = num_frames / (int) secondsBetween;
Log.i(TAG, "Estimated FPS: " + fps);
// Release the VideoCapture object
capture.release();
}
EDIT: added code to call the printFps
function