1.添加特征值model。

This commit is contained in:
luocai 2024-05-24 10:23:05 +08:00
parent 8660c10742
commit 09cb96f97b
11 changed files with 91 additions and 19 deletions

View File

@ -9,6 +9,7 @@ add_executable(Analyser Analyser.rc
CategoryLogSinkBackend.h CategoryLogSinkBackend.cpp CategoryLogSinkBackend.h CategoryLogSinkBackend.cpp
Widget.h Widget.cpp Widget.h Widget.cpp
ModuleCommunication.h ModuleCommunication.cpp ModuleCommunication.h ModuleCommunication.cpp
PalmFeatureTableModel.h PalmFeatureTableModel.cpp
) )
target_link_libraries(Analyser target_link_libraries(Analyser

View File

@ -1,7 +1,7 @@
#ifndef MODULECOMMUNICATION_H #ifndef MODULECOMMUNICATION_H
#define MODULECOMMUNICATION_H #define MODULECOMMUNICATION_H
#include "Database.h" #include "DataStructure.h"
#include <QObject> #include <QObject>
#include <memory> #include <memory>

View File

@ -0,0 +1,17 @@
#include "PalmFeatureTableModel.h"
PalmFeatureTableModel::PalmFeatureTableModel(QObject *parent) {
}
int PalmFeatureTableModel::rowCount(const QModelIndex &parent) const {
return static_cast<int>(m_features.size());
}
int PalmFeatureTableModel::columnCount(const QModelIndex &parent) const {
return 2;
}
QVariant PalmFeatureTableModel::data(const QModelIndex &index, int role) const {
QVariant ret;
return ret;
}

View File

@ -0,0 +1,19 @@
#ifndef __PALMFEATURETABLEMODEL_H__
#define __PALMFEATURETABLEMODEL_H__
#include "DataStructure.h"
#include <QAbstractTableModel>
class PalmFeatureTableModel : public QAbstractTableModel {
Q_OBJECT
public:
PalmFeatureTableModel(QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = QModelIndex()) const final;
int columnCount(const QModelIndex &parent = QModelIndex()) const final;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const final;
private:
PalmFeatures m_features;
};
#endif // __PALMFEATURETABLEMODEL_H__

View File

@ -1,7 +1,9 @@
#include "Widget.h" #include "Widget.h"
#include "BoostLog.h" #include "BoostLog.h"
#include "CategoryLogSinkBackend.h" #include "CategoryLogSinkBackend.h"
#include "Database.h"
#include "ModuleCommunication.h" #include "ModuleCommunication.h"
#include "PalmFeatureTableModel.h"
#include <QComboBox> #include <QComboBox>
#include <QFormLayout> #include <QFormLayout>
#include <QGridLayout> #include <QGridLayout>
@ -11,6 +13,7 @@
#include <QPushButton> #include <QPushButton>
#include <QSerialPortInfo> #include <QSerialPortInfo>
#include <QTabWidget> #include <QTabWidget>
#include <QTableView>
#include <QTextBrowser> #include <QTextBrowser>
#include <QTimer> #include <QTimer>
#include <QVBoxLayout> #include <QVBoxLayout>
@ -51,10 +54,12 @@ Widget::Widget(QWidget *parent) : QWidget{parent} {
connect(btn, &QPushButton::clicked, this, &Widget::onClearLogButtonClicked); connect(btn, &QPushButton::clicked, this, &Widget::onClearLogButtonClicked);
logLayout->addWidget(btn, 0, Qt::AlignBottom | Qt::AlignRight); logLayout->addWidget(btn, 0, Qt::AlignBottom | Qt::AlignRight);
m_featureTableView = new QTableView();
auto tabWidget = new QTabWidget(); auto tabWidget = new QTabWidget();
tabWidget->addTab(m_logBrowser, "日志"); tabWidget->addTab(m_logBrowser, "日志");
tabWidget->addTab(new QWidget(), "视频流"); tabWidget->addTab(new QWidget(), "视频流");
tabWidget->addTab(new QWidget(), "已注册列表"); tabWidget->addTab(m_featureTableView, "本地特征值列表");
m_commandGroupBox = initializeCommandGroupBox(); m_commandGroupBox = initializeCommandGroupBox();
@ -68,6 +73,8 @@ Widget::Widget(QWidget *parent) : QWidget{parent} {
layout->addWidget(tabWidget, 3); layout->addWidget(tabWidget, 3);
m_database = std::make_shared<Database>(); m_database = std::make_shared<Database>();
m_featureModel = new PalmFeatureTableModel(this);
m_featureTableView->setModel(m_featureModel);
QTimer::singleShot(0, this, [this]() { QTimer::singleShot(0, this, [this]() {
onSerialRefreshButtonClicked(); onSerialRefreshButtonClicked();
@ -194,14 +201,14 @@ QGroupBox *Widget::initializeUvcGroupBox() {
} }
void Widget::onNewPalmFeature(const PalmFeature &feature) { void Widget::onNewPalmFeature(const PalmFeature &feature) {
auto palms = m_database->palmFeatures();
if (std::find(palms.cbegin(), palms.cend(), feature) != palms.cend()) {
LOG(warning) << "本地数据库已有相同特征数据。";
return;
}
if (!m_database->addPalmFeature(feature)) { if (!m_database->addPalmFeature(feature)) {
LOG(error) << "add palm feature failed."; LOG(error) << "add palm feature failed.";
} }
auto palms = m_database->palmFeatures();
for (auto &p : palms) {
LOG(info) << p.id << " " << p.username << " " << p.feature.size();
}
} }
void Widget::onSerialConnectButtonClicked() { void Widget::onSerialConnectButtonClicked() {

View File

@ -1,7 +1,7 @@
#ifndef WIDGET_H #ifndef WIDGET_H
#define WIDGET_H #define WIDGET_H
#include "Database.h" #include "DataStructure.h"
#include <QWidget> #include <QWidget>
class QPushButton; class QPushButton;
@ -9,7 +9,10 @@ class QTextBrowser;
class QComboBox; class QComboBox;
class QLineEdit; class QLineEdit;
class QGroupBox; class QGroupBox;
class QTableView;
class ModuleCommunication; class ModuleCommunication;
class PalmFeatureTableModel;
class Database;
class Widget : public QWidget { class Widget : public QWidget {
Q_OBJECT Q_OBJECT
@ -63,6 +66,9 @@ private:
std::shared_ptr<ModuleCommunication> m_communication; std::shared_ptr<ModuleCommunication> m_communication;
std::shared_ptr<Database> m_database; std::shared_ptr<Database> m_database;
PalmFeatureTableModel *m_featureModel = nullptr;
QTableView *m_featureTableView = nullptr;
}; };
#endif // WIDGET_H #endif // WIDGET_H

View File

@ -1,5 +1,6 @@
add_library(Database add_library(Database
Database.h Database.cpp Database.h Database.cpp
DataStructure.h DataStructure.cpp
# shell.c # shell.c
sqlite3.h sqlite3.c sqlite3.h sqlite3.c
sqlite3ext.h sqlite3ext.h

View File

@ -0,0 +1,5 @@
#include "DataStructure.h"
bool operator==(const PalmFeature &lhs, const PalmFeature &rhs) {
return lhs.feature == rhs.feature;
}

23
Database/DataStructure.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef __DATASTRUCTURE_H__
#define __DATASTRUCTURE_H__
#include <string>
#include <vector>
class PalmFeature {
public:
int64_t id; // 对应本地数据库的id
std::string username;
std::vector<uint8_t> feature;
};
using PalmFeatures = std::vector<PalmFeature>;
bool operator==(const PalmFeature &lhs, const PalmFeature &rhs);
namespace std {
template <>
struct hash<PalmFeature> {};
} // namespace std
#endif // __DATASTRUCTURE_H__

View File

@ -23,8 +23,8 @@ bool Database::addPalmFeature(const PalmFeature &palm) {
return true; return true;
} }
std::vector<PalmFeature> Database::palmFeatures() const { PalmFeatures Database::palmFeatures() const {
std::vector<PalmFeature> ret; PalmFeatures ret;
sqlite3_stmt *statement = nullptr; sqlite3_stmt *statement = nullptr;
constexpr const char *sql = "SELECT * FROM palm_feature"; constexpr const char *sql = "SELECT * FROM palm_feature";
if (sqlite3_prepare_v2(m_sqlite, sql, -1, &statement, NULL) != SQLITE_OK) { if (sqlite3_prepare_v2(m_sqlite, sql, -1, &statement, NULL) != SQLITE_OK) {

View File

@ -1,23 +1,16 @@
#ifndef __DATABASE_H__ #ifndef __DATABASE_H__
#define __DATABASE_H__ #define __DATABASE_H__
#include <string> #include "DataStructure.h"
#include <vector>
struct sqlite3; struct sqlite3;
class PalmFeature {
public:
int64_t id; // 对应本地数据库的id
std::string username;
std::vector<uint8_t> feature;
};
class Database{ class Database{
public: public:
bool open(const std::string &path); bool open(const std::string &path);
bool addPalmFeature(const PalmFeature &palm); bool addPalmFeature(const PalmFeature &palm);
std::vector<PalmFeature> palmFeatures() const; PalmFeatures palmFeatures() const;
private: private:
void initializeTables(); void initializeTables();