• Jadilah orang yang sesederhana mungkin yang kamu bisa
  • Jadilah orang yang sesederhana mungkin yang kamu bisa
  • Jadilah orang yang sesederhana mungkin yang kamu bisa
  • Jadilah orang yang sesederhana mungkin yang kamu bisa

Search

Loading...

Simple Face Detection using OpenCV | Visual Studio

Hmm... Bingung mau nulis kata pengantarnya (:p). Langsung aja kali ya?? Hehe, kali ini saya mau sharing kodingan Simple Face Detection using OpenCV | Visual Studio. Nah lhoo apa itu OpenCV? Biar lebih jelasnya mengenai apa itu OpenCV kalian tinggal baca aja postingan saya sebelumnya tentang apa itu OpenCV disini. Nahh kalau mengenai projek yang bakalan kita bikin adalah sebuah projek sederhana untuk mendeteksi wajah dari foto yang sudah kita tentukan. Kenapa menggunakan OpenCV? Jawabannya simple, karena lebih mudah dan Open Source!! haha

Oke langsung saja ke tahapan membuat projeknya :
1. Bagi kamu yang belum install OpenCV kamu ikutin langkah-langkah pada postingan saya sebelumnya dan saya sarankan menggunakan OpenCV 2.1.0 karena saya menggunakan yang 2.1.0. Postingannya install OpenCV-nya disini. Untuk nama projeknya terserah kalian, tapi kalau saya namanya FaceDetection.

2. Setelah selesai meng-install dan setting Project Directories kamu tinggal copy-paste kodingan di bawah :

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"

//fungsi deteksi wajah
void detect_and_draw(IplImage* img){
//inisialisasi
CvHaarClassifierCascade *cascade=0;
CvMemStorage *storage=0;

IplImage* gray=img;

//baca file xml u/ image detection
if(!cascade){
char * file="C:/OpenCV2.1/data/haarcascades/haarcascade_frontalface_alt.xml";
cascade=(CvHaarClassifierCascade*) cvLoad(file, 0, 0, 0);
storage=cvCreateMemStorage(0);
}

//deteksi wajah
CvSeq* faces=cvHaarDetectObjects(
gray, 
cascade,
storage,
1.1,
3,
CV_HAAR_DO_CANNY_PRUNING,
cvSize(10, 10));

int i;

//menggambar kotak merah pd bagian wajah yg ditemukan
for(i=0; i<(faces ? faces->total : 0) ; i++){
CvRect* r=(CvRect*) cvGetSeqElem(faces, i);
cvRectangle(img,
cvPoint(r->x, r->y),
cvPoint(r->x + r->width, r->y + r->height),
CV_RGB(255, 0, 0),
1, 8, 0
);
}

//menampilkan gambar hasil deteksi
cvNamedWindow("hasil");
cvShowImage("hasil", img);

cvWaitKey(0);
}

int main(array<System::String ^> ^args){
//load image asli
const char* filename="dank.jpg";
IplImage* img=cvLoadImage(filename);

//panggil fungsi
detect_and_draw(img);
}


3. Untuk yang berwarna merah sesuaikan dengan nama file foto yang sudah kalian masukkan di dalam projeknya. Kalau saya file fotonya ada di C:\Users\Ozi Priawadi\Documents\Visual Studio 2012\Projects\FaceDetection\FaceDetection\dank.jpg. Kalau yang berwarna biru sesuaikan dengan folder instalan OpenCV yang sudah kamu buat. 

4. Setelah selesai coba klik RUN projeknya dan hasilnya kira-kira seperti ini





Related Post



31 comments:

  1. wah, makasih mas. berguna banget nih buat belajar awal coding

    Sekalian mau nanya, klo mau face detection dari video caranya gmn ya?

    ReplyDelete
  2. om saya nyoba kenapa kyk gini yah??
    1>------ Rebuild All started: Project: mencoba, Configuration: Debug Win32 ------
    1> again.cpp
    1>c:\opencv\build\include\opencv2\flann\logger.h(66): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
    1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\stdio.h(234) : see declaration of 'fopen'
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(6): error C2065: 'Iplimage' : undeclared identifier
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(6): error C2065: 'img' : undeclared identifier
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(6): error C2448: 'detect_and_draw' : function-style initializer appears to be a function definition
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(15): warning C4129: 'o' : unrecognized character escape sequence
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(15): warning C4129: 'd' : unrecognized character escape sequence
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(15): warning C4129: 'h' : unrecognized character escape sequence
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(15): warning C4129: 'h' : unrecognized character escape sequence
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(44): error C2065: 'array' : undeclared identifier
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(44): error C2653: 'system' : is not a class or namespace name
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(44): error C2589: '^' : illegal token on right side of '::'
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(44): error C2059: syntax error : '::'
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(44): error C2143: syntax error : missing ';' before '{'
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(44): error C2447: '{' : missing function header (old-style formal list?)
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(46): warning C4129: 'p' : unrecognized character escape sequence
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(46): warning C4129: 'd' : unrecognized character escape sequence
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(46): warning C4129: 'p' : unrecognized character escape sequence
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(46): warning C4129: 'm' : unrecognized character escape sequence
    1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(46): warning C4129: 'W' : unrecognized character escape sequence
    ========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

    ReplyDelete
    Replies
    1. Baca disini broh http://stackoverflow.com/questions/14386/fopen-deprecated-warning

      Delete
    2. unyuk yang ini
      1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(15): warning C4129: 'o' : unrecognized character escape sequence
      1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(15): warning C4129: 'd' : unrecognized character escape sequence
      1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(15): warning C4129: 'h' : unrecognized character escape sequence
      1>c:\users\pheko\documents\visual studio 2010\projects\mencoba\again.cpp(15): warning C4129: 'h' : unrecognized character escape sequence

      masalahnya pada alamat pemanggilan haarcasascade
      contoh
      "C:\opencv2.4.7\data\haarcascades\haarcascade_frontalface_alt.xml"
      seharusnya yang dibawah ini
      "C:/opencv2.4.7/data/haarcascades/haarcascade_frontalface_alt.xml"
      semoga membantu

      Delete
    3. Siph siphh.. Makasih banyak :)

      Delete
    4. bisa dicoba ini

      #include
      #include
      #include

      using namespace std;
      using namespace cv;

      //fungsi deteksi wajah
      int main()
      {
      IplImage *img = cvLoadImage("saintek.jpg");

      //inisialisasi
      CvHaarClassifierCascade *cascade=0;
      CvMemStorage *storage=0;

      IplImage* gray=img;

      //baca file xml u/ image detection
      if(!cascade)
      {
      char * file="C:/opencv2.4.7/data/haarcascades/haarcascade_frontalface_alt.xml";
      cascade=(CvHaarClassifierCascade*) cvLoad(file, 0, 0, 0);
      storage=cvCreateMemStorage(0);
      }

      //deteksi wajah
      CvSeq* faces=cvHaarDetectObjects(gray,cascade,storage,1.1,3,CV_HAAR_DO_CANNY_PRUNING,cvSize(10, 10));

      int i;

      //menggambar kotak merah pd bagian wajah yg ditemukan
      for(i=0; i<(faces ? faces->total : 0) ; i++)
      {
      CvRect* r=(CvRect*) cvGetSeqElem(faces, i);
      cvRectangle(img,cvPoint(r->x, r->y),cvPoint(r->x + r->width, r->y + r->height),CV_RGB(255, 0, 0), 1, 8, 0);
      }

      //menampilkan gambar hasil deteksi
      cvNamedWindow("hasil",1);
      cvShowImage("hasil", img);
      cvWaitKey(0);
      cvDestroyWindow("hasil");
      cvReleaseImage(&img);
      return 0;
      }

      Delete
    5. sayakan hanya modif dikit punya anda....salam kenal

      Delete
    6. Siphh.. Salam kenal ya :)
      Udah lama nggak ngoding ginian lagi hoho

      Delete
  3. bro,mksd dari kode ini gmn?

    CvSeq* faces=cvHaarDetectObjects(
    gray,
    cascade,
    storage,
    1.1,
    3,
    CV_HAAR_DO_CANNY_PRUNING,
    cvSize(10, 10));

    ReplyDelete
    Replies
    1. itu format method dari haarcascade didalamnya parameter2 yang dirilis opencv

      formatnya ini:
      cvHaarDetectObjects Method (image, cascade, storage, scaleFactor, minNeighbors, flags, minSize)

      penjelasannya:
      1. image (IntPtr)
      Image to detect objects in.
      2. cascade (IntPtr)
      Haar classifier cascade in internal representation
      3. storage (IntPtr)
      Memory storage to store the resultant sequence of the object candidate rectangles
      4. scaleFactor (Double)
      The factor by which the search window is scaled between the subsequent scans, for example, 1.1 means increasing window by 10%
      5. minNeighbors (Int32)
      Minimum number (minus 1) of neighbor rectangles that makes up an object. All the groups of a smaller number of rectangles than min_neighbors-1 are rejected. If min_neighbors is 0, the function does not any grouping at all and returns all the detected candidate rectangles, which may be useful if the user wants to apply a customized grouping procedure
      6. flags (Int32)
      Mode of operation. Currently the only flag that may be specified is CV_HAAR_DO_CANNY_PRUNING. If it is set, the function uses Canny edge detector to reject some image regions that contain too few or too much edges and thus can not contain the searched object. The particular threshold values are tuned for face detection and in this case the pruning speeds up the processing
      7. minSize (MCvSize)
      Minimum window size. By default, it is set to the size of samples the classifier has been trained on (~20x20 for face detection).

      semoga membantu

      Delete
  4. Sebelumnya saya terimakasih atas Tutorial Codenya.Mas Ozi :D

    ini saya ada masalah di --->
    1>c:\users\gani\documents\visual studio 2010\projects\facedetection\facedetection\facedetection.cpp(57): error C2065: 'array' : undeclared identifier
    1>c:\users\gani\documents\visual studio 2010\projects\facedetection\facedetection\facedetection.cpp(57): error C2653: 'System' : is not a class or namespace name
    1>c:\users\gani\documents\visual studio 2010\projects\facedetection\facedetection\facedetection.cpp(57): error C2065: 'String' : undeclared identifier
    1>c:\users\gani\documents\visual studio 2010\projects\facedetection\facedetection\facedetection.cpp(57): error C2059: syntax error : '>'
    1>c:\users\gani\documents\visual studio 2010\projects\facedetection\facedetection\facedetection.cpp(57): error C2143: syntax error : missing ';' before '{'
    1>c:\users\gani\documents\visual studio 2010\projects\facedetection\facedetection\facedetection.cpp(57): error C2447: '{' : missing function header (old-style formal list?)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Terimakasih

    ReplyDelete
    Replies
    1. dicoba ini

      #include
      #include
      #include

      using namespace std;
      using namespace cv;

      //fungsi deteksi wajah
      int main()
      {
      IplImage *img = cvLoadImage("saintek.jpg");

      //inisialisasi
      CvHaarClassifierCascade *cascade=0;
      CvMemStorage *storage=0;

      IplImage* gray=img;

      //baca file xml u/ image detection
      if(!cascade)
      {
      char * file="C:/opencv2.4.7/data/haarcascades/haarcascade_frontalface_alt.xml";
      cascade=(CvHaarClassifierCascade*) cvLoad(file, 0, 0, 0);
      storage=cvCreateMemStorage(0);
      }

      //deteksi wajah
      CvSeq* faces=cvHaarDetectObjects(gray,cascade,storage,1.1,3,CV_HAAR_DO_CANNY_PRUNING,cvSize(10, 10));

      int i;

      //menggambar kotak merah pd bagian wajah yg ditemukan
      for(i=0; i<(faces ? faces->total : 0) ; i++)
      {
      CvRect* r=(CvRect*) cvGetSeqElem(faces, i);
      cvRectangle(img,cvPoint(r->x, r->y),cvPoint(r->x + r->width, r->y + r->height),CV_RGB(255, 0, 0), 1, 8, 0);
      }

      //menampilkan gambar hasil deteksi
      cvNamedWindow("hasil",1);
      cvShowImage("hasil", img);
      cvWaitKey(0);
      cvDestroyWindow("hasil");
      cvReleaseImage(&img);
      return 0;
      }

      Delete
  5. gan, ini pke kamera laptop atau usb camera??

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. bang saya tidak error di program tapi ketika running ada tulisan begini :
    Unhandled exception at 0x75dc812f in Face Detection.exe: Microsoft C++ exception: cv::Exception at memory location 0x003bedf4..

    mohon bantuannya. terimakasih..:)

    ReplyDelete
    Replies
    1. http://stackoverflow.com/questions/12687684/how-to-find-out-what-is-causing-cvexception-at-memory-location

      Delete
    2. Mas tegar ini baik hati sekali..hehehe..:)

      Delete
  8. mau tanya, apa ada yang punya jurnal tentang metode haar feature yang tulis oleh viola dan jones saya lagi nyari referensi untuk proposal usul skripsi saya, terimaksih ditunggu jawabannya

    ReplyDelete
    Replies
    1. waduh, ane nggak punya. Mungkin bawah ane ada. hehe

      Delete
  9. punten om om senior,.. ko aku pas di build outputnya ada tulisan kya gini yaa,..

    1>------ Build started: Project: bebas, Configuration: Debug Win32 ------
    1> bebas.cpp
    1>c:\users\fixiouser\documents\visual studio 2010\projects\bebas\bebas\bebas.cpp(1): fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    jadi kalau di run tuh, ngga bisa error terus.
    kenapa tuh sensei?
    minta pencerahannya yaa,. sensei soalnya baru belajar,..
    thanks

    ReplyDelete
    Replies
    1. Coba ini bro, ohh iya kalau ada error coba copy errornya trus paste di google. Ntar insyaallah ada solusinya :D
      http://stackoverflow.com/questions/19329104/error-c1083-cannot-open-include-file-but-i-have-a-path-to-the-include-file

      Delete
  10. >MSVCRT.lib(crtexe.obj) : error LNK2001: unresolved external symbol main
    1>C:\Users\Asus\Documents\Visual Studio 2012\Eample Code\eyes\eyes\x64\Release\eyes.exe : fatal error LNK1120: 1 unresolved externals

    error ini gimana solusinya ya,, mohon pencerahannya

    ReplyDelete
    Replies
    1. Coba ini bro http://stackoverflow.com/questions/13905109/error-lnk2019-unresolved-external-symbol-main-referenced-in-function-tmaincrt

      Delete
  11. kenapa pas waktu debug.. ga mau muncul nyala webcam,,, tp ada peringatan

    Unhandled exception at at 0x000007FE425F7B8C in eyes.exe: Microsoft C++ exception: cv::Exception at memory location 0x0000001099A9F320.

    dan ada opsi break sama continue,,, mohon pencerahannya lagi

    ReplyDelete
  12. 'FaceDetection.exe': Loaded 'C:\Users\arulz\Documents\Visual Studio 2010\Projects\FaceDetection\Debug\FaceDetection.exe', Symbols loaded.
    'FaceDetection.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file
    'FaceDetection.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file
    'FaceDetection.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Cannot find or open the PDB file
    The program '[4332] FaceDetection.exe: Native' has exited with code -1073741515 (0xc0000135).

    ini knpa ya mas

    teruss...ada peringatan

    the program can't start because highgui210d.dll is missing from your computer. try reinstalling the program to fix this problem

    ReplyDelete
  13. saya juga mau nanya, soalnya uda nyari-nyari di google tapi belum juga dapet solusinya
    jadi pas saya build, keluar error seperti ini:
    Error 6 error LNK2019: unresolved external symbol "void __cdecl cv::text::computeNMChannels(class cv::_InputArray const &,class cv::_OutputArray const &,int)" (?computeNMChannels@text@cv@@YAXABV_InputArray@2@ABV_OutputArray@2@H@Z) referenced in function _main
    Error 7 error LNK1120: 6 unresolved externals

    saya menggunakan vs2010 dengan opencv 3.0
    mohon pencerahanya
    terimakasih

    ReplyDelete
    Replies
    1. Coba cek lagi mas, udah bener belum naroh librarynya. Coba baca ini deh http://stackoverflow.com/questions/18360540/error-lnk2019-unresolved-external-symbol-opencv

      Delete
  14. Mas. Kalaw gambar yg di jadikan patokan diambil dari database itu gimana codingnya mas?...

    ReplyDelete

Copyright © 2015 priawadi.com All Rights Reserved.