Anda di halaman 1dari 4

// uts deteksi ngantuk.

cpp : Defines the entry point for the console


application.

#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <math.h>
#include <time.h>
#include <windows.h>

using namespace std;


using namespace cv;

// header fungsi //
int detectAndDisplay( Mat frame, int pilih ); //fungsi deteksi wajah dan
mata
cv::Mat Display1(Mat frame); //fungsi display

// global variabel //
String muka = "haarcascade_frontalface_alt2.xml"; // file xml yang
dipakai
String mata = "haarcascade_eye_tree_eyeglasses.xml";

CascadeClassifier muka_cascade;
CascadeClassifier mata_cascade;

string window_name1 = "deteksi kantuk"; // nama window


int deteksi,pilih,buff_det, level;
RNG rng(12345);
int status;
double mulai=0, selesai=0;

// fungsi utama //
int main( int argc, const char** argv )
{

double dif_t=0;

CvCapture* capture;
capture = cvCaptureFromCAM(0);
cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH,320);
cvSetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT,240);

cvNamedWindow("deteksi kantuk", CV_WINDOW_AUTOSIZE);

Mat frame;
muka_cascade.load( muka );
mata_cascade.load( mata );

if( !muka_cascade.load( muka ) ){ printf("--(!)Error loading\n");}//


load file xml
if( !mata_cascade.load( mata ) ){ printf("--(!)Error loading\n");}

for(;;){
frame = cvQueryFrame( capture );
// camera capture
resize(frame, frame, Size(), 0.7, 0.7, INTER_LINEAR); //
resize image skala 0,7

detectAndDisplay(frame,pilih);
//panggil fungsi deteksi wajah dan mata

if (deteksi>buff_det){mulai = GetTickCount();}
// jika mata mulai terpejam maka hitung waktu dimulai

if (deteksi==1)
{
selesai = GetTickCount();
// hitung waktu berhenti
dif_t = (selesai-mulai)/1000;
// menghitung selang waktu mulai da selesai dlm s
}

if(dif_t>=0.5 && dif_t<1){level=1;}


// pengambilan keputusan
else if(dif_t>=1 && dif_t<2){level=2;}
else if(dif_t>=2){level=3;}
else {level=0;}

buff_det = deteksi;
system("cls");
// display
cout<<"PROGRAM DETEKSI KANTUK SEDERHANA "<<endl;
cout<<"-- BERDASARKAN LAMA DURASI MATA TERPEJAM--"<<endl<<endl;
cout<<"mata terpejam = "<<deteksi<<endl<<"t mata terpejam :
"<<dif_t<<" detik"<<endl;
switch (level)
{
case 0: cout<<"STATUS : NORMAL"<<endl; break;
case 1: {cout<<"STATUS : PERINGATAN LEVEL1: MULAI
MENGANTUK"<<endl;//<<Beep(500,2000); // beep 500 hz t=2 s
break;}
case 2: {cout<<"STATUS : PERINGATAN LEVEL2:
MENGANTUK"<<endl;//<<Beep(550,3000); // beep 550 hz t=3 s
break;}
case 3: {cout<<"STATUS : PERINGATAN LEVEL1: SANGAT
MENGANTUK"<<endl;//<<//Beep(700,5000); //beep 700 hz t=5 s
break;}
default: break;
}
dif_t=0;
if (cvWaitKey(33) == 29) break;
}
return 0;
}
//--fungsi detect dan display--//
int detectAndDisplay( Mat frame, int pilih )
{
std::vector<Rect> faces;
std::vector<Rect> eyes;
Mat frame_gray;

cvtColor( frame, frame_gray, CV_BGR2GRAY );


equalizeHist( frame_gray, frame_gray );

//-- Detect faces --

muka_cascade.detectMultiScale( frame_gray, faces, 1.1, 2,


0|CV_HAAR_FIND_BIGGEST_OBJECT,Size(20, 20) );

for( int i = 0; i < faces.size(); i++ )


{
Mat faceROI = frame_gray( faces[i] );

//-- In each face, detect eyes --


mata_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0
|CV_HAAR_SCALE_IMAGE, Size(5, 5) );

//-- Draw the face --


Point center( faces[i].x + faces[i].width*0.5, faces[i].y +
faces[i].height*0.5 );
ellipse( frame, center, Size( faces[i].width*0.5,
faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 0 ), 2, 8, 0 );

if(faces.size()!=0 && eyes.size()==0 ){deteksi=1;}


else if(faces.size()==0){deteksi=0;}
else {deteksi=0;}

for( int j = 0; j < eyes.size(); j++ )


{ //-- Draw the eyes --
Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5,
faces[i].y + eyes[j].y + eyes[j].height*0.5 );
int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
circle( frame, center, radius, Scalar( 0, 0, 255 ), 2, 8, 0 );
}
}
Display1(frame);
return deteksi;
}

//-- Fungsi Display --


cv::Mat Display1(Mat frame)
{
imshow( window_name1, frame );
return frame;
}
Hasil Dari Program Di atas saat Status Normal

Hasil Dari Program Di atas saat Status Peringatan Level 2 : Mengantuk