人脸识别 一对一循环目录

56 篇文章 1 订阅
51 篇文章 4 订阅
 private void button1_Click(object sender, EventArgs e)
        {
            //if (pictureBox1.Image != null)
            //{
            //    Image image = pictureBox1.Image;
            //    pictureBox1.Image = null;
            //    image.Dispose();

            //}
            StringBuilder stringBuilder = null;
            //  开始监视代码运行时间
            System.Diagnostics.Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start(); 
                               //  需要测试的代码 ....

            //去读ArrayList里面图片的位置
            foreach (Object item in Contrast())
            {
                stringBuilder = new StringBuilder(path);
                stringBuilder.Append(@"\");
                stringBuilder.Append(item.ToString());
                // pictureBox1.Image = Image.FromFile(stringBuilder.ToString(),false );
                this.textBox1.Text += stringBuilder.ToString() + "\n\n";
            }
            //MessageBox.Show(user().ToString());
            stopwatch.Stop(); //  停止监视
            TimeSpan timespan = stopwatch.Elapsed; //  获取当前实例测量得出的总时间
            double seconds = timespan.TotalSeconds;  //  总秒数
            MessageBox.Show("对比完成 !使用秒数:" + seconds);

        }

        static string path = System.Environment.CurrentDirectory + @"\相片";

        /// <summary>
        /// 1.bmp 为传输进来相片 对比文件相册文件内所有的图片 大于百分之八十存入ArrayList集合返回
        /// </summary>
        /// <returns></returns>
        public ArrayList Contrast()
        {


            // 读取图片1
            FileStream file1 = new FileStream("1.bmp", FileMode.Open);
            byte[] img1 = new byte[file1.Length];
            file1.Read(img1, 0, (int)file1.Length);

            //第二种方法
            DirectoryInfo folder = new DirectoryInfo(path);
            ArrayList list = new ArrayList();

            foreach (FileInfo file in folder.GetFiles("*.bmp"))
            {

                // 读取图片2
                FileStream file2 = new FileStream("相片/" + file.Name, FileMode.Open);
                byte[] img2 = new byte[file2.Length];
                file2.Read(img2, 0, (int)file2.Length);

                // 提取特征
                byte[] feature1 = new byte[nRetInit * 2];   // 此处的 nRetInit 为 face_init 的返回值
                byte[] feature2 = new byte[nRetInit * 2];   // 此处的 nRetInit 为 face_init 的返回值

                int nRet1 = face_get_feature_from_image(hCtx[0], img1, (int)file1.Length, feature1);
                int nRet2 = face_get_feature_from_image(hCtx[0], img2, (int)file2.Length, feature2);

                //Console.WriteLine("提取特征1={0} \n" + strFeat1, nRet1);
                // Console.WriteLine("提取特征2={0} \n" + strFeat2, nRet2);

                // 比对得到分数
                int nScore = face_comp_feature(hCtx[0], feature1, feature2);
                // Console.Write("比对分数:{0}", nScore);
                if (nScore > 80)
                {
                    list.Add(file.Name);
                }
                // MessageBox.Show(file.Name);
                file2.Close();
            }
            CountObject obj;
            for (int i = 0; i < 5; i++)
            {
                obj = new CountObject();
                //obj = null; // 这一步,只是为了更清晰些验证引用的对象是否释放!
                GC.Collect();

            }
            file1.Close();

            return list;
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值