houxiao
2017-01-11 97dbd476ee99f36286cd866b2208ceead3019b31
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
#include "PL_SensetimeFaceDetect.h"
#include "MaterialBuffer.h"
#include "logger.h"
 
#include "SensetimeFaceAPIWrapper/src/FaceDBPool.h"
#include <opencv2/opencv.hpp>
#include <cv_face.h>
 
struct PL_SensetimeFaceDetect_Internal
{
    bool payError;
    PL_SensetimeFaceDetectConfig config;
    SensetimeFaceDetectResult lastResult;
    
    PL_SensetimeFaceDetect_Internal() : 
        payError(true), config(), lastResult()
    {
    }
    
    ~PL_SensetimeFaceDetect_Internal()
    {
    }
    
    void reset()
    {
        payError = true;
        
        PL_SensetimeFaceDetectConfig _config;
        config = _config;
        
        SensetimeFaceDetectResult _lastResult;
        lastResult = _lastResult;
    }
};
 
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();
    
    if (args != nullptr)
    {
        PL_SensetimeFaceDetectConfig* config = (PL_SensetimeFaceDetectConfig*)args;
        in->config = *config;
    }
 
    return true;
}
 
void PL_SensetimeFaceDetect::finit()
{
    PL_SensetimeFaceDetect_Internal* in = (PL_SensetimeFaceDetect_Internal*)internal;
 
}
 
bool PL_SensetimeFaceDetect::pay(const PipeMaterial& pm)
{
    PL_SensetimeFaceDetect_Internal* in = (PL_SensetimeFaceDetect_Internal*)internal;
    
    in->payError = true;
    {
        SensetimeFaceDetectResult _lastResult;
        in->lastResult = _lastResult;
    }
 
    if (pm.type != PipeMaterial::PMT_FRAME || !(in->config.payWithDbFrame))
    {
        LOG_ERROR << "Only support PMT_FRAME (SensetimeFaceDetectDbFrame)";
        return false;
    }
    
    if (pm.buffer == nullptr)
        return false;
    
    SensetimeFaceDetectDbFrame* frame = (SensetimeFaceDetectDbFrame*)pm.buffer;
    if (frame->type != MB_Frame::MBFT_YUV420)
    {
        LOG_ERROR << "Only support MBFT_YUV420";
        return false;
    }
 
    faceDB* _faceDB = (faceDB*)frame->_faceDB;
    if (_faceDB == nullptr)
    {
        LOG_ERROR << "faceDB is null";
        return false;
    }
    
    const size_t expectedYUVSize = frame->width * frame->height * 1.5;
    if (frame->buffSize < expectedYUVSize)
    {
        LOG_WARN << "image size not ok";
        return false;
    }
    
    cv::Mat yuvMat(cv::Size(frame->width,frame->height), CV_8UC3, frame->buffer);
    cv_feature_t* feature = _faceDB->extract_feature(yuvMat);
    in->lastResult.st_id = _faceDB->search_db(feature);
    //#todo release feature
    
    in->lastResult.school_id = frame->school_id;
    
    in->payError = false;
    return true;
}
 
bool PL_SensetimeFaceDetect::gain(PipeMaterial& pm)
{
    PL_SensetimeFaceDetect_Internal* in = (PL_SensetimeFaceDetect_Internal*)internal;
 
    pm.former = this;
    
    if (in->payError)
    {
        return false;
    }
    
    if (! in->config.resultStructOnly)
    {
        LOG_ERROR << "Only support resultStructOnly";
        return false;
    }
    
    pm.type = PipeMaterial::PMT_BYTES;
    pm.buffer = &(in->lastResult);
    pm.buffSize = sizeof(SensetimeFaceDetectResult);
 
    return true;
}