c++ 调用yolov3-yolov4

#ifdef _WIN32
#define OPENCV
#define GPU
#endif



#include <iostream>
#include <windows.h>
#include "yolo_v2_class.hpp"	// imported functions from DLL
#include <opencv2/opencv.hpp>	// C++
#include "opencv2/highgui/highgui.hpp"  
#pragma comment(lib, "opencv_world340.lib")//引入链接库

using namespace cv;
using namespace std;



uchar* BGR;
HDC hScreen, g_stcMemDc;
int Screenshot_W = 800;
int Screenshot_H = 600;

BITMAPINFOHEADER bmi = { 0 };

void Screenshot()
{
	if (BGR == NULL)
		BGR = new uchar[Screenshot_W * Screenshot_H * 3];
	if (hScreen == NULL)
		hScreen = CreateDC(L"DISPLAY", 0, 0, 0);
	if (bmi.biSize == 0)
	{
		bmi.biSize = sizeof(BITMAPINFOHEADER);
		bmi.biPlanes = 1;
		bmi.biBitCount = 24;
		bmi.biWidth = Screenshot_W;
		bmi.biHeight = -Screenshot_H;
		bmi.biCompression = BI_RGB;
		bmi.biSizeImage = Screenshot_W * Screenshot_H;
	}
	g_stcMemDc = CreateCompatibleDC(hScreen);
	HBITMAP hBitmap = CreateCompatibleBitmap(hScreen, Screenshot_W, Screenshot_H);
	SelectObject(g_stcMemDc, hBitmap);
	BitBlt(g_stcMemDc, 0, 0, Screenshot_W, Screenshot_H, hScreen, 0, 0, SRCCOPY);
	GetDIBits(g_stcMemDc, hBitmap, 0, Screenshot_H, BGR, (BITMAPINFO*)&bmi, DIB_RGB_COLORS);
	DeleteDC(g_stcMemDc);
	DeleteObject(hBitmap);
}


vector<bbox_t> DetectionObject; //0 角色|1 怪物|2 领主
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_PLAIN, 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, cv::Scalar(0, 0, 0), 1);

			putText(mat_img, obj_name, cv::Point2f(i.x, i.y - 16), 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, cv::Scalar(50, 255, 0),1);

		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;
}

int main()
{
	string names_file = "./model/coco.names";
	String cfg = "./model/yolov4.cfg";

	String weight = "./model/yolov4.weights";
	
	Detector detector(cfg, weight);//初始化检测器//
	
	Mat ScreenshotMat(Screenshot_H, Screenshot_W, CV_8UC3, Scalar(0, 0, 255));
	while (true)
	{
		     Screenshot();
		     ScreenshotMat.data = BGR;

			DetectionObject = detector.detect(ScreenshotMat);

			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);
			//测试是否成功读入分类对象文件
			for (size_t i = 0; i < obj_names.size(); i++)
			{
				std::cout << obj_names[i] << std::endl;
			}
			
			std::vector<bbox_t> result_vec = detector.detect(ScreenshotMat);
			draw_boxes(ScreenshotMat, result_vec, obj_names);
			
		   imshow("【OpenCV - YOLO】", ScreenshotMat);
		   waitKey(1);

	}
	return 0;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微笑 ❶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值