using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using OpenCvSharp; using OpenCvSharp.Extensions; using System.IO; namespace Face { public partial class Form1 : Form { private Mat _flame; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //PictureBoxのサイズに合わせて表示 pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; } private void button1_Click(object sender, EventArgs e) { //VideoCapture作成 using (var capture = new VideoCapture()) { //カメラの起動  capture.Open(0); if (!capture.IsOpened()) { throw new Exception("capture initialization failed"); } //画像取得用のMatを作成 _flame = new Mat(); while (true) { try { capture.Read(_flame); if (_flame.Empty()) { break; } if (_flame.Size().Width > 0) { //PictureBoxに表示 MatをBitMapに変換 pictureBox1.Image = BitmapConverter.ToBitmap(_flame); } int key = Cv2.WaitKey(); if (this.IsDisposed) { break; } } catch (Exception) { break; } } } } private void button2_Click(object sender, EventArgs e) { var capture = _flame; if (capture == null) { return; } pictureBox2.Image = BitmapConverter.ToBitmap(capture); //タイマーをONにする timer1.Enabled = true; } private void button3_Click(object sender, EventArgs e) { //タイマーをOFFにする timer1.Enabled = false; } private void timer1_Tick(object sender, EventArgs e) { string inputImagePath = null; inputImagePath = _flame; Bitmap retBitmap = GetResultBitmap(inputImagePath); pictureBox2.SizeMode = PictureBoxSizeMode.Zoom; pictureBox2.Image = retBitmap; } Bitmap GetResultBitmap(string inputImagePath) { // 結果画像 Mat matRetImage = null; // 顔認識用カスケードファイルパス string classifierFilePath = Application.StartupPath + "\\haarcascade_frontalface_alt2.xml"; string classifierFilePath2 = Application.StartupPath + "\\haarcascade_frontalface_default.xml"; if (!File.Exists(classifierFilePath)) { MessageBox.Show("顔認識用カスケードファイルがみつかりません"); } if (!File.Exists(classifierFilePath2)) { MessageBox.Show("顔認識用カスケードファイルがみつかりません"); } // 顔認識用カスケード分類器を作成 using (var haarCascade = new CascadeClassifier(classifierFilePath)) using (var haarCascade2 = new CascadeClassifier(classifierFilePath2)) // 判定画像ファイルをロード using (var matSrcImage = new Mat(inputImagePath, ImreadModes.Color)) using (var matGrayscaleImage = new Mat()) { matRetImage = matSrcImage.Clone(); // 入力画像をグレースケール化 Cv2.CvtColor( src: matSrcImage, dst: matGrayscaleImage, code: ColorConversionCodes.BGR2GRAY); // 顔認識を実行 var faces = haarCascade.DetectMultiScale( image: matGrayscaleImage, scaleFactor: 1.1, minNeighbors: 4, minSize: new OpenCvSharp.Size(100, 100)); var faces2 = haarCascade2.DetectMultiScale( image: matGrayscaleImage, scaleFactor: 1.1, minNeighbors: 4, minSize: new OpenCvSharp.Size(100, 100)); // 認識した顔の周りを枠線で囲む foreach (var face in faces) { Cv2.Rectangle( img: matRetImage, rect: new Rect(face.X, face.Y, face.Width, face.Height), //color: new Scalar(0, 255, 255), color: new Scalar(0, 0, 255), thickness: 2); } foreach (var face in faces2) { Cv2.Rectangle( img: matRetImage, rect: new Rect(face.X, face.Y, face.Width, face.Height), //color: new Scalar(0, 255, 255), color: new Scalar(0, 0, 255), thickness: 2); } } // 結果画像を表示 Bitmap retBitmap = BitmapConverter.ToBitmap(matRetImage); matRetImage.Dispose(); return retBitmap; } } }