移动端的qt版本人脸流程
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
 
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <unistd.h>
#include <algorithm>
#include <iostream>
#include <thread>
 
#include "FiStdDefEx.h"
#include "detect.h"
 
faceTracking::faceTracking(int devId, int chan, int maxFaceNum, int minWidth, int yaw, int pitch, int roll,
                           int confidence, int sampSize, int interval) {
    nChannel = chan < MAX_CHANNEL ? chan : MAX_CHANNEL;
    nMaxFaceNum = maxFaceNum < MAX_FACE_NUM ? maxFaceNum : MAX_FACE_NUM;
    nSampleSize = sampSize / 80 * 80;
    setThresVal(confidence, minWidth, yaw, pitch, roll);
    nDetectionIntervalFrame = interval < 1 ? 1 : interval;
    int ret = initDecode(devId);
    if (ret < 0) {
        printf("initDecode:%d video:%d\n", ret, devId);
    }
    printf("initDecode:%d video:%d\n", ret, devId);
    ret = initTrack();
    printf("initTrack:%d \n", ret);
}
 
faceTracking::~faceTracking() {
    printf("~faceTracking()");
    THFT_Release();
    vc.release();
}
 
int faceTracking::initDecode(int devId) {
    if (devId <= 0) {
        devId = 0;
    }
 
    bool bOpen = vc.open(devId);
    if (!bOpen) {
        printf("Open devId %d failed.\n", devId);
        return -1;
    }
    nWidth = vc.get(CV_CAP_PROP_FRAME_WIDTH);
    nHeight = vc.get(CV_CAP_PROP_FRAME_HEIGHT);
    printf("FRAME_WIDTH=%d,FRAME_HEIGHT=%d\n", nWidth, nHeight);
 
    return 0;
}
 
int faceTracking::initTrack(void) {
    printf("THFI_Create_Ex:nChannel:%d\n", nChannel);
    THFT_Param detParam;
    detParam.nImageWidth = nWidth;
    detParam.nImageHeight = nHeight;
    detParam.nMaxFaceNum = nMaxFaceNum;
    detParam.nSampleSize = nSampleSize;
    detParam.nDetectionIntervalFrame = nDetectionIntervalFrame;
 
    int num = THFT_Create(nChannel, &detParam);
    if (num != nChannel) {
        printf("THFI_Create failed!(ret=%d)\n", num);
        return -1;
    }
    printf("THFI_Create:ret:%d\n", num);
 
    for (int i = 0; i < nChannel; i++) {
        std::map<long, float> *faces = new std::map<long, float>;
        recChanFaces.insert(std::pair<int, std::map<long, float>>(i, *faces));
    }
 
    return 0;
}
 
int faceTracking::resetWH(int w, int h, int chan) {
    printf("THFT_Reset:%d %d %d\n", chan, w, h);
    if ((chan >= nChannel) || (w <= 0) || (h <= 0)) {
        printf("THFT_Reset params error!\n");
        return -1;
    }
    THFT_Param detParam;
    detParam.nImageWidth = w;
    detParam.nImageHeight = h;
    detParam.nMaxFaceNum = nMaxFaceNum;
    detParam.nSampleSize = nSampleSize;
    detParam.nDetectionIntervalFrame = nDetectionIntervalFrame;
 
    int ret = THFT_Reset(nChannel, &detParam);
    if (ret != 0) {
        printf("THFT_Reset failed!(ret=%d)\n", ret);
        return -1;
    }
    return 0;
}
 
int faceTracking::detect(int chan, BYTE *imgData, THFT_FaceInfo *outObjs) {
    double t1, t2;
    t1 = msecond();
    int num = THFT_FaceTracking(chan, imgData, outObjs);
    t2 = msecond();
    printf("THFI_DetectFace time = %04f ms\n", (float) (t2 - t1));
 
    if (num < 0) {
        printf("THFI_DetectFace:(ret=%d)\n", num);
        return -1;
    }
 
    return num;
}
 
void faceTracking::drawFaceRect(cv::Mat& frame, RECT rcFace)
{
    int lenghtPercent = 8;
    auto color = cvScalar(225, 225, 225, 128);
    int thick = 2;
    cv::rectangle(frame, cv::Point(rcFace.left, rcFace.top),
                  cv::Point(rcFace.right, rcFace.bottom),
                  color, 1, CV_AA, 0);
 
    cv::line(frame, cv::Point(rcFace.left, rcFace.top),
             cv::Point(rcFace.left, rcFace.top +
                       (rcFace.bottom - rcFace.top)/lenghtPercent),
             color, thick, CV_AA, 0);
    cv::line(frame, cv::Point(rcFace.left, rcFace.top),
             cv::Point(rcFace.left +
                       (rcFace.right - rcFace.left)/lenghtPercent,
                       rcFace.top),
             color, thick, CV_AA, 0);
 
    cv::line(frame, cv::Point(rcFace.left, rcFace.bottom -
                              (rcFace.bottom - rcFace.top)/lenghtPercent),
             cv::Point(rcFace.left, rcFace.bottom),
             color, thick, CV_AA, 0);
    cv::line(frame, cv::Point(rcFace.left, rcFace.bottom),
             cv::Point(rcFace.left +
                       (rcFace.right - rcFace.left)/lenghtPercent,
                       rcFace.bottom),
             color, thick, CV_AA, 0);
 
    cv::line(frame, cv::Point(rcFace.right -
                              (rcFace.right - rcFace.left)/lenghtPercent,
                              rcFace.bottom),
             cv::Point(rcFace.right, rcFace.bottom),
             color, thick, CV_AA, 0);
    cv::line(frame, cv::Point(rcFace.right, rcFace.bottom),
             cv::Point(rcFace.right,
                       rcFace.bottom -
                       (rcFace.bottom - rcFace.top)/lenghtPercent),
            color, thick, CV_AA, 0);
 
    cv::line(frame, cv::Point(rcFace.right -
                              (rcFace.right - rcFace.left)/lenghtPercent,
                              rcFace.top),
             cv::Point(rcFace.right, rcFace.top),
             color, thick, CV_AA, 0);
    cv::line(frame, cv::Point(rcFace.right, rcFace.top),
             cv::Point(rcFace.right,
                       rcFace.top +
                       (rcFace.bottom - rcFace.top)/lenghtPercent),
            color, thick, CV_AA, 0);
}
 
void faceTracking::run(int chan) {
    printf("faceTracking chan: %d \n", chan);
    std::thread([&](short chan, faceTracking *pThis) {
        cv::Mat frame;
        double showT1, showT2;
        static bool isAdvNow = false;
        while (true) {
            if (!frame.empty()) {
                frame.release();
            }
            pThis->vc >> frame;
 
            if (frame.empty()) {
                usleep(30000);//30ms
                continue;
            }
 
            cv::flip(frame, frame, 1);
 
            THFT_FaceInfo *pFaceInfos = new THFT_FaceInfo[pThis->nMaxFaceNum];
            //            double t1, t2;
            //            t1 = msecond();
            int num = THFT_FaceTracking(chan, frame.data, pFaceInfos);
            //            t2 = msecond();
            //            printf("THFI_DetectFace time = %04f ms, num: %d \n", (float) (t2 - t1), num);
 
            for (int i = 0; i < num; i++) {
                showT1 = msecond();
                if (isAdvNow == true) {
                    emit signalAdvertise(false);
                    isAdvNow = false;
                }
                //draw face rect
                drawFaceRect(frame, pFaceInfos[i].rcFace);
 
 
 
//                                char title[128];
//                                auto it = mIDName.find(pFaceInfos[i].nFaceID);
//                                if (it != mIDName.end()){
//                                    sprintf(title, "id=%ld %s", pFaceInfos[i].nFaceID, it->second.c_str());
//                                } else {
//                                    sprintf(title, "id=%ld", pFaceInfos[i].nFaceID);
//                                }
 
//                                cv::putText(frame, title, cv::Point(pFaceInfos[i].rcFace.left + 1, pFaceInfos[i].rcFace.bottom - 5),
//                                            cv::FONT_HERSHEY_COMPLEX, 1.0f, cvScalar(0, 255, 255),
//                                            1, 8, false);
            }
 
            cv::Mat frameNew;
            cv::cvtColor(frame, frameNew, cv::COLOR_BGR2RGB);
            ImgToShow imgS;
            imgS.nWidth = frameNew.cols;
            imgS.nHeight = frameNew.rows;
            imgS.imgData = new BYTE[frameNew.cols * frameNew.rows * 3];
            memcpy(imgS.imgData, frameNew.data, frameNew.cols * frameNew.rows * 3);
            emit drawImage(imgS);
            //            std::unique_lock<std::mutex> lockfs(mtxFrameShow, std::defer_lock);
            //            lockfs.lock();
            //            qFrameShow.push(imgS);
            //            lockfs.unlock();
 
            if (num <= 0) {
                delete[] pFaceInfos;
            } else if (num > 0) {
                char *img = new char[frame.cols * frame.rows * 3];
                memcpy(img, frame.data, frame.cols * frame.rows * 3);
                //                printf("sendFacesToExtract: wxh = %d x %d img:%p\n", frame.cols, frame.rows, img);
                sendFacesToExtract(chan, pFaceInfos, num, (BYTE *) img, frame.cols, frame.rows);
            }
 
            showT2 = msecond();
 
            if (showT2 - showT1 > pThis->screenSaveTime_ms) {
                //                std::cout << "showT2 - showT1:" << showT2 - showT1 << " > " << pThis->screenSaveTime_ms <<  std::endl;
                if (isAdvNow == false) {
                    emit signalAdvertise(true);
                    isAdvNow = true;
                }
            }
        }
    }, short(chan), this).detach();
}
 
 
bool faceTracking::sendFacesToExtract(int chan, THFT_FaceInfo *faces, int num, BYTE *imgData, int width, int height) {
    //todo
    bool ok = false;
    ImgToExtract *img2extr = new ImgToExtract;
 
    int faceCount = 0;
 
    for (int i = 0; i < num; i++) {
        if ((faces[i].fAngle.confidence >= treshConfidence) &&
                (faces[i].rcFace.right - faces[i].rcFace.left >= treshMinWidth) &&
                (std::abs(faces[i].fAngle.yaw) <= treshYaw) &&
                (std::abs(faces[i].fAngle.pitch) <= treshPitch) &&
                (std::abs(faces[i].fAngle.roll) <= treshRoll)) {
            auto scoreIter = recChanFaces[chan].find(faces[i].nFaceID);
            if (scoreIter == recChanFaces[chan].end()) {
                recChanFaces[chan].insert(std::pair<long, float>(faces[i].nFaceID, faces[i].fAngle.confidence));
                ok = true;
                THFT_FaceInfo *vface = new THFT_FaceInfo(faces[i]);
                img2extr->vFaces.emplace_back(vface);
                faceCount++;
 
                auto it = mIDTime.find(faces[i].nFaceID);
                if (it == mIDTime.end()) {
                    mIDTime[faces[i].nFaceID] = msecond();
                }
            } else {
                if ((scoreIter->second + confidenceInc) < faces[i].fAngle.confidence) {
                    recChanFaces[chan][faces[i].nFaceID] = faces[i].fAngle.confidence;
                    ok = true;
                    THFT_FaceInfo *vface = new THFT_FaceInfo(faces[i]);
                    img2extr->vFaces.emplace_back(vface);
                    faceCount++;
                } else {
                    double nowT = msecond();
                    auto it = mIDTime.find(faces[i].nFaceID);
                    if ((it == mIDTime.end()) || (nowT > (interval + mIDTime[faces[i].nFaceID]))){
                        mIDTime[faces[i].nFaceID] = msecond();
                        recChanFaces[chan][faces[i].nFaceID] = faces[i].fAngle.confidence;
                        ok = true;
                        THFT_FaceInfo *vface = new THFT_FaceInfo(faces[i]);
                        img2extr->vFaces.emplace_back(vface);
                        faceCount++;
                    }
                }
            }
        } else {
            auto it = mIDName.find(faces[i].nFaceID);
            if (it != mIDName.end()){
                emit signalTips(std::string(it->second) + std::string(",请进"));
            } else {
                emit signalTips(std::string("请正视镜头"));
            }
        }
    }
    if (ok) {
        img2extr->nWidth = width;
        img2extr->nHeight = height;
        img2extr->imgData = imgData;
        pushQImg2Extr(img2extr);
    } else {
        delete imgData;
        delete img2extr;
    }
    delete[] faces;
 
    return ok;
}
 
void faceTracking::setThresVal(int confidence, int width, int yaw, int pitch, int roll) {
    treshYaw = yaw < MAX_ANGLE ? yaw : MAX_ANGLE;
    treshPitch = pitch < MAX_ANGLE ? pitch : MAX_ANGLE;
    treshRoll = roll < MAX_ANGLE ? roll : MAX_ANGLE;
    treshMinWidth = width > MIN_WIDTH ? width : MIN_WIDTH;
    treshConfidence = confidence > MIN_CONFIDENCE ? (float) confidence / 100 : (float) MIN_CONFIDENCE / 100;
}