QT–动态人流量监测系统
文章目录
QT--动态人流量监测系统简介:项目功能1. pro文件2.widget.h文件3.widget.cpp文件4.widget.ui文件5.注意6.工程源码简介:
本项目使用了百度AI的动态人流量监测api,以人体头肩为主要识别目标,适用于低空俯拍,出入口场景,可用于统计当前图像的锁定人数和经过的人数项目功能
本项目分为相机模块和图像识别模块相机模块使用了两个button复用、一个滑动条按下打开摄像头button,开始准备拍照,button变成关闭摄像头button,再点击就关闭摄像头。在打开摄像头之前,开始button是无法使用的。当打开摄像头后按下开始button,button变成停止button,同时开始拍摄照片,由于是动态监测人流量,所以抽祯频率为5fps,也就是每秒需要拍摄上传5张照片,第一打开需要连接网络,所以可能有些慢。滑动条在点击开始之前是无法使用的,当开始拍照后,可拖动滑动条切换窗口界面,进入监测界面图像识别模块图像识别模块可将相机模块拍摄的照片上传网络,进行人体识别,判断当前图片指定区域的人数、进入指定区域的人数,离开指定区域的人数将图片上传到网络上后可获得返回的人数数值和渲染图片,显示在监测界面图片中的人被锁定后会在人体头部出现矩形框左边框为指定区域画面左上角分别为总的锁定人数、进入指定区域的总人数、离开指定区域的总人数左下角为此次打开运行的人数1. pro文件
QT += core gui network multimedia multimediawidgets//network 为HTTP通信必加//multimedia multimediawidgets 为相机必加
2.widget.h文件
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QCamera>#include <QCameraViewfinder>#include <QCameraImageCapture>#include <QTimer>#include <QNetworkAccessManager>#include <QNetworkReply>#include <QNetworkRequest>#include <QJsonObject>namespace Ui {class Widget;}class Widget : public QWidget{Q_OBJECTpublic:explicit Widget(QWidget *parent = nullptr);~Widget();public:QTimer *timer;QCamera *camera; //系统摄像头设备QCameraViewfinder *finder; //摄像头取景器QCameraImageCapture *capture; //截图部件QImage Image; //保存照片bool Switch; //开关int Position; //保存滑动条值QImage img ; //返回照片int ins = 0; //进入人数int outs = 0; //离开人数void photo(); //拍照void display();//显示void photograph(); //相机初始化void cameraImageCaptured(int id, QImage image); //获得照片/********************网络**************************************/QNetworkAccessManager *manager; //网络访问管理器QNetworkReply * reply; //网络回复QNetworkRequest *requst; //网络请求bool connection = false;void initialization(); //网络void sendHTTP(); //发送请求private slots:void on_pushButton_clicked();void on_pushButton_2_clicked();void on_horizontalSlider_sliderMoved(int position);private:Ui::Widget *ui;};#endif // WIDGET_H
3.widget.cpp文件
#include "widget.h"#include "ui_widget.h"#include <QBuffer>#include <QByteArray>#include <QImageReader>#include <QJsonDocument>#include <QJsonObject>#include <QThread>Widget::Widget(QWidget *parent) :QWidget(parent),ui(new Ui::Widget){ui->setupUi(this);photograph(); //相机initialization(); //网络}Widget::~Widget(){delete ui;}//网络void Widget::initialization(){manager = new QNetworkAccessManager(this);requst = new QNetworkRequest;QString request_url = "/rest/2.0/image-classify/v1/body_tracking"; //填入百度AI的请求URLQString toke = "24.6878d2808d1dd6be2a3a760c82d0979a.2592000.1570716999.282335-17208954";//填入百度AI的access_token,具体可看百度AI的文档QString url = request_url + "?access_token=" + toke; //注意格式requst->setUrl(QUrl(url));requst->setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded"); //头不能填错Content-Typeconnect(manager,&QNetworkAccessManager::finished,this,[=](QNetworkReply *reply){QByteArray array = reply->readAll(); //获取信息qDebug() << array;QJsonObject json1 = QJsonDocument::fromJson(array).object();QString image = json1.value("image").toString();QByteArray ba; //解析返回渲染图ba.append(image);QByteArray ba1 = ba.fromBase64(ba);QBuffer buffer(&ba1);buffer.open(QIODevice::ReadOnly);QImageReader reader(&buffer,"jpg");img = reader.read();int person_num = json1.value("person_num").toInt();QJsonObject object = json1.value("person_count").toObject();qDebug() << object;int in = object.value("in").toInt();int out = object.value("out").toInt();ins += in;outs += out;QString str = "当前人数:" + QString::number(person_num);//返回人数ui->label_2->setText(str);QString str1 = "进入人数:" + QString::number(ins);ui->label_3->setText(str1);QString str2 = "离开人数:" + QString::number(outs);ui->label_4->setText(str2);});}//发送请求void Widget::sendHTTP(){QByteArray ba;QBuffer buf(&ba);Image.save(&buf, "jpg");QByteArray SendData = "";SendData.append("&dynamic=true"); //封装发送信息SendData.append("&case_id=1");SendData.append("&case_init=false");SendData.append("&image=");SendData.append(ba.toBase64().toPercentEncoding());SendData.append("&show=true");SendData.append("&area=1,1,700,1,700,719,1,719");qDebug() << SendData;manager->post(*requst,SendData);}//相机初始化void Widget::photograph(){this->resize(640,450); //设置窗口大小camera = new QCamera(this);//系统摄像头设备finder = new QCameraViewfinder(this);//摄像头取景器部件capture = new QCameraImageCapture(camera); //截图部件timer = new QTimer(this);ui->verticalLayout->addWidget(finder);//在布局器上添加finderui->label->setMaximumSize(0,0); //设置照片初始化大小ui->pushButton_2->setEnabled(false);Switch = false;Position = 0;connect(timer,&QTimer::timeout,this,&Widget::photo); //监听定时器connect(capture,&QCameraImageCapture::imageCaptured,this,&Widget::cameraImageCaptured); //图像捕获capture->setCaptureDestination(QCameraImageCapture::CaptureToFile); //设置要捕获的对象camera->setCaptureMode(QCamera::CaptureStillImage); //摄像机配置为静止帧捕获。camera->setViewfinder(finder); //设置取景器}//拍照void Widget::photo(){capture->capture(); //拍照sendHTTP();display(); //显示}//获得图像void Widget::cameraImageCaptured(int id, QImage image){Image = image;}//打开摄像头void Widget::on_pushButton_clicked(){if(!Switch){camera->start();//打开摄像头ui->pushButton->setText("关闭摄像头");ui->pushButton_2->setEnabled(true); //打开buttonSwitch = true;}else{camera->stop();ui->pushButton->setText("打开摄像头");ui->pushButton_2->setEnabled(false);Switch = false;// connection = false;}}//定时拍照void Widget::on_pushButton_2_clicked(){if(!timer->isActive()){timer->start(200);ui->pushButton_2->setText("停止");}else{timer->stop();ui->pushButton_2->setText("开始");}}//滑动切换void Widget::on_horizontalSlider_sliderMoved(int position){Position = position;display();}//显示void Widget::display(){ui->horizontalSlider->setMinimum(0);ui->horizontalSlider->setMaximum(this->width());ui->label->setMaximumSize(Position * (16/9),Position);QPixmap pixmap = QPixmap::fromImage(img );QPixmap fitpixmap = pixmap.scaled(Position * (16/9),Position, Qt::KeepAspectRatio, Qt::SmoothTransformation); // 按比例缩放ui->label->setPixmap(fitpixmap);qDebug() << Position;}
4.widget.ui文件
左边的空为一个部局器右边的空内为一个label当前人数、进入人数、离开人数为三个label5.注意
有的版本的QT编译运行,在连接网络时会出现下列错误
work.ssl: QSslSocket::connectToHostEncrypted: TLS initialization failed1
QT5.13的目前我不知道如何解决
QT5.12的需要在QT安装目录下找到下列两个文件
选择与自己编译器相同的文件,我的是64的,就选64的文件
将找到的两个文件放入项目的可执行文件所在文件夹下,即bebug或release,再编译运行就可以了
6.工程源码
github.