Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include "stdafx.h"
- #include <iostream>
- #include <fstream>
- #include <sstream>
- #include "opencv2/core/core.hpp"
- #include "opencv2/opencv.hpp"
- #include "opencv2/face.hpp"
- #include "opencv2/highgui/highgui.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include "opencv2/objdetect/objdetect.hpp"
- namespace FaceTracking4 {
- using namespace System;
- using namespace System::ComponentModel;
- using namespace System::Collections;
- using namespace System::Windows::Forms;
- using namespace System::Data;
- using namespace System::Drawing;
- using namespace cv;
- using namespace std;
- using namespace System::Runtime::InteropServices;
- /// <summary>
- /// Summary for Form1
- /// </summary>
- public ref class Form1 : public System::Windows::Forms::Form
- {
- public:
- Form1(void)
- {
- InitializeComponent();
- //
- //TODO: Add the constructor code here
- //
- }
- static void read_csv(const string& filename, vector<Mat>& images, vector<int>& labels, char separator)
- {
- std::ifstream file(filename.c_str(), ifstream::in);
- if (!file)
- {
- string error_message = "No valid input file was given, please check the given filename.";
- CV_Error(CV_StsBadArg, error_message);
- }
- string line, path, classlabel;
- while (getline(file, line))
- {
- stringstream liness(line);
- getline(liness, path, separator);
- getline(liness, classlabel);
- if(!path.empty() && !classlabel.empty())
- {
- images.push_back(imread(path, 0));
- labels.push_back(atoi(classlabel.c_str()));
- }
- }
- }
- protected:
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- ~Form1()
- {
- if (components)
- {
- delete components;
- }
- }
- protected:
- private: System::Windows::Forms::Panel^ panel1;
- private: System::Windows::Forms::Button^ button1;
- private: System::Windows::Forms::OpenFileDialog^ openFileDialog1;
- private: System::Windows::Forms::PictureBox^ pictureBox1;
- private:
- /// <summary>
- /// Required designer variable.
- /// </summary>
- System::ComponentModel::Container ^components;
- #pragma region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- void InitializeComponent(void)
- {
- this->panel1 = (gcnew System::Windows::Forms::Panel());
- this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
- this->button1 = (gcnew System::Windows::Forms::Button());
- this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog());
- this->panel1->SuspendLayout();
- (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->BeginInit();
- this->SuspendLayout();
- //
- // panel1
- //
- this->panel1->AutoScroll = true;
- this->panel1->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
- this->panel1->Controls->Add(this->pictureBox1);
- this->panel1->Location = System::Drawing::Point(16, 15);
- this->panel1->Name = L"panel1";
- this->panel1->Size = System::Drawing::Size(676, 396);
- this->panel1->TabIndex = 3;
- //
- // pictureBox1
- //
- this->pictureBox1->Location = System::Drawing::Point(3, 3);
- this->pictureBox1->Name = L"pictureBox1";
- this->pictureBox1->Size = System::Drawing::Size(668, 388);
- this->pictureBox1->TabIndex = 0;
- this->pictureBox1->TabStop = false;
- //
- // button1
- //
- this->button1->Location = System::Drawing::Point(301, 421);
- this->button1->Name = L"button1";
- this->button1->Size = System::Drawing::Size(103, 23);
- this->button1->TabIndex = 0;
- this->button1->Text = L"Load Video";
- this->button1->UseVisualStyleBackColor = true;
- this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
- //
- // openFileDialog1
- //
- this->openFileDialog1->FileName = L"openFileDialog1";
- //
- // Form1
- //
- this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
- this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
- this->ClientSize = System::Drawing::Size(704, 452);
- this->Controls->Add(this->button1);
- this->Controls->Add(this->panel1);
- this->Name = L"Form1";
- this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
- this->Text = L"Face Tracking";
- this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
- this->Move += gcnew System::EventHandler(this, &Form1::Form1_Move);
- this->panel1->ResumeLayout(false);
- (cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->EndInit();
- this->ResumeLayout(false);
- }
- #pragma endregion
- private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
- {
- //pictureBox1->Image=Image::FromFile("5.jpg");
- if(openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
- {
- string fn_csv = "test.txt";
- vector<Mat> images;
- vector<int> labels;
- // Read in the data (fails if no valid input filename is given, but you'll get an error message):
- try
- {
- read_csv(fn_csv, images, labels,';');
- //images.push_back(imread("person0/0.jpg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(0);
- //images.push_back(imread("person0/1.jpg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(0);
- //images.push_back(imread("person1/0.jpg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(1);
- //images.push_back(imread("person1/1.jpg", CV_LOAD_IMAGE_GRAYSCALE)); labels.push_back(1);
- }
- catch (cv::Exception& e)
- {
- //cerr << "Error opening file \"" << fn_csv << "\". Reason: " << e.msg << endl;
- // nothing more we can do
- MessageBox::Show("Error opening file");
- return;
- }
- // Get the height from the first image. We'll need this
- // later in code to reshape the images to their original
- // size AND we need to reshape incoming faces to this size:
- int im_width = images[0].cols;
- int im_height = images[0].rows;
- // Create a FaceRecognizer and train it on the given images:
- Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
- //Ptr<FaceRecognizer> model = createFisherFaceRecognizer(0, DBL_MAX);
- model->train(images, labels);
- CascadeClassifier haar_cascade;
- haar_cascade.load("haarcascade_frontalface_default.xml");
- char *fileName = (char*) Marshal::StringToHGlobalAnsi(openFileDialog1->FileName).ToPointer();
- VideoCapture cap(fileName);
- if(!cap.isOpened()) // if not success, exit program
- {
- MessageBox::Show("Cannot open the video file");
- return;
- }
- double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
- //MessageBox::Show(fps.ToString());
- namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
- moveWindow("MyVideo",this->Left + 40,this->Top + 60);
- pictureBox1->Width=cap.get(CV_CAP_PROP_FRAME_WIDTH);
- pictureBox1->Height=cap.get(CV_CAP_PROP_FRAME_HEIGHT);
- double count;
- count = 1;
- while(1)
- {
- //cap.set(CV_CAP_PROP_POS_FRAMES, count); //start the video at 300ms
- Mat frame;
- bool bSuccess = cap.read(frame); // read a new frame from video
- if (!bSuccess) //if not success, break loop
- {
- //MessageBox::Show("The end");
- destroyWindow("MyVideo");
- break;
- }
- Mat original = frame.clone();
- // Convert the current frame to grayscale:
- Mat gray;
- cvtColor(original, gray, CV_BGR2GRAY);
- // Find the faces in the frame:
- vector< Rect_<int> > faces;
- haar_cascade.detectMultiScale(gray, faces);
- // At this point you have the position of the faces in
- // faces. Now we'll get the faces, make a prediction and
- // annotate it in the video. Cool or what?
- for(int i = 0; i < faces.size(); i++)
- {
- // Process face by face:
- Rect face_i = faces[i];
- // Crop the face from the image. So simple with OpenCV C++:
- Mat face = gray(face_i);
- // Resizing the face is necessary for Eigenfaces and Fisherfaces. You can easily
- // verify this, by reading through the face recognition tutorial coming with OpenCV.
- // Resizing IS NOT NEEDED for Local Binary Patterns Histograms, so preparing the
- // input data really depends on the algorithm used.
- //
- // I strongly encourage you to play around with the algorithms. See which work best
- // in your scenario, LBPH should always be a contender for robust face recognition.
- //
- // Since I am showing the Fisherfaces algorithm here, I also show how to resize the
- // face you have just found:
- Mat face_resized;
- cv::resize(face, face_resized, cv::Size(im_width, im_height), 1.0, 1.0, INTER_CUBIC);
- // Now perform the prediction, see how easy that is:
- int prediction = model->predict(face_resized);
- // And finally write all we've found out to the original image!
- // First of all draw a green rectangle around the detected face:
- rectangle(original, face_i, CV_RGB(0, 255,0), 1);
- // Create the text we will annotate the box with:
- string box_text = format("Prediction = %d", prediction);
- //string box_text = format("Prediction");
- // Calculate the position for annotated text (make sure we don't
- // put illegal values in there):
- int pos_x = std::max(face_i.tl().x - 10, 0);
- int pos_y = std::max(face_i.tl().y - 10, 0);
- // And now put it into the image:
- putText(original, box_text,cv::Point(pos_x, pos_y), FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);
- }
- //imshow("MyVideo", frame); //show the frame in "MyVideo" window
- imshow("MyVideo", original); //show the frame in "MyVideo" window
- //pictureBox1->Width=cap.get(CV_CAP_PROP_FRAME_WIDTH);
- //pictureBox1->Height=cap.get(CV_CAP_PROP_FRAME_HEIGHT);
- /*System::Drawing::Graphics^ graphics2 = pictureBox1->CreateGraphics();
- System::IntPtr ptr2(original.ptr());
- System::Drawing::Bitmap^ b2 = gcnew System::Drawing::Bitmap(original.cols,original.rows,original.step,System::Drawing::Imaging::PixelFormat::Format24bppRgb,ptr2);
- System::Drawing::RectangleF rect2(0,0, pictureBox1->Width,pictureBox1->Height);
- //System::Drawing::RectangleF rect2(0,0, original.cols,original.rows);
- graphics2->DrawImage(b2,rect2);
- //pictureBox1->Refresh();
- delete graphics2;*/
- /*assert(original.type() == CV_8UC3);
- if ((pictureBox1->Image == nullptr) || (pictureBox1->Width != original.cols) || (pictureBox1->Height != original.rows))
- {
- pictureBox1->Width = original.cols;
- pictureBox1->Height = original.rows;
- pictureBox1->Image = gcnew System::Drawing::Bitmap(original.cols, original.rows);
- }
- // Create System::Drawing::Bitmap from cv::Mat
- System::Drawing::Bitmap^ bmpImage = gcnew Bitmap(original.cols, original.rows, original.step,System::Drawing::Imaging::PixelFormat::Format24bppRgb,System::IntPtr(original.data));
- // Draw Bitmap over a PictureBox
- Graphics^ g = Graphics::FromImage(pictureBox1->Image);
- g->DrawImage(bmpImage, 0, 0, original.cols, original.rows);
- pictureBox1->Refresh();
- delete g;*/
- /*System::Drawing::Graphics^ graphics = pictureBox1->CreateGraphics();
- System::IntPtr ptr(original.ptr());
- System::Drawing::Bitmap^ b = gcnew System::Drawing::Bitmap(original.cols,original.rows,original.step,System::Drawing::Imaging::PixelFormat::Format24bppRgb,ptr);
- System::Drawing::RectangleF rect(0,0,pictureBox1->Width,pictureBox1->Height);
- graphics->DrawImage(b,rect);
- delete graphics;*/
- if(waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
- {
- MessageBox::Show("esc key is pressed by user");
- destroyWindow("MyVideo");
- destroyAllWindows();
- break;
- }
- //count+=5;
- count++;
- //cout <<count<<endl;
- }
- //MessageBox::Show(openFileDialog1->FileName);
- //pictureBox1->Image=Image::FromFile(openFileDialog1->FileName);
- System::Drawing::Image^ img = pictureBox1->Image;
- pictureBox1->Image = nullptr;
- delete img;
- }
- }
- private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
- {
- }
- private: System::Void Form1_Move(System::Object^ sender, System::EventArgs^ e)
- {
- moveWindow("MyVideo",this->Left + 40,this->Top + 60);
- //updateWindow("MyVideo");
- //cvGetWindowHandle("MyVideo");
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement