This commit is contained in:
amass 2025-05-04 11:54:40 +08:00
parent 33a0c50ea3
commit 5330379d4c
6 changed files with 73 additions and 5 deletions

View File

@ -8,6 +8,7 @@ add_executable(PassengerStatistics main.cpp
Camera.h Camera.cpp
ImageUtilities.h ImageUtilities.cpp
NngServer.h NngServer.cpp
OpenCVAlgorithm.h OpenCVAlgorithm.cpp
RtspServer.h RtspServer.cpp
RwAlgorithm.h RwAlgorithm.cpp
Settings.h Settings.cpp

42
Main/OpenCVAlgorithm.cpp Normal file
View File

@ -0,0 +1,42 @@
#include "OpenCVAlgorithm.h"
#include "Core/Logger.h"
#include <opencv2/objdetect.hpp>
#include <opencv2/opencv.hpp>
class OpenCVAlgorithmPrivate {
public:
cv::HOGDescriptor hog;
std::array<uint8_t, OpenCVAlgorithm::DetectImageWidth * OpenCVAlgorithm::DetectImageHeight * 3> bgrBuffer;
cv::Mat convertNV21ToMat(int width, int height, const uint8_t *nv21_data) {
cv::Mat yuv(height + height / 2, width, CV_8UC1, (void *)nv21_data);
cv::Mat bgr(height, width, CV_8UC3, bgrBuffer.data());
cv::cvtColor(yuv, bgr, cv::COLOR_YUV2BGR_NV21);
return bgr;
}
};
OpenCVAlgorithm::OpenCVAlgorithm() : m_d{new OpenCVAlgorithmPrivate()} {
m_d->hog.setSVMDetector(cv::HOGDescriptor::getDefaultPeopleDetector());
}
void OpenCVAlgorithm::detect(const uint8_t *imageData, int width, int height) {
using namespace std::chrono;
auto begin = system_clock::now();
auto frame = m_d->convertNV21ToMat(width, height, imageData);
auto elapsed = duration_cast<milliseconds>(system_clock::now() - begin);
LOG(info) << "convert image(" << width << "x" << height << ") elapsed: " << elapsed;
std::vector<cv::Rect> detectedPeople;
begin = system_clock::now();
m_d->hog.detectMultiScale(frame, detectedPeople, 0, cv::Size(16, 16), // 增大步长,减少窗口数量
cv::Size(32, 32), 1.05, // 增大缩放因子,减少金字塔层数
2);
elapsed = duration_cast<milliseconds>(system_clock::now() - begin);
LOG(info) << "people number: " << detectedPeople.size() << ", elapsed: " << elapsed;
}
OpenCVAlgorithm::~OpenCVAlgorithm() {
if (m_d != nullptr) {
delete m_d;
}
}

21
Main/OpenCVAlgorithm.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef __OPENCVALGORITHM_H__
#define __OPENCVALGORITHM_H__
#include <array>
#include <cstdint>
class OpenCVAlgorithmPrivate;
class OpenCVAlgorithm {
public:
static constexpr int DetectImageWidth = 576;
static constexpr int DetectImageHeight = 320;
OpenCVAlgorithm();
void detect(const uint8_t *imageData, int width, int height);
~OpenCVAlgorithm();
private:
OpenCVAlgorithmPrivate *m_d = nullptr;
};
#endif // __OPENCVALGORITHM_H__

View File

@ -4,7 +4,7 @@
#include <filesystem>
#include <opencv2/opencv.hpp>
bool Algorithm::initialize() {
bool RwAlgorithm::initialize() {
using namespace std::chrono;
if (m_firstTime == system_clock::time_point{}) {
m_firstTime = system_clock::now();
@ -38,7 +38,7 @@ bool Algorithm::initialize() {
return m_handle != nullptr;
}
void Algorithm::detect(const uint8_t *imageData) {
void RwAlgorithm::detect(const uint8_t *imageData) {
if (m_handle == nullptr) {
initialize();
}
@ -49,7 +49,7 @@ void Algorithm::detect(const uint8_t *imageData) {
ds_pedestrian_det_hisi(m_handle, m_buffer.data(), bboxes);
}
void Algorithm::NV21ToBGR24(const unsigned char *nv21Data, unsigned char *bgr24Data, int width, int height) {
void RwAlgorithm::NV21ToBGR24(const unsigned char *nv21Data, unsigned char *bgr24Data, int width, int height) {
cv::Mat nv21Image(height + height / 2, width, CV_8UC1, const_cast<unsigned char *>(nv21Data));
cv::Mat bgrImage(height, width, CV_8UC3, bgr24Data);
cv::cvtColor(nv21Image, bgrImage, cv::COLOR_YUV2BGR_NV21);

View File

@ -5,7 +5,7 @@
#include <chrono>
#include <cstdint>
class Algorithm {
class RwAlgorithm {
public:
static constexpr int DetectImageWidth = 1024;
static constexpr int DetectImageHeight = 576;

View File

@ -1,5 +1,6 @@
#include "VideoInput.h"
#include "Core/Logger.h"
#include "OpenCVAlgorithm.h"
#include "get_scene_param_videomode.h"
#include "rw_mpp_api.h"
#include <chrono>
@ -11,6 +12,7 @@ public:
int32_t encodeChannel = -1;
int32_t scaleChannel = -1;
S_vdec_config decoderConfig;
OpenCVAlgorithm algorithm;
};
VideoInput::VideoInput(int32_t width, int32_t height) : m_d(new VideoInputPrivate()), m_width(width), m_height(height) {
@ -122,7 +124,7 @@ bool VideoInput::startEncode() {
}
m_d->scaleChannel = 0;
S_vscale_cfg scaleConfig = {2960, 1664, 1280, 720};
S_vscale_cfg scaleConfig = {2960, 1664, OpenCVAlgorithm::DetectImageWidth, OpenCVAlgorithm::DetectImageHeight};
status = rw_mpp__vscale_start(m_d->scaleChannel, &scaleConfig);
if (status != 0) {
LOG(error) << "rw_mpp__vscale_start() failed, status: " << status;
@ -159,6 +161,8 @@ void VideoInput::setPacketHandler(const PacketHandler &hanlder) {
void VideoInputPrivate::processImage(S_mpp_img &image, S_mpp_img &detectImage, uint64_t frameIndex) {
// algorithm.detect(detectImage.nv21);
// algorithm.detect(detectImage.nv21, detectImage.width, detectImage.height);
}
void VideoInputPrivate::processFrame(S_mpp_img &image) {