Files
AdvancedVision/NeuralDetector/Main.cpp
T
2017-12-20 16:41:32 +01:00

125 lines
2.3 KiB
C++

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include "Training.h"
#include "Camera.h"
#include "FeatureExtractor.h"
using namespace std;
using namespace cv;
int run();
int main(int argc, char** argv)
{
Camera cam(1);
Training tr;
cout << "Welcome to Jarvis" << endl;
bool running = true;
while (running)
{
cout << endl << "What would you like to do?" << endl;
cout << "Calibrate the camera (c), take pictures (p), train the network (t), run the neural network (r) or exit (e)?" << endl;
char c;
cin >> c;
cout << endl;
switch (c) {
case 'c':
cam.Calibrate();
break;
case 'p':
tr.CreateTrainingSet();
break;
case 't':
tr.LoadTrainingSet();
break;
case 'r':
run();
break;
case 'e':
running = false;
break;
default:
return 0;
}
}
cout << "The program has finished" << endl;
return 0;
}
int run()
{
Camera cam = Camera(1);
NeuralNetwork bpn;
bpn.Read();
cout << "The neural network is ready to be used" << endl;
cout << "Please place an item below the camera and press the spacebar" << endl << endl;
bool running = true;
while (running)
{
//Take picture and pre-process
Mat image, gray_image, binaryImage;
image = cam.takeImage();
cvtColor(image, gray_image, CV_BGR2GRAY);
//Find contour
vector<Point> contour;
FeatureExtractor::findContour(gray_image, contour);
//Extrax features
FeatureExtractor ext = FeatureExtractor(contour);
Mat descriptors = Mat_<double>();
ext.Extract(descriptors);
descriptors = transpose(descriptors);
string cls;
bpn.Predict(descriptors, cls);
cout << "It\'s a " << cls << endl;
// teken de contouren
Point2f vertices2f[4];
ext.Rectangle().points(vertices2f);
// Convert them so we can use them in a fillConvexPoly
Point vertices[4];
for (int i = 0; i < 4; ++i) {
vertices[i] = vertices2f[i];
}
// Now we can fill the rotated rectangle with our specified color
fillConvexPoly(image, vertices, 4, Scalar(0, 0, 255));
vector<vector<Point>> contours;
contours.push_back(contour);
drawContours(image, contours, -1, CV_RGB(0, 255, 0), 4);
putText(image, cls, cvPoint(15, 30), FONT_HERSHEY_COMPLEX, 1.0, cvScalar(0, 0, 0), 1, CV_AA);
imshow("Neural detection", image);
auto c = waitKey(0);
if (c == 27)
{
running = false;
}
destroyAllWindows();
}
}