OpenCvSharpを使用して、Webカメラでキャプチャーしながら顔検出をしてみる。
基本的には静止画の時と同じで、その部分のコードを動画のキャプチャーに埋め込んだだけ。
- [void][System.Reflection.Assembly]::LoadFrom((Join-Path $pwd OpenCvSharp.dll))
- #顔を検出するためにHaar分類器のカスケードを用いる
- $color = [OpenCvSharp.CvColor]::Red
- $scale = 1.04;
- $scaleFactor = 1.139;
- $minNeighbors = 2;
- $camera = [OpenCvSharp.CvCapture]::FromCamera(0)
- $window = New-Object OpenCvSharp.CvWindow("SampleCapture")
- $srcWidth = $camera.FrameWidth
- $srcHeight = $camera.FrameHeight
- $srcSize = New-Object OpenCvSharp.CvSize([int]$srcWidth, [int]$srcHeight)
- $srcIplImage = New-Object OpenCvSharp.IplImage( $srcSize, [OpenCvSharp.BitDepth]::U8, 1)
- $dstWidth = $camera.FrameWidth / $scale
- $dstHeight = $camera.FrameHeight / $scale
- $dstSize = New-Object OpenCvSharp.CvSize([int]$dstWidth, [int]$dstHeight)
- $dstIplImage = New-Object OpenCvSharp.IplImage( $dstSize, [OpenCvSharp.BitDepth]::U8, 1)
- while ( [OpenCvSharp.CvWindow]::WaitKey(10) -lt 0 ) {
- $iplImage = $camera.QueryFrame()
- #顔検出用の画像の生成
- [OpenCvSharp.Cv]::CvtColor($iplImage,$srcIplImage, [OpenCvSharp.ColorConversion]::BgraToGray)
- [OpenCvSharp.Cv]::Resize($srcIplImage, $dstIplImage, [OpenCvSharp.Interpolation]::Linear)
- [OpenCvSharp.Cv]::EqualizeHist($dstIplImage,$dstIplImage)
- $cascade = [OpenCvSharp.CvHaarClassifierCascade]::FromFile((Join-Path $pwd "haarcascade_frontalface_alt.xml"))
- $storage = New-Object OpenCvSharp.CvMemStorage
- $storage.Clear()
- # 顔の検出
- $faces = [OpenCvSharp.Cv]::HaarDetectObjects($dstIplImage, $cascade, $storage, $scaleFactor, $minNeighbors, 0, (New-Object OpenCvSharp.CvSize(30, 30)));
- Write-Host $faces.Total
- # 検出された全ての顔位置に四角い枠を描画する
- for ($i = 0; $i -lt $faces.Total; $i++) {
- $r = $faces[$i].Rect
- $pt1 = New-Object OpenCvSharp.CvPoint($r.x, $r.y)
- $pt2 = new-object OpenCvSharp.CvPoint(($r.x + $r.Width),($r.y + $r.Height ))
- $iplImage.Rectangle($pt1, $pt2, $color, 3, 8, 0)
- }
- # 丸で囲む
- # for ($i = 0; $i -lt $faces.Total; $i++) {
- # $r = $faces[$i].Rect
- # $X = [OpenCvSharp.Cv]::Round(($r.X + $r.Width * 0.5) * $Scale)
- # $Y = [OpenCvSharp.Cv]::Round(($r.Y + $r.Height * 0.5) * $Scale)
- # $center = New-Object OpenCvSharp.CvPoint($X, $Y)
- # $radius = [OpenCvSharp.Cv]::Round(($r.Width + $r.Height) * 0.25 * $Scale)
- # $iplImage.Circle($center, $radius, $color, 3, [OpenCvSharp.LineType]::AntiAlias, 0)
- # }
- # ウィンドウに表示
- $window.Image = $iplImage
- }
- $srcIplImage.Dispose()
- $dstIplImage.Dispose()
- $iplImage.Dispose()
- $window.Dispose()
- $camera.Dispose()
0 コメント:
コメントを投稿