Skip to content

Commit

Permalink
Merge PR #203 from felipe-m/fps
Browse files Browse the repository at this point in the history
Include fps analysis in verilator simulation
  • Loading branch information
felipe-m authored Oct 8, 2023
2 parents b3ad99e + 15d8240 commit bf17d62
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 4 deletions.
Binary file added sim_fpga/tutorial/tut03/imgs/fps.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions sim_fpga/tutorial/tut03/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ The graphical interface allows debugging more easily. For example, the next scre

![2nd camera frame](imgs/camera_proc_06_sm.png)

## Frames per second (FPS)

Information about the frames per second processed is at the bottom, it includes two methods for calculating it.
At this frame size (160x120) it gets around 6 fps, in [tutorial 4](../tut04) the frame size is 640x480, and the frame rate is around 2 fps.

![Screenshot with frame period and fps](imgs/fps.jpg)





Expand Down
24 changes: 22 additions & 2 deletions sim_fpga/tutorial/tut03/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <SDL_opengl.h>
#include <assert.h>
#include <stdio.h>
#include <chrono> // to measure fps

#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
Expand Down Expand Up @@ -437,14 +438,22 @@ int main(int argc, char **argv) {
// -------------------- Init Video Input
cv::Mat input_feed;
cv::Mat resized_input_feed;
// start default camera
cv::VideoCapture cap(0);

double timed_fps;
unsigned int millis_elapsed = 0;

// get opencv frame per seconds (FPS)
double cam_fps = cap.get(cv::CAP_PROP_FPS);

if (!cap.isOpened()) {
std::cout << "cannot open camera";
}

cap >> input_feed;
auto time_capture = std::chrono::high_resolution_clock::now();
auto old_time_capture = std::chrono::high_resolution_clock::now();
cap >> input_feed; // capture image from camera
cv::resize(input_feed,resized_input_feed,cv::Size(IMG_COLS,IMG_ROWS),cv::INTER_LINEAR);

// init dut, tracing and sim elements
Expand Down Expand Up @@ -529,7 +538,9 @@ int main(int argc, char **argv) {
step_n_cycles = frames_per_iteration * IMG_PXLS;
}

cap >> input_feed;
old_time_capture = time_capture; // save old capture
time_capture = std::chrono::high_resolution_clock::now(); // new time capture
cap >> input_feed; // capture image from camera
cv::resize(input_feed,resized_input_feed,cv::Size(IMG_COLS,IMG_ROWS),cv::INTER_LINEAR);

// assert(input_feed.channels() == 3 && input_feed.cols == cols &&
Expand Down Expand Up @@ -680,8 +691,17 @@ int main(int argc, char **argv) {
ImGui::Text("Centroid: 0x%x - Proximity: %i", centroid, proximity);
//ImGui::SameLine();


// the 1st capture will be wrong, but just only the first
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
time_capture - old_time_capture);
millis_elapsed = elapsed.count();
timed_fps = 1000.0 / millis_elapsed;
ImGui::Text("Timed %i ms (%.1f FPS)", millis_elapsed, timed_fps);
ImGui::Text("Application average %.3f ms/frame (%.1f FPS)",
1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);

ImGui::Text("Maximum camera fps %.1f", cam_fps);
ImGui::End();
}

Expand Down
Binary file added sim_fpga/tutorial/tut04/imgs/fps_sm.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions sim_fpga/tutorial/tut04/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ The color processing is the same as in tutorial 3 and 2 (only changing some gene

* [../../../phys_fpga/ulx3s/apio/ov7670x2_colorcentroid_160x120/color_proc.v](../../../phys_fpga/ulx3s/apio/ov7670x2_colorcentroid_160x120/color_proc.v)

Frames per second has been included, they can be seen at the bottom of the window.
It includes two measurement methods and the maximum fps reported by opencv.
With this resolution around 2 frames per second can be achieved.

Note that in [tutorial 3](../tut03) the frame size is 160x120, and the frame rate was above 6 fps.

![Screenshot with frame period and fps](imgs/fps_sm.jpg)

The rest is the same as in tutorial 3.


Expand Down
25 changes: 23 additions & 2 deletions sim_fpga/tutorial/tut04/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <SDL_opengl.h>
#include <assert.h>
#include <stdio.h>
#include <chrono> // to measure fps

#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
Expand Down Expand Up @@ -435,14 +436,22 @@ int main(int argc, char **argv) {
// -------------------- Init Video Input
cv::Mat input_feed;
cv::Mat resized_input_feed;
// start default camera
cv::VideoCapture cap(0);

double timed_fps;
unsigned int millis_elapsed = 0;

// get opencv frame per seconds (FPS)
double cam_fps = cap.get(cv::CAP_PROP_FPS);

if (!cap.isOpened()) {
std::cout << "cannot open camera";
}

cap >> input_feed;
auto time_capture = std::chrono::high_resolution_clock::now();
auto old_time_capture = std::chrono::high_resolution_clock::now();
cap >> input_feed; // capture image from camera
cv::resize(input_feed,resized_input_feed,cv::Size(IMG_COLS,IMG_ROWS),cv::INTER_LINEAR);

// init dut, tracing and sim elements
Expand Down Expand Up @@ -527,7 +536,9 @@ int main(int argc, char **argv) {
step_n_cycles = frames_per_iteration * IMG_PXLS;
}

cap >> input_feed;
old_time_capture = time_capture; // save old capture
time_capture = std::chrono::high_resolution_clock::now(); // new time capture
cap >> input_feed; // capture image from camera
cv::resize(input_feed,resized_input_feed,cv::Size(IMG_COLS,IMG_ROWS),cv::INTER_LINEAR);

// assert(input_feed.channels() == 3 && input_feed.cols == cols &&
Expand Down Expand Up @@ -678,8 +689,18 @@ int main(int argc, char **argv) {
ImGui::Text("Centroid: 0x%x - Proximity: %i", centroid, proximity);
//ImGui::SameLine();

// the 1st capture will be wrong, but just only the first
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
time_capture - old_time_capture);
millis_elapsed = elapsed.count();
timed_fps = 1000.0 / millis_elapsed;
ImGui::Text("Timed %i ms (%.1f FPS)", millis_elapsed, timed_fps);

ImGui::Text("Application average %.3f ms/frame (%.1f FPS)",
1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);

ImGui::Text("Maximum camera fps %.1f", cam_fps);

ImGui::End();
}

Expand Down

0 comments on commit bf17d62

Please sign in to comment.