blob: 4344cc947ee828e9349d4cb9c759ff303b52cacc [file] [log] [blame]
Brian Silverman58899fd2019-03-24 11:03:11 -07001#include "y2019/vision/target_finder.h"
2
Parker Schuh2a1447c2019-02-17 00:25:29 -08003#include <fstream>
4
Parker Schuh2a1447c2019-02-17 00:25:29 -08005#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"
Parker Schuh5e8e3a52019-02-24 13:36:19 -08009#include "y2019/jevois/camera/image_stream.h"
10#include "y2019/jevois/camera/reader.h"
Parker Schuh2a1447c2019-02-17 00:25:29 -080011#include "y2019/jevois/serial.h"
Brian Silvermance4825f2019-02-17 18:28:39 -080012#include "y2019/jevois/structures.h"
13#include "y2019/jevois/uart.h"
Austin Schuh4e2629d2019-03-28 14:44:37 -070014#include "y2019/vision/image_writer.h"
Brian Silverman58899fd2019-03-24 11:03:11 -070015
16// This has to be last to preserve compatibility with other headers using AOS
17// logging.
18#include "glog/logging.h"
Parker Schuh2a1447c2019-02-17 00:25:29 -080019
20using ::aos::events::DataSocket;
21using ::aos::events::RXUdpSocket;
22using ::aos::events::TCPServer;
23using ::aos::vision::DataRef;
24using ::aos::vision::Int32Codec;
25using ::aos::monotonic_clock;
26using ::y2019::jevois::open_via_terminos;
27using aos::vision::Segment;
28
Parker Schuh5e8e3a52019-02-24 13:36:19 -080029class CameraStream : public ::y2019::camera::ImageStreamEvent {
Parker Schuh2a1447c2019-02-17 00:25:29 -080030 public:
31 CameraStream(::aos::vision::CameraParams params, const ::std::string &fname)
32 : ImageStreamEvent(fname, params) {}
33
34 void ProcessImage(DataRef data, monotonic_clock::time_point monotonic_now) {
Brian Silverman58899fd2019-03-24 11:03:11 -070035 LOG(INFO) << "got frame: " << data.size();
Parker Schuh2a1447c2019-02-17 00:25:29 -080036
Parker Schuh5e8e3a52019-02-24 13:36:19 -080037 if (on_frame_) on_frame_(data, monotonic_now);
Parker Schuh2a1447c2019-02-17 00:25:29 -080038 }
39
Parker Schuh5e8e3a52019-02-24 13:36:19 -080040 void set_on_frame(const std::function<
41 void(DataRef, monotonic_clock::time_point)> &on_frame) {
42 on_frame_ = on_frame;
43 }
44
45 private:
46 std::function<void(DataRef, monotonic_clock::time_point)> on_frame_;
Parker Schuh2a1447c2019-02-17 00:25:29 -080047};
48
49int open_terminos(const char *tty_name) { return open_via_terminos(tty_name); }
50
51std::string GetFileContents(const std::string &filename) {
52 std::ifstream in(filename, std::ios::in | std::ios::binary);
53 if (in) {
54 std::string contents;
55 in.seekg(0, std::ios::end);
56 contents.resize(in.tellg());
57 in.seekg(0, std::ios::beg);
58 in.read(&contents[0], contents.size());
59 in.close();
60 return (contents);
61 }
62 fprintf(stderr, "Could not read file: %s\n", filename.c_str());
63 exit(-1);
64}
65
Parker Schuh5e8e3a52019-02-24 13:36:19 -080066using aos::vision::ImageRange;
67using aos::vision::RangeImage;
68using aos::vision::ImageFormat;
69
Parker Schuh2a1447c2019-02-17 00:25:29 -080070int main(int argc, char **argv) {
71 (void)argc;
72 (void)argv;
73 using namespace y2019::vision;
Brian Silvermane9924fd2019-03-02 15:20:42 -080074 using frc971::jevois::CameraCommand;
Parker Schuh2a1447c2019-02-17 00:25:29 -080075 // gflags::ParseCommandLineFlags(&argc, &argv, false);
Brian Silverman58899fd2019-03-24 11:03:11 -070076 FLAGS_logtostderr = true;
77 google::InitGoogleLogging(argv[0]);
Parker Schuh2a1447c2019-02-17 00:25:29 -080078
79 int itsDev = open_terminos("/dev/ttyS0");
Parker Schuh5e8e3a52019-02-24 13:36:19 -080080 frc971::jevois::CobsPacketizer<frc971::jevois::uart_to_camera_size()> cobs;
81 // Uncomment these to printf directly to stdout to get debug info...
82 // dup2(itsDev, 1);
83 // dup2(itsDev, 2);
Parker Schuh2a1447c2019-02-17 00:25:29 -080084
85 TargetFinder finder_;
Austin Schuh4e2629d2019-03-28 14:44:37 -070086 ImageWriter writer;
87 uint32_t image_count = 0;
88 bool log_images = false;
Parker Schuh2a1447c2019-02-17 00:25:29 -080089
Parker Schuh2a1447c2019-02-17 00:25:29 -080090 aos::vision::CameraParams params0;
Ben Fredricksona8c3d552019-03-03 14:14:53 -080091 params0.set_exposure(50);
Parker Schuh2a1447c2019-02-17 00:25:29 -080092 params0.set_brightness(40);
93 params0.set_width(640);
Ben Fredricksona8c3d552019-03-03 14:14:53 -080094 params0.set_fps(15);
Parker Schuh2a1447c2019-02-17 00:25:29 -080095 params0.set_height(480);
96
Brian Silverman20b57772019-03-23 22:02:49 -070097 aos::vision::FastYuyvYPooledThresholder thresholder;
98
Parker Schuh2a1447c2019-02-17 00:25:29 -080099 ::std::unique_ptr<CameraStream> camera0(
100 new CameraStream(params0, "/dev/video0"));
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800101 camera0->set_on_frame([&](DataRef data,
102 monotonic_clock::time_point monotonic_now) {
Parker Schuh2a1447c2019-02-17 00:25:29 -0800103 aos::vision::ImageFormat fmt{640, 480};
Brian Silverman20b57772019-03-23 22:02:49 -0700104 aos::vision::BlobList imgs =
105 aos::vision::FindBlobs(thresholder.Threshold(fmt, data.data(), 120));
Parker Schuh2a1447c2019-02-17 00:25:29 -0800106 finder_.PreFilter(&imgs);
Brian Silverman58899fd2019-03-24 11:03:11 -0700107 LOG(INFO) << "Blobs: " << imgs.size();
Parker Schuh2a1447c2019-02-17 00:25:29 -0800108
Austin Schuh32ffac22019-03-09 22:42:02 -0800109 constexpr bool verbose = false;
Austin Schuh6e56faf2019-03-10 14:04:57 -0700110 ::std::vector<Polygon> raw_polys;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800111 for (const RangeImage &blob : imgs) {
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800112 // Convert blobs to contours in the corrected space.
Brian Silverman86891e52019-03-23 22:02:52 -0700113 ContourNode *contour = finder_.GetContour(blob);
Austin Schuh6e56faf2019-03-10 14:04:57 -0700114 ::std::vector<::Eigen::Vector2f> unwarped_contour =
Austin Schuhe5015972019-03-09 17:47:34 -0800115 finder_.UnWarpContour(contour);
Austin Schuh6e56faf2019-03-10 14:04:57 -0700116 const Polygon polygon =
117 finder_.FindPolygon(::std::move(unwarped_contour), verbose);
118 if (!polygon.segments.empty()) {
Parker Schuh2a1447c2019-02-17 00:25:29 -0800119 raw_polys.push_back(polygon);
120 }
121 }
Brian Silverman58899fd2019-03-24 11:03:11 -0700122 LOG(INFO) << "Polygons: " << raw_polys.size();
Parker Schuh2a1447c2019-02-17 00:25:29 -0800123
124 // Calculate each component side of a possible target.
Austin Schuh32ffac22019-03-09 22:42:02 -0800125 ::std::vector<TargetComponent> target_component_list =
126 finder_.FillTargetComponentList(raw_polys, verbose);
Brian Silverman58899fd2019-03-24 11:03:11 -0700127 LOG(INFO) << "Components: " << target_component_list.size();
Parker Schuh2a1447c2019-02-17 00:25:29 -0800128
129 // Put the compenents together into targets.
Austin Schuh32ffac22019-03-09 22:42:02 -0800130 ::std::vector<Target> target_list =
Parker Schuh2a1447c2019-02-17 00:25:29 -0800131 finder_.FindTargetsFromComponents(target_component_list, verbose);
Brian Silverman58899fd2019-03-24 11:03:11 -0700132 LOG(INFO) << "Potential Target: " << target_list.size();
Parker Schuh2a1447c2019-02-17 00:25:29 -0800133
134 // Use the solver to generate an intermediate version of our results.
Austin Schuh32ffac22019-03-09 22:42:02 -0800135 ::std::vector<IntermediateResult> results;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800136 for (const Target &target : target_list) {
137 results.emplace_back(finder_.ProcessTargetToResult(target, verbose));
138 }
Brian Silverman58899fd2019-03-24 11:03:11 -0700139 LOG(INFO) << "Raw Results: " << results.size();
Parker Schuh2a1447c2019-02-17 00:25:29 -0800140
Alex Perrybac3d3f2019-03-10 14:26:51 -0700141 results = finder_.FilterResults(results, 30, verbose);
Brian Silverman58899fd2019-03-24 11:03:11 -0700142 LOG(INFO) << "Results: " << results.size();
Brian Silvermance4825f2019-02-17 18:28:39 -0800143
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800144 // TODO: Select top 3 (randomly?)
145
Brian Silvermanc41fb862019-03-02 21:14:46 -0800146 frc971::jevois::CameraFrame frame{};
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800147
148 for (size_t i = 0; i < results.size() && i < frame.targets.max_size();
149 ++i) {
150 const auto &result = results[i].extrinsics;
151 frame.targets.push_back(frc971::jevois::Target{
152 static_cast<float>(result.z), static_cast<float>(result.y),
153 static_cast<float>(result.r2), static_cast<float>(result.r1)});
154 }
155
156 frame.age = std::chrono::duration_cast<frc971::jevois::camera_duration>(
157 aos::monotonic_clock::now() - monotonic_now);
158
Brian Silverman86891e52019-03-23 22:02:52 -0700159 // If we succeed in writing our delimiter, then write out the rest of
160 // the frame. If not, no point in continuing.
Brian Silvermance4825f2019-02-17 18:28:39 -0800161 if (write(itsDev, "\0", 1) == 1) {
Brian Silvermance4825f2019-02-17 18:28:39 -0800162 const auto serialized_frame = frc971::jevois::UartPackToTeensy(frame);
163 // We don't really care if this succeeds or not. If it fails for some
164 // reason, we'll just try again with the next frame, and the other end
165 // will find the new packet just fine.
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800166 ssize_t n =
167 write(itsDev, serialized_frame.data(), serialized_frame.size());
168
169 if (n != (ssize_t)serialized_frame.size()) {
Brian Silverman58899fd2019-03-24 11:03:11 -0700170 LOG(INFO) << "Some problem happened";
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800171 }
Brian Silvermance4825f2019-02-17 18:28:39 -0800172 }
Austin Schuh4e2629d2019-03-28 14:44:37 -0700173
174 if (log_images) {
175 if ((image_count % 5) == 0) {
176 writer.WriteImage(data);
177 }
178 ++image_count;
179 }
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800180 });
Parker Schuh2a1447c2019-02-17 00:25:29 -0800181
182 aos::events::EpollLoop loop;
183
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800184 while (true) {
185 std::this_thread::sleep_for(std::chrono::milliseconds(1));
Parker Schuh2a1447c2019-02-17 00:25:29 -0800186 camera0->ReadEvent();
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800187
188 {
189 constexpr size_t kBufferSize = frc971::jevois::uart_to_teensy_size();
190 char data[kBufferSize];
191 ssize_t n = read(itsDev, &data[0], kBufferSize);
192 if (n >= 1) {
193 cobs.ParseData(gsl::span<const char>(&data[0], n));
194 auto packet = cobs.received_packet();
195 if (!packet.empty()) {
196 auto calibration_question =
197 frc971::jevois::UartUnpackToCamera(packet);
198 if (calibration_question) {
199 const auto &calibration = *calibration_question;
Alex Perry3bf1bee2019-02-23 20:01:15 -0800200 IntrinsicParams *intrinsics = finder_.mutable_intrinsics();
201 intrinsics->mount_angle = calibration.calibration(0, 0);
202 intrinsics->focal_length = calibration.calibration(0, 1);
203 intrinsics->barrel_mount = calibration.calibration(0, 2);
204
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800205 switch (calibration.camera_command) {
Brian Silvermane9924fd2019-03-02 15:20:42 -0800206 case CameraCommand::kNormal:
Brian Silvermanbac77542019-03-03 13:57:00 -0800207 case CameraCommand::kAs:
Austin Schuh4e2629d2019-03-28 14:44:37 -0700208 log_images = false;
209 break;
210 case CameraCommand::kLog:
211 log_images = true;
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800212 break;
Brian Silvermane9924fd2019-03-02 15:20:42 -0800213 case CameraCommand::kUsb:
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800214 return 0;
Brian Silvermane9924fd2019-03-02 15:20:42 -0800215 case CameraCommand::kCameraPassthrough:
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800216 return system("touch /tmp/do_not_export_sd_card");
217 }
218 } else {
Brian Silverman58899fd2019-03-24 11:03:11 -0700219 fprintf(stderr, "bad frame\n");
Parker Schuh5e8e3a52019-02-24 13:36:19 -0800220 }
221 cobs.clear_received_packet();
222 }
223 }
224 }
Parker Schuh2a1447c2019-02-17 00:25:29 -0800225 }
226
227 // TODO: Fix event loop on jevois:
228 // loop.Add(camera0.get());
229 // loop.Run();
230}