blob: bf6a85505114e0680dc1ca57f0c225493d5e482d [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"
Filip Kujawa8c76e5d2023-04-08 16:20:27 -07008#include "y2023/vision/yolov5.h"
Filip Kujawa3004f202023-02-12 16:41:40 -08009
Filip Kujawab72ca2d2023-02-26 17:38:54 -080010// The best_x and best_y are pixel (x, y) cordinates. The 'best'
11// game piece is picked on proximity to the specified cordinates.
12// The cordinate should represent where we want to intake a game piece.
13// (0, 360) was chosen without any testing, just a cordinate that
14// seemed reasonable.
15
16DEFINE_uint32(
17 best_x, 0,
18 "The 'best' game piece is picked based on how close it is to this x value");
19
20DEFINE_uint32(
21 best_y, 360,
22 "The 'best' game piece is picked based on how close it is to this y value");
23
Filip Kujawa3004f202023-02-12 16:41:40 -080024namespace y2023 {
25namespace vision {
Filip Kujawaf3b8adb2023-04-07 21:00:49 -070026using aos::monotonic_clock;
Filip Kujawa3004f202023-02-12 16:41:40 -080027GamePiecesDetector::GamePiecesDetector(aos::EventLoop *event_loop)
Filip Kujawab72ca2d2023-02-26 17:38:54 -080028 : game_pieces_sender_(event_loop->MakeSender<GamePieces>("/camera")) {
Filip Kujawaf3b8adb2023-04-07 21:00:49 -070029 model = MakeYOLOV5();
30 model->LoadModel("edgetpu_model.tflite");
Filip Kujawa8c76e5d2023-04-08 16:20:27 -070031
Filip Kujawaf3b8adb2023-04-07 21:00:49 -070032 event_loop->MakeWatcher(
33 "/camera", [this, event_loop](const CameraImage &camera_image) {
34 const monotonic_clock::time_point eof = monotonic_clock::time_point(
35 std::chrono::nanoseconds(camera_image.monotonic_timestamp_ns()));
36 const monotonic_clock::duration age = event_loop->monotonic_now() - eof;
37 if (age > std::chrono::milliseconds(100)) {
38 VLOG(1) << "Behind, skipping";
39 return;
40 }
Filip Kujawa8c76e5d2023-04-08 16:20:27 -070041
Filip Kujawaf3b8adb2023-04-07 21:00:49 -070042 this->ProcessImage(camera_image);
43 });
Filip Kujawa3004f202023-02-12 16:41:40 -080044}
45
46void GamePiecesDetector::ProcessImage(const CameraImage &image) {
Filip Kujawa8c76e5d2023-04-08 16:20:27 -070047 cv::Mat image_color_mat(cv::Size(image.cols(), image.rows()), CV_8UC2,
48 (void *)image.data()->data());
49 std::vector<Detection> detections;
50 cv::Mat image_mat(cv::Size(image.cols(), image.rows()), CV_8UC3);
Filip Kujawa8c76e5d2023-04-08 16:20:27 -070051 cv::cvtColor(image_color_mat, image_mat, cv::COLOR_YUV2BGR_YUYV);
Filip Kujawa3004f202023-02-12 16:41:40 -080052
Filip Kujawa8c76e5d2023-04-08 16:20:27 -070053 detections = model->ProcessImage(image_mat);
Filip Kujawab72ca2d2023-02-26 17:38:54 -080054
Filip Kujawa3004f202023-02-12 16:41:40 -080055 auto builder = game_pieces_sender_.MakeBuilder();
56
Filip Kujawab72ca2d2023-02-26 17:38:54 -080057 std::vector<flatbuffers::Offset<GamePiece>> game_pieces_offsets;
Filip Kujawa3004f202023-02-12 16:41:40 -080058
Filip Kujawab72ca2d2023-02-26 17:38:54 -080059 float lowest_distance = std::numeric_limits<float>::max();
60 int best_distance_index = 0;
Filip Kujawab72ca2d2023-02-26 17:38:54 -080061
Filip Kujawa8c76e5d2023-04-08 16:20:27 -070062 for (size_t i = 0; i < detections.size(); i++) {
Filip Kujawab72ca2d2023-02-26 17:38:54 -080063 auto box_builder = builder.MakeBuilder<Box>();
Filip Kujawa8c76e5d2023-04-08 16:20:27 -070064 box_builder.add_h(detections[i].box.height);
65 box_builder.add_w(detections[i].box.width);
66 box_builder.add_x(detections[i].box.x);
67 box_builder.add_y(detections[i].box.y);
Filip Kujawab72ca2d2023-02-26 17:38:54 -080068 auto box_offset = box_builder.Finish();
69
70 auto game_piece_builder = builder.MakeBuilder<GamePiece>();
Filip Kujawa8c76e5d2023-04-08 16:20:27 -070071 switch (detections[i].class_id) {
72 case 0:
73 game_piece_builder.add_piece_class(Class::CONE_DOWN);
74 break;
75 case 1:
76 game_piece_builder.add_piece_class(Class::CONE_UP);
77 break;
78 case 2:
79 game_piece_builder.add_piece_class(Class::CUBE);
80 break;
81 default:
82 game_piece_builder.add_piece_class(Class::CONE_DOWN);
83 }
Filip Kujawab72ca2d2023-02-26 17:38:54 -080084 game_piece_builder.add_box(box_offset);
Filip Kujawa8c76e5d2023-04-08 16:20:27 -070085 game_piece_builder.add_confidence(detections[i].confidence);
Filip Kujawab72ca2d2023-02-26 17:38:54 -080086 auto game_piece = game_piece_builder.Finish();
87 game_pieces_offsets.push_back(game_piece);
88
89 // Center x and y values.
90 // Inference returns the top left corner of the bounding box
91 // but we want the center of the box for this.
92
Filip Kujawa8c76e5d2023-04-08 16:20:27 -070093 const int center_x = detections[i].box.x + detections[i].box.width / 2;
94 const int center_y = detections[i].box.y + detections[i].box.height / 2;
Filip Kujawab72ca2d2023-02-26 17:38:54 -080095
96 // Find difference between target x, y and the x, y
97 // of the bounding box using Euclidean distance.
98
99 const int dx = FLAGS_best_x - center_x;
100 const int dy = FLAGS_best_y - center_y;
101 const float distance = std::sqrt(dx * dx + dy * dy);
102
103 if (distance < lowest_distance) {
104 lowest_distance = distance;
105 best_distance_index = i;
106 }
107 };
Filip Kujawa3004f202023-02-12 16:41:40 -0800108
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700109 auto game_pieces_vector = builder.fbb()->CreateVector(game_pieces_offsets);
Filip Kujawa3004f202023-02-12 16:41:40 -0800110
111 auto game_pieces_builder = builder.MakeBuilder<GamePieces>();
112 game_pieces_builder.add_game_pieces(game_pieces_vector);
Filip Kujawab72ca2d2023-02-26 17:38:54 -0800113 game_pieces_builder.add_best_piece(best_distance_index);
Filip Kujawa3004f202023-02-12 16:41:40 -0800114
115 builder.CheckOk(builder.Send(game_pieces_builder.Finish()));
116}
117
118} // namespace vision
Filip Kujawa8c76e5d2023-04-08 16:20:27 -0700119} // namespace y2023