Qt编写百度离线版人脸识别+比对+活体检测

在AI技术发展迅猛的今天,很多设备都希望加上人脸识别功能,好像不加上点人脸识别功能感觉不够高大上,都往人脸识别这边靠,手机刷脸解锁,刷脸支付,刷脸开门,刷脸金融,刷脸安防,是不是以后还可以刷脸匹配男女交友?
很多人认为人脸识别直接用opencv做,其实那只是极其基础的识别个人脸,然并卵,好比学C++写了个hello类似。拿到人脸区域图片只是万里长征的第一步,真正能够起作用的是人脸特征值的提取,然后用于搜索和查找人脸,比如两张图片比较相似度,从一堆人脸库中找到最相似的人脸,对当前人脸识别是否是活体等。
对于可以接入外网的设备,可以直接通过在线api的http请求方式获得结果,但是有很多应用场景是离线的,或者说不通外网,只能局域网使用,为了安全性考虑,这个时候就要求所有的人脸处理在本地完成,本篇文章采用的百度离线SDK作为解决方案。可以去官网申请,默认有6个免费的密钥使用三个月,需要与本地设备的指纹信息匹配,感兴趣的同学可以自行去官网下载SDK。
百度离线人脸识别SDK文件比较大,光模型文件就645MB,估计这也许是识别率比较高的一方面原因吧,不断训练得出的模型库,本篇文章只放出Qt封装部分源码。官网对应的使用说明还是非常详细的,只要是学过编程的人就可以看懂。
第一步:初始化SDK
第二步:执行动作,比如查找人脸、图片比对、特征值比对等

 

完整头文件代码:

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
#ifndef FACEBAIDULOCAL_H
#define FACEBAIDULOCAL_H
 
/**
 * 百度离线版人脸识别+人脸比对等功能类 作者:feiyangqingyun(QQ:517216493) 2018-8-30
 * 1:支持活体检测
 * 2:可设置最大队列中的图片数量
 * 3:多线程处理,通过type控制当前处理类型
 * 4:支持单张图片检索相似度最高的图片
 * 5:支持指定目录图片生成特征文件
 * 6:支持两张图片比对方式
 * 7:可设置是否快速查找
 * 8:可设置是否统计用时
 */
 
#include <QtCore>
#include <QtGui>
#if (QT_VERSION > QT_VERSION_CHECK(5,0,0))
#include <QtWidgets>
#endif
#include "baidu_face_api.h"
 
class FaceBaiDuLocal : public QThread
{
    Q_OBJECT
public:
    static FaceBaiDuLocal *Instance();
    explicit FaceBaiDuLocal(QObject *parent = 0);
    ~FaceBaiDuLocal();
 
protected:
    void run();
 
private:
    static QScopedPointer<FaceBaiDuLocal> self;
 
    BaiduFaceApi *api;
    std::vector<TrackFaceInfo> *faces;
 
    QMutex mutex;                   //锁对象
    bool stopped;                   //线程停止标志位
 
    int maxCount;                   //最大图片张数
    int type;                       //当前处理类型
    int percent;                    //最小人脸百分比
    int delayms;                    //减去毫秒数,用于造假
    bool findFast;                  //是否快速模式
    bool countTime;                 //统计用时
    bool busy;                      //是否正忙
 
    QList<QString> flags;           //等待处理的图像队列的名称
    QList<QImage> imgs;             //等待处理的图像队列
    QList<QImage> imgs2;            //等待处理的比对图像队列
 
    QString sdkPath;                //SDK目录
    QString imgDir;                 //图片目录
    QImage oneImg;                  //单张图片比对找出最大特征图像
    QList<QString> imgNames;        //图像队列
    QList<QList<float> > features;  //特征队列
 
signals:
    //人脸区域坐标返回
    void receiveFaceRect(const QString &flag, const QRect &rect, int msec);
    //获取人脸区域坐标失败
    void receiveFaceRectFail(const QString &flag);
 
    //人脸特征返回
    void receiveFaceFeature(const QString &flag, const QList<float> &feature, int msec);
    //获取人脸特征失败
    void receiveFaceFeatureFail(const QString &flag);
 
    //人脸比对结果返回
    void receiveFaceCompare(const QString &flag, float result, int msec);
    //人脸比对失败
    void receiveFaceCompareFail(const QString &flag);
 
    //单张图片检索最大相似度结果返回
    void receiveFaceCompareOne(const QString &flag, const QImage &srcImg, const QString &targetName, float result);
    //所有人脸特征提取完毕
    void receiveFaceFeatureFinsh();
 
    //活体检测返回
    void receiveFaceLive(const QString &flag, float result, int msec);
    //活体检测失败
    void receiveFaceLiveFail(const QString &flag);
 
public slots:
    //初始化SDK
    void init();
    //停止处理线程
    void stop();
    //获取当前是否忙
    bool getBusy();
 
    //设置图片队列最大张数
    void setMaxCount(int maxCount);
    //设置当前处理类型
    void setType(int type);
    //设置最小人脸百分比
    void setPercent(int percent);
    //设置减去毫秒数
    void setDelayms(int delayms);
    //设置是否快速模式
    void setFindFast(bool findFast);
    //设置是否统计用时
    void setCountTime(bool countTime);
    //设置是否忙
    void setBusy(bool busy);
 
    //设置SDK目录
    void setSDKPath(const QString &sdkPath);
    //设置要将图片提取出特征的目录
    void setImgDir(const QString &imgDir);
    //设置单张需要检索的图片
    void setOneImg(const QString &flag, const QImage &oneImg);
 
    //往队列中追加单张图片等待处理
    void append(const QString &flag, const QImage &img);
    //往队列中追加两张图片等待比对
    void append(const QString &flag, const QImage &img, const QImage &img2);
 
 
    //自动加载目录下的所有图片的特征
    void getFaceFeatures(const QString &imgDir);
 
    //获取人脸区域
    bool getFaceRect(const QString &flag, const QImage &img, QRect &rect, int &msec);
 
    //活体检测
    bool getFaceLive(const QString &flag, const QImage &img, float &result, int &msec);
 
    //获取人脸特征
    bool getFaceFeature(const QString &flag, const QImage &img, QList<float> &feature, int &msec);
 
    //人脸比对,传入两张照片特征
    float getFaceCompare(const QString &flag, const QList<float> &feature1, const QList<float> &feature2);
    //人脸比对,传入两张照片
    bool getFaceCompare(const QString &flag, const QImage &img1, const QImage &img2, float &result, int &msec);
 
    //从一堆图片中找到最像的一张图片
    void getFaceOne(const QString &flag, const QImage &img, QString &targetName, float &result);
    //指定特征找到照片
    void getFaceOne(const QString &flag, const QList<float> &feature, QString &targetName, float &result);
 
    //添加人脸
    void appendFace(const QString &flag, const QImage &img, const QString &txtFile);
    //删除人脸
    void deleteFace(const QString &flag);
};
 
#endif // FACEBAIDULOCAL_H

完整实现文件代码:

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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#include "facebaidulocal.h"
 
#define TIMEMS qPrintable(QTime::currentTime().toString("HH:mm:ss zzz"))
 
QByteArray getImageData(const QImage &image)
{
    QByteArray imageData;
    QBuffer buffer(&imageData);
    image.save(&buffer, "JPG");
    imageData = imageData.toBase64();
    return imageData;
}
 
QScopedPointer<FaceBaiDuLocal> FaceBaiDuLocal::self;
FaceBaiDuLocal *FaceBaiDuLocal::Instance()
{
    if (self.isNull()) {
        QMutex mutex;
        QMutexLocker locker(&mutex);
        if (self.isNull()) {
            self.reset(new FaceBaiDuLocal);
        }
    }
 
    return self.data();
}
 
FaceBaiDuLocal::FaceBaiDuLocal(QObject *parent) : QThread(parent)
{
    //注册信号中未知的数据类型
    qRegisterMetaType<QList<float> >("QList<float>");
    stopped = false;
 
    maxCount = 100;
    type = 1;
    percent = 8;
    delayms = 0;
    findFast = false;
    countTime = true;
    busy = false;
 
    sdkPath = qApp->applicationDirPath() + "/facesdk";
    imgDir = "";
    oneImg = QImage();
 
    api = new BaiduFaceApi;
    faces = new std::vector<TrackFaceInfo>();
}
 
FaceBaiDuLocal::~FaceBaiDuLocal()
{
    delete api;
    this->stop();
    this->wait(1000);
}
 
void FaceBaiDuLocal::run()
{
    this->init();
    while(!stopped) {
        int count = flags.count();
        if (count > 0) {
            QMutexLocker lock(&mutex);
            busy = true;
            if (type == 0) {
                QString flag = flags.takeFirst();
                QImage img = imgs.takeFirst();
 
                QRect rect;
                int msec;
                if (getFaceRect(flag, img, rect, msec)) {
                    emit receiveFaceRect(flag, rect, msec);
                } else {
                    emit receiveFaceRectFail(flag);
                }
            } else if (type == 1) {
                QString flag = flags.takeFirst();
                QImage img = imgs.takeFirst();
 
                QList<float> feature;
                int msec;
                if (getFaceFeature(flag, img, feature, msec)) {
                    emit receiveFaceFeature(flag, feature, msec);
                } else {
                    emit receiveFaceFeatureFail(flag);
                }
            } else if (type == 2) {
                QString flag = flags.takeFirst();
                QImage img1 = imgs.takeFirst();
                QImage img2 = imgs2.takeFirst();
 
                float result;
                int msec;
                if (getFaceCompare(flag, img1, img2, result, msec)) {
                    emit receiveFaceCompare(flag, result, msec);
                } else {
                    emit receiveFaceCompareFail(flag);
                }
            } else if (type == 3) {
                flags.takeFirst();
 
                getFaceFeatures(imgDir);
            } else if (type == 4) {
                QString flag = flags.takeFirst();
 
                QString targetName;
                float result;
                getFaceOne(flag, oneImg, targetName, result);
                if (!targetName.isEmpty()) {
                    emit receiveFaceCompareOne(flag, oneImg, targetName, result);
                }
            } else if (type == 5) {
                QString flag = flags.takeFirst();
                QImage img = imgs.takeFirst();
 
                float result;
                int msec;
                if (getFaceLive(flag, img, result, msec)) {
                    emit receiveFaceLive(flag, result, msec);
                } else {
                    emit receiveFaceLiveFail(flag);
                }
            }
        }
 
        msleep(100);
        busy = false;
    }
 
    stopped = false;
}
 
void FaceBaiDuLocal::init()
{
    int res = api->sdk_init();
    res = api->is_auth();
    if(res != 1) {
        qDebug() << TIMEMS << QString("init sdk error: %1").arg(res);
        return;
    } else {
        //设置最小人脸,默认30
        api->set_min_face_size(percent);
        //设置光照阈值,默认40
        api->set_illum_thr(20);
        //设置角度阈值,默认15
        //api->set_eulur_angle_thr(30, 30, 30);
        qDebug() << TIMEMS << "init sdk ok";
    }
}
 
void FaceBaiDuLocal::stop()
{
    stopped = true;
}
 
bool FaceBaiDuLocal::getBusy()
{
    return this->busy;
}
 
void FaceBaiDuLocal::setMaxCount(int maxCount)
{
    if (maxCount <= 1000) {
        this->maxCount = maxCount;
    }
}
 
void FaceBaiDuLocal::setType(int type)
{
    if (this->type != type) {
        this->type = type;
        this->flags.clear();
        this->imgs.clear();
        this->imgs2.clear();
    }
}
 
void FaceBaiDuLocal::setPercent(int percent)
{
    this->percent = percent;
}
 
void FaceBaiDuLocal::setDelayms(int delayms)
{
    this->delayms = delayms;
}
 
void FaceBaiDuLocal::setFindFast(bool findFast)
{
    this->findFast = findFast;
}
 
void FaceBaiDuLocal::setCountTime(bool countTime)
{
    this->countTime = countTime;
}
 
void FaceBaiDuLocal::setBusy(bool busy)
{
    this->busy = busy;
}
 
void FaceBaiDuLocal::setSDKPath(const QString &sdkPath)
{
    this->sdkPath = sdkPath;
}
 
void FaceBaiDuLocal::setImgDir(const QString &imgDir)
{
    this->imgDir = imgDir;
    this->flags.clear();
    this->flags.append("imgDir");
    this->type = 3;
}
 
void FaceBaiDuLocal::setOneImg(const QString &flag, const QImage &oneImg)
{
    setType(4);
 
    //需要将图片重新拷贝一个,否则当原图像改变之后也会改变
    this->oneImg = oneImg.copy();
    this->flags.append(flag);
}
 
void FaceBaiDuLocal::append(const QString &flag, const QImage &img)
{
    QMutexLocker lock(&mutex);
    int count = flags.count();
    if (count < maxCount) {
        flags.append(flag);
        imgs.append(img);
    }
}
 
void FaceBaiDuLocal::append(const QString &flag, const QImage &img, const QImage &img2)
{
    QMutexLocker lock(&mutex);
    int count = flags.count();
    if (count < maxCount) {
        flags.append(flag);
        imgs.append(img);
        imgs2.append(img2);
    }
}
 
void FaceBaiDuLocal::getFaceFeatures(const QString &imgDir)
{
    imgNames.clear();
    features.clear();
 
    //载入指定目录图像处理特征
    QDir imagePath(imgDir);
    QStringList filter;
    filter << "*.jpg" << "*.bmp" << "*.png" << "*.jpeg" << "*.gif";
    imgNames.append(imagePath.entryList(filter));
 
    qDebug() << TIMEMS << "getFaceFeatures" << imgNames;
 
    //从目录下读取同名的txt文件(存储的特征)
    //如果存在则从文件读取特征,如果不存在则转码解析出特征
    //转码完成后将得到的特征存储到同名txt文件
    int count = imgNames.count();
    for (int i = 0; i < count; i++) {
        QList<float> feature;
        int msec;
 
        QString imgName = imgNames.at(i);
        QStringList list = imgName.split(".");
        QString txtName = imgDir + "/" + list.at(0) + ".txt";
        QFile file(txtName);
 
        if (file.exists()) {
            if (file.open(QFile::ReadOnly)) {
                QString data = file.readAll();
                file.close();
 
                qDebug() << TIMEMS << "readFaceFeature" << txtName;
 
                QStringList list = data.split(",");
                foreach (QString str, list) {
                    if (!str.isEmpty()) {
                        feature.append(str.toFloat());
                    }
                }
            }
        } else {
            QImage img(imgDir + "/" + imgName);
            bool ok = getFaceFeature(imgName, img, feature, msec);
 
            if (ok) {
                emit receiveFaceFeature(imgName, feature, msec);
                if (file.open(QFile::WriteOnly)) {
                    QStringList list;
                    foreach (float fea, feature) {
                        list.append(QString::number(fea));
                    }
 
                    qDebug() << TIMEMS << "writeFaceFeature" << txtName;
 
                    file.write(list.join(",").toLatin1());
                    file.close();
                }
            }
        }
 
        features.append(feature);
        msleep(1);
    }
 
    qDebug() << TIMEMS << "getFaceFeatures finsh";
    emit receiveFaceFeatureFinsh();
}
 
bool FaceBaiDuLocal::getFaceRect(const QString &flag, const QImage &img, QRect &rect, int &msec)
{
    //qDebug() << TIMEMS << flag << "getFaceRect";
 
    QTime time;
    if (countTime) {
        time.start();
    }
 
    faces->clear();
    QByteArray imageData = getImageData(img);
    int result = api->track_max_face(faces, imageData.constData(), 1);
 
    if (result == 1) {
        TrackFaceInfo info = faces->at(0);
        FaceInfo ibox = info.box;
        float width = ibox.mWidth;
        float x = ibox.mCenter_x;
        float y = ibox.mCenter_y;
 
        rect = QRect(x - width / 2, y - width / 2, width, width);
        if (countTime) {
            msec = time.elapsed() - delayms;
        } else {
            msec = delayms;
        }
 
        msec = msec < 0 ? 0 : msec;
        return true;
    } else {
        return false;
    }
 
    return false;
}
 
bool FaceBaiDuLocal::getFaceLive(const QString &flag, const QImage &img, float &result, int &msec)
{
    //qDebug() << TIMEMS << flag << "getFaceLive";
 
    QTime time;
    if (countTime) {
        time.start();
    }
 
    result = 0;
    QByteArray imageData = getImageData(img);
    std::string value = api->rgb_liveness_check(imageData.constData(), 1);
 
    QString data = value.c_str();
    data = data.replace("\t", "");
    data = data.replace("\"", "");
    data = data.replace(" ", "");
 
    int index = -1;
    QStringList list = data.split("\n");
    foreach (QString str, list) {
        index = str.indexOf("score:");
        if (index >= 0) {
            result = str.mid(6, 4).toFloat();
            break;
        }
    }
 
    if (index >= 0) {
        if (countTime) {
            msec = time.elapsed() - delayms;
        } else {
            msec = delayms;
        }
 
        msec = msec < 0 ? 0 : msec;
        return true;
    } else {
        return false;
    }
 
    return false;
}
 
bool FaceBaiDuLocal::getFaceFeature(const QString &flag, const QImage &img, QList<float> &feature, int &msec)
{
    //qDebug() << TIMEMS << flag << "getFaceFeature" << img.width() << img.height() << img.size();
 
    QTime time;
    if (countTime) {
        time.start();
    }
 
    const float *fea = nullptr;
    QByteArray imageData = getImageData(img);
    int result = api->get_face_feature(imageData.constData(), 1, fea);
 
    if (result == 512) {
        feature.clear();
        for (int i = 0; i < 512; i++) {
            feature.append(fea[i]);
        }
 
        if (countTime) {
            msec = time.elapsed() - delayms;
        } else {
            msec = delayms;
        }
 
        msec = msec < 0 ? 0 : msec;
        return true;
    } else {
        return false;
    }
 
    return false;
}
 
float FaceBaiDuLocal::getFaceCompare(const QString &flag, const QList<float> &feature1, const QList<float> &feature2)
{
    //qDebug() << TIMEMS << flag << "getFaceCompareXXX";
 
    std::vector<float> fea1, fea2;
    for (int i = 0; i < 512; i++) {
        fea1.push_back(feature1.at(i));
        fea2.push_back(feature2.at(i));
    }
 
    float result = api->compare_feature(fea1, fea2);
    //过滤非法的值
    result = result > 100 ? 0 : result;
    return result;
}
 
bool FaceBaiDuLocal::getFaceCompare(const QString &flag, const QImage &img1, const QImage &img2, float &result, int &msec)
{
    //qDebug() << TIMEMS << flag << "getFaceCompare";
 
    result = 0;
    bool ok1, ok2;
    QList<float> feature1, feature2;
    int msec1, msec2;
    QString flag1, flag2;
    if (flag.contains("|")) {
        QStringList list = flag.split("|");
        flag1 = list.at(0);
        flag2 = list.at(1);
    } else {
        flag1 = flag;
        flag2 = flag;
    }
 
    QTime time;
    if (countTime) {
        time.start();
    }
 
    ok1 = getFaceFeature(flag1, img1, feature1, msec1);
    if (ok1) {
        emit receiveFaceFeature(flag1, feature1, msec1);
    }
 
    ok2 = getFaceFeature(flag2, img2, feature2, msec2);
    if (ok2) {
        emit receiveFaceFeature(flag2, feature2, msec2);
    }
 
    if (ok1 && ok2) {
        result = getFaceCompare(flag, feature1, feature2);
 
        if (countTime) {
            msec = time.elapsed() - delayms;
        } else {
            msec = delayms;
        }
 
        msec = msec < 0 ? 0 : msec;
        return true;
    } else {
        return false;
    }
 
    return false;
}
 
void FaceBaiDuLocal::getFaceOne(const QString &flag, const QImage &img, QString &targetName, float &result)
{
    QList<float> feature;
    int msec;
    bool ok = getFaceFeature(flag, img, feature, msec);
    if (ok) {
        emit receiveFaceFeature(flag, feature, msec);
        getFaceOne(flag, feature, targetName, result);
    }
}
 
void FaceBaiDuLocal::getFaceOne(const QString &flag, const QList<float> &feature, QString &targetName, float &result)
{
    //用当前图片的特征与特征数据库比对
    result = 0;
    int count = imgNames.count();
    for (int i = 0; i < count; i++) {
        QString imgName = imgNames.at(i);
        float currentResult = getFaceCompare(flag, feature, features.at(i));
        //qDebug() << TIMEMS << "getFaceOne" << imgName << currentResult;
 
        if (currentResult > result) {
            result = currentResult;
            targetName = imgName;
        }
    }
 
    qDebug() << TIMEMS << "getFaceOne result" << targetName << result;
}
 
void FaceBaiDuLocal::appendFace(const QString &flag, const QImage &img, const QString &txtFile)
{
    QList<float> feature;
    int msec;
 
    QImage image = img;
    bool ok = getFaceFeature(flag, image, feature, msec);
    msleep(100);
 
    qDebug() << TIMEMS << "getFaceFeature result" << ok << "appendFace" << txtFile;
 
    if (ok) {
        emit receiveFaceFeature(flag, feature, msec);
 
        //保存txt文件
        QFile file(txtFile);
        if (file.open(QFile::WriteOnly)) {
            QStringList list;
            foreach (float fea, feature) {
                list.append(QString::number(fea));
            }
 
            file.write(list.join(",").toLatin1());
            file.close();
        }
 
        //保存图片文件
        QString imgName = txtFile;
        imgName = imgName.replace("txt", "jpg");
        image.save(imgName, "jpg");
 
        imgNames.append(QFileInfo(imgName).fileName());
        features.append(feature);
    }
}
 
void FaceBaiDuLocal::deleteFace(const QString &flag)
{
    //从图片名称中找到标识符
    int index = imgNames.indexOf(flag);
    if (index >= 0) {
        imgNames.removeAt(index);
        features.removeAt(index);
 
        //删除图片文件
        QString imgFileName = QString("%1/face/%2.jpg").arg(qApp->applicationDirPath()).arg(flag);
        QFile imgFile(imgFileName);
        imgFile.remove();
        qDebug() << TIMEMS << "delete faceImage" << imgFileName;
 
        //删除特征文件
        QString txtFileName = QString("%1/face/%2.txt").arg(qApp->applicationDirPath()).arg(flag);
        QFile txtFile(txtFileName);
        txtFile.remove();
        qDebug() << TIMEMS << "delete faceTxt" << txtFileName;
    }
}
posted @   飞扬青云  阅读(1627)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示