Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame^] | 1 | #include <fstream> |
| 2 | |
| 3 | #include "aos/logging/implementations.h" |
| 4 | #include "aos/logging/logging.h" |
| 5 | #include "aos/vision/blob/codec.h" |
| 6 | #include "aos/vision/blob/find_blob.h" |
| 7 | #include "aos/vision/events/socket_types.h" |
| 8 | #include "aos/vision/events/udp.h" |
| 9 | #include "aos/vision/image/image_stream.h" |
| 10 | #include "aos/vision/image/reader.h" |
| 11 | |
| 12 | #include "y2019/jevois/serial.h" |
| 13 | #include "y2019/vision/target_finder.h" |
| 14 | |
| 15 | using ::aos::events::DataSocket; |
| 16 | using ::aos::events::RXUdpSocket; |
| 17 | using ::aos::events::TCPServer; |
| 18 | using ::aos::vision::DataRef; |
| 19 | using ::aos::vision::Int32Codec; |
| 20 | using ::aos::monotonic_clock; |
| 21 | using ::y2019::jevois::open_via_terminos; |
| 22 | using aos::vision::Segment; |
| 23 | |
| 24 | class CameraStream : public ::aos::vision::ImageStreamEvent { |
| 25 | public: |
| 26 | CameraStream(::aos::vision::CameraParams params, const ::std::string &fname) |
| 27 | : ImageStreamEvent(fname, params) {} |
| 28 | |
| 29 | void ProcessImage(DataRef data, monotonic_clock::time_point monotonic_now) { |
| 30 | LOG(INFO, "got frame: %d\n", (int)data.size()); |
| 31 | |
| 32 | static unsigned i = 0; |
| 33 | |
| 34 | /* |
| 35 | std::ofstream ofs(std::string("/jevois/data/debug_viewer_jpeg_") + |
| 36 | std::to_string(i) + ".yuyv", |
| 37 | std::ofstream::out); |
| 38 | ofs << data; |
| 39 | ofs.close(); |
| 40 | */ |
| 41 | if (on_frame) on_frame(data, monotonic_now); |
| 42 | ++i; |
| 43 | |
| 44 | if (i == 200) exit(-1); |
| 45 | } |
| 46 | |
| 47 | std::function<void(DataRef, monotonic_clock::time_point)> on_frame; |
| 48 | }; |
| 49 | |
| 50 | int open_terminos(const char *tty_name) { return open_via_terminos(tty_name); } |
| 51 | |
| 52 | std::string GetFileContents(const std::string &filename) { |
| 53 | std::ifstream in(filename, std::ios::in | std::ios::binary); |
| 54 | if (in) { |
| 55 | std::string contents; |
| 56 | in.seekg(0, std::ios::end); |
| 57 | contents.resize(in.tellg()); |
| 58 | in.seekg(0, std::ios::beg); |
| 59 | in.read(&contents[0], contents.size()); |
| 60 | in.close(); |
| 61 | return (contents); |
| 62 | } |
| 63 | fprintf(stderr, "Could not read file: %s\n", filename.c_str()); |
| 64 | exit(-1); |
| 65 | } |
| 66 | |
| 67 | int main(int argc, char **argv) { |
| 68 | (void)argc; |
| 69 | (void)argv; |
| 70 | using namespace y2019::vision; |
| 71 | // gflags::ParseCommandLineFlags(&argc, &argv, false); |
| 72 | ::aos::logging::Init(); |
| 73 | ::aos::logging::AddImplementation( |
| 74 | new ::aos::logging::StreamLogImplementation(stderr)); |
| 75 | |
| 76 | int itsDev = open_terminos("/dev/ttyS0"); |
| 77 | dup2(itsDev, 1); |
| 78 | dup2(itsDev, 2); |
| 79 | |
| 80 | TargetFinder finder_; |
| 81 | |
| 82 | // Check that our current results match possible solutions. |
| 83 | aos::vision::CameraParams params0; |
| 84 | params0.set_exposure(0); |
| 85 | params0.set_brightness(40); |
| 86 | params0.set_width(640); |
| 87 | // params0.set_fps(10); |
| 88 | params0.set_height(480); |
| 89 | |
| 90 | ::std::unique_ptr<CameraStream> camera0( |
| 91 | new CameraStream(params0, "/dev/video0")); |
| 92 | camera0->on_frame = [&](DataRef data, |
| 93 | monotonic_clock::time_point /*monotonic_now*/) { |
| 94 | aos::vision::ImageFormat fmt{640, 480}; |
| 95 | aos::vision::BlobList imgs = aos::vision::FindBlobs( |
| 96 | aos::vision::DoThresholdYUYV(fmt, data.data(), 120)); |
| 97 | finder_.PreFilter(&imgs); |
| 98 | |
| 99 | bool verbose = false; |
| 100 | std::vector<std::vector<Segment<2>>> raw_polys; |
| 101 | for (const RangeImage &blob : imgs) { |
| 102 | std::vector<Segment<2>> polygon = finder_.FillPolygon(blob, verbose); |
| 103 | if (polygon.empty()) { |
| 104 | } else { |
| 105 | raw_polys.push_back(polygon); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Calculate each component side of a possible target. |
| 110 | std::vector<TargetComponent> target_component_list = |
| 111 | finder_.FillTargetComponentList(raw_polys); |
| 112 | |
| 113 | // Put the compenents together into targets. |
| 114 | std::vector<Target> target_list = |
| 115 | finder_.FindTargetsFromComponents(target_component_list, verbose); |
| 116 | |
| 117 | // Use the solver to generate an intermediate version of our results. |
| 118 | std::vector<IntermediateResult> results; |
| 119 | for (const Target &target : target_list) { |
| 120 | results.emplace_back(finder_.ProcessTargetToResult(target, verbose)); |
| 121 | } |
| 122 | |
| 123 | results = finder_.FilterResults(results); |
| 124 | }; |
| 125 | |
| 126 | aos::events::EpollLoop loop; |
| 127 | |
| 128 | for (int i = 0; i < 100; ++i) { |
| 129 | std::this_thread::sleep_for(std::chrono::milliseconds(20)); |
| 130 | camera0->ReadEvent(); |
| 131 | } |
| 132 | |
| 133 | // TODO: Fix event loop on jevois: |
| 134 | // loop.Add(camera0.get()); |
| 135 | // loop.Run(); |
| 136 | } |