Anda di halaman 1dari 8

Nama : Tarmuji Rauf

Nim : 426 14 002

Nomor 1
// 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;
Nama : Tarmuji Rauf
Nim : 426 14 002
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:
Nama : Tarmuji Rauf
Nim : 426 14 002
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 --
Nama : Tarmuji Rauf
Nim : 426 14 002
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


Nama : Tarmuji Rauf
Nim : 426 14 002

Hasil Dari Program Di atas saat Status Peringatan Level 1 : Sangat Mengantuk
Nama : Tarmuji Rauf
Nim : 426 14 002

Nomor 3
#include<opencv/cvaux.h>
#include<opencv/highgui.h>
#include<opencv/cxcore.h>
#include<iostream>

using namespace std;


int main()
{
CvSize size = cvSize(640,480);

IplImage* frame = cvLoadImage("boal_merah.jpg");

cvNamedWindow("Hasil", CV_WINDOW_AUTOSIZE );
cvNamedWindow("HSV", CV_WINDOW_AUTOSIZE );

CvScalar hsv_min = cvScalar(150, 84, 130, 0);


CvScalar hsv_max = cvScalar(358, 256, 255, 0);
IplImage *hsv_frame = cvCreateImage(cvGetSize(frame),8,3);

IplImage* thresholded = cvCreateImage(cvGetSize(frame),


IPL_DEPTH_8U, 1);
cvCvtColor(frame, hsv_frame, CV_BGR2HSV);
cvInRangeS(hsv_frame, hsv_min, hsv_max, thresholded);
CvMemStorage* storage = cvCreateMemStorage(0);
cvSmooth(thresholded, thresholded, CV_GAUSSIAN, 9, 9 );
CvSeq* circles = cvHoughCircles(thresholded, storage,
CV_HOUGH_GRADIENT, 2, thresholded->height/4, 100, 50, 10, 400);
int i =0;
for (i = 0; i< circles->total; i++)
{
float* p = (float*)cvGetSeqElem( circles, i );
printf("Ball! x=%f y=%f r=%f\n\r",p[0],p[1],p[2] );
cvCircle( frame, cvPoint(cvRound(p[0]),cvRound(p[1])),3,
CV_RGB(0,255,0), -1, 8, 0 );
cvCircle( frame,
cvPoint(cvRound(p[0]),cvRound(p[1])),cvRound(p[2]), CV_RGB(255,0,0), 3, 8,
0 );
}
cout<<"Ada "<<i<<" bola"<<endl;
cvShowImage("Hasil", frame );
cvShowImage("HSV", hsv_frame);
cvShowImage("After Color Filtering", thresholded );
cvReleaseMemStorage(&storage);
Nama : Tarmuji Rauf
Nim : 426 14 002
cvDestroyWindow("mywindow" );
cvWaitKey ( 0 );
return 0;
}

Hasil Dari Program Di atas Deteksi


Nama : Tarmuji Rauf
Nim : 426 14 002