C# OpenCvSharp DNN Gaze Estimation

news2024/11/18 13:36:05

目录

介绍

效果

模型信息

项目

代码

frmMain.cs

GazeEstimation.cs

下载


C# OpenCvSharp DNN Gaze Estimation

介绍

训练源码地址:https://github.com/deepinsight/insightface/tree/master/reconstruction/gaze

效果

模型信息

Inputs
-------------------------
name:input
tensor:Float[1, 3, 160, 160]
---------------------------------------------------------------

Outputs
-------------------------
name:output
tensor:Float[1, 962, 3]
---------------------------------------------------------------

项目

代码

frmMain.cs

using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace OpenCvSharp_Yolov8_Demo
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        string startupPath;
        Mat image;
        Mat result_image;

        YOLOv8_face face_detector = new YOLOv8_face("model/yolov8n-face.onnx", 0.45f, 0.5f);
        GazeEstimation gaze_predictor=new GazeEstimation("model/generalizing_gaze_estimation_with_weak_supervision_from_synthetic_views_1x3x160x160.onnx");

        private void Form1_Load(object sender, EventArgs e)
        {

            image_path = "img_test/1.jpg";
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            textBox1.Text = "";
            pictureBox2.Image = null;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }
            textBox1.Text = "";
            pictureBox2.Image = null;
            button2.Enabled = false;
            Application.DoEvents();

            image = new Mat(image_path);
            List<Face> ltFace = face_detector.Detect(new Mat(image_path));

            if (ltFace.Count > 0)
            {
                result_image = image.Clone();
                //face_detector.DrawPred(ltFace, result_image);
                String info = "";
                foreach (Face item in ltFace)
                {
                    gaze_predictor.Detect(image, item);
                    result_image = gaze_predictor.DrawOn(result_image, item,out info);
                }
                pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
                textBox1.Text = info;
            }
            else
            {
                textBox1.Text = "无信息";
            }

            button2.Enabled = true;
        }

    }
}
 

using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace OpenCvSharp_Yolov8_Demo
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        string startupPath;
        Mat image;
        Mat result_image;

        YOLOv8_face face_detector = new YOLOv8_face("model/yolov8n-face.onnx", 0.45f, 0.5f);
        GazeEstimation gaze_predictor=new GazeEstimation("model/generalizing_gaze_estimation_with_weak_supervision_from_synthetic_views_1x3x160x160.onnx");

        private void Form1_Load(object sender, EventArgs e)
        {

            image_path = "img_test/1.jpg";
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            textBox1.Text = "";
            pictureBox2.Image = null;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }
            textBox1.Text = "";
            pictureBox2.Image = null;
            button2.Enabled = false;
            Application.DoEvents();

            image = new Mat(image_path);
            List<Face> ltFace = face_detector.Detect(new Mat(image_path));

            if (ltFace.Count > 0)
            {
                result_image = image.Clone();
                //face_detector.DrawPred(ltFace, result_image);
                String info = "";
                foreach (Face item in ltFace)
                {
                    gaze_predictor.Detect(image, item);
                    result_image = gaze_predictor.DrawOn(result_image, item,out info);
                }
                pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
                textBox1.Text = info;
            }
            else
            {
                textBox1.Text = "无信息";
            }

            button2.Enabled = true;
        }

    }
}

GazeEstimation.cs

 unsafe public class GazeEstimation
    {

        float[] mean = new float[] { 0.5f, 0.5f, 0.5f };
        float[] std = new float[] { 0.5f, 0.5f, 0.5f };
        int[] iris_idx_481 = new int[] { 248, 252, 224, 228, 232, 236, 240, 244 };
        int num_eye = 481;
        int input_size = 160;
        float[] eye_kps;
        Net net;

        public GazeEstimation(string modelpath)
        {
            net = CvDnn.ReadNetFromOnnx(modelpath);
            eye_kps = new float[num_eye * 2 * 3];
        }

        public void Detect(Mat img, Face box)
        {

            Point kps_right_eye = box.kpt[1];
            Point kps_left_eye = box.kpt[0];
            float[] center = new float[] { (kps_right_eye.X + kps_left_eye.X) * 0.5f, (kps_right_eye.Y + kps_left_eye.Y) * 0.5f };
            float _size = (float)(Math.Max(box.rect.Width / 1.5f, Math.Abs(kps_right_eye.X - kps_left_eye.X)) * 1.5f);

            float _scale = input_size / _size;
            //transform
            float cx = center[0] * _scale;
            float cy = center[1] * _scale;

            float[] data = new float[] { _scale, 0, (float)(-cx + input_size * 0.5), 0, _scale, (float)(-cy + input_size * 0.5) };
            Mat M = new Mat(2, 3, MatType.CV_32F, data);

            Mat cropped = new Mat();
            Cv2.WarpAffine(img, cropped, M, new Size(input_size, input_size));

            Mat rgbimg = new Mat();
            Cv2.CvtColor(cropped, rgbimg, ColorConversionCodes.BGR2RGB);

            Mat normalized_mat = Normalize(rgbimg);

            Mat blob = CvDnn.BlobFromImage(normalized_mat);

            net.SetInput(blob);

            Mat[] outs = new Mat[3] { new Mat(), new Mat(), new Mat() };
            string[] outBlobNames = net.GetUnconnectedOutLayersNames().ToArray();

            net.Forward(outs, outBlobNames);

            float* opred = (float*)outs[0].Data;//outs[0]的形状是(1,962,3)
            Mat IM = new Mat();
            Cv2.InvertAffineTransform(M, IM);
            //trans_points
            float scale = (float)Math.Sqrt(IM.At<float>(0, 0) * IM.At<float>(0, 0) + IM.At<float>(0, 1) * IM.At<float>(0, 1));
            int row = outs[0].Size(1);
            int col = outs[0].Size(2);

            for (int i = 0; i < row; i++)
            {
                eye_kps[i * 3] = IM.At<float>(0, 0) * opred[i * 3] + IM.At<float>(0, 1) * opred[i * 3 + 1] + IM.At<float>(0, 2);
                eye_kps[i * 3 + 1] = IM.At<float>(1, 0) * opred[i * 3] + IM.At<float>(1, 1) * opred[i * 3 + 1] + IM.At<float>(1, 2);
                eye_kps[i * 3 + 2] = opred[i * 3 + 2] * scale;
            }

        }

        public Mat DrawOn(Mat srcimg, Face box, out string info)
        {
            StringBuilder sb = new StringBuilder();

            float rescale = 300.0f / box.rect.Width;
            Mat eimg = new Mat();
            Cv2.Resize(srcimg, eimg, new Size(), rescale, rescale);
            //draw_item
            int row = num_eye * 2;
            for (int i = 0; i < row; i++)
            {
                float tmp = eye_kps[i * 3];
                eye_kps[i * 3] = eye_kps[i * 3 + 1] * rescale;
                eye_kps[i * 3 + 1] = tmp * rescale;
                eye_kps[i * 3 + 2] *= rescale;
            }
            //angles_and_vec_from_eye
            int slice = num_eye * 3;
            float[] theta_x_y_vec_l = new float[5];

            float[] eye_kps_l = new float[481 * 3];
            float[] eye_kps_r = new float[481 * 3];

            Array.Copy(eye_kps, 0, eye_kps_l, 0, 481 * 3);
            Array.Copy(eye_kps, 481 * 3, eye_kps_r, 0, 481 * 3);

            angles_and_vec_from_eye(eye_kps_l, iris_idx_481, theta_x_y_vec_l);
            float[] theta_x_y_vec_r = new float[5];

            angles_and_vec_from_eye(eye_kps_r, iris_idx_481, theta_x_y_vec_r);

            float[] gaze_pred = new float[] { (float)((theta_x_y_vec_l[0] + theta_x_y_vec_r[0]) * 0.5), (float)((theta_x_y_vec_l[1] + theta_x_y_vec_r[1]) * 0.5) };

            float diag = (float)Math.Sqrt((float)eimg.Rows * eimg.Cols);

            float[] eye_pos_left = new float[] { 0, 0 };
            float[] eye_pos_right = new float[] { 0, 0 };
            for (int i = 0; i < 8; i++)
            {
                int ind = iris_idx_481[i];
                eye_pos_left[0] += eye_kps[ind * 3];
                eye_pos_left[1] += eye_kps[ind * 3 + 1];
                eye_pos_right[0] += eye_kps[slice + ind * 3];
                eye_pos_right[1] += eye_kps[slice + ind * 3 + 1];
            }
            eye_pos_left[0] /= 8;
            eye_pos_left[1] /= 8;
            eye_pos_right[0] /= 8;
            eye_pos_right[1] /= 8;

            float dx = (float)(0.4 * diag * Math.Sin(theta_x_y_vec_l[1]));
            float dy = (float)(0.4 * diag * Math.Sin(theta_x_y_vec_l[0]));
            Point eye_left_a = new Point(eye_pos_left[1], eye_pos_left[0]);  左眼的箭头线的起始点坐标
            Point eye_left_b = new Point(eye_pos_left[1] + dx, eye_pos_left[0] + dy);   左右的箭头线的终点坐标

            Cv2.ArrowedLine(eimg, eye_left_a, eye_left_b, new Scalar(0, 0, 255), 5, LineTypes.AntiAlias, 0, 0.18);
            float yaw_deg_l = (float)(theta_x_y_vec_l[1] * (180 / Math.PI));
            float pitch_deg_l = (float)(-theta_x_y_vec_l[0] * (180 / Math.PI));

            dx = (float)(0.4 * diag * Math.Sin(theta_x_y_vec_r[1]));
            dy = (float)(0.4 * diag * Math.Sin(theta_x_y_vec_r[0]));

            Point eye_right_a = new Point(eye_pos_right[1], eye_pos_right[0]);  右眼的箭头线的起始点坐标
            Point eye_right_b = new Point(eye_pos_right[1] + dx, eye_pos_right[0] + dy);  右眼的箭头线的终点坐标
            Cv2.ArrowedLine(eimg, eye_right_a, eye_right_b, new Scalar(0, 0, 255), 5, LineTypes.AntiAlias, 0, 0.18);

            float yaw_deg_r = (float)(theta_x_y_vec_r[1] * (180 / Math.PI));
            float pitch_deg_r = (float)(-theta_x_y_vec_r[0] * (180 / Math.PI));

            Cv2.Resize(eimg, eimg, new Size(srcimg.Cols, srcimg.Rows));
            //draw Yaw, Pitch
            string label = String.Format("L-Yaw : {0:f2}", yaw_deg_l);
            sb.AppendLine(label);
            Cv2.PutText(eimg, label, new Point(eimg.Cols - 200, 30), HersheyFonts.HersheySimplex, 0.7, new Scalar(0, 255, 0), 2);

            label = String.Format("L-Pitch :{0:f2}", pitch_deg_l);
            sb.AppendLine(label);
            Cv2.PutText(eimg, label, new Point(eimg.Cols - 200, 60), HersheyFonts.HersheySimplex, 0.7, new Scalar(0, 255, 0), 2);

            label = String.Format("R-Yaw : {0:f2}", yaw_deg_r);
            sb.AppendLine(label);
            Cv2.PutText(eimg, label, new Point(eimg.Cols - 200, 90), HersheyFonts.HersheySimplex, 0.7, new Scalar(0, 255, 0), 2);

            label = String.Format("R-Pitch : {0:f2}", pitch_deg_r);
            sb.AppendLine(label);
            Cv2.PutText(eimg, label, new Point(eimg.Cols - 200, 120), HersheyFonts.HersheySimplex, 0.7, new Scalar(0, 255, 0), 2);

            info = sb.ToString();

            return eimg;

        }

        public Mat Normalize(Mat src)
        {
            Mat[] bgr = src.Split();
            for (int i = 0; i < bgr.Length; ++i)
            {
                bgr[i].ConvertTo(bgr[i], MatType.CV_32FC1, 1.0 / (255.0 * std[i]), (0.0 - mean[i]) / std[i]);
            }

            Cv2.Merge(bgr, src);

            foreach (Mat channel in bgr)
            {
                channel.Dispose();
            }

            return src;
        }

        /// <summary>
        /// 输入参数eye的形状是(481,3)
        /// 输入参数iris_lms_idx的长度shi
        /// 输出theta_x_y_vec的长度是5, 分别是theta_x, theta_y, vec[0], vec[1], vec[2]
        /// </summary>
        void angles_and_vec_from_eye(float[] eye, int[] iris_lms_idx, float[] theta_x_y_vec)
        {

            float[] mean = new float[] { 0, 0, 0 };
            for (int i = 0; i < 32; i++)
            {
                mean[0] += eye[i * 3];
                mean[1] += eye[i * 3 + 1];
                mean[2] += eye[i * 3 + 2];
            }
            mean[0] /= 32;
            mean[1] /= 32;
            mean[2] /= 32;

            float[] p_iris = new float[8 * 3];
            for (int i = 0; i < 8; i++)
            {
                int ind = iris_lms_idx[i];
                p_iris[i * 3] = eye[ind * 3] - mean[0];
                p_iris[i * 3 + 1] = eye[ind * 3 + 1] - mean[1];
                p_iris[i * 3 + 2] = eye[ind * 3 + 2] - mean[2];
            }

            float[] mean_p_iris = new float[] { 0, 0, 0 };
            for (int i = 0; i < 8; i++)
            {
                mean_p_iris[0] += p_iris[i * 3];
                mean_p_iris[1] += p_iris[i * 3 + 1];
                mean_p_iris[2] += p_iris[i * 3 + 2];
            }
            mean_p_iris[0] /= 8;
            mean_p_iris[1] /= 8;
            mean_p_iris[2] /= 8;

            float l2norm_p_iris = (float)Math.Sqrt(mean_p_iris[0] * mean_p_iris[0] + mean_p_iris[1] * mean_p_iris[1] + mean_p_iris[2] * mean_p_iris[2]);
            theta_x_y_vec[2] = mean_p_iris[0] / l2norm_p_iris;  ///vec[0]
            theta_x_y_vec[3] = mean_p_iris[1] / l2norm_p_iris;  ///vec[1]
            theta_x_y_vec[4] = mean_p_iris[2] / l2norm_p_iris;  ///vec[2]

            //angles_from_vec
            float x = -theta_x_y_vec[4];
            float y = theta_x_y_vec[3];
            float z = -theta_x_y_vec[2];
            float theta = (float)Math.Atan2(y, x);
            float phi = (float)(Math.Atan2(Math.Sqrt(x * x + y * y), z) - Math.PI * 0.5);
            theta_x_y_vec[0] = phi;
            theta_x_y_vec[1] = theta;
        }

    }

 unsafe public class GazeEstimation
    {

        float[] mean = new float[] { 0.5f, 0.5f, 0.5f };
        float[] std = new float[] { 0.5f, 0.5f, 0.5f };
        int[] iris_idx_481 = new int[] { 248, 252, 224, 228, 232, 236, 240, 244 };
        int num_eye = 481;
        int input_size = 160;
        float[] eye_kps;
        Net net;

        public GazeEstimation(string modelpath)
        {
            net = CvDnn.ReadNetFromOnnx(modelpath);
            eye_kps = new float[num_eye * 2 * 3];
        }

        public void Detect(Mat img, Face box)
        {

            Point kps_right_eye = box.kpt[1];
            Point kps_left_eye = box.kpt[0];
            float[] center = new float[] { (kps_right_eye.X + kps_left_eye.X) * 0.5f, (kps_right_eye.Y + kps_left_eye.Y) * 0.5f };
            float _size = (float)(Math.Max(box.rect.Width / 1.5f, Math.Abs(kps_right_eye.X - kps_left_eye.X)) * 1.5f);

            float _scale = input_size / _size;
            //transform
            float cx = center[0] * _scale;
            float cy = center[1] * _scale;

            float[] data = new float[] { _scale, 0, (float)(-cx + input_size * 0.5), 0, _scale, (float)(-cy + input_size * 0.5) };
            Mat M = new Mat(2, 3, MatType.CV_32F, data);

            Mat cropped = new Mat();
            Cv2.WarpAffine(img, cropped, M, new Size(input_size, input_size));

            Mat rgbimg = new Mat();
            Cv2.CvtColor(cropped, rgbimg, ColorConversionCodes.BGR2RGB);

            Mat normalized_mat = Normalize(rgbimg);

            Mat blob = CvDnn.BlobFromImage(normalized_mat);

            net.SetInput(blob);

            Mat[] outs = new Mat[3] { new Mat(), new Mat(), new Mat() };
            string[] outBlobNames = net.GetUnconnectedOutLayersNames().ToArray();

            net.Forward(outs, outBlobNames);

            float* opred = (float*)outs[0].Data;//outs[0]的形状是(1,962,3)
            Mat IM = new Mat();
            Cv2.InvertAffineTransform(M, IM);
            //trans_points
            float scale = (float)Math.Sqrt(IM.At<float>(0, 0) * IM.At<float>(0, 0) + IM.At<float>(0, 1) * IM.At<float>(0, 1));
            int row = outs[0].Size(1);
            int col = outs[0].Size(2);

            for (int i = 0; i < row; i++)
            {
                eye_kps[i * 3] = IM.At<float>(0, 0) * opred[i * 3] + IM.At<float>(0, 1) * opred[i * 3 + 1] + IM.At<float>(0, 2);
                eye_kps[i * 3 + 1] = IM.At<float>(1, 0) * opred[i * 3] + IM.At<float>(1, 1) * opred[i * 3 + 1] + IM.At<float>(1, 2);
                eye_kps[i * 3 + 2] = opred[i * 3 + 2] * scale;
            }

        }

        public Mat DrawOn(Mat srcimg, Face box, out string info)
        {
            StringBuilder sb = new StringBuilder();

            float rescale = 300.0f / box.rect.Width;
            Mat eimg = new Mat();
            Cv2.Resize(srcimg, eimg, new Size(), rescale, rescale);
            //draw_item
            int row = num_eye * 2;
            for (int i = 0; i < row; i++)
            {
                float tmp = eye_kps[i * 3];
                eye_kps[i * 3] = eye_kps[i * 3 + 1] * rescale;
                eye_kps[i * 3 + 1] = tmp * rescale;
                eye_kps[i * 3 + 2] *= rescale;
            }
            //angles_and_vec_from_eye
            int slice = num_eye * 3;
            float[] theta_x_y_vec_l = new float[5];

            float[] eye_kps_l = new float[481 * 3];
            float[] eye_kps_r = new float[481 * 3];

            Array.Copy(eye_kps, 0, eye_kps_l, 0, 481 * 3);
            Array.Copy(eye_kps, 481 * 3, eye_kps_r, 0, 481 * 3);

            angles_and_vec_from_eye(eye_kps_l, iris_idx_481, theta_x_y_vec_l);
            float[] theta_x_y_vec_r = new float[5];

            angles_and_vec_from_eye(eye_kps_r, iris_idx_481, theta_x_y_vec_r);

            float[] gaze_pred = new float[] { (float)((theta_x_y_vec_l[0] + theta_x_y_vec_r[0]) * 0.5), (float)((theta_x_y_vec_l[1] + theta_x_y_vec_r[1]) * 0.5) };

            float diag = (float)Math.Sqrt((float)eimg.Rows * eimg.Cols);

            float[] eye_pos_left = new float[] { 0, 0 };
            float[] eye_pos_right = new float[] { 0, 0 };
            for (int i = 0; i < 8; i++)
            {
                int ind = iris_idx_481[i];
                eye_pos_left[0] += eye_kps[ind * 3];
                eye_pos_left[1] += eye_kps[ind * 3 + 1];
                eye_pos_right[0] += eye_kps[slice + ind * 3];
                eye_pos_right[1] += eye_kps[slice + ind * 3 + 1];
            }
            eye_pos_left[0] /= 8;
            eye_pos_left[1] /= 8;
            eye_pos_right[0] /= 8;
            eye_pos_right[1] /= 8;

            float dx = (float)(0.4 * diag * Math.Sin(theta_x_y_vec_l[1]));
            float dy = (float)(0.4 * diag * Math.Sin(theta_x_y_vec_l[0]));
            Point eye_left_a = new Point(eye_pos_left[1], eye_pos_left[0]);  左眼的箭头线的起始点坐标
            Point eye_left_b = new Point(eye_pos_left[1] + dx, eye_pos_left[0] + dy);   左右的箭头线的终点坐标

            Cv2.ArrowedLine(eimg, eye_left_a, eye_left_b, new Scalar(0, 0, 255), 5, LineTypes.AntiAlias, 0, 0.18);
            float yaw_deg_l = (float)(theta_x_y_vec_l[1] * (180 / Math.PI));
            float pitch_deg_l = (float)(-theta_x_y_vec_l[0] * (180 / Math.PI));

            dx = (float)(0.4 * diag * Math.Sin(theta_x_y_vec_r[1]));
            dy = (float)(0.4 * diag * Math.Sin(theta_x_y_vec_r[0]));

            Point eye_right_a = new Point(eye_pos_right[1], eye_pos_right[0]);  右眼的箭头线的起始点坐标
            Point eye_right_b = new Point(eye_pos_right[1] + dx, eye_pos_right[0] + dy);  右眼的箭头线的终点坐标
            Cv2.ArrowedLine(eimg, eye_right_a, eye_right_b, new Scalar(0, 0, 255), 5, LineTypes.AntiAlias, 0, 0.18);

            float yaw_deg_r = (float)(theta_x_y_vec_r[1] * (180 / Math.PI));
            float pitch_deg_r = (float)(-theta_x_y_vec_r[0] * (180 / Math.PI));

            Cv2.Resize(eimg, eimg, new Size(srcimg.Cols, srcimg.Rows));
            //draw Yaw, Pitch
            string label = String.Format("L-Yaw : {0:f2}", yaw_deg_l);
            sb.AppendLine(label);
            Cv2.PutText(eimg, label, new Point(eimg.Cols - 200, 30), HersheyFonts.HersheySimplex, 0.7, new Scalar(0, 255, 0), 2);

            label = String.Format("L-Pitch :{0:f2}", pitch_deg_l);
            sb.AppendLine(label);
            Cv2.PutText(eimg, label, new Point(eimg.Cols - 200, 60), HersheyFonts.HersheySimplex, 0.7, new Scalar(0, 255, 0), 2);

            label = String.Format("R-Yaw : {0:f2}", yaw_deg_r);
            sb.AppendLine(label);
            Cv2.PutText(eimg, label, new Point(eimg.Cols - 200, 90), HersheyFonts.HersheySimplex, 0.7, new Scalar(0, 255, 0), 2);

            label = String.Format("R-Pitch : {0:f2}", pitch_deg_r);
            sb.AppendLine(label);
            Cv2.PutText(eimg, label, new Point(eimg.Cols - 200, 120), HersheyFonts.HersheySimplex, 0.7, new Scalar(0, 255, 0), 2);

            info = sb.ToString();

            return eimg;

        }

        public Mat Normalize(Mat src)
        {
            Mat[] bgr = src.Split();
            for (int i = 0; i < bgr.Length; ++i)
            {
                bgr[i].ConvertTo(bgr[i], MatType.CV_32FC1, 1.0 / (255.0 * std[i]), (0.0 - mean[i]) / std[i]);
            }

            Cv2.Merge(bgr, src);

            foreach (Mat channel in bgr)
            {
                channel.Dispose();
            }

            return src;
        }

        /// <summary>
        /// 输入参数eye的形状是(481,3)
        /// 输入参数iris_lms_idx的长度shi
        /// 输出theta_x_y_vec的长度是5, 分别是theta_x, theta_y, vec[0], vec[1], vec[2]
        /// </summary>
        void angles_and_vec_from_eye(float[] eye, int[] iris_lms_idx, float[] theta_x_y_vec)
        {

            float[] mean = new float[] { 0, 0, 0 };
            for (int i = 0; i < 32; i++)
            {
                mean[0] += eye[i * 3];
                mean[1] += eye[i * 3 + 1];
                mean[2] += eye[i * 3 + 2];
            }
            mean[0] /= 32;
            mean[1] /= 32;
            mean[2] /= 32;

            float[] p_iris = new float[8 * 3];
            for (int i = 0; i < 8; i++)
            {
                int ind = iris_lms_idx[i];
                p_iris[i * 3] = eye[ind * 3] - mean[0];
                p_iris[i * 3 + 1] = eye[ind * 3 + 1] - mean[1];
                p_iris[i * 3 + 2] = eye[ind * 3 + 2] - mean[2];
            }

            float[] mean_p_iris = new float[] { 0, 0, 0 };
            for (int i = 0; i < 8; i++)
            {
                mean_p_iris[0] += p_iris[i * 3];
                mean_p_iris[1] += p_iris[i * 3 + 1];
                mean_p_iris[2] += p_iris[i * 3 + 2];
            }
            mean_p_iris[0] /= 8;
            mean_p_iris[1] /= 8;
            mean_p_iris[2] /= 8;

            float l2norm_p_iris = (float)Math.Sqrt(mean_p_iris[0] * mean_p_iris[0] + mean_p_iris[1] * mean_p_iris[1] + mean_p_iris[2] * mean_p_iris[2]);
            theta_x_y_vec[2] = mean_p_iris[0] / l2norm_p_iris;  ///vec[0]
            theta_x_y_vec[3] = mean_p_iris[1] / l2norm_p_iris;  ///vec[1]
            theta_x_y_vec[4] = mean_p_iris[2] / l2norm_p_iris;  ///vec[2]

            //angles_from_vec
            float x = -theta_x_y_vec[4];
            float y = theta_x_y_vec[3];
            float z = -theta_x_y_vec[2];
            float theta = (float)Math.Atan2(y, x);
            float phi = (float)(Math.Atan2(Math.Sqrt(x * x + y * y), z) - Math.PI * 0.5);
            theta_x_y_vec[0] = phi;
            theta_x_y_vec[1] = theta;
        }

    }

下载

源码下载

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1359035.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

正则表达式解析与应用:深度剖析正则表达式的威力

&#x1f604; 19年之后由于某些原因断更了三年&#xff0c;23年重新扬帆起航&#xff0c;推出更多优质博文&#xff0c;希望大家多多支持&#xff5e; &#x1f337; 古之立大事者&#xff0c;不惟有超世之才&#xff0c;亦必有坚忍不拔之志 &#x1f390; 个人CSND主页——Mi…

pytorch机器学习各种激活函数总结(不完整学习更新中~)

pytorch各种激活函数总结 0.思维导图预览1. ReLU函数1.1 改进版ReLU6函数 2. Sigmoid函数3. Softmax函数4. Tanh函数5.&#xff08;学习后更新&#xff09; 0.思维导图预览 1. ReLU函数 ReLU&#xff08;Rectified Linear Unit&#xff09;线性整流函数 其公式为&#xff1a; …

常用API(String,StringBuilder,StringJoiner)

文章目录 1.API1.1API概述1.2如何使用API帮助文档 2.String类2.1String类概述2.2String类的特点2.3String类的构造方法2.4创建字符串对象两种方式的区别2.5字符串的比较2.5.1号的作用2.5.2equals方法的作用 2.6用户登录案例2.6.1案例需求2.6.2代码实现 2.7遍历字符串案例2.7.1案…

软件测试|SQL AND和OR运算符解析

简介 在SQL&#xff08;Structured Query Language&#xff09;中&#xff0c;AND和OR是两个常用的逻辑运算符。它们用于组合条件来构建复杂的查询语句&#xff0c;帮助我们更精确地过滤和检索数据。本文将详细介绍SQL中的AND和OR运算符&#xff0c;包括其语法、用法以及使用时…

数据结构学习 jz63股票的最大利润

关键词&#xff1a;动态规划 滚动数组优化 这题不要被动态规划吓到了&#xff0c;其实很简单。 用时16min 题目&#xff1a; 思路&#xff1a; 最大利润的实现办法&#xff1a;在最低的时候买入&#xff0c;在最高的时候卖出。 dp状态&#xff1a; dp[i]第i天如果卖出的最…

uniapp中组件库的Checkbox 复选框 的丰富使用方法

目录 #平台差异说明 #基本使用 #自定义形状 #禁用checkbox #自定义形状 #自定义颜色 #横向排列形式 #横向两端排列形式 API #Checkbox Props #CheckboxGroup Props #CheckboxGroup Event 复选框组件一般用于需要多个选择的场景&#xff0c;该组件功能完整&#xff…

STM32疑难杂症

1.keil的奇怪问题 创建的数组分配内存到0x10000000地址的时候,数据总是莫名其妙的出现问题,取消勾选就正常了 stm32f407内部有一个CCM内存,这部分内存只能由内核控制,任何外设都不能够进行访问。这样问题就来了,如果使用keil5进行编程时勾选了这个选项(下图),则编译的…

八、Lua脚本详解—— 超详细操作演示!

八、Lua脚本详解 —— 超详细操作演示&#xff01; 八、Lua脚本详解8.1 Lua 简介8.2 Linux 系统的Lua8.2.1 Lua 下载8.2.2 Lua 安装8.2.3 Hello World 8.3 Win 系统的Lua8.4 Lua 脚本基础8.4.1 注释8.4.2 数据类型8.4.3 标识符8.4.4 运算符8.4.5 函数8.4.6 流程控制语句8.4.7 循…

2021-05-08 51单片机74HC164、74LS164、74HCT164、74HC154、74HCT154应用三极管控制继电器

74HC164、74HCT164是8位边沿触发式移位寄存器&#xff0c;串行输入数据&#xff0c;然后并行输出。数据通过两个输入端&#xff08;DSA或DSB&#xff09;之一串行输入&#xff1b;任一输入端可以用作高电平使能端&#xff0c;控制另一输入端的数据输入。两个输入端或者连接在一…

IMS SIP register消息中的Contact header field

SIP register中的Contact还要承载User Agent的能力信息。 实网下抓取的UE log如上&#xff0c;下面就主要看下Contact header field要包含的内容及其含义。 Contact header field设置为包括 UE IP地址或FQDN的SIP URI。 如上图contact中sip:69a5de6a-a03e-46d6-ad7a-b0d974c8f…

大创项目推荐 深度学习图像修复算法 - opencv python 机器视觉

文章目录 0 前言2 什么是图像内容填充修复3 原理分析3.1 第一步&#xff1a;将图像理解为一个概率分布的样本3.2 补全图像 3.3 快速生成假图像3.4 生成对抗网络(Generative Adversarial Net, GAN) 的架构3.5 使用G(z)生成伪图像 4 在Tensorflow上构建DCGANs最后 0 前言 &#…

函数式编程及应用

目录 什么是Lambdalambda表达式的类型及实现方式类型语法 常用函数式接口Customer 函数式编程在Stream中的应用总结参考资料 什么是Lambda Lambda 表达式是 JDK8 的一个新特性&#xff0c;可以取代大部分的匿名内部类&#xff0c;写出更优雅的Java代码。 Lambda 表达式描述了一…

图片剪切软件,让图片处理更加高效

随着数字技术的不断发展&#xff0c;图片剪切也在不断进步。从手动裁剪到自动识别&#xff0c;图片剪切正变得越来越简单、高效&#xff0c;在这里面&#xff0c;图片处理软件的作用是不可忽视的。 所需工具&#xff1a; 一个【首助编辑高手】软件 需要剪切的图片 操作步骤…

【python】魔术方法大全(一)--基础篇

如果对你有帮助&#xff0c;欢迎微信搜索【海哥python】关注这个互联网苟且偷生的工具人。 什么是魔术方法 所谓魔法方法&#xff0c;它的官方的名字实际上叫special method&#xff0c;是Python的一种高级语法&#xff0c;允许你在类中自定义函数&#xff0c;并绑定到类的特殊…

Weblogic安全漫谈(四)

黑名单机制必然会推动两种研究方向的发展&#xff1a;一是挖掘不在黑名单的新组件&#xff0c;是为绕过规则&#xff1b;二是发掘检查的盲区&#xff0c;是为绕过逻辑。 CVE-2020-14756 二次反序列化具有对抗检查逻辑的天生丽质&#xff0c;在CVE-2018-2893中就有利用字节数组…

Kubeadmin实现k8s集群:

Kubeadmin来快速搭建一个k8s集群&#xff1a; 二进制搭建适合大集群&#xff0c;50台以上的主机&#xff0c; 但是kubeadm更适合中小企业的业务集群 环境&#xff1a; Master&#xff1a;20.0.0.71 2核4G 或者4核8G docker kubelet kubectl flannel Node1&#xff1a;20.…

面试题:vue2中option API的和vue3中composition API中的数据和方法能否交互?

结论&#xff1a; vue2中option API中的数据和方法可以从vue3中的composition API中进行调用&#xff0c; 而vue3中的composition API是不可以从vue2中的数据进行调用。 原理&#xff1a; 因为composition API中的函数setup在页面的生命周期中要比vue2中option API中的data、…

MR实战:词频统计

文章目录 一、实战概述二、提出任务三、完成任务&#xff08;一&#xff09;准备数据1、在虚拟机上创建文本文件2、上传文件到HDFS指定目录 &#xff08;二&#xff09;实现步骤1、创建Maven项目2、添加相关依赖3、创建日志属性文件4、创建词频统计映射器类5、创建词频统计归并…

六、Spring 声明式事务

本章概要 声明式事务概念 编程式事务声明式事务Spring事务管理器 基于注解的声明式事务 准备工作基本事务控制事务属性&#xff1a;只读事务属性&#xff1a;超时时间事务属性&#xff1a;事务异常事务属性&#xff1a;事务隔离级别事务属性&#xff1a;事务传播行为 6.1 声…

phpstudy_pro 关于多版本php的问题

我在phpstudy中安装了多个PHP版本 我希望不同的网站可以对应不同的PHP版本&#xff0c;则在nginx配置文件中需要知道不同的PHP版本的监听端口是多少&#xff0c;如下图所示 然而找遍了php.ini配置&#xff0c;并未对listen进行设置&#xff0c;好奇是怎么实现不同的PHP监听不同…