Skip to content
Snippets Groups Projects
FacialRec.cpp 2.27 KiB
Newer Older
johni's avatar
johni committed
//This is our project

#include <iostream>
johni's avatar
johni committed
#include <string>

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

using namespace std;
using namespace cv;

Mat picture;
char key;

johni's avatar
johni committed
class FacialRec {

};

int main() {
	//Starting the video
	VideoCapture videoCapture(0);

	if (!videoCapture.isOpened()) {
		cout << "Unable to open video file." << endl;
		return 1;
	}

	namedWindow("Webcam", CV_WINDOW_AUTOSIZE);

johni's avatar
johni committed

	while(true) {
		Mat frame;
		videoCapture.retrieve(frame);
		bool success = videoCapture.read(frame);
		if (!success) {
			cout << "Could not read from video file" << endl;
			return 1;
		}

		//show some test text
		Mat frame_with_text = frame.clone();
		putText(frame_with_text, "Press Spacebar to take a picture", Point2f(110,100), FONT_HERSHEY_SIMPLEX, 2.0,  Scalar(255,0,0,0), 3);
		putText(frame_with_text, "Press ESC to close the application", Point2f(90,600), FONT_HERSHEY_SIMPLEX, 2.0,  Scalar(0,0,255,255), 3);

		imshow("Webcam", frame_with_text);

		key = waitKey(30);

		if (key == 27) { //escape key pressed: stop program
			cout << "ESC pressed. Program closing..." << endl;
			break;
Noah Eigenfeld's avatar
Noah Eigenfeld committed
		else if (key == ' ') { //spacebar pressed: take a picture
			picture = frame;
			key = -1;

			while (true) {
				Mat picture_with_text = picture.clone();
				putText(picture_with_text, "Press 's' to save", Point2f(375,100), FONT_HERSHEY_SIMPLEX, 2.0,  Scalar(255,0,0,0), 3);
johni's avatar
johni committed
				putText(picture_with_text, "Press ESC/Spacebar to return", Point2f(160,600), FONT_HERSHEY_SIMPLEX, 2.0,  Scalar(0,0,255), 3);

				imshow("Webcam", picture_with_text);
johni's avatar
johni committed

				key = waitKey(30);
johni's avatar
johni committed

				if (key == 27 || key == 32) { //spacebar or ESC pressed
					cout << "ESC or SPACE pressed. Returning to video..." << endl;
					break;
Noah Eigenfeld's avatar
Noah Eigenfeld committed
				} else if (key == 115) {
johni's avatar
johni committed
          string name;
          string directory = "./images/";
          cout << "Please select a name for the image." << endl;
          getline(cin, name);
          directory.append(name);
          directory.append(".jpg");
          Point org;
          org.x = 15;
          org.y = 100;
johni's avatar
johni committed
          putText(picture, name, org, FONT_HERSHEY_SIMPLEX, 2, Scalar(0, 0, 255), 3);
johni's avatar
johni committed
					bool maybe = imwrite(directory, picture);
					cout << "s was pressed. saving image, success: " << maybe << endl;
johni's avatar
johni committed

johni's avatar
johni committed
}