houxiao
2016-12-30 cc445067d1f61e12dbea4e6458f2c85ba58f01bf
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "PL_SensetimeFaceDetect.h"
#include "MaterialBuffer.h"
#include "logger.h"
 
#include <opencv2/opencv.hpp>
#include <cv_face.h>
 
struct PL_SensetimeFaceDetect_Internal
{
    //uint8_t buffer[1920*1080*4];
    //size_t buffSize;
    //size_t buffSizeMax;
    MB_Frame lastFrame;
    SensetimeFaceDetectConfig config;
 
    bool payError;
    
    cv_handle_t handle_track;
    
    PL_SensetimeFaceDetect_Internal() : 
        //buffSize(0), buffSizeMax(sizeof(buffer)), 
        lastFrame(), config(), payError(true), 
        handle_track(nullptr)
    {
    }
    
    ~PL_SensetimeFaceDetect_Internal()
    {
    }
    
    void reset()
    {
        //buffSize = 0;
        payError = true;
        
        MB_Frame _lastFrame;
        lastFrame = _lastFrame;
        SensetimeFaceDetectConfig _config;
        config = _config;
        
        handle_track = nullptr;
    }
};
 
PipeLineElem* create_PL_SensetimeFaceDetect()
{
    return new PL_SensetimeFaceDetect;
}
 
PL_SensetimeFaceDetect::PL_SensetimeFaceDetect() : internal(new PL_SensetimeFaceDetect_Internal)
{
}
 
PL_SensetimeFaceDetect::~PL_SensetimeFaceDetect()
{
    delete (PL_SensetimeFaceDetect_Internal*)internal;
    internal= nullptr;
}
 
bool PL_SensetimeFaceDetect::init(void* args)
{
    PL_SensetimeFaceDetect_Internal* in = (PL_SensetimeFaceDetect_Internal*)internal;
    in->reset();
    
    SensetimeFaceDetectConfig* config = (SensetimeFaceDetectConfig*)args;
    in->config = *config;
    if (in->config.point_size == 21)
        in->config.point_size_config = CV_DETECT_ENABLE_ALIGN_21;
    else if (in->config.point_size == 106)
        in->config.point_size_config = CV_DETECT_ENABLE_ALIGN_106;
    else
    {
        LOG_ERROR << "alignment point size must be 21 or 106";
        return false;
    }
 
    // init handle
    cv_result_t cv_result = cv_face_create_tracker(&(in->handle_track), nullptr, 
                                in->config.point_size_config | CV_FACE_TRACKING_TWO_THREAD);
    if (cv_result != CV_OK)
    {
        LOG_ERROR << "cv_face_create_tracker failed, error code" << cv_result;
        return false;
    }
 
    int val = 0;
    cv_result = cv_face_track_set_detect_face_cnt_limit(in->handle_track, in->config.detect_face_cnt_limit, &val);
    if (cv_result != CV_OK)
    {
        LOG_ERROR << "cv_face_track_set_detect_face_cnt_limit failed, error : " << cv_result;
        return false;
    }
    else
        LOG_ERROR << "detect face count limit : " << val;
    
    return true;
}
 
void PL_SensetimeFaceDetect::finit()
{
    PL_SensetimeFaceDetect_Internal* in = (PL_SensetimeFaceDetect_Internal*)internal;
    
    // destroy track handle
    cv_face_destroy_tracker(in->handle_track);
    in->handle_track = nullptr;
}
 
int doFaceDetect(PL_SensetimeFaceDetect_Internal* in, 
                uint8_t* buffer, size_t width, size_t height, size_t stride, cv_pixel_format cvPixFmt)
{
    //resize(bgr_frame, bgr_frame, Size(frame_width, frame_height), 0, 0, INTER_LINEAR);
 
    int face_count = 0;
    cv_result_t cv_result = CV_OK;
    cv_face_t* p_face = nullptr;
    
    // realtime track
    cv_result = cv_face_track(in->handle_track, buffer, cvPixFmt,
                            width, height, stride,
                            CV_FACE_UP, &p_face, &face_count);
    if (cv_result != CV_OK)
    {
        LOG_ERROR << "cv_face_track failed, error : " << cv_result;
        cv_face_release_tracker_result(p_face, face_count);
        return -1;
    }
 
    // draw the video
    cv::Mat yuvMat(cv::Size(1920,1080), CV_8UC3, buffer);//#todo
    cv::Mat yMat(cv::Size(1920,1080), CV_8UC1, buffer);
    for (int i = 0; i < face_count; i++)
    {
        LOGP(DEBUG, "face: %d-----[%d, %d, %d, %d]-----id: %d", i,
            p_face[i].rect.left, p_face[i].rect.top,
            p_face[i].rect.right, p_face[i].rect.bottom, p_face[i].ID);
            
        LOGP(DEBUG, "face pose: [yaw: %.2f, pitch: %.2f, roll: %.2f, eye distance: %.2f]",
            p_face[i].yaw,
            p_face[i].pitch, p_face[i].roll, p_face[i].eye_dist);
 
        cv::Scalar scalar_color = CV_RGB(p_face[i].ID * 53 % 256,
            p_face[i].ID * 93 % 256,
            p_face[i].ID * 143 % 256);
        
        //cv::rectangle(yMat, cv::Point2f(0, 0), cv::Point2f(50, 50), scalar_color, 2);
        //cv::rectangle(yMat, cv::Point2f(500, 500), cv::Point2f(550, 550), scalar_color, 2);
        
        cv::rectangle(yMat, cv::Point2f(static_cast<float>(p_face[i].rect.left),
            static_cast<float>(p_face[i].rect.top)),
            cv::Point2f(static_cast<float>(p_face[i].rect.right),
            static_cast<float>(p_face[i].rect.bottom)), scalar_color, 2);
        
        for (int j = 0; j < p_face[i].points_count; j++)
        {
            cv::circle(yMat, cv::Point2f(p_face[i].points_array[j].x,
                p_face[i].points_array[j].y), 1, cv::Scalar(255, 255, 255));
        }
    }
    
    //if (face_count > 0)
    //{
    //    static size_t f=0;
    //    char fname[50];
    //    sprintf(fname, "face-%u.yuv420", ++f);
    //    FILE * pFile = fopen (fname,"wb");
    //    fwrite (yuvMat.data , sizeof(char), 1920*1080*1.5, pFile);
    //    printf("write face file %s\n", fname);
    //    fclose(pFile);
    //}
 
    // release the memory of face
    cv_face_release_tracker_result(p_face, face_count);
 
    return face_count;
}
 
bool PL_SensetimeFaceDetect::pay(const PipeMaterial& pm)
{
    PL_SensetimeFaceDetect_Internal* in = (PL_SensetimeFaceDetect_Internal*)internal;
 
    if (pm.type != PipeMaterial::PMT_FRAME)
    {
        LOG_ERROR << "PL_H264Encoder::pay only support PMT_FRAME";
        return false;
    }
    
    if (pm.buffer == nullptr)
        return false;
    
    MB_Frame* frame = (MB_Frame*)pm.buffer;
    if (frame->type != MB_Frame::MBFT_YUV420)
    {
        LOG_ERROR << "PL_H264Encoder::pay only support MBFT_YUV420";
        return false;
    }
 
    int face_count = doFaceDetect(in, (uint8_t*)frame->buffer, 1920, 1080, 1920, CV_PIX_FMT_YUV420P);//#todo
    if (face_count < 0)
    {
        in->payError = true;
        return false;
    }
    else
        in->payError = false;
    
    //in->buffer readly
 
    in->lastFrame.type = MB_Frame::MBFT_YUV420;
    in->lastFrame.buffer = frame->buffer;//#todo should copy
    in->lastFrame.buffSize = frame->buffSize;
    in->lastFrame.width = frame->width;
    in->lastFrame.height = frame->height;
    in->lastFrame.pts = frame->pts;
 
    return true;
}
 
bool PL_SensetimeFaceDetect::gain(PipeMaterial& pm)
{
    PL_SensetimeFaceDetect_Internal* in = (PL_SensetimeFaceDetect_Internal*)internal;
 
    if (!in->payError)
    {
        pm.type = PipeMaterial::PMT_FRAME;
        pm.buffer = &(in->lastFrame);
        pm.buffSize = 0;
        pm.former = this;
    }
    pm.former = this;
    return !in->payError;
}