人脸识别技术开发人证比对访客系统

原作者 panmingzhi

最近有个小区用到了虹软的人脸识别,效果还不错。又有一个项目要用人证访客对比,分享一下项目,希望可以帮到有需要的。

码字前先上项目地址:https://gitee.com/panmingzhi/IdCardFaceIdentifier

首先是读证的问题,我们使用的是华视CVR100U,公司已经用这个型号6年了,以前一卡通的资料都用它录,除了不好看,质量杠杠的。大部人的身份证都是很多年前办理的,所有比对的相似度不要太高。

视频采集还是使用的Aforge,使用 NewFrame 一方面要显示到实时画面,另一方面要异步的与当前读到的证件进行比对。这里请不尝试在NewFrame回调事件中直接显示到pictureBox,请使用如下方式,百试不爽:

private void VideoCaptureDevice_NewFrame(object sender, AForge.Video.NewFrameEventArgs eventArgs)

        {

            Bitmap newFrame = (Bitmap)eventArgs.Frame.Clone();

            //如果使用视频文件请注释下面的这行代码,表示不对图像进行水平翻转

            newFrame.RotateFlip(RotateFlipType.Rotate180FlipY);

            lock (sync)

扫描二维码关注公众号,回复: 5461528 查看本文章

            {

                if (currentFrame != null)

                {

                    currentFrame.Dispose();

                    currentFrame = null;

                }

                currentFrame = newFrame;

                skinPictureBox2.Invalidate();

            }

        }

/**

        * 绘制当前帧到控制,必须与获取当前帧互斥

        */

        private void skinPictureBox2_Paint(object sender, PaintEventArgs e)

        {

            lock (synCurrentMat)

            {

                Bitmap bitmap = GetCurrentFrame();

                if (bitmap != null)

                {

                    e.Graphics.DrawImage(bitmap, new Rectangle(0, 0, skinPictureBox2.Width, skinPictureBox2.Height), new Rectangle(0, 0, bitmap.Width, bitmap.Height), GraphicsUnit.Pixel);

                }

            }

        }

//异步的人证对比时,使用此方法再获取实时截图

        public Bitmap GetCurrentFrame()

        {

            if (captureToken.Token.IsCancellationRequested) return null;

            lock (sync)

            {

                return (currentFrame == null || currentFrame.Width == 0 || currentFrame.Height == 0) ? null : AForge.Imaging.Image.Clone(currentFrame);

            }

        }

份证的读取频率是每1秒读一次,读到证件后,10秒内为人证比对时间,这10秒将不再读证。10秒内比对成功,显示成功提示2-3秒后,重新再开始读证。

在读证时,如果证件被正确读取到后,要重新拿开再放置证件,这个在华视的产品说明书与SDK都有说明,其它厂家如鼎识也是一样的。

华视阅读器读到的身份证图片与证件信息默认保存在SDK目录下的wz.txt与wz.bmp,使用wz.bmp做比对时,经常报内存出错,后面我将bmp先转成jpg保存一次后再做人证比对,似乎就没问题。

证件比对时还是延续了之前方式,先将证件图片解析成FaceModel,然后将当前视频截图连续与些FaceModel比对,每次读到证件时都更新一次FaceModel。

/**

        * 转换人脸图片为人脸模板

        **/

        private FaceModel ToFaceModel(Bitmap bitmap)

        {

            LocateResult locate;

            var code = _detection.Detect(bitmap, out locate);

            if (code == ErrorCode.Ok && locate.HasFace && locate.FaceCount == 1)

            {

                using (var feature = _recognize.ExtractFeature(locate.OffInput, locate.Faces[0], locate.FacesOrient[0]))

                {

                    return feature.FeatureData;

                }

            }

            locate.Dispose();

            return null;

        }

        /**

        * 添加新的比对模型

        */

        public void AddToCompare(Bitmap bitmap)

        {

            log.Debug("添加新的比对模型");

            faceModel = ToFaceModel(bitmap);

        }

        /**

        * 截图比对证件

        */

        public bool Match(Bitmap snapshot)

        {

            var result1 = _processor.LocateExtract(snapshot);

            if(result1 == null || result1.Length <= 0)

            {

                return false;

            }

            var sim = _processor.Match(result1[0].FeatureData, faceModel.Data, true);

            return sim > 0.7;

        }

这时截图:(本人不上像,鬼画桃胡将就一下)

1、提示放证


2、读到证件后立即比对

3、比对显示结果后将重新回到第一步

猜你喜欢

转载自www.cnblogs.com/Zzz-/p/10493969.html
今日推荐