Filip Kujawa | 3004f20 | 2023-02-12 16:41:40 -0800 | [diff] [blame] | 1 | #include "y2023/vision/game_pieces.h" |
| 2 | |
Filip Kujawa | b72ca2d | 2023-02-26 17:38:54 -0800 | [diff] [blame^] | 3 | #include <ctime> |
| 4 | |
Filip Kujawa | 3004f20 | 2023-02-12 16:41:40 -0800 | [diff] [blame] | 5 | #include "aos/events/event_loop.h" |
| 6 | #include "aos/events/shm_event_loop.h" |
| 7 | #include "frc971/vision/vision_generated.h" |
| 8 | |
Filip Kujawa | b72ca2d | 2023-02-26 17:38:54 -0800 | [diff] [blame^] | 9 | // The best_x and best_y are pixel (x, y) cordinates. The 'best' |
| 10 | // game piece is picked on proximity to the specified cordinates. |
| 11 | // The cordinate should represent where we want to intake a game piece. |
| 12 | // (0, 360) was chosen without any testing, just a cordinate that |
| 13 | // seemed reasonable. |
| 14 | |
| 15 | DEFINE_uint32( |
| 16 | best_x, 0, |
| 17 | "The 'best' game piece is picked based on how close it is to this x value"); |
| 18 | |
| 19 | DEFINE_uint32( |
| 20 | best_y, 360, |
| 21 | "The 'best' game piece is picked based on how close it is to this y value"); |
| 22 | |
Filip Kujawa | 3004f20 | 2023-02-12 16:41:40 -0800 | [diff] [blame] | 23 | namespace y2023 { |
| 24 | namespace vision { |
| 25 | GamePiecesDetector::GamePiecesDetector(aos::EventLoop *event_loop) |
Filip Kujawa | b72ca2d | 2023-02-26 17:38:54 -0800 | [diff] [blame^] | 26 | : game_pieces_sender_(event_loop->MakeSender<GamePieces>("/camera")) { |
Filip Kujawa | 3004f20 | 2023-02-12 16:41:40 -0800 | [diff] [blame] | 27 | event_loop->MakeWatcher("/camera", [this](const CameraImage &camera_image) { |
| 28 | this->ProcessImage(camera_image); |
| 29 | }); |
| 30 | } |
| 31 | |
Filip Kujawa | b72ca2d | 2023-02-26 17:38:54 -0800 | [diff] [blame^] | 32 | // TODO(FILIP): Actually do inference. |
| 33 | |
Filip Kujawa | 3004f20 | 2023-02-12 16:41:40 -0800 | [diff] [blame] | 34 | void GamePiecesDetector::ProcessImage(const CameraImage &image) { |
| 35 | // Param is not used for now. |
| 36 | (void)image; |
| 37 | |
Filip Kujawa | b72ca2d | 2023-02-26 17:38:54 -0800 | [diff] [blame^] | 38 | const int detection_count = 5; |
| 39 | |
Filip Kujawa | 3004f20 | 2023-02-12 16:41:40 -0800 | [diff] [blame] | 40 | auto builder = game_pieces_sender_.MakeBuilder(); |
| 41 | |
Filip Kujawa | b72ca2d | 2023-02-26 17:38:54 -0800 | [diff] [blame^] | 42 | std::vector<flatbuffers::Offset<GamePiece>> game_pieces_offsets; |
Filip Kujawa | 3004f20 | 2023-02-12 16:41:40 -0800 | [diff] [blame] | 43 | |
Filip Kujawa | b72ca2d | 2023-02-26 17:38:54 -0800 | [diff] [blame^] | 44 | float lowest_distance = std::numeric_limits<float>::max(); |
| 45 | int best_distance_index = 0; |
| 46 | srand(time(0)); |
| 47 | |
| 48 | for (int i = 0; i < detection_count; i++) { |
| 49 | int h = rand() % 1000; |
| 50 | int w = rand() % 1000; |
| 51 | int x = rand() % 250; |
| 52 | int y = rand() % 250; |
| 53 | |
| 54 | auto box_builder = builder.MakeBuilder<Box>(); |
| 55 | box_builder.add_h(h); |
| 56 | box_builder.add_w(w); |
| 57 | box_builder.add_x(x); |
| 58 | box_builder.add_y(y); |
| 59 | auto box_offset = box_builder.Finish(); |
| 60 | |
| 61 | auto game_piece_builder = builder.MakeBuilder<GamePiece>(); |
| 62 | game_piece_builder.add_piece_class(y2023::vision::Class::CONE_DOWN); |
| 63 | game_piece_builder.add_box(box_offset); |
| 64 | game_piece_builder.add_confidence(0.9); |
| 65 | auto game_piece = game_piece_builder.Finish(); |
| 66 | game_pieces_offsets.push_back(game_piece); |
| 67 | |
| 68 | // Center x and y values. |
| 69 | // Inference returns the top left corner of the bounding box |
| 70 | // but we want the center of the box for this. |
| 71 | |
| 72 | const int center_x = x + w / 2; |
| 73 | const int center_y = y + h / 2; |
| 74 | |
| 75 | // Find difference between target x, y and the x, y |
| 76 | // of the bounding box using Euclidean distance. |
| 77 | |
| 78 | const int dx = FLAGS_best_x - center_x; |
| 79 | const int dy = FLAGS_best_y - center_y; |
| 80 | const float distance = std::sqrt(dx * dx + dy * dy); |
| 81 | |
| 82 | if (distance < lowest_distance) { |
| 83 | lowest_distance = distance; |
| 84 | best_distance_index = i; |
| 85 | } |
| 86 | }; |
Filip Kujawa | 3004f20 | 2023-02-12 16:41:40 -0800 | [diff] [blame] | 87 | |
| 88 | flatbuffers::FlatBufferBuilder fbb; |
Filip Kujawa | b72ca2d | 2023-02-26 17:38:54 -0800 | [diff] [blame^] | 89 | auto game_pieces_vector = fbb.CreateVector(game_pieces_offsets); |
Filip Kujawa | 3004f20 | 2023-02-12 16:41:40 -0800 | [diff] [blame] | 90 | |
| 91 | auto game_pieces_builder = builder.MakeBuilder<GamePieces>(); |
| 92 | game_pieces_builder.add_game_pieces(game_pieces_vector); |
Filip Kujawa | b72ca2d | 2023-02-26 17:38:54 -0800 | [diff] [blame^] | 93 | game_pieces_builder.add_best_piece(best_distance_index); |
Filip Kujawa | 3004f20 | 2023-02-12 16:41:40 -0800 | [diff] [blame] | 94 | |
| 95 | builder.CheckOk(builder.Send(game_pieces_builder.Finish())); |
| 96 | } |
| 97 | |
| 98 | } // namespace vision |
| 99 | } // namespace y2023 |