houxiao
2017-04-11 3839ef806215a981ef9b2e83dafaab5e1d694764
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
#include "FaceCache.h"
#include "PipeLine.h"
#include "MaterialBuffer.h"
#include "PL_SensetimeFaceTrack.h"
#include <logger.h>
#include <Logger/src/logger.hpp>
 
#include <opencv/cv.h>
#include <opencv/cxcore.h>
#include <opencv/highgui.h>
 
#include <vector>
 
#include "PbFaceList.pb.h"
 
#include <libyuv.h>
 
struct FcPmBreackerContext
{
    uint8_t frameYUV[1920*1080*4];
    size_t frameYUVSize;
    
    uint8_t frameRGB[1920*1080*4];
    size_t frameRGBSize;
 
    size_t width;
    size_t height;
    
    st_ff_vect_t faceFeatures;
    
    bool dataAvailable;
    
    FcPmBreackerContext() :
            frameYUV(), frameYUVSize(0), frameRGB(), frameRGBSize(0),
            faceFeatures(), width(0), height(0), dataAvailable(false)
    {
    }
    
    void reset()
    {
        frameYUVSize = 0;
        frameRGBSize = 0;
        width = 0;
        height = 0;
        dataAvailable = false;
        faceFeatures.clear();
    }
    
    bool convertYUV420ToRGB()
    {
#define SUBSAMPLE(v, a) ((((v) + (a) - 1)) / (a))
 
        int src_height = height;
        int src_width = width;
        const uint8* src_y = (const uint8*)(frameYUV);
        const uint8* src_u = (const uint8*)(src_y + (src_height * src_width));
        const uint8* src_v = (const uint8*)(src_u + (src_height * src_width / 4));
 
        libyuv::I420ToRGB24(src_y, src_width,
                           src_u, SUBSAMPLE(src_width, 2),
                           src_v, SUBSAMPLE(src_width, 2),
                           frameRGB, 3 * src_width,
                           src_width, src_height);
        frameRGBSize = src_height * src_width * 3;
        return true;
    }
};
 
bool fc_pm_breaker_ptr(const PipeMaterial* pm, void* args)
{
    FcPmBreackerContext* ctx = (FcPmBreackerContext*)args;
    
    const st_ff_vect_t& faceFeatures(*(const st_ff_vect_t*)(pm->buffer));
    
    ctx->faceFeatures = faceFeatures;
    
    ctx->dataAvailable &= true;
    return true;
}
 
bool fc_pm_breaker_frame(const PipeMaterial* pm, void* args)
{
    FcPmBreackerContext* ctx = (FcPmBreackerContext*)args;
    
    const MB_Frame* lastFrame((const MB_Frame*)(pm->buffer));
    if (lastFrame->type != MB_Frame::MBFT_YUV420)
    {
        ctx->dataAvailable &= false;
        return false;
    }
    
    if (lastFrame->buffSize == 0)
    {
        ctx->dataAvailable &= false;
        return false;
    }
 
    ctx->frameYUVSize = lastFrame->buffSize;
    ctx->width = lastFrame->width;
    ctx->height = lastFrame->height;
    memcpy(ctx->frameYUV, lastFrame->buffer, ctx->frameYUVSize);
    
    ctx->dataAvailable &= true;
    return true;
}
 
#ifdef USE_ST_SDK
FaceCache::FaceCache() : _ctx(new FcPmBreackerContext)
{
}
 
FaceCache::~FaceCache()
{
    delete (FcPmBreackerContext*)_ctx;
}
#endif
 
// returns count of face
int FaceCache::cachePm(const PipeMaterial& pm)
{
    if (_ctx == nullptr)
        _ctx = new FcPmBreackerContext;
    
    FcPmBreackerContext& ctx(*(FcPmBreackerContext*)_ctx);
    ctx.reset();
    ctx.dataAvailable = true;
    pm.breake(PipeMaterial::PMT_PTR, MB_Frame::MBFT__FIRST, fc_pm_breaker_ptr, _ctx);
    pm.breake(PipeMaterial::PMT_FRAME, MB_Frame::MBFT__FIRST, fc_pm_breaker_frame, _ctx);
    
    if (ctx.dataAvailable)
        return ctx.faceFeatures.size();
    else
        return 0;
}
 
bool FaceCache::getFaceListPb(uint8_t* buffer, size_t& buffMaxSize)
{
    FcPmBreackerContext& ctx(*(FcPmBreackerContext*)_ctx);
    if (!ctx.dataAvailable)
        return false;
    
    PbFaceList pbFaceList;
    pbFaceList.set_magic(pbFaceList.magic());
    pbFaceList.set_image_count(ctx.faceFeatures.size());
    pbFaceList.set_src_width(ctx.width);
    pbFaceList.set_src_height(ctx.height);
    
    cv::Mat yMat(cv::Size(ctx.width, ctx.height), CV_8UC1, ctx.frameYUV);
 
    for (int i = 0; i < ctx.faceFeatures.size(); i++)
    {
        const FaceRect& faceRect(ctx.faceFeatures[i].rect);
        cv::Mat roiMat(yMat, cv::Rect(faceRect.leftTop.x, faceRect.leftTop.y, faceRect.rightBottom.x - faceRect.leftTop.x, faceRect.rightBottom.y - faceRect.leftTop.y));
        
        PbFaceList_FaceListImage pbFaceListImage;
        pbFaceListImage.set_idx(i);
        pbFaceListImage.set_size(roiMat.elemSize());
        pbFaceListImage.set_type(PbFaceList_FaceListImage_ImageType_IT_Y);
        pbFaceListImage.set_width(roiMat.cols);
        pbFaceListImage.set_height(roiMat.rows);
        pbFaceListImage.set_top_left_x(faceRect.leftTop.x);
        pbFaceListImage.set_top_left_y(faceRect.leftTop.y);
        
        pbFaceListImage.add_img(roiMat.ptr(), roiMat.total() * roiMat.elemSize());
    }
 
    size_t s = pbFaceList.ByteSize();
    buffMaxSize = std::min(s, buffMaxSize);
    pbFaceList.SerializeToArray(buffer, buffMaxSize);
 
    return buffMaxSize > 0;
}
 
bool FaceCache::getFaceListImage(int* buffIdx, size_t& count, uint8_t* buffImg, size_t& buffImgMaxSize)
{
    FcPmBreackerContext& ctx(*(FcPmBreackerContext*)_ctx);
 
    if (ctx.frameRGBSize == 0)
    {
        //#todo should optimize not convert the whole image
        if (! ctx.convertYUV420ToRGB())
            return false;
    }
    
    count = 0;
    uint8_t* pBuff = buffImg;
    size_t totalSize = 0;
 
    cv::Mat rgbMat(cv::Size(ctx.width, ctx.height), CV_8UC3, ctx.frameRGB);
    for (st_ff_vect_t::const_iterator ffiter = ctx.faceFeatures.begin(); ffiter != ctx.faceFeatures.end(); ++ffiter)
    {
        const FaceRect& faceRect(ffiter->rect);
        cv::Mat roiMat(rgbMat, cv::Rect(faceRect.leftTop.x, faceRect.leftTop.y, faceRect.rightBottom.x - faceRect.leftTop.x, faceRect.rightBottom.y - faceRect.leftTop.y));
        
        //isContinuous
        size_t s = roiMat.total() * roiMat.elemSize();
        if (totalSize + s <= buffImgMaxSize)
        {
            memcpy(pBuff, roiMat.ptr(), s);
            *buffIdx = int(totalSize);
            
            pBuff += s;
            totalSize += s;
            count++;
            buffIdx++;
        }
    }
 
    buffImgMaxSize = totalSize;
    return (count != 0);
}