[C#/WINFORM/EMGUCV] 얼굴 인식을 사용해 마우스 제어하기
■ 얼굴 인식을 사용해 마우스를 제어하는 방법을 보여준다. ▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
using System; using System.Drawing; using System.Runtime.InteropServices; using System.Windows.Forms; using Emgu.CV; using Emgu.CV.Structure; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Import ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 커서 위치 구하기 - GetCursorPos(point) /// <summary> /// 커서 위치 구하기 /// </summary> /// <param name="point">위치</param> /// <returns>처리 결과</returns> [DllImport("user32.dll")] private static extern bool GetCursorPos(out Point point); #endregion #region 커서 위치 설정하기 - SetCursorPos(x, y) /// <summary> /// 커서 위치 설정하기 /// </summary> /// <param name="x">X 좌표</param> /// <param name="y">Y 좌표</param> /// <returns>처리 결과</returns> [DllImport("user32.dll")] private static extern bool SetCursorPos(int x, int y); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 캡처 /// </summary> private Capture capture; /// <summary> /// 분류기 /// </summary> private CascadeClassifier classifier; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.classifier = new CascadeClassifier("haarcascade_frontalface_alt2.xml"); if(this.capture == null) { try { this.capture = new Capture(); } catch(Exception exception) { MessageBox.Show(exception.Message); return; } } Application.Idle += Application_Idle; FormClosed += Form_FormClosed; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 애플리케이션 휴지시 처리하기 - Application_Idle(sender, e) /// <summary> /// 애플리케이션 휴지시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Application_Idle(object sender, EventArgs e) { Image<Bgr, Byte> captureImage = this.capture.QueryFrame(); Image<Gray, Byte> grayscaleImage = captureImage.Convert<Gray, Byte>(); grayscaleImage._EqualizeHist(); Rectangle imageRectangle = grayscaleImage.ROI; Rectangle mouseStableRectangle = new Rectangle ( (int)(imageRectangle.Width * 0.4), (int)(imageRectangle.Height * 0.4), (int)(imageRectangle.Width * 0.2), (int)(imageRectangle.Height * 0.2) ); captureImage.Draw(mouseStableRectangle, new Bgr(255, 0, 0), 1); Rectangle[] faceRectangleArray = this.classifier.DetectMultiScale ( grayscaleImage, 1.1, 10, new Size(20, 20), Size.Empty ); if(faceRectangleArray.Length > 0) { Rectangle maximumFaceRectangle = faceRectangleArray[0]; for(int i = 1; i < faceRectangleArray.Length; i++) { if(faceRectangleArray[i].Width * faceRectangleArray[i].Height > maximumFaceRectangle.Width * maximumFaceRectangle.Height) { maximumFaceRectangle = faceRectangleArray[i]; } } captureImage.Draw(maximumFaceRectangle, new Bgr(255, 255, 0.0), 1); Point maximumFaceRectangleCenter = new Point ( maximumFaceRectangle.X + maximumFaceRectangle.Width / 2, maximumFaceRectangle.Y + maximumFaceRectangle.Height / 2 ); Point imageRectangleCenter = new Point ( imageRectangle.X + imageRectangle.Width / 2, imageRectangle.Y + imageRectangle.Height / 2 ); captureImage.Draw ( new Cross2DF(maximumFaceRectangleCenter, maximumFaceRectangle.Width * 0.1f, maximumFaceRectangle.Height * 0.1f), new Bgr(0, 255, 0), 1 ); if(!mouseStableRectangle.Contains(maximumFaceRectangleCenter)) { double horizontalFraction = (double)(maximumFaceRectangleCenter.X - imageRectangleCenter.X) / imageRectangle.Width; double verticalFraction = (double)(maximumFaceRectangleCenter.Y - imageRectangleCenter.Y) / imageRectangle.Height; Rectangle primaryScreenRectangle = Screen.PrimaryScreen.Bounds; int maximumMouseSpeed = primaryScreenRectangle.Width / 20; Point mousePoint; GetCursorPos(out mousePoint); mousePoint.X = Math.Min(Math.Max(0, mousePoint.X + (int)((maximumMouseSpeed / 2) * horizontalFraction)), primaryScreenRectangle.Width ); mousePoint.Y = Math.Min(Math.Max(0, mousePoint.Y + (int)((maximumMouseSpeed / 2) * verticalFraction )), primaryScreenRectangle.Height); SetCursorPos(mousePoint.X, mousePoint.Y); } } this.imageBox.Image = captureImage; } #endregion #region 폼 닫는 경우 처리하기 - Form_FormClosed(sender, e) /// <summary> /// 폼 닫는 경우 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_FormClosed(object sender, FormClosedEventArgs e) { if(this.capture != null) { this.capture.Dispose(); } } #endregion #region 수평 반전 버튼 클릭시 처리하기 - flipHorizontalButton_Click(sender, e) /// <summary> /// 수평 반전 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void flipHorizontalButton_Click(object sender, EventArgs e) { if(this.capture != null) { this.capture.FlipHorizontal = !this.capture.FlipHorizontal; } } #endregion } } |
TestProject.zip