|
| 1 | +#include <fstream> |
| 2 | +#include <iostream> |
| 3 | +#include <string> |
| 4 | + |
| 5 | +// to display |
| 6 | +#include <opencv2/core.hpp> |
| 7 | +#include <opencv2/imgproc.hpp> |
| 8 | +#include <opencv2/highgui.hpp> |
| 9 | + |
| 10 | +// ion |
| 11 | +#include <ion/ion.h> |
| 12 | +#include "ion-bb-core/bb.h" |
| 13 | +#include "ion-bb-core/rt.h" |
| 14 | +#include "ion-bb-image-io/bb.h" |
| 15 | +#include "ion-bb-image-io/rt.h" |
| 16 | +#include "ion-bb-image-processing/bb.h" |
| 17 | +#include "ion-bb-image-processing/rt.h" |
| 18 | + |
| 19 | +#include "ion-bb-image-io/ghc/filesystem.hpp" |
| 20 | + |
| 21 | +using namespace ion; |
| 22 | + |
| 23 | +void display_and_save(int32_t width, int32_t height, std::string directory_path, rawHeader header_info){ |
| 24 | + |
| 25 | + Builder b; |
| 26 | + b.set_target(Halide::get_host_target()); |
| 27 | + |
| 28 | + Port dispose{ "dispose", Halide::type_of<bool>() }; |
| 29 | + Port gain0_p{ "gain0", Halide::type_of<int32_t>() }; |
| 30 | + Port gain1_p{ "gain1", Halide::type_of<int32_t>() }; |
| 31 | + Port exposure0_p{ "exposure0", Halide::type_of<int32_t>() }; |
| 32 | + Port exposure1_p{ "exposure1", Halide::type_of<int32_t>() }; |
| 33 | + Port wp{ "width", Halide::type_of<int32_t>() }; |
| 34 | + Port hp{ "height", Halide::type_of<int32_t>() }; |
| 35 | + |
| 36 | + // obtain sensor images |
| 37 | + auto n = b.add("u3v_camera")(gain0_p, gain1_p, exposure0_p, exposure1_p) |
| 38 | + .set_param( |
| 39 | + Param{"pixel_format_ptr", "Mono12"}, |
| 40 | + Param{"num_sensor", "2"}, |
| 41 | + Param{"frame_sync", "true"}, |
| 42 | + Param{"gain_key", "SensorGain"}, |
| 43 | + Param{"exposure_key", "SensorShutter"} |
| 44 | + ); |
| 45 | + Port lp = n["output0"]; |
| 46 | + Port rp = n["output1"]; |
| 47 | + Port fcp = n["frame_count"]; |
| 48 | + |
| 49 | + n = b.add("image_io_binarysaver")(rp, lp, fcp, dispose, wp, hp).set_param( |
| 50 | + Param{"output_directory", directory_path}, |
| 51 | + Param{"fps", "60.0"}); |
| 52 | + Port terminator = n["output"]; |
| 53 | + |
| 54 | + // TODO replace the following section (bayer2BGR) |
| 55 | + // with the exisiting BBs |
| 56 | + n = b.add("image_io_bayer2bgr")(lp, wp, hp); |
| 57 | + lp = n["output"]; |
| 58 | + n = b.add("image_io_bayer2bgr")(rp, wp, hp); |
| 59 | + rp = n["output"]; |
| 60 | + n = b.add("core_reorder_buffer_3d_uint8")(lp).set_param(Param{"dim0", "1"}, Param{"dim1", "2"}, Param{"dim2", "0"}); |
| 61 | + lp = n["output"]; |
| 62 | + n = b.add("core_reorder_buffer_3d_uint8")(rp).set_param(Param{"dim0", "1"}, Param{"dim1", "2"}, Param{"dim2", "0"}); |
| 63 | + rp = n["output"]; |
| 64 | + |
| 65 | + // display images |
| 66 | + n = b.add("image_io_gui_display")(lp).set_param( |
| 67 | + Param{"idx", "0"}, |
| 68 | + Param{"width", std::to_string(width)}, |
| 69 | + Param{"height", std::to_string(height)} |
| 70 | + ); |
| 71 | + Port display_output0_p = n["output"]; |
| 72 | + n = b.add("image_io_gui_display")(rp).set_param( |
| 73 | + Param{"idx", "1"}, |
| 74 | + Param{"width", std::to_string(width)}, |
| 75 | + Param{"height", std::to_string(height)}); |
| 76 | + Port display_output1_p = n["output"]; |
| 77 | + |
| 78 | + PortMap pm; |
| 79 | + /* input */ |
| 80 | + pm.set(dispose, false); |
| 81 | + pm.set(wp, width); |
| 82 | + pm.set(hp, height); |
| 83 | + |
| 84 | + /* output */ |
| 85 | + Halide::Buffer<int> out0 = Halide::Buffer<int>::make_scalar(); |
| 86 | + Halide::Buffer<int> out1 = Halide::Buffer<int>::make_scalar(); |
| 87 | + pm.set(display_output0_p, out0); |
| 88 | + pm.set(display_output1_p, out1); |
| 89 | + |
| 90 | + Halide::Buffer<int32_t> out = Halide::Buffer<int32_t>::make_scalar(); |
| 91 | + pm.set(terminator, out); |
| 92 | + |
| 93 | + int32_t gain0 = 0; |
| 94 | + int32_t gain1 = 480; |
| 95 | + int32_t exposure0 = 1000; |
| 96 | + int32_t exposure1 = 1000; |
| 97 | + |
| 98 | + for (int i=0; i< 400; ++i) { |
| 99 | + pm.set(gain0_p, gain0++); |
| 100 | + pm.set(gain1_p, gain1--); |
| 101 | + pm.set(exposure0_p, exposure0); |
| 102 | + pm.set(exposure1_p, exposure1); |
| 103 | + b.run(pm); |
| 104 | + } |
| 105 | + |
| 106 | + pm.set(dispose, true); |
| 107 | + b.run(pm); |
| 108 | + |
| 109 | + cv::destroyAllWindows(); |
| 110 | +} |
| 111 | + |
| 112 | +void open_and_check(int32_t& width, int32_t& height, const ghc::filesystem::path output_directory, uint32_t& file_idx, std::ifstream& ifs, bool *finished) { |
| 113 | + auto file_path = output_directory / ("raw-" + ::std::to_string(file_idx++) + ".bin"); |
| 114 | + |
| 115 | + ifs = ::std::ifstream(file_path, ::std::ios::binary); |
| 116 | + if (ifs.fail()) { |
| 117 | + *finished = true; |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + int32_t version = 0; |
| 122 | + ifs.read(reinterpret_cast<char*>(&version), sizeof(int32_t)); |
| 123 | + ifs.read(reinterpret_cast<char*>(&width), sizeof(int32_t)); |
| 124 | + ifs.read(reinterpret_cast<char*>(&height), sizeof(int32_t)); |
| 125 | + |
| 126 | + ifs = ::std::ifstream(file_path, ::std::ios::binary); |
| 127 | + |
| 128 | + // skip header (size is 512) |
| 129 | + ifs.seekg(512, ::std::ios_base::beg); |
| 130 | +} |
| 131 | + |
| 132 | +bool load_header_file(ghc::filesystem::path output_directory, rawHeader header_info) |
| 133 | +{ |
| 134 | + std::ifstream ifs; |
| 135 | + int width_, height_; |
| 136 | + bool finished_ = false; |
| 137 | + uint32_t file_idx_ = 0; |
| 138 | + |
| 139 | + // first look |
| 140 | + open_and_check(width_, height_, output_directory, file_idx_, ifs, &finished_); |
| 141 | + if (finished_) { return false; } |
| 142 | + |
| 143 | + bool ret = true; |
| 144 | + ret = ret && width_ == header_info.width_; |
| 145 | + ret = ret && height_ == header_info.height_; |
| 146 | + |
| 147 | + auto log_filename = output_directory / "frame_log.txt"; |
| 148 | + std::cout << "log written in " << log_filename << std::endl; |
| 149 | + std::ofstream ofs(log_filename, std::ofstream::out); |
| 150 | + ofs << width_ << "x" << height_ << "\n"; |
| 151 | + |
| 152 | + /* there's no audio recording feature yet so offset != 0 */ |
| 153 | + uint32_t offset_frame_count; |
| 154 | + const size_t size = static_cast<size_t>(width_ *height_*sizeof(uint16_t)); |
| 155 | + |
| 156 | + // first frame count |
| 157 | + ifs.read(reinterpret_cast<char *>(&offset_frame_count), sizeof(offset_frame_count)); |
| 158 | + ifs.seekg(2*size, std::ios::cur); |
| 159 | + |
| 160 | + uint32_t frame_index = offset_frame_count; |
| 161 | + ofs << "offset_frame_count: " << offset_frame_count << "\n"; |
| 162 | + |
| 163 | + uint32_t frame_count = frame_index; |
| 164 | + ofs << frame_index++ << " : " << frame_count << "\n"; |
| 165 | + |
| 166 | + uint skip_count = 0; |
| 167 | + |
| 168 | + while (!finished_) { |
| 169 | + ifs.read(reinterpret_cast<char *>(&frame_count), sizeof(frame_count)); |
| 170 | + while( frame_index < frame_count ){ |
| 171 | + ofs << frame_index++ << " : x" << "\n"; |
| 172 | + ++skip_count; |
| 173 | + } |
| 174 | + ofs << frame_index++ << " : " << frame_count << "\n"; |
| 175 | + ifs.seekg(2 * size, std::ios::cur); |
| 176 | + |
| 177 | + // rotate |
| 178 | + ifs.peek(); |
| 179 | + if (ifs.eof()) { |
| 180 | + open_and_check(width_, height_, output_directory, file_idx_, ifs, &finished_); |
| 181 | + } else if (ifs.fail()) { |
| 182 | + throw std::runtime_error("Invalid to acccess file."); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + uint total_frame = frame_count - offset_frame_count; |
| 187 | + std::cout << (total_frame-skip_count)*1.0 / total_frame << std::endl; |
| 188 | + ofs << (total_frame-skip_count)*1.0 / total_frame << "\n"; |
| 189 | + ofs.close(); |
| 190 | + |
| 191 | + ifs.close(); |
| 192 | + return ret; |
| 193 | +} |
| 194 | + |
| 195 | +int main() { |
| 196 | + int32_t width = 1920; |
| 197 | + int32_t height = 1080; |
| 198 | + float fps = 60.0f; |
| 199 | + |
| 200 | + ghc::filesystem::path test_directory = "u3v_framerate_test"; |
| 201 | + std::string output_directory_prefix = "u3v_framerate_test"; |
| 202 | + |
| 203 | + if(! ghc::filesystem::is_directory(test_directory)){ |
| 204 | + bool ret = ghc::filesystem::create_directory(test_directory); |
| 205 | + } |
| 206 | + |
| 207 | + rawHeader header_info = { |
| 208 | + 0, width, height, |
| 209 | + 0.5f, 1.0f, 1.5f, 1.2f, 2.2f, 0.2f, |
| 210 | + 0, 0, 0, 0, width, height, width, height, fps}; |
| 211 | + |
| 212 | + for (int i = 0; i < 50; ++i){ |
| 213 | + ghc::filesystem::path output_directory = test_directory / (output_directory_prefix + std::to_string(i)); |
| 214 | + if(! ghc::filesystem::is_directory(output_directory)){ |
| 215 | + bool ret = ghc::filesystem::create_directory(output_directory); |
| 216 | + } |
| 217 | + display_and_save(width, height, output_directory, header_info); |
| 218 | + bool ret = load_header_file(output_directory, header_info); |
| 219 | + |
| 220 | + if (!ret){ |
| 221 | + std::runtime_error("header info is incorrect at test " + std::to_string(i) ); |
| 222 | + } |
| 223 | + } |
| 224 | + return 0; |
| 225 | +} |
0 commit comments