Pages

Tuesday, April 23, 2013

Create Face Detection - OpenCV - MVS

Install libemgucv-windows-x86-2.3.0.1416.exe
Buka Microsoft Visual Studio
Bikin project di C#, new Project, pilih MPF Application, ketik Project Name-nya, OK

References
Add Reference
Browse
ke folder Emgu
bin
pilih Emgu.CV + Emgu.CV.UI + Emgu.CV.Util
OK

Buka tab MainWIndow.xaml.cs
Tambahkan syntax:
using Emgu.CV;
using Emgu.CV.UI;
using Emgu.CV.Structure;
using Emgu.CV.Util;


Buka tab MainWIndow.xaml
Pake toolbox 'Image'
Tempatkan kursor pada Title="MainWindow"
Ke properties, Loaded diisi dengan window_loaded, atau tambahkan atribut Loaded="Window_Loaded" di dekat Title="MainWindow"

References
Add Reference - .NET - system.drawing

haarcascade_frontalface_alt yg di Emgu, copy in ke projek MVC bin - debug
Clik kana Projek d MVS
Add exiting item
pilih haarcascade_frontalface_alt.xml yg ada di debug MVS
add

Tambahin ke MainWIndow.xaml.cs
using System.Windows.Threading;
using System.Runtime.InteropServices;

Copy code di dalam
public partial class MainWindow : Window
    {
...
    }
Code-nya sbb:
        Capture capture;
        HaarCascade haarCascade;
        DispatcherTimer timer;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            capture = new Capture();
            haarCascade = new HaarCascade(@"haarcascade_frontalface_alt.xml");
            timer = new DispatcherTimer();
            timer.Tick += new EventHandler(timer_Tick);
            timer.Interval = new TimeSpan(0, 0, 0, 0, 1);
            timer.Start();
            Console.Write("lewat");

        }

        void timer_Tick(object sender, EventArgs e)
        {
            Image currentFrame = capture.QueryFrame();

            if (currentFrame != null)
            {
                Image grayFrame = currentFrame.Convert();

                var detectedFaces = grayFrame.DetectHaarCascade(haarCascade)[0];

                foreach (var face in detectedFaces)
                    currentFrame.Draw(face.rect, new Bgr(0, double.MaxValue, 0), 3);

                image1.Source = ToBitmapSource(currentFrame);
            }

        }

        [DllImport("gdi32")]
        private static extern int DeleteObject(IntPtr o);

        public static BitmapSource ToBitmapSource(IImage image)
        {
            using (System.Drawing.Bitmap source = image.Bitmap)
            {
                IntPtr ptr = source.GetHbitmap(); //obtain the Hbitmap

                BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                    ptr,
                    IntPtr.Zero,
                    Int32Rect.Empty,
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

                DeleteObject(ptr); //release the HBitmap
                return bs;
            }
        }
DEBUG MENNNN!!!!
RASAKAN SENSASINYA! WAHAHAHHAHAHAH
Kalau ada yang error, pada (@"haarcascade_frontalface_alt.xml");, periksa lagi tadi file haarcascade_frontalface_alt.xml - nya yang di masukkin ke References, biasanya kalau laptop yang 64bit, ada tree -nya ((@"haarcascade_frontalface_alt_tree.xml");).

Kalau masih ada error, click kanan pada project, then clean.
DEBUG LAGI MENNNN!!!!

No comments:

Post a Comment