Commit 3e40da03 authored by Wagner José Kramer Vieira's avatar Wagner José Kramer Vieira
Browse files

Update with starting server

parent bb7fe1f7
......@@ -5,7 +5,7 @@
#-------------------------------------------------
QT += core gui
QT += network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = VSSS
......@@ -29,14 +29,16 @@ SOURCES += \
main.cpp \
mainwindow.cpp \
backend.cpp \
global.cpp
global.cpp \
localserver.cpp
LIBS += -lstdc++fs
HEADERS += \
mainwindow.h \
global.h \
backend.h
backend.h \
localserver.h
FORMS += \
mainwindow.ui
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.8.1, 2019-10-12T14:24:16. -->
<!-- Written by QtCreator 4.8.1, 2019-10-27T01:09:54. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
......
......@@ -10,15 +10,60 @@
#include "ui_mainwindow.h"
#include <experimental/filesystem>
#include <math.h>
#include <QDebug>
#define PI 3.14159265
using namespace std;
using namespace cv;
using namespace Global;
using namespace std::chrono;
namespace fs = std::experimental::filesystem;
std::chrono::high_resolution_clock::time_point lastFrame;
QTimer timer;
double counter = 0;
Threader::Threader(){
}
void Threader::run(){
auto time = high_resolution_clock::now();
if(!capActive){
cap.release();
cap.~VideoCapture();
timer.stop();
counter = 0;
return;
}
if (fabs(counter) == fabs(cap.get(CAP_PROP_FRAME_COUNT)))
{
mtx.lock();
cap.set(CAP_PROP_POS_FRAMES, 0);
counter = 0;
mtx.unlock();
}
ThreadVariables var;
mtx.lock();
cap >> var.frame;
mtx.unlock();
qApp->processEvents();
if(!var.frame.empty())
frameMod(&var);
else{
mtx.lock();
cap = VideoCapture("/home/kramer/campo.mp4");
cap.set(CAP_PROP_POS_FRAMES, 0);
counter = 0;
mtx.unlock();
}
qDebug() << duration_cast<milliseconds>(high_resolution_clock::now() - time).count();
counter++;
frames++;
}
vector<Threader*> threads;
void countFrames(){
fps = frames;
frames = 0;
......@@ -39,54 +84,44 @@ double angleBetweenTwoPoints(Point2d p1, Point2d p2) {
}
}
size_t actual = 0;
void startCapture(){
cap = VideoCapture("/home/kramer/campo.mp4");
//cap.set(CAP_PROP_FRAME_WIDTH, 640);
//cap.set(CAP_PROP_FRAME_HEIGHT, 480);
cap.set(CAP_PROP_FPS, 30);
//cap.set(CAP_PROP_FPS, 30);
countFrames();
timer.setInterval(33);
threads.clear();
threads.emplace_back(new Threader());
threads.emplace_back(new Threader());
timer.setTimerType(Qt::PreciseTimer);
timer.setInterval(1000/10);
timer.callOnTimeout(&getFrame);
timer.start();
qApp->processEvents();
}
void getFrame(){
if(!capActive){
cap.release();
cap.~VideoCapture();
timer.stop();
return;
}
if (fabs(counter) == fabs(cap.get(CAP_PROP_FRAME_COUNT)))
{
cap.set(CAP_PROP_POS_FRAMES, 0);
counter = 0;
}
lastFrame = std::chrono::high_resolution_clock::now();
cap >> frame;
counter++;
frames++;
threads[actual]->start();
qApp->processEvents();
frameMod();
actual = (actual + 1)%2;
}
void frameMod(){
warpPerspective(frame, correctedFrame, Global::homography, correctedFrame.size());
cvtColor(correctedFrame, frameHSV, COLOR_BGR2HSV);
void frameMod(ThreadVariables * var){
thread enemy(&findEnemies) ;
thread ball(&findBall);
thread ally(&detectAlly);
warpPerspective(var->frame, var->correctedFrame, Global::homography, var->correctedFrame.size());
cvtColor(var->correctedFrame, var->frameHSV, COLOR_BGR2HSV);
thread enemy(&findEnemies, var) ;
thread ball(&findBall, var);
thread ally(&detectAlly, var);
enemy.join();
ball.join();
ally.join();
qApp->processEvents();
estrategia();
//cout << std::chrono::duration_cast<std::chrono::milliseconds>
// (std::chrono::high_resolution_clock::now() - lastFrame).count() << endl;
}
void calibration() {
......@@ -130,14 +165,14 @@ void calibration() {
calibrateCamera(object_points, image_points, image.size(), intrinsic, distCoeffs, rvecs, tvecs);
}
void findBall() {
Mat frame_HSV = frameHSV;
void findBall(Global::ThreadVariables* var) {
Mat frame_HSV = var->frameHSV;
inRange(frame_HSV, Scalar(Global::ballLH, Global::ballLS, Global::ballLV),
Scalar(Global::ballUH, Global::ballUS, Global::ballUV), thresholdBall);
Scalar(Global::ballUH, Global::ballUS, Global::ballUV), var->thresholdBall);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(thresholdBall, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
findContours(var->thresholdBall, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
qApp->processEvents();
std::sort(contours.begin(), contours.end(), []( std::vector<cv::Point> contour1, std::vector<cv::Point> contour2 ) {
double i = fabs( contourArea(cv::Mat(contour1)) );
......@@ -149,24 +184,24 @@ void findBall() {
float radius;
minEnclosingCircle(contours[0], center, radius);
if(radius > 10){
ballPosition = center;
var->ballPosition = center;
return;
}
}
ballPosition = Point2d(static_cast<double>(-NAN), static_cast<double>(-NAN));
var->ballPosition = Point2d(static_cast<double>(-NAN), static_cast<double>(-NAN));
}
void detectAlly() {
void detectAlly(Global::ThreadVariables* var) {
Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
Mat frame_HSV = frameHSV;
Mat frame_HSV = var->frameHSV;
inRange(frame_HSV, Scalar(Global::allyLH, Global::allyLS, Global::allyLV),
Scalar(Global::allyUH, Global::allyUS, Global::allyUV), thresholdAllyC);
Scalar(Global::allyUH, Global::allyUS, Global::allyUV), var->thresholdAllyC);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(thresholdAllyC, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
findContours(var->thresholdAllyC, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
qApp->processEvents();
lastAllyFormats.clear();
......@@ -184,14 +219,14 @@ void detectAlly() {
approxPolyDP(contour, points, 0.05 * arcLength(contour, true), true);
if (points.size() == 3) {
if(!rob3Found){
posRobots[0] = (points[0] + points[1] + points[2])/3;
var->posRobots[0] = (points[0] + points[1] + points[2])/3;
lastAllyFormats.emplace_back(points);
rob3Found = true;
}
}
if (points.size() == 4) {
if(!rob4Found){
posRobots[1] = (points[0] + points[1] + points[2] + points[3])/4;
var->posRobots[1] = (points[0] + points[1] + points[2] + points[3])/4;
lastAllyFormats.emplace_back(points);
rob4Found = true;
}
......@@ -199,7 +234,7 @@ void detectAlly() {
}
if (points.size() == 5) {
if(!rob5Found){
posRobots[2] = (points[0] + points[1] + points[2] + points[3] + points[4])/5;
var->posRobots[2] = (points[0] + points[1] + points[2] + points[3] + points[4])/5;
lastAllyFormats.emplace_back(points);
}
......@@ -210,9 +245,9 @@ void detectAlly() {
contours.clear();
hierarchy.clear();
inRange(frame_HSV, Scalar(Global::allyFLH, Global::allyFLS, Global::allyFLV),
Scalar(Global::allyFUH, Global::allyFUS, Global::allyFUV), thresholdAllyF);
Scalar(Global::allyFUH, Global::allyFUS, Global::allyFUV), var->thresholdAllyF);
findContours(thresholdAllyF, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
findContours(var->thresholdAllyF, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
qApp->processEvents();
vector<vector<Point2d>> triangles;
lastTriangles.clear();
......@@ -260,22 +295,22 @@ void detectAlly() {
Point2d media = (triangles[0][0] + triangles[0][1] + triangles[0][2]
+ triangles[k][0] + triangles[k][1] + triangles[k][2]) / 6;
vector<tuple<double, size_t>> distRobots;
distRobots.emplace_back(tuple(norm(posRobots[0] - media), 0));
distRobots.emplace_back(tuple(norm(posRobots[1] - media), 1));
distRobots.emplace_back(tuple(norm(posRobots[2] - media), 2));
distRobots.emplace_back(tuple(norm(var->posRobots[0] - media), 0));
distRobots.emplace_back(tuple(norm(var->posRobots[1] - media), 1));
distRobots.emplace_back(tuple(norm(var->posRobots[2] - media), 2));
sort(distRobots.begin(), distRobots.end(), [=](tuple<double, int> a, tuple<double, int> b){
return get<double>(a) < get<double>(b);
});
size_t robotIndex = get<size_t>(distRobots[0]);
frenteRobots[robotIndex] = (triangles[0][(x+2)%3] + triangles[k][(y+2)%3]) / 2;
var->frenteRobots[robotIndex] = (triangles[0][(x+2)%3] + triangles[k][(y+2)%3]) / 2;
sort(triangles[0].begin(), triangles[0].end(), [=](Point2d a, Point2d b){
return norm(posRobots[robotIndex] - a) > norm(posRobots[robotIndex] - b);
return norm(var->posRobots[robotIndex] - a) > norm(var->posRobots[robotIndex] - b);
});
sort(triangles[k].begin(), triangles[k].end(), [=](Point2d a, Point2d b){
return norm(posRobots[robotIndex] - a) > norm(posRobots[robotIndex] - b);
return norm(var->posRobots[robotIndex] - a) > norm(var->posRobots[robotIndex] - b);
});
contornosRobots[robotIndex].clear();
Point2d aux = frenteRobots[robotIndex] - posRobots[robotIndex];
var->contornosRobots[robotIndex].clear();
Point2d aux = var->frenteRobots[robotIndex] - var->posRobots[robotIndex];
Point2d esq, dir;
double angulo = atan2(aux.y, aux.x) * 180 / PI;
if(angulo < -5 && angulo > -175 ){
......@@ -320,11 +355,11 @@ void detectAlly() {
aux = dir - rb;
Point2d lb(aux.x*cos(-90*PI/180)-aux.y*sin(-90 * PI/180), aux.x*sin(-90 * PI/180)+aux.y*cos(-90 * PI/180));
lb+=rb;
contornosRobots[robotIndex].emplace_back(esq);
contornosRobots[robotIndex].emplace_back(dir);
contornosRobots[robotIndex].emplace_back(rb);
contornosRobots[robotIndex].emplace_back(lb);
posRobots[robotIndex] = (esq + dir + rb + lb)/4;
var->contornosRobots[robotIndex].emplace_back(esq);
var->contornosRobots[robotIndex].emplace_back(dir);
var->contornosRobots[robotIndex].emplace_back(rb);
var->contornosRobots[robotIndex].emplace_back(lb);
var->posRobots[robotIndex] = (esq + dir + rb + lb)/4;
triangles.erase(triangles.begin() + 0);
triangles.erase(triangles.begin() + static_cast<int>(k - 1));
}
......@@ -332,15 +367,15 @@ void detectAlly() {
}
}
void findEnemies() {
Mat frame_HSV = frameHSV;
void findEnemies(Global::ThreadVariables* var) {
Mat frame_HSV = var->frameHSV;
inRange(frame_HSV, Scalar(Global::enemyLH, Global::enemyLS, Global::enemyLV),
Scalar(Global::enemyUH, Global::enemyUS, Global::enemyUV), thresholdEnemy);
Scalar(Global::enemyUH, Global::enemyUS, Global::enemyUV), var->thresholdEnemy);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(5, 5));
erode(thresholdBall, thresholdBall, kernel, Point(1, 1), 1);
findContours(thresholdEnemy, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
erode(var->thresholdEnemy, var->thresholdEnemy, kernel, Point(1, 1), 1);
findContours(var->thresholdEnemy, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0));
vector<Moments> mu;
for (size_t i = 0; i < contours.size(); i++) {
......@@ -370,12 +405,12 @@ void findEnemies() {
mc.push_back(vectorBackup[k]);
ret.push_back(ponto);
}
enemies = ret;;
var->enemies = ret;;
}else if (mc.size() > 0){
enemies = mc;
var->enemies = mc;
}else{
mc.push_back(Point2f(-NAN, -NAN));
enemies = mc;
var->enemies = mc;
}
}
......
......@@ -3,7 +3,8 @@
#include <chrono>
#include <opencv2/opencv.hpp>
#include <QTimer>
#include <QThread>
#include "global.h"
extern std::chrono::high_resolution_clock::time_point lastFrame;
extern QTimer timer;
extern double counter;
......@@ -13,9 +14,19 @@ void startCapture();
void calibration();
void gerenciador();
void getFrame();
void frameMod();
void findBall();
void detectAlly();
void findEnemies();
void frameMod(Global::ThreadVariables* var);
void findBall(Global::ThreadVariables* var);
void detectAlly(Global::ThreadVariables* var);
void findEnemies(Global::ThreadVariables* var);
void estrategia();
class Threader : public QThread{
public:
Threader();
void run();
};
extern std::vector<Threader*> threads;
#endif // BACKEND_HPP
#include <opencv2/opencv.hpp>
#include "global.h"
#include <mutex>
#include <QDebug>
using namespace std;
using namespace cv;
namespace Global {
int defaultImageHeight;
vector<vector<Point2d>> lastTriangles;
vector<vector<Point2d>> lastAllyFormats;
bool showAllyThreshold = true;
bool showAllyFThreshold = true;
Mutex mtx;
int cameraStatus = 0;
VideoCapture cap;
bool capActive = false;
Mat frame;
Mat correctedFrame;
Mat frameHSV;
Mat thresholdAllyF;
Mat thresholdAllyC;
Mat thresholdBall;
Mat thresholdEnemy;
int frames = 0;
int fps = 0;
bool frameModReady = true;
bool enemyDetectionReady = true;
bool ballDetectionReady = true;
bool allyDetectionReady = true;
bool strategyCompilerReady = true;
ThreadVariables::~ThreadVariables(){
Global::frame = frame;
Global::frameHSV = frameHSV;
Global::correctedFrame = correctedFrame;
Global::thresholdBall = thresholdBall;
Global::thresholdAllyF = thresholdAllyF;
Global::thresholdAllyC = thresholdAllyC;
Global::thresholdEnemy = thresholdEnemy;
Global::posRobots = posRobots;
Global::frenteRobots = frenteRobots;
Global::contornosRobots = contornosRobots;
Global::enemies = enemies;
Global::ballPosition = ballPosition;
}
ThreadVariables::ThreadVariables(){
posRobots = vector<Point2d>(3);
frenteRobots = vector<Point2d>(3);
contornosRobots = vector<vector<Point2d>>(3);
};
int defaultImageHeight;
vector<vector<Point2d>> lastTriangles;
vector<vector<Point2d>> lastAllyFormats;
bool showAllyThreshold = true;
bool showAllyFThreshold = true;
Mutex mtx;
int cameraStatus = 0;
VideoCapture cap;
bool capActive = false;
Mat frame;
Mat correctedFrame;
Mat frameHSV;
Mat thresholdAllyF;
Mat thresholdAllyC;
Mat thresholdBall;
Mat thresholdEnemy;
int frames = 0;
int fps = 0;
bool frameModReady = true;
bool enemyDetectionReady = true;
bool ballDetectionReady = true;
bool allyDetectionReady = true;
bool strategyCompilerReady = true;
int actualTab;
const double proporcao = 7.5;
const int comprimento = 150;
const int altura = 130;
const cv::Point2d marcas[] = {
cv::Point2d(25, 37.5),
cv::Point2d(65, 37.5),
cv::Point2d(105, 37.5),
cv::Point2d(25, 112.5),
cv::Point2d(65, 112.5),
cv::Point2d(105, 112.5)
};
const int alturaGol = 40;
const int alturaAG = 70;
const int larguraAG = 15;
int ballUH = 20;
int ballLH = 9;
int ballUS = 255;
int ballLS = 120;
int ballUV = 255;
int ballLV = 120;
cv::Point2d ballPosition(0, 0);
int actualTab; ///< Index atual do tab widget
/**
* @defgroup Global
* @brief Constantes e variáveis globais do software
* @addtogroup Global
* @{
* @defgroup Constantes
* @brief Constantes do software
* @addtogroup Constantes
* @{
*/
const double proporcao = 7.5; ///< Proporção do tamanho do campo para píxels, equivale a 1 cm
const int comprimento = 150; ///< Largura do campo em cm
const int altura = 130; ///< Altura do campo em cm
const cv::Point2d marcas[] = {
cv::Point2d(25, 37.5),
cv::Point2d(65, 37.5),
cv::Point2d(105, 37.5),
cv::Point2d(25, 112.5),
cv::Point2d(65, 112.5),
cv::Point2d(105, 112.5)
}; ///< Posição das marcações do campo
const int alturaGol = 40; ///< Altura do gol
const int alturaAG = 70; ///< Altura da área do gol
const int larguraAG = 15; ///< Largura da área do gol
/**
*@}
* @defgroup Bola
* @brief Variáveis utilizadas no reconhecimento da bola
* @addtogroup Bola
* @{
*/
int ballUH = 20; ///< Hue máximo
int ballLH = 9; ///< Hue mínimo
int ballUS = 255; ///< Saturation máximo
int ballLS = 120; ///< Saturation mínimo
int ballUV = 255; ///< Value máximo
int ballLV = 120; ///< Value mínimo
cv::Point2d ballPosition(0, 0);
///@}
int enemyUH = 60;
int enemyLH = 25;
int enemyUS = 255;
int enemyLS = 120;
int enemyUV = 255;
int enemyLV = 120;
double distanceTolerance = 15;
vector<cv::Point2d> enemies;
/**
* @defgroup Oponentes
* @brief Variáveis usadas no reconhecimento dos oponentes
* @addtogroup Oponentes
* @{
*/
int enemyUH = 60; ///< Hue máximo
int enemyLH = 25; ///< Hue mínimo
int enemyUS = 255; ///< Saturation máximo
int enemyLS = 120; ///< Saturation mínimo
int enemyUV = 255; ///< Value máximo
int enemyLV = 120; ///< Value mínimo
double distanceTolerance = 15; ///< Tolerância de distancia entre pontos para considerar como um robô
vector<cv::Point2d> enemies;
///@}
vector<Point2d> posRobots(3);
vector<Point2d> frenteRobots(3);
vector<vector<Point2d>> contornosRobots(3);
/**
* @defgroup Aliados
* @brief Vaiáveis de localização dos nossos robôs
* @addtogroup Aliados
* @{
*/
vector<Point2d> posRobots(3);
vector<Point2d> frenteRobots(3);
vector<vector<Point2d>> contornosRobots(3);
int allyUH = 150;
int allyLH = 100;
int allyUS = 255;
int allyLS = 120;
int allyUV = 255;
int allyLV = 120;
int allyUH = 150; ///< Hue máximo
int allyLH = 100; ///< Hue mínimo
int allyUS = 255; ///< Saturation máximo
int allyLS = 120; ///< Saturation mínimo
int allyUV = 255; ///< Value máximo
int allyLV = 120; ///< Value mínimo
int allyFUH = 180;
int allyFLH = 146;
int allyFUS = 255;
int allyFLS = 120;
int allyFUV = 255;
int allyFLV = 120;
int DISTANCE = 15;
int allyFUH = 180; ///< Hue máximo
int allyFLH = 146; ///< Hue mínimo
int allyFUS = 255; ///< Saturation máximo
int allyFLS = 120; ///< Saturation mínimo
int allyFUV = 255; ///< Value máximo
int allyFLV = 120; ///< Value mínimo
int DISTANCE = 15;
/**
* @}
* @defgroup Calibração
* @brief Variáveis usadas na calibração da câmera
* @addtogroup Calibração
* @{
*/
std::vector<cv::Point2d> corners(4); ///< Cantos do campo
cv::Point2d midLeft; ///< Centro esquerdo do campo
cv::Point2d midRight; ///< Centro direito do campo
double rotation = 0; ///< Angulo de rotação do campo
int confStatus = 0; ///< Estado atual da configuração
bool confConcluded = false; ///< Diz se a configuração foi terminada ou não
cv::Mat homography; ///< Homografia para correção de perspectiva
std::string calibrationFramesPath = "/home/kramer/frames"; ///< Caminho para os frames de calibração
std::vector<std::vector<cv::Point3f>> object_points; ///<Variável utilizada pela função de calibração
std::vector<std::vector<cv::Point2f>> image_points; ///<Variável utilizada pela função de calibração
std::vector<cv::Point2d> corners(4);
cv::Point2d midLeft;
cv::Point2d midRight;
double rotation = 0;
int confStatus = 0;
bool confConcluded = false;
cv::Mat homography;
std::string calibrationFramesPath = "/home/kramer/frames";
std::vector<std::vector<cv::Point3f>> object_points;
std::vector<std::vector<cv::Point2f>> image_points;
cv:: Mat intrinsic = cv::Mat(3, 3, CV_32FC1); ///<Variável utilizada pela função de calibração
cv::Mat distCoeffs; ///<Variável utilizada pela função de calibração
std::vector<cv::Mat> rvecs; ///<Variável utilizada pela função de calibração
std::vector<cv::Mat> tvecs; ///<Variável utilizada pela função de calibração
///@}
///@}