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