update (i'm sorry)

This commit is contained in:
ackimixs
2024-01-24 15:53:58 +01:00
parent 4e1d9d4255
commit a3b9ae3c06
6 changed files with 73 additions and 42 deletions

View File

@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.25)
project(detection_pot VERSION 1.0.0)
project(detection_pot VERSION 1.0.1)
set(CMAKE_CXX_STANDARD 17)
@@ -24,10 +24,10 @@ target_link_libraries( calibration ${OpenCV_LIBS} )
set(arucoDetectionSources
${COMMON_SOURCES}
arucoDetector.cpp
utils/ArucoTag.cpp
utils/ArucoTag.h
utils/ArucoDetector.cpp
utils/ArucoDetector.h
aruco/ArucoTag.cpp
aruco/ArucoTag.h
aruco/ArucoDetector.cpp
aruco/ArucoDetector.h
)
add_executable(arucoDetector ${arucoDetectionSources})

View File

@@ -2,12 +2,13 @@
ArucoDetector::ArucoDetector(const Type::RobotPose& pose, const std::string& calibrationPath, const Team team, const int cameraId, const bool headless) : robotPose(pose), headless(headless), team(team)
{
// this->detector = cv::aruco::ArucoDetector(getPredefinedDictionary(cv::aruco::DICT_4X4_50), cv::aruco::DetectorParameters());
// this->dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50);
// this->parameters = cv::aruco::DetectorParameters();
// opencv 4.8
this->detector = cv::aruco::ArucoDetector(getPredefinedDictionary(cv::aruco::DICT_4X4_50), cv::aruco::DetectorParameters());
this->dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50);
this->parameters = cv::aruco::DetectorParameters();
// 4.6
this->dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50);
// this->dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_4X4_50);
this->transformationMatrix = (cv::Mat_<double>(4, 4) <<
@@ -22,6 +23,9 @@ ArucoDetector::ArucoDetector(const Type::RobotPose& pose, const std::string& cal
if (!cap.isOpened()) {
std::cerr << "Error opening camera." << std::endl;
} else
{
started = true;
}
if (!headless)
@@ -67,15 +71,26 @@ void ArucoDetector::addArucoTag(const ArucoTag& tag)
this->arucoTags.push_back(tag);
}
std::pair<int, std::vector<std::pair<ArucoTag, std::pair<cv::Mat, cv::Mat>>>> ArucoDetector::detectArucoTags()
std::pair<int, std::vector<std::pair<ArucoTag, std::pair<cv::Mat, cv::Mat>>>> ArucoDetector::detectArucoTags(std::vector<ArucoTag> tags)
{
if (tags.empty())
{
tags = this->arucoTags;
}
if (!started)
{
std::pair<int, std::vector<std::pair<ArucoTag, std::pair<cv::Mat, cv::Mat>>>> result;
result.first = -2;
return result;
}
cv::Mat frame;
cap >> frame; // Capture frame from the camera
std::pair<int, std::vector<std::pair<ArucoTag, std::pair<cv::Mat, cv::Mat>>>> result;
if (frame.empty()) {
std::cerr << "Error capturing frame." << std::endl;
result.first = -2;
return result;
}
@@ -86,14 +101,14 @@ std::pair<int, std::vector<std::pair<ArucoTag, std::pair<cv::Mat, cv::Mat>>>> Ar
std::vector<int> markerIds;
std::vector<std::vector<cv::Point2f>> markerCorners;
cv::aruco::detectMarkers(frame, this->dictionary, markerCorners, markerIds);
// 4.6
// cv::aruco::detectMarkers(frame, this->dictionary, markerCorners, markerIds);
// opencv 4.8
// detector.detectMarkers(frame, markerCorners, markerIds);
detector.detectMarkers(frame, markerCorners, markerIds);
if (!markerIds.empty())
{
std::cout << "Detected " << markerIds.size() << " markers." << std::endl;
if (!headless)
{
cv::aruco::drawDetectedMarkers(frame, markerCorners, markerIds);
@@ -103,12 +118,12 @@ std::pair<int, std::vector<std::pair<ArucoTag, std::pair<cv::Mat, cv::Mat>>>> Ar
{
int id = markerIds[i];
if (std::find_if(arucoTags.begin(), arucoTags.end(), [id](const ArucoTag& tag) { return tag.id == id; }) == arucoTags.end())
if (std::find_if(tags.begin(), tags.end(), [id](const ArucoTag& tag) { return tag.id == id; }) == tags.end())
{
continue;
}
ArucoTag tag = *std::find_if(arucoTags.begin(), arucoTags.end(), [id](const ArucoTag& tag) { return tag.id == id; });
ArucoTag tag = *std::find_if(tags.begin(), tags.end(), [id](const ArucoTag& tag) { return tag.id == id; });
cv::Mat rvec, tvec;
@@ -150,13 +165,12 @@ std::pair<int, std::vector<std::pair<ArucoTag, std::pair<cv::Mat, cv::Mat>>>> Ar
if (!headless)
{
cv::imshow("ArUco Detection", frame);
}
if (cv::waitKey(10) == 27)
{
// Press 'Esc' to exit
result.first = 1;
return result;
if (cv::waitKey(10) == 27)
{
// Press 'Esc' to exit
result.first = 1;
return result;
}
}
result.first = 0;

View File

@@ -1,6 +1,6 @@
#pragma once
#include "utils.h"
#include "../utils/utils.h"
#include "ArucoTag.h"
class ArucoDetector {
@@ -14,12 +14,12 @@ class ArucoDetector {
cv::VideoCapture cap;
// 4.6
cv::Ptr<cv::aruco::Dictionary> dictionary;
// cv::Ptr<cv::aruco::Dictionary> dictionary;
// 4.8
// cv::aruco::Dictionary dictionary;
// cv::aruco::DetectorParameters parameters;
// cv::aruco::ArucoDetector detector;
cv::aruco::Dictionary dictionary;
cv::aruco::DetectorParameters parameters;
cv::aruco::ArucoDetector detector;
bool headless;
@@ -27,6 +27,8 @@ class ArucoDetector {
Team team;
bool started = false;
public:
ArucoDetector(const Type::RobotPose& pose, const std::string& calibrationPath, Team team, int cameraId = 0, bool headless = false);
@@ -34,7 +36,7 @@ public:
~ArucoDetector();
std::pair<int, std::vector<std::pair<ArucoTag, std::pair<cv::Mat, cv::Mat>>>> detectArucoTags();
std::pair<int, std::vector<std::pair<ArucoTag, std::pair<cv::Mat, cv::Mat>>>> detectArucoTags(std::vector<ArucoTag> tags = {});
void readCameraParameters(const std::string& path);

View File

@@ -1,8 +1,9 @@
#include "utils/ArucoDetector.h"
#include "aruco/ArucoDetector.h"
#include <iostream>
#include <thread>
#include <atomic>
#include <optional>
std::atomic<bool> stopRequested(false);
@@ -44,35 +45,46 @@ int main(int argc, char *argv[])
const std::string calibrationPath = argv[2];
// End argument parser
std::optional<std::thread> userInput;
std::thread userInput(userInputThread);
if (headless)
{
userInput = std::thread(userInputThread);
}
const auto robotPose = Type::RobotPose{cv::Point3f(0, 0, 0), CV_PI/2};
ArucoDetector detector(robotPose, calibrationPath, BLUE, cameraId, headless);
while (true) {
const auto res = detector.detectArucoTags();
auto whiteFlower = ArucoTag(36, "White flower", 20, FLOWER);
whiteFlower.setFlowerObjectRepresentation();
auto purpleFlower = ArucoTag(13, "Purple flower", 20, FLOWER);
purpleFlower.setFlowerObjectRepresentation();
if (res.first == -2)
auto solarPanel = ArucoTag(47, "Solar panel", 50, SOLAR_PANEL);
while (true) {
const auto [code, res] = detector.detectArucoTags({solarPanel});
if (code == -2)
{
std::cerr << "Error: Could not capture frame." << std::endl;
return -2;
}
if (res.first == 1)
if (code == 1)
{
break;
stopRequested = true;
}
for (auto p : res.second)
for (auto [tags, matrix] : res)
{
if (p.first.type == FLOWER)
if (tags.type == FLOWER)
{
ArucoDetector::flowerDetector(p.first, p.second.first, p.second.first);
} else if (p.first.type == SOLAR_PANEL)
ArucoDetector::flowerDetector(tags, matrix.first, matrix.first);
} else if (tags.type == SOLAR_PANEL)
{
ArucoDetector::solarPanelDetector(p.first, p.second.first, p.second.first, robotPose);
ArucoDetector::solarPanelDetector(tags, matrix.first, matrix.first, robotPose);
}
}
@@ -83,7 +95,10 @@ int main(int argc, char *argv[])
}
// Wait for the user input thread to finish
userInput.join();
if (userInput.has_value())
{
userInput.value().join();
}
return 0;
}