移动端的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
#include "mainwidget.h"
#include <QVBoxLayout>
#include <QDebug>
#include <QScreen>
#include <QRect>
#include <QGuiApplication>
#include <QStyleOption>
#include <unistd.h>
#include <QObject>
#include <QImage>
#include <QMetaType>
 
#include "faceLib/common.h"
#include "opencv2/opencv.hpp"
 
void initDbPersons(int chan);
 
mainWidget::mainWidget(QWidget *parent)
    : QWidget(parent)
    , ft(0,1)
    , fe(1)
{
    qRegisterMetaType<ImgToShow>("ImgToShow");//注册ImgToShow类型
    qRegisterMetaType<std::string>("std::string");//注册ImgToShow类型
    initDbPersons(0);
 
    ft.setnSampleSize(640);
    printf("do Threads... \n");
    fe.run(0);
    ft.run(0);
 
    initParams();
    initTimer();
 
    QObject::connect(&ft,SIGNAL(drawImage(ImgToShow)),this,SLOT(on_ImageShow(ImgToShow)));
    QObject::connect(&ft,SIGNAL(signalAdvertise(bool)),this,SLOT(slotAdvertise(bool)));
    QObject::connect(&ft,SIGNAL(signalTips(std::string)),this,SLOT(slotTips(std::string)));
    QObject::connect(&fe,SIGNAL(signalTips(std::string)),this,SLOT(slotTips(std::string)));
}
 
mainWidget::~mainWidget()
{
    delete pTimer;
    delete pDateTime;
 
    delete pLabelVideo; //video
    delete pLabelAdv;
    delete pPixAdv; //advertise pic
    delete pStackWgt;
}
 
void mainWidget::initParams()
{
    QList<QScreen *> list_screen =  QGuiApplication::screens();  //多显示器
    QRect rect = list_screen.at(0)->geometry();
    qDebug() << rect.width() << rect.height();
//    this->resize(rect.width(), rect.height());
 
    this->resize(800, 600);
 
    this->setObjectName("mainWidget");
    this->setStyleSheet("QWidget#mainWidget{"
                        "border-image: url(:/res/background.png);}");
    pPixAdv = new QPixmap(":/res/adv.jpg");
 
    QVBoxLayout *vLayout = new QVBoxLayout(this);
    pLabelVideo = new myLabel;
    pLabelAdv = new QLabel;
    pStackWgt = new QStackedWidget(this);
    pStackWgt->addWidget(pLabelVideo);
    pStackWgt->addWidget(pLabelAdv);
 
    vLayout->addWidget(pStackWgt);
    vLayout->setMargin(10);
    vLayout->setStretchFactor(pStackWgt,10);
    this->setLayout(vLayout);
 
    this->show();//FullScreen();
}
 
void mainWidget::initTimer()
{
    pTimer = new QTimer;
    pTimer->setSingleShot(true);
    connect(pTimer,SIGNAL(timeout()),this,SLOT(slotClear()));
}
 
void mainWidget::paintEvent(QPaintEvent *)
{
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
 
void mainWidget::moveEvent(QMoveEvent *)
{
    this->move(QPoint(0,0));
}
 
void mainWidget::slotClear()
{
    pLabelVideo->setMyText(QString(""));
    pLabelVideo->setMyShow(false);
}
 
void mainWidget::slotAdvertise(bool value)
{
    qDebug()<< "void MainWidget::slotAdvertise()" << value;
    if (value)
    {
        std::cout << "111cpStackWgt: " << pStackWgt->width() << "x" << pStackWgt->height() << std::endl;
        QSize picSize(pStackWgt->width(),pStackWgt->height());
        QPixmap scaledPixmap = pPixAdv->scaled(picSize);
        pLabelAdv->setPixmap(scaledPixmap);
        pStackWgt->setCurrentIndex(1);
    } else {
        pStackWgt->setCurrentIndex(0);
    }
}
 
void initDbPersons(int chan)
{
    std::string basePath = "/data/disk2/face/pic/";
    std::vector<std::string> persons = {
            "chenshijun",
            "chenshijun2",
            "liuxin",
            "shiyajun",
            "suntianyu",
            "wangzong",
            "zhangzengfei"
    };
    cv::Mat frame;
    for (auto person:persons) {
        THFT_FaceInfo faceInfo;
        frame = cv::imread(basePath + person + ".jpg", cv::IMREAD_COLOR);
        int ret = THFT_FaceDetect(chan, frame.data, frame.cols, frame.rows, &faceInfo,
                                  1, 0);
        if (ret > 0) {
            THFI_FacePos facePos;
            BYTE *feature = new BYTE[2560];
            memcpy(&facePos, &faceInfo, sizeof(THFI_FacePos));
            ret = EF_Extract((short) chan, frame.data, frame.cols,
                             frame.rows, 3, &facePos, feature);
            if (ret == 1) {
                mFaceRec.insert(std::pair<std::string, BYTE *>(person, feature));
                printf("insert face :%s %p\n", person.c_str(), feature);
            }
        }
    }
}
 
void mainWidget::on_ImageShow(ImgToShow image)
{
    if((image.nWidth > 0) && (image.nHeight > 0) && (image.imgData != nullptr)){
        QImage qimg((const unsigned char*)image.imgData, image.nWidth, image.nHeight, image.nWidth * 3,
                            QImage::Format_RGB888);
        auto qpixmap = QPixmap::fromImage(qimg);
        QSize picSize(pStackWgt->width(),pStackWgt->height());
        QPixmap scaledPixmap = qpixmap.scaled(picSize);
        pLabelVideo->setMyPixmap(scaledPixmap);
        update();
        delete image.imgData;
    }
}
 
void mainWidget::slotTips(std::string str)
{
    pLabelVideo->setMyText(QString(str.data()));
    pLabelVideo->setMyShow(true);
    pTimer->start(2000);
}