Anda di halaman 1dari 4

L1

LAMPIRAN

Lampiran Kode Program

for(;;)
{

intkeyPressed = 0;
FILE *trainFile;
stringpersonName;
intpersonNumber;
Ptr<FaceRecognizer> model = createFisherFaceRecognizer();
Ptr<FaceRecognizer> model1 = createFisherFaceRecognizer();
//Ptr<FaceRecognizer>asdf = createLBPHFaceRecognizer();

if (kbhit())
keyPressed = getch();

if (keyPressed == VK_ESCAPE) { // Check if the user hit the 'Escape' key


break; // Stop processing input.
}

switch (keyPressed)
{
case'c':
printf("id : ");

changeKeyboardMode(0);
scanf("%d", &id);
changeKeyboardMode(1);

break;

case'n':
printf("Masukkannama: ");
/*scanf("%s",newPersonName);*/
strcpy(newPersonName, "newPerson");
changeKeyboardMode(0);

fgets(newPersonName, sizeof(newPersonName)-1, stdin);


changeKeyboardMode(1);

// Remove 1 or 2 newline characters if they were appended (eg:


Linux).

i = strlen(newPersonName);

if (i> 0 && (newPersonName[i-1] == 10 || newPersonName[i-1]


== 13)) {
newPersonName[i-1] = 0;
L2

i--;
}
if (i> 0 && (newPersonName[i-1] == 10 || newPersonName[i-1]
== 13)) {
newPersonName[i-1] = 0;
i--;
}

if (i> 0) {
printf("Menyimpangambarhingga '%s' ...\n",
newPersonName);

newPersonFaces=0; // restart training a new person


saveNextFaces = true;

}
else {
printf("Namatidakterdeteksi. Tekan 'n' untuk
mengulang.\n");
}

break;

case't': // Start training

saveNextFaces = FALSE; // stop saving next faces.


// Store the saved data into the training file.
printf("\nMenyimpan data training barudari '%s'.\n",
newPersonName);

// Append the new person to the end of the training data.


trainFile = fopen("train.txt", "a");

for (i=0; i<newPersonFaces; i++) {


snprintf(cstr, sizeof(cstr)-1,
"D:/Skripsi/facerec/Debug/data/%d_%s%d.jpg",
nPersons+1, newPersonName, i+1);

fprintf(trainFile, "%s;%d;%s\n", cstr, nPersons+1,


newPersonName);
}
fclose(trainFile);
fn_csv=string(argv[2]);

try
{
baca_csv(fn_csv, images, labels, pName);
fn_csv.clear();
images.empty();
labels.empty();
}
catch (cv::Exception& e)
{
L3

cerr<<"Error opening file \""<<fn_csv<<"\". Reason: "<<


e.msg <<endl;
exit(1);
}

nPersons++;

saveNextFaces = FALSE;
newPersonFaces = 0;

model.release();
face_resized.release();
face.release();
frame.release();
original.release();
gray.release();
faces.empty();

continue;
break;
}

/*Ptr<FaceRecognizer> model = createFisherFaceRecognizer();*/


model->train(images, labels);
model->save("facedata.xml");
model1->load("facedata.xml");

cap>> frame;
original = frame.clone();
//Mat gray;
cvtColor(original, gray, CV_BGR2GRAY);
//vector<Rect_<int>> faces;
haar_cascade.detectMultiScale(gray, faces);
for(inti = 0; i<faces.size(); i++)
{
Rectface_i = faces[i];
face = gray(face_i);
face_resized;
cv::resize(face, face_resized, Size(im_width, im_height), 1.0, 1.0,
INTER_CUBIC);

prediction = model1->predict(face_resized);
//int prediction1 = model1->predict(face_resized);
//snprintf(text, sizeof(text)-1, "Name: '%s'", pName[prediction-
1].c_str());
if (prediction == -1)
{
snprintf(text, sizeof(text)-1, "Name: '%s'",
pName[(prediction+1)].c_str());
}
else
{
L4

snprintf(text, sizeof(text)-1, "Name: '%s'", pName[(prediction)*5].c_str());


}

if(face.rows>=200 &&face.cols>=200){
rectangle(original, face_i, CV_RGB(0, 255,0), 1);
stringbox_text = format("Prediction = %d ", prediction);

intpos_x = std::max(face_i.tl().x - 10, 0);


intpos_y = std::max(face_i.tl().y - 10, 0);
int pos_x1 = std::max(face_i.tl().x - 10, 0);
int pos_y1 = std::max(face_i.tl().y - 40, 0);
putText(original, box_text, Point(pos_x, pos_y),
FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);
putText(original, box_text1, Point(pos_x1, pos_y1),
FONT_HERSHEY_PLAIN, 1.0, CV_RGB(0,255,0), 2.0);
}

imshow("SistemAbsendenganPengenalanWajah", original);

if(face.cols>0)
{
if(saveNextFaces&&newPersonFaces<5)
{

snprintf(cstr, sizeof(cstr)-1, "data/%d_%s%d.jpg",


nPersons+1, newPersonName, newPersonFaces+1);

printf("Menyimpanwajahdari '%s' kegambar


'%s'.\n", newPersonName, cstr);
face.cols = 250;
face.rows = 250;
imwrite(cstr,face);
newPersonFaces++;
if(newPersonFaces==5)
{
printf("Penyimpanangambartelahselesai, tekan 't'
untukmemulai training data\n");
}