Skip to content

Commit 08073d1

Browse files
committed
First commit
1 parent 5818bf5 commit 08073d1

22 files changed

+34498
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ compile_commands.json
5050

5151
# QtCreator local machine specific files for imported projects
5252
*creator.user*
53+
54+
db.ini

FaceRec.pro

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
QT += core gui multimedia multimediawidgets
2+
3+
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
4+
5+
CONFIG += c++17 link_pkgconfig
6+
7+
PKGCONFIG += opencv4
8+
9+
LIBS += -lmysqlcppconn
10+
# You can make your code fail to compile if it uses deprecated APIs.
11+
# In order to do so, uncomment the following line.
12+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
13+
14+
SOURCES += \
15+
database.cpp \
16+
facepickframe.cpp \
17+
facerecframe.cpp \
18+
main.cpp \
19+
mainwindow.cpp \
20+
recognizer.cpp \
21+
traindialog.cpp \
22+
trainthread.cpp
23+
24+
HEADERS += \
25+
database.h \
26+
facepickframe.h \
27+
facerecframe.h \
28+
mainwindow.h \
29+
recognizer.h \
30+
traindialog.h \
31+
trainthread.h
32+
33+
FORMS += \
34+
facepickframe.ui \
35+
facerecframe.ui \
36+
mainwindow.ui \
37+
traindialog.ui
38+
39+
# Default rules for deployment.
40+
qnx: target.path = /tmp/$${TARGET}/bin
41+
else: unix:!android: target.path = /opt/$${TARGET}/bin
42+
!isEmpty(target.path): INSTALLS += target
43+
44+
DISTFILES += \
45+
db.ini

database.cpp

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "database.h"
2+
3+
Database::Database()
4+
{
5+
if (access("db.ini", F_OK) != 0) {
6+
throw std::runtime_error("db.ini not found!");
7+
exit(-1);
8+
}
9+
QSettings *dbSettings = new QSettings("db.ini", QSettings::IniFormat);
10+
const string host = dbSettings->value("DB/Host").toString().toStdString();
11+
const string username = dbSettings->value("DB/Username").toString().toStdString();
12+
const string password = dbSettings->value("DB/Password").toString().toStdString();
13+
const string database = dbSettings->value("DB/Database").toString().toStdString();
14+
Driver *driver = mysql::get_driver_instance();
15+
this->conn = driver->connect(host, username, password);
16+
this->conn->setSchema(database);
17+
delete dbSettings;
18+
}
19+
20+
void Database::incrTimes(int sid) {
21+
unique_ptr<PreparedStatement> stmt(this->conn->prepareStatement("update student set times=times+1 where id=?"));
22+
stmt->setInt(1, sid);
23+
stmt->executeUpdate();
24+
unique_ptr<PreparedStatement> stmt2(this->conn->prepareStatement("insert into attendance(name, sid, time) values((select name from student where id=?), ?, ?)"));
25+
stmt2->setInt(1, sid);
26+
stmt2->setInt(2, sid);
27+
time_t curtime = 0;
28+
time(&curtime);
29+
char timeStr[100];
30+
strftime(timeStr, 100, "%Y-%m-%d %H:%M:%S", localtime(&curtime));
31+
stmt2->setDateTime(3, timeStr);
32+
stmt2->executeUpdate();
33+
}
34+
35+
int Database::newStudent(student student) {
36+
unique_ptr<PreparedStatement> stmt(this->conn->prepareStatement("select count(id) as c from student where id=?"));
37+
unique_ptr<PreparedStatement> stmt2(this->conn->prepareStatement("insert into student(id, name) values(?,?)"));
38+
stmt->setInt(1, student.sid);
39+
unique_ptr<ResultSet> res(stmt->executeQuery());
40+
if (res->getInt("c") > 0) {
41+
return -1;
42+
}
43+
stmt2->setInt(1, student.sid);
44+
stmt2->setString(2, student.name);
45+
stmt2->execute();
46+
return 0;
47+
}
48+
49+
Database::~Database() {
50+
this->conn->close();
51+
delete conn;
52+
}

database.h

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef DATABASE_H
2+
#define DATABASE_H
3+
4+
#include <QDebug>
5+
#include <QSettings>
6+
#include <QMessageBox>
7+
8+
#include <mysql/jdbc.h>
9+
10+
#include <time.h>
11+
#include <string.h>
12+
#include <unistd.h>
13+
14+
using namespace sql;
15+
using namespace std;
16+
17+
typedef struct {
18+
int sid;
19+
string name;
20+
int times;
21+
} student;
22+
23+
typedef struct {
24+
int id;
25+
string name;
26+
int sid;
27+
tm time;
28+
} attendance;
29+
30+
class Database
31+
{
32+
public:
33+
Database();
34+
~Database();
35+
int newStudent(student student);
36+
void incrTimes(int sid);
37+
private:
38+
Connection *conn;
39+
};
40+
41+
#endif // DATABASE_H

facepickframe.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "facepickframe.h"
2+
#include "ui_facepickframe.h"
3+
4+
FacePickFrame::FacePickFrame(QWidget *mainWindow, QWidget *parent) : QFrame(parent), ui(new Ui::FacePickFrame) {
5+
this->ui->setupUi(this);
6+
this->mainWindow = mainWindow;
7+
8+
connect(this->ui->pushButton, &QPushButton::clicked, this, [this]() {
9+
QString nameInput = this->ui->lineEdit->text();
10+
QString sidInput = this->ui->lineEdit_2->text();
11+
if (nameInput == "" || sidInput == "") {
12+
QMessageBox::information(this, "Error", "姓名和学号不能为空");
13+
return;
14+
}
15+
student stu = {sidInput.toInt(), nameInput.toStdString(), 0};
16+
17+
});
18+
}
19+
20+
FacePickFrame::~FacePickFrame()
21+
{
22+
this->camera->stop();
23+
this->camera->unload();
24+
this->mainWindow->show();
25+
delete ui;
26+
}
27+
28+
void FacePickFrame::closeEvent(QCloseEvent *e) {
29+
this->camera->stop();
30+
this->camera->unload();
31+
this->mainWindow->show();
32+
}
33+
34+
void FacePickFrame::showEvent(QShowEvent *e) {
35+
this->camera = new QCamera(QCameraInfo::defaultCamera());
36+
this->camera->setCaptureMode(QCamera::CaptureMode::CaptureViewfinder);
37+
this->camera->setViewfinder(this->ui->widget_2);
38+
this->camera->start();
39+
}

facepickframe.h

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef FACEPICKFRAME_H
2+
#define FACEPICKFRAME_H
3+
4+
#include <QFrame>
5+
#include <QPushButton>
6+
#include <QMessageBox>
7+
#include <QCloseEvent>
8+
#include <QShowEvent>
9+
#include <QCameraInfo>
10+
#include <QCamera>
11+
#include <QVideoWidget>
12+
#include <QCameraViewfinderSettings>
13+
14+
#include "database.h"
15+
#include "traindialog.h"
16+
#include "trainthread.h"
17+
18+
namespace Ui {
19+
class FacePickFrame;
20+
}
21+
22+
class FacePickFrame : public QFrame
23+
{
24+
Q_OBJECT
25+
26+
public:
27+
explicit FacePickFrame(QWidget *mainWindow, QWidget *parent = nullptr);
28+
~FacePickFrame();
29+
30+
private:
31+
Ui::FacePickFrame *ui;
32+
QWidget *mainWindow;
33+
QCamera *camera;
34+
35+
protected:
36+
void closeEvent(QCloseEvent *e) override;
37+
void showEvent(QShowEvent *e) override;
38+
};
39+
40+
#endif // FACEPICKFRAME_H

0 commit comments

Comments
 (0)