blob: a2c5f0e7e4b02b15ba460a492ed3754ee0b95c82 [file] [log] [blame]
Alex Perrye9775c32019-03-09 10:48:57 -08001#include <fstream>
2#include <sys/stat.h>
3
4#include "y2019/vision/image_writer.h"
5
6namespace y2019 {
7namespace vision {
8
9void ImageWriter::WriteImage(::aos::vision::DataRef data) {
10 LOG(INFO, "Writing image %d", image_count_);
11 std::ofstream ofs(
12 dir_path_ + file_prefix_ + std::to_string(image_count_) + ".yuyv",
13 std::ofstream::out);
14 ofs << data;
15 ofs.close();
16 ++image_count_;
17}
18
19void ImageWriter::ProcessImage(::aos::vision::DataRef data,
20 size_t num_targets) {
21 ++debounce_count_;
22 if (debounce_count_ < 10) {
23 return;
24 }
25 // Write the image if there are fewer targets in this frame than the last.
26 if (num_targets < previous_num_targets_) {
27 WriteImage(previous_image_);
28 WriteImage(data);
29 debounce_count_ = 0;
30 }
31 //data.swap(previous_image_);
32 data.copy(previous_image_, sizeof(previous_image_));
33}
34
35void ImageWriter::SetDirPath() {
36 std::string base_path = "/jevois/data/run_";
37 for (int i = 0;; ++i) {
38 struct stat st;
39 std::string option = base_path + std::to_string(i);
40 if (stat(option.c_str(), &st) != 0) {
41 file_prefix_ = option + "/";
42 LOG(INFO, "Writing to %s\n", file_prefix_.c_str());
43 mkdir(file_prefix_.c_str(), 0777);
44 break;
45 }
46 }
47}
48
49} // namespace vision
50} // namespace y2019