fast/Plugin/Fast/YoloV4.cpp

137 lines
4.5 KiB
C++
Raw Normal View History

2025-01-20 10:30:01 +08:00
// Vcs-Client.cpp : <20><><EFBFBD><EFBFBD>Ӧ<EFBFBD>ó<EFBFBD><C3B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>
//
#include "stdafx.h"
#include "YoloV4.h"
CYoloV4::CYoloV4()
{
InitYoloV4();
}
CYoloV4::~CYoloV4()
{
}
void CYoloV4::InitYoloV4()
{
char path[1000];
int filelen = GetModuleFileName(NULL, path, 1000);
int i = filelen;
while (path[i] != '\\')i--;
path[i + 1] = '\0';
CString strModulePath = path;
CString names_file = strModulePath + "\\yolov4-tiny\\vocbox.names";
CString cfg_file = strModulePath + "\\yolov4-tiny\\yolov4-tiny-custom.cfg";
CString weights_file = strModulePath + "\\yolov4-tiny\\yolov4-tiny-custom_last.weights";
m_pDetector = new Detector(cfg_file.GetBuffer(), weights_file.GetBuffer());
m_vObjects_Names = ObjectsNamesFromFile(names_file);
}
std::vector<std::string> CYoloV4::ObjectsNamesFromFile(CString strFilename)
{
int pos = 0;
CStdioFile file;
CString strText = _T("");
std::vector<std::string> file_lines;
if (file.Open(strFilename, CFile::modeRead))
{
file.Seek(pos, CFile::begin);
while (file.ReadString(strText))
{
pos = file.GetPosition();//<2F><>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>;
file_lines.push_back(strText.GetBuffer());
}
file.Close();
return file_lines;
}
}
void CYoloV4::DrawObjectBoxes(cv::Mat mat_img, std::vector<bbox_t> result_vec, std::vector<std::string> obj_names,
int current_det_fps, int current_cap_fps)
{
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 max_width = (text_size.width > i.w + 2) ? text_size.width : (i.w + 2);
max_width = std::max(max_width, (int)i.w + 2);
//max_width = std::max(max_width, 283);
std::string coords_3d;
if (!std::isnan(i.z_3d)) {
std::stringstream ss;
ss << std::fixed << std::setprecision(2) << "x:" << i.x_3d << "m y:" << i.y_3d << "m z:" << i.z_3d << "m ";
coords_3d = ss.str();
cv::Size const text_size_3d = getTextSize(ss.str(), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.8, 1, 0);
int const max_width_3d = (text_size_3d.width > i.w + 2) ? text_size_3d.width : (i.w + 2);
if (max_width_3d > max_width) max_width = max_width_3d;
}
cv::rectangle(mat_img, cv::Point2f(std::max((int)i.x - 1, 0), std::max((int)i.y - 65, 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 - 16), cv::FONT_HERSHEY_COMPLEX_SMALL, 1.6, cv::Scalar(0, 0, 0), 4);
if (!coords_3d.empty()) putText(mat_img, coords_3d, cv::Point2f(i.x, i.y - 1), cv::FONT_HERSHEY_COMPLEX_SMALL, 0.8, cv::Scalar(0, 0, 0), 1);
}
}
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);
}
}
void CYoloV4::TargetDetection(int nCameraIdx, cv::Mat &mat_img, vector<CRect> &findRects)
{
CRect findRect;
try {
cv::Mat dst = mat_img;
//cv::Size newSize(mat_img.size().width / 4, mat_img.size().height / 4); // <20><><EFBFBD><EFBFBD><EFBFBD>µijߴ<C4B3>
//cv::resize(mat_img, dst, newSize); // <20><><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD>ߴ<EFBFBD>
auto det_image = m_pDetector->mat_to_image_resize(dst);
auto start = std::chrono::steady_clock::now();
std::vector<bbox_t> result_vec = m_pDetector->detect_resized(*det_image, dst.size().width, dst.size().height);
auto end = std::chrono::steady_clock::now();
std::chrono::duration<double> spent = end - start;
std::cout << " Time: " << spent.count() << " sec \n";
DrawObjectBoxes(dst, result_vec, m_vObjects_Names);
for (int i = 0; i < result_vec.size(); i++)
{
bbox_t result_area = result_vec.at(i);
std::string obj_name = m_vObjects_Names[result_area.obj_id];
//if (result_area.prob >= 0.9 && (0 == obj_name.compare("up") ))
{
CRect rect;
rect.left = result_area.x;
rect.top = result_area.y;
rect.right = result_area.x + result_area.w;
rect.bottom = result_area.y + result_area.h;
findRects.push_back(rect);
}
}
}
catch (std::exception &e) { std::cerr << "exception: " << e.what() << "\n"; getchar(); }
catch (...) { std::cerr << "unknown exception \n"; getchar(); }
}