Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #246 from jtwhit/frame_write
Browse files Browse the repository at this point in the history
[cv] Switched frame writing to its own thread and reduced its frequency.
  • Loading branch information
jeqyin authored May 26, 2019
2 parents cc1b525 + e2fa206 commit 6fb6d2b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
33 changes: 9 additions & 24 deletions onboard/cv/main.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "perception.hpp"
#include <unistd.h>
#include <thread>

using namespace cv;
using namespace std;
Expand Down Expand Up @@ -55,14 +56,12 @@ void disk_record_init() {
#endif
}

void write_curr_frame_to_disk(Mat &rgb, Mat & depth, int counter ) {
string fileName = to_string(counter);
void write_curr_frame_to_disk(Mat rgb, Mat depth, int counter) {
string fileName = to_string(counter / FRAME_WRITE_INTERVAL);
while(fileName.length() < 4){
fileName = '0'+fileName;
}
cv::imwrite(rgb_foldername + fileName + std::string(".jpg"), rgb );
//std::string file_str = std::string("depth_") + std::to_string(counter);// + std::string(".jpg");

cv::imwrite(depth_foldername + fileName + std::string(".exr"), depth );
}

Expand All @@ -86,26 +85,21 @@ int main() {
obstacleMessage.detected = false;

int tennisBuffer = 0;

while (true) {
if (!cam_grab_succeed(cam, counter_fail)) break;

auto start = chrono::high_resolution_clock::now();
Mat src = cam.image();

#if PERCEPTION_DEBUG
// imshow("image", src);
#endif

Mat depth_img = cam.depth();

// write to disk if permitted
#if WRITE_CURR_FRAME_TO_DISK
write_curr_frame_to_disk(src, depth_img, j );
if (j % FRAME_WRITE_INTERVAL == 0) {
Mat rgb_copy = src.clone(), depth_copy = depth_img.clone();
thread write_thread(write_curr_frame_to_disk, rgb_copy, depth_copy, j);
write_thread.detach();
}
#endif



/* Tennis ball detection*/
tennisMessage.found = false;
#if TB_DETECTION
Expand Down Expand Up @@ -163,15 +157,6 @@ int main() {
waitKey(FRAME_WAITKEY);
#endif

auto end = chrono::high_resolution_clock::now();

auto delta = chrono::duration_cast<chrono::duration<double>>(end - start);
frame_time += delta.count();
#if PERCEPTION_DEBUG
if(j % 100 == 0){
// cout << "framerate: " << 1.0f/(frame_time/j) << endl;
}
#endif
j++;
}

Expand Down
2 changes: 2 additions & 0 deletions onboard/cv/perception.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#define PI 3.14159265
const float inf = -std::numeric_limits<float>::infinity();

const int FRAME_WRITE_INTERVAL = 10; // How many frames between writing to disk

#if ZED_SDK_PRESENT
const int FRAME_WAITKEY = 1; // for cv::imshow, set to 1 if using zed, set to 0 if offline test
#else
Expand Down

0 comments on commit 6fb6d2b

Please sign in to comment.