OpenCVSharp4 識別物體系列之識別臉 眼 鼻子

直接上代碼

            FrameSource cameraSource = FrameSource.CreateFrameSource_Camera(0);

            while (true)
            {
                Mat frame = new Mat();
                CascadeClassifier faceCascade = new CascadeClassifier("haarcascade_eye_tree_eyeglasses.xml");
                float scalingFactor = 0.75f;

                cameraSource.NextFrame(frame);
                Cv2.Resize(frame, frame, new Size(), scalingFactor, scalingFactor, InterpolationFlags.Area);
                Mat frameGray = frame.CvtColor(ColorConversionCodes.BGR2GRAY);
                frameGray = frameGray.EqualizeHist();// 均衡直方圖,防止太亮或太暗,影響檢測效果

                Rect[] faces = faceCascade.DetectMultiScale(frameGray, 1.1, 2, HaarDetectionType.ScaleImage, new Size(30, 30));

                Mat result = frameGray.Clone();
                foreach (Rect face in faces)
                {
                    var center = new Point
                    {
                        X = (int)(face.X + face.Width * 0.5),
                        Y = (int)(face.Y + face.Height * 0.5)
                    };
                    var axes = new Size
                    {
                        Width = (int)(face.Width * 0.5),
                        Height = (int)(face.Height * 0.5)
                    };
                    Cv2.Ellipse(result, center, axes, 0, 0, 360, new Scalar(255, 0, 255), 4);
                }

                Cv2.ImShow("Faces by Haar", result);
                Cv2.WaitKey(20);
            }

非常簡單,直接使用前人訓(xùn)練出來的模型進(jìn)行detect即可。

至于haar模型的訓(xùn)練,參見
https://blog.csdn.net/sazass/article/details/93073840

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容