#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTimer>
#include <QDateTime>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <qdebug.h>
#include <yolo_v2_class.hpp>
/*#ifdef _WIN32
#define OPENCV
#define GPU
#endif*/
using namespace cv;
#pragma comment(lib, "D:/work/Face/face/facdet/yolo_cpp_dll.lib")//引入链接库 ,写入自己的文件路径
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(timerUpdate()));
timer->start(1000);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::timerUpdate()
{
QDateTime time = QDateTime::currentDateTime();
QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
ui->datetime->setText(str);
}
static cv::Scalar obj_id_to_color(int obj_id) {
int const colors[6][3] = { { 1,0,1 },{ 0,0,1 },{ 0,1,1 },{ 0,1,0 },{ 1,1,0 },{ 1,0,0 } };
int const offset = obj_id * 123457 % 6;
int const color_scale = 150 + (obj_id * 123457) % 100;
cv::Scalar color(colors[offset][0], colors[offset][1], colors[offset][2]);
color *= color_scale;
return color;
}
void draw_boxes(cv::Mat mat_img, std::vector<bbox_t> result_vec, std::vector<std::string> obj_names,
int current_det_fps = -1, int current_cap_fps = -1)
{
int const colors[6][3] = { { 1,0,1 },{ 0,0,1 },{ 0,1,1 },{ 0,1,0 },{ 1,1,0 },{ 1,0,0 } };
for (auto &i : result_vec) {
cv::Scalar color = obj_id_to_color(i.obj_id);
cv::rectangle(mat_img, cv::Rect(i.x, i.y, i.w, i.h), color, 2);
if (obj_names.size() > i.obj_id) {
std::string obj_name = obj_names[i.obj_id];
if (i.track_id > 0) obj_name += " - " + std::to_string(i.track_id);
cv::Size const text_size = getTextSize(obj_name, cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, 2, 0);
int const max_width = (text_size.width > i.w + 2) ? text_size.width : (i.w + 2);
cv::rectangle(mat_img, cv::Point2f(std::max((int)i.x - 1, 0), std::max((int)i.y - 30, 0)),
cv::Point2f(std::min((int)i.x + max_width, mat_img.cols-1), std::min((int)i.y, mat_img.rows-1)),
color, CV_FILLED, 8, 0);
putText(mat_img, obj_name, cv::Point2f(i.x, i.y - 10), cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, cv::Scalar(0, 0, 0), 2);
}
}
if (current_det_fps >= 0 && current_cap_fps >= 0) {
std::string fps_str = "FPS detection: " + std::to_string(current_det_fps) + " FPS capture: " + std::to_string(current_cap_fps);
putText(mat_img, fps_str, cv::Point2f(10, 20), cv::FONT_HERSHEY_COMPLEX_SMALL, 1.2, cv::Scalar(50, 255, 0), 2);
}
}
std::vector<std::string> objects_names_from_file(std::string const filename) {
std::ifstream file(filename);
std::vector<std::string> file_lines;
if (!file.is_open()) return file_lines;
for(std::string line; getline(file, line);) file_lines.push_back(line);
std::cout << "object names loaded \n";
return file_lines;
}
void MainWindow::on_pushButton_clicked()
{
Image=imread("D:/work/Face/face/facdet/test.jpg");
imshow("t",Image);
QImage disImage;
disImage= QImage((const unsigned char*)(Image.data), Image.cols, Image.rows, QImage::Format_RGB888);
ui->label->setPixmap(QPixmap::fromImage(disImage));
ui->label->resize(ui->label->pixmap()->size());
}
void MainWindow::on_pushButton_2_clicked()
{
std::string names_file = "D:/work/Face/face/facdet/coco.names";
std::string cfg_file = "D:/work/Face/face/facdet/yolov3.cfg";
std::string weights_file = "D:/work/Face/face/facdet/yolov3.weights"; //以上三行修改对应的文件路径
Detector detector(cfg_file,weights_file,0);
//std::vector<std::string> obj_names = objects_names_from_file(names_file); //调用获得分类对象名称
//或者使用以下四行代码也可实现读入分类对象文件
std::vector<std::string> obj_names;
std::ifstream ifs(names_file.c_str());
std::string line;
while (getline(ifs, line)) obj_names.push_back(line);
capture.open("D:/work/Face/face/facdet/test1.mp4"); //输入视频文件路径
if (!capture.isOpened())
{
printf("文件打开失败");
qDebug()<<"fail";
}
cv::Mat frame;
while (true)
{
capture >> frame;
/* std::string frame_str;
std::vector<unsigned char> buff;
cv::imencode(".jpg", frame, buff);
frame_str.resize(buff.size());
memcpy(&frame_str[0], buff.data(), buff.size());*/
std::vector<bbox_t> result_vec = detector.detect(frame);
draw_boxes(frame, result_vec, obj_names);
cv::namedWindow("test", CV_WINDOW_NORMAL);
cv::imshow("test", frame);
cv::waitKey(3);
}
}
win10+Qt上使用动态链接库运行YOLOv3
4星 · 超过85%的资源 需积分: 5 3 浏览量
2020-03-07
14:49:17
上传
评论 7
收藏 3.19MB ZIP 举报
小破孩罗yy
- 粉丝: 10
- 资源: 3
最新资源
- 基于STM32使用HAL库实现USB组合设备之多路CDC源码+说明文档.zip
- 金融贸易项目springboot
- mybatis动态sqlSQL 映射 XML 文件是所有 sql 语句
- 基于基于STM32的智能家居系统源码+qt上位机源码.zip
- 深圳房地产资源数据报告
- 基于stm32的智能门禁系统源码+设计文档+演示视频.zip
- cef + chromium 完整源码支持h265和h264
- 基于SpringBoot的API管理平台源代码+数据库,以项目的形式管理API文档,可以进行API的编辑、测试、Mock等操作
- protobuf 3.11版本,静态编译
- 2023NOC创客智慧编程赛项真题图形化-选拔赛(有解析)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
评论1