blob: 0e2546fa1b5013c71b92d3efb0beb8dc2c2e9caa [file] [log] [blame]
Filip Kujawa3004f202023-02-12 16:41:40 -08001#include "y2023/vision/game_pieces.h"
2
Filip Kujawab72ca2d2023-02-26 17:38:54 -08003#include <ctime>
4
Filip Kujawa3004f202023-02-12 16:41:40 -08005#include "aos/events/event_loop.h"
6#include "aos/events/shm_event_loop.h"
7#include "frc971/vision/vision_generated.h"
8
Filip Kujawab72ca2d2023-02-26 17:38:54 -08009// 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
15DEFINE_uint32(
16 best_x, 0,
17 "The 'best' game piece is picked based on how close it is to this x value");
18
19DEFINE_uint32(
20 best_y, 360,
21 "The 'best' game piece is picked based on how close it is to this y value");
22
Filip Kujawa3004f202023-02-12 16:41:40 -080023namespace y2023 {
24namespace vision {
25GamePiecesDetector::GamePiecesDetector(aos::EventLoop *event_loop)
Filip Kujawab72ca2d2023-02-26 17:38:54 -080026 : game_pieces_sender_(event_loop->MakeSender<GamePieces>("/camera")) {
Filip Kujawa3004f202023-02-12 16:41:40 -080027 event_loop->MakeWatcher("/camera", [this](const CameraImage &camera_image) {
28 this->ProcessImage(camera_image);
29 });
30}
31
Filip Kujawab72ca2d2023-02-26 17:38:54 -080032// TODO(FILIP): Actually do inference.
33
Filip Kujawa3004f202023-02-12 16:41:40 -080034void GamePiecesDetector::ProcessImage(const CameraImage &image) {
35 // Param is not used for now.
36 (void)image;
37
Filip Kujawab72ca2d2023-02-26 17:38:54 -080038 const int detection_count = 5;
39
Filip Kujawa3004f202023-02-12 16:41:40 -080040 auto builder = game_pieces_sender_.MakeBuilder();
41
Filip Kujawab72ca2d2023-02-26 17:38:54 -080042 std::vector<flatbuffers::Offset<GamePiece>> game_pieces_offsets;
Filip Kujawa3004f202023-02-12 16:41:40 -080043
Filip Kujawab72ca2d2023-02-26 17:38:54 -080044 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 Kujawa3004f202023-02-12 16:41:40 -080087
88 flatbuffers::FlatBufferBuilder fbb;
Filip Kujawab72ca2d2023-02-26 17:38:54 -080089 auto game_pieces_vector = fbb.CreateVector(game_pieces_offsets);
Filip Kujawa3004f202023-02-12 16:41:40 -080090
91 auto game_pieces_builder = builder.MakeBuilder<GamePieces>();
92 game_pieces_builder.add_game_pieces(game_pieces_vector);
Filip Kujawab72ca2d2023-02-26 17:38:54 -080093 game_pieces_builder.add_best_piece(best_distance_index);
Filip Kujawa3004f202023-02-12 16:41:40 -080094
95 builder.CheckOk(builder.Send(game_pieces_builder.Finish()));
96}
97
98} // namespace vision
99} // namespace y2023