前提
Visual Studio2019でUSBカメラを使って、
リアルタイムで顔認識するソフトを作ろうと思いましたが、
カメラの画像を顔認識処理にどうやって渡すか分かりません。
ご教授のほどをお願い申し上げます
実現したいこと
リアルタイムでUSBカメラから1秒ごとに画像を受けて、
顔認識をかけて表示出来るようにしたいです。
ピクチャーボックス1で現在映っている画像を表示し、
ピクチャーボックス2で顔認識した画像を表示したいです
発生している問題・エラーメッセージ
// 判定画像ファイルをロードのところでエラーになっております。
エラーメッセージ
error CS1503: 引数 1: は 'OpenCvSharp.Mat' から 'string' へ変換することはできません
該当のソースコード
Form1.cs
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Threading.Tasks; 9using System.Windows.Forms; 10 11using OpenCvSharp; 12using OpenCvSharp.Extensions; 13using System.IO; 14 15namespace Face 16{ 17 public partial class Form1 : Form 18 { 19 private Mat _flame; 20 public Form1() 21 { 22 InitializeComponent(); 23 } 24 25 private void Form1_Load(object sender, EventArgs e) 26 { 27 //PictureBoxのサイズに合わせて表示 28 pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; 29 pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; 30 } 31 32 private void button1_Click(object sender, EventArgs e) 33 { 34 //VideoCapture作成 35 using (var capture = new VideoCapture()) 36 { 37 //カメラの起動 38 capture.Open(0); 39 40 if (!capture.IsOpened()) 41 { 42 throw new Exception("capture initialization failed"); 43 } 44 45 //画像取得用のMatを作成 46 _flame = new Mat(); 47 48 while (true) 49 { 50 try 51 { 52 capture.Read(_flame); 53 if (_flame.Empty()) 54 { 55 break; 56 } 57 58 if (_flame.Size().Width > 0) 59 { 60 //PictureBoxに表示 MatをBitMapに変換 61 pictureBox1.Image = BitmapConverter.ToBitmap(_flame); 62 } 63 64 int key = Cv2.WaitKey(); 65 66 if (this.IsDisposed) 67 { 68 break; 69 } 70 } 71 72 catch (Exception) 73 { 74 break; 75 } 76 } 77 } 78 } 79 80 private void button2_Click(object sender, EventArgs e) 81 { 82 var capture = _flame; 83 84 if (capture == null) 85 { 86 return; 87 } 88 89 pictureBox2.Image = BitmapConverter.ToBitmap(capture); 90 91 //タイマーをONにする 92 timer1.Enabled = true; 93 } 94 95 private void button3_Click(object sender, EventArgs e) 96 { 97 //タイマーをOFFにする 98 timer1.Enabled = false; 99 } 100 101 private void timer1_Tick(object sender, EventArgs e) 102 { 103 // 顔認識用カスケードファイルパス 104 string classifierFilePath = Application.StartupPath + "\\haarcascade_frontalface_alt2.xml"; 105 string classifierFilePath2 = Application.StartupPath + "\\haarcascade_frontalface_default.xml"; 106 if (!File.Exists(classifierFilePath)) 107 { 108 MessageBox.Show("顔認識用カスケードファイルがみつかりません"); 109 } 110 if (!File.Exists(classifierFilePath2)) 111 { 112 MessageBox.Show("顔認識用カスケードファイルがみつかりません"); 113 } 114 115 // 顔認識用カスケード分類器を作成 116 using (var haarCascade = new CascadeClassifier(classifierFilePath)) 117 using (var haarCascade2 = new CascadeClassifier(classifierFilePath2)) 118 119 // 判定画像ファイルをロード 120 using (var matSrcImage = new Mat(_flame, ImreadModes.Color)) // ←←←ここでエラー(_flame) 121 using (var matGrayscaleImage = new Mat()) 122 { 123 Mat matRetImage = matSrcImage.Clone(); 124 125 // 入力画像をグレースケール化 126 Cv2.CvtColor( 127 src: matSrcImage, 128 dst: matGrayscaleImage, 129 code: ColorConversionCodes.BGR2GRAY); 130 131 // 顔認識を実行 132 var faces = haarCascade.DetectMultiScale( 133 image: matGrayscaleImage, 134 scaleFactor: 1.1, 135 minNeighbors: 4, 136 minSize: new OpenCvSharp.Size(100, 100)); 137 138 var faces2 = haarCascade2.DetectMultiScale( 139 image: matGrayscaleImage, 140 scaleFactor: 1.1, 141 minNeighbors: 4, 142 minSize: new OpenCvSharp.Size(100, 100)); 143 144 // 認識した顔の周りを枠線で囲む 145 foreach (var face in faces) 146 { 147 Cv2.Rectangle( 148 img: matRetImage, 149 rect: new Rect(face.X, face.Y, face.Width, face.Height), 150 //color: new Scalar(0, 255, 255), 151 color: new Scalar(0, 0, 255), 152 thickness: 2); 153 } 154 foreach (var face in faces2) 155 { 156 Cv2.Rectangle( 157 img: matRetImage, 158 rect: new Rect(face.X, face.Y, face.Width, face.Height), 159 //color: new Scalar(0, 255, 255), 160 color: new Scalar(0, 0, 255), 161 thickness: 2); 162 } 163 164 pictureBox2.Image = BitmapConverter.ToBitmap(matRetImage); 165 166 } 167 } 168 } 169}
試したこと
色々ウェブサイトで解決策を探したが見つかりませんでした。
USBカメラは認識できています。
補足情報(FW/ツールのバージョンなど)
あなたの回答
tips
プレビュー