11
yanjinhui
2025-04-30 752223f279965b562e3d086b78f01efb55925ae4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Runtime.InteropServices;
using OpenCvSharp;
 
namespace FaceAI
{  
    /**
         * @brief   人脸表情属性枚举
         */
    enum BDFaceAttributeEmotionType
    {
        BDFACE_ATTRIBUTE_EMOTION_FROWN = 0,     // 皱眉
        BDFACE_ATTRIBUTE_EMOTION_SMILE = 1,     // 笑
        BDFACE_ATTRIBUTE_EMOTION_CALM = 2,      // 平静
    };
 
    /**
     * @brief   人脸种族属性枚举
     */
    enum BDFaceRace
    {
        BDFACE_RACE_YELLOW = 0, // 黄种人
        BDFACE_RACE_WHITE = 1,  // 白种人
        BDFACE_RACE_BLACK = 2,  // 黑种人
        BDFACE_RACE_INDIAN = 3, // 印第安人
    };
 
    /**
     * @brief   眼镜状态属性枚举
     */
    enum BDFaceGlasses
    {
        BDFACE_NO_GLASSES = 0,   // 无眼镜
        BDFACE_GLASSES = 1,      // 有眼镜
        BDFACE_SUN_GLASSES = 2,  // 墨镜
    };
 
    /**
     * @brief   性别属性枚举
     */
    enum BDFaceGender
    {
        BDFACE_GENDER_FEMAILE = 0, // 女性
        BDFACE_GENDER_MALE = 1,    // 男性
    };
 
 
    /**
     * @brief   人脸属性结构体
     */
    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    struct BDFaceAttribute
    {
        public int age;                            // 年龄
        public BDFaceRace race;                    // 种族
        public BDFaceAttributeEmotionType emotion; // 表情
        public BDFaceGlasses glasses;              // 戴眼镜状态
        public BDFaceGender gender;                // 性别
    };
 
    // 人脸属性示例及接口
    class FaceAttr
    {
        // 获取人脸属性
        [DllImport("BaiduFaceApi.dll", EntryPoint = "face_attr", CharSet = CharSet.Ansi
            , CallingConvention = CallingConvention.Cdecl)]
        public static extern int face_attr(IntPtr ptr, IntPtr mat);
 
        // 测试获取人脸属性
        public void test_get_face_attr()
        {
            int max_face_num = 5; // 设置最多检测跟踪人数(多人脸检测),默认为1,最多可设为50
 
            BDFaceAttribute[] attr_info = new BDFaceAttribute[max_face_num];
            int size = Marshal.SizeOf(typeof(BDFaceAttribute));
            IntPtr ptT = Marshal.AllocHGlobal(size * max_face_num);
            string img_path = "../images/rgb.png";
            Mat mat = Cv2.ImRead(img_path);
            int faceNum = face_attr(ptT, mat.CvPtr);
            Console.WriteLine("faceNum is:" + faceNum);
            for (int index = 0; index < faceNum; index++)
            {
                IntPtr ptr = new IntPtr();
                if (8 == IntPtr.Size)
                {
                    ptr = (IntPtr)(ptT.ToInt64() + size * index);
                }
                else if (4 == IntPtr.Size)
                {
                    ptr = (IntPtr)(ptT.ToInt32() + size * index);
                }
 
                attr_info[index] = (BDFaceAttribute)Marshal.PtrToStructure(ptr, typeof(BDFaceAttribute));
                // 年龄
                Console.WriteLine("age is {0}:", attr_info[index].age);
                // 种族
                Console.WriteLine("race is:{0}", attr_info[index].race);
                // 表情
                Console.WriteLine("emotion is:{0}", attr_info[index].emotion);
                // 戴眼镜状态
                Console.WriteLine("glasses is:{0}", attr_info[index].glasses);
                // 性别
                Console.WriteLine("gender is:{0}", attr_info[index].gender);
            }
            Marshal.FreeHGlobal(ptT);
        }
       
    }
}