#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "opencv/highgui.h"
#include "opencv2/opencv.hpp"
#include "../stasm/stasm_lib.h"
#include "../stasm/stasm_landmarks.h"


static const char* path = "./img.jpg";
cv::VideoCapture cap(0);
int foundface;
float landmarks[2 * stasm_NLANDMARKS];


int main(int argc, char** argv)
{
	cv::Mat_<unsigned char> img, frame;

	if(!cap.isOpened())
	{
		std::cout<<"camera is not opened";
		return -1;
	}

	std::cout<<"Reading Habits Monitor is working...";
		while(1)
	{
        cap >> img; // get a new frame from camera
		cv::cvtColor(img, frame, CV_BGR2GRAY);

		if(!frame.data)
		{
			std::cout<<"No image loaded";
			return -1;
		}

		if(!stasm_search_single(&foundface, landmarks, (const char*)frame.data, frame.cols, frame.rows, path, "../data"))
		{
			std::cout<<"stasm_search_single failed";
			return -1;
		}

        if (!foundface)
			std::cout<<"No face found"<<std::endl;

		stasm_force_points_into_image(landmarks,frame.cols, frame.rows);
		for(int i = 0; i < stasm_NLANDMARKS; i++)
			frame(cvRound(landmarks[i*2+1]), cvRound(landmarks[i*2])) = 255;

		//cv::flip(frame, output, 1);//mirror output
		cv::imshow("camera", frame);
        if(cv::waitKey(30) >= 0) break;
	}

	return 0;
}