From 33a0c50ea3f9d8c805173e29113bbbbb77c67806 Mon Sep 17 00:00:00 2001 From: amass <168062547@qq.com> Date: Sat, 3 May 2025 11:52:25 +0800 Subject: [PATCH] update. --- .vscode/c_cpp_properties.json | 1 + Main/CMakeLists.txt | 7 +++++ Main/RwAlgorithm.cpp | 56 +++++++++++++++++++++++++++++++++++ Main/RwAlgorithm.h | 24 +++++++++++++++ Main/VideoInput.cpp | 33 ++++++++++++++++----- 5 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 Main/RwAlgorithm.cpp create mode 100644 Main/RwAlgorithm.h diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 08bb42f..fffbaa2 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -4,6 +4,7 @@ "name": "Linux", "includePath": [ "${workspaceFolder}/**", + "3rdparty/ds_pedestrian_mot_hisi/include", "/opt/aarch64-v01c01-linux-gnu-gcc/lib/libdatachannel-0.22.6/include", "/opt/aarch64-v01c01-linux-gnu-gcc/lib/boost_1_88_0/include", "/opt/aarch64-v01c01-linux-gnu-gcc/lib/ZLMediaKit/include", diff --git a/Main/CMakeLists.txt b/Main/CMakeLists.txt index 51c7cba..a1a432d 100644 --- a/Main/CMakeLists.txt +++ b/Main/CMakeLists.txt @@ -9,6 +9,7 @@ add_executable(PassengerStatistics main.cpp ImageUtilities.h ImageUtilities.cpp NngServer.h NngServer.cpp RtspServer.h RtspServer.cpp + RwAlgorithm.h RwAlgorithm.cpp Settings.h Settings.cpp VideoInput.h VideoInput.cpp @@ -25,6 +26,8 @@ add_executable(PassengerStatistics main.cpp target_include_directories(PassengerStatistics PRIVATE ${CMAKE_SOURCE_DIR}/3rdparty/rw_mpp/include PRIVATE ${CMAKE_SOURCE_DIR}/3rdparty/fsan_sensorsdk/include + PRIVATE ${CMAKE_SOURCE_DIR}/3rdparty/libsetscene/include + PRIVATE ${CMAKE_SOURCE_DIR}/3rdparty/ds_pedestrian_mot_hisi/include PRIVATE ${ZLMediaKit_INCLUDE_DIR} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ) @@ -32,6 +35,8 @@ target_include_directories(PassengerStatistics target_link_directories(PassengerStatistics PRIVATE ${CMAKE_SOURCE_DIR}/3rdparty/rw_mpp/lib PRIVATE ${CMAKE_SOURCE_DIR}/3rdparty/fsan_sensorsdk/libs + PRIVATE ${CMAKE_SOURCE_DIR}/3rdparty/libsetscene/lib + PRIVATE ${CMAKE_SOURCE_DIR}/3rdparty/ds_pedestrian_mot_hisi/libs PRIVATE ${ZLMediaKit_LIBRARY_DIRS} PRIVATE ${OPENSSL_LIBRARY_DIRS} ) @@ -46,6 +51,8 @@ target_link_libraries(PassengerStatistics PRIVATE Boost::json PRIVATE rw_mpp PRIVATE sensorsdk + PRIVATE setscene + PRIVATE ds_pedestrian_mot_Hi3516DV500 PRIVATE mk_api PRIVATE opencv_world PRIVATE ${SCTP_LIBRARIES} diff --git a/Main/RwAlgorithm.cpp b/Main/RwAlgorithm.cpp new file mode 100644 index 0000000..9a54254 --- /dev/null +++ b/Main/RwAlgorithm.cpp @@ -0,0 +1,56 @@ +#include "RwAlgorithm.h" +#include "Core/Logger.h" +#include +#include +#include + +bool Algorithm::initialize() { + using namespace std::chrono; + if (m_firstTime == system_clock::time_point{}) { + m_firstTime = system_clock::now(); + } + if (duration_cast(system_clock::now() - m_firstTime) < seconds(10)) { + return false; + } + + constexpr auto licensePath = "/kdata/net.lic"; + bool licenseExisted = std::filesystem::exists(licensePath); + if (licenseExisted && std::filesystem::file_size(licensePath) <= 0) { + LOG(warning) << "license[" << licensePath << "] content is empty, remove it."; + std::filesystem::remove(licensePath); + licenseExisted = false; + } + ds_pedestrian_hisi_set_lic_path("/kdata"); + int status = + ds_pedestrian_hisi_init(&m_handle, "/system/models/ds_mot_m0_2100.bin", "/system/models/ds_mot_m1_2100.bin", + DetectImageWidth, DetectImageHeight, 3); // 不知道什么原因,直接崩溃 + + if (status != 0) { + LOG(error) << "ds_pedestrian_hisi_init() failed, error: " << status; + m_handle = NULL; + } else { + if (!licenseExisted && std::filesystem::exists(licensePath)) { + system("sync"); + } + LOG(info) << "algorithm initialize succeeded."; + } + + return m_handle != nullptr; +} + +void Algorithm::detect(const uint8_t *imageData) { + if (m_handle == nullptr) { + initialize(); + } + if (m_handle == nullptr) return; + NV21ToBGR24(imageData, m_buffer.data(), DetectImageWidth, DetectImageHeight); + + std::vector bboxes; + ds_pedestrian_det_hisi(m_handle, m_buffer.data(), bboxes); +} + +void Algorithm::NV21ToBGR24(const unsigned char *nv21Data, unsigned char *bgr24Data, int width, int height) { + cv::Mat nv21Image(height + height / 2, width, CV_8UC1, const_cast(nv21Data)); + cv::Mat bgrImage(height, width, CV_8UC3, bgr24Data); + cv::cvtColor(nv21Image, bgrImage, cv::COLOR_YUV2BGR_NV21); +} diff --git a/Main/RwAlgorithm.h b/Main/RwAlgorithm.h new file mode 100644 index 0000000..945e096 --- /dev/null +++ b/Main/RwAlgorithm.h @@ -0,0 +1,24 @@ +#ifndef __ALGORITHM_H__ +#define __ALGORITHM_H__ + +#include +#include +#include + +class Algorithm { +public: + static constexpr int DetectImageWidth = 1024; + static constexpr int DetectImageHeight = 576; + bool initialize(); + void detect(const uint8_t *imageData); + +protected: + void NV21ToBGR24(const unsigned char *nv21Data, unsigned char *bgr24Data, int width, int height); + +private: + void *m_handle = nullptr; + std::array m_buffer; + std::chrono::system_clock::time_point m_firstTime; +}; + +#endif // __ALGORITHM_H__ \ No newline at end of file diff --git a/Main/VideoInput.cpp b/Main/VideoInput.cpp index a081b1f..dfc3baa 100644 --- a/Main/VideoInput.cpp +++ b/Main/VideoInput.cpp @@ -1,5 +1,6 @@ #include "VideoInput.h" #include "Core/Logger.h" +#include "get_scene_param_videomode.h" #include "rw_mpp_api.h" #include @@ -17,12 +18,18 @@ VideoInput::VideoInput(int32_t width, int32_t height) : m_d(new VideoInputPrivat if (status != 0) { LOG(error) << "rw_mpp__vdec_init() failed, status: " << status; } + RWISP_SceneAuto_init(); // 初始化 ISP 库 + SceneAuto_Cmd command; + command.commandID = FaceAE_EN; + command.faceFlag = 0x01; + RWISP_SetSceneAuto(&command); } VideoInput::~VideoInput() { if (isStarted()) { stop(); } + RWISP_SceneAuto_deinit(); rw_mpp__vdec_finalize(); } @@ -85,17 +92,28 @@ bool VideoInput::startEncode() { config.profile = H264_main; config.raw_max_width = 2960; config.raw_max_height = 1664; - config.width = 1280; - config.height = 720; + config.width = 1920; + config.height = 1080; config.gop = 5; - config.framerate = 25; + config.framerate = 20; config.rc_type = RC_VBR; + // config.rc_type = RC_FIXQP; S_venc_rc_vbr vbr; - memset(&vbr, 0, sizeof(S_venc_rc_vbr)); - vbr.max_bitrate = 1024; - vbr.stats_time = 1; - config.rc_param = &vbr; + S_venc_rc_fixQp fixQp; + if (config.rc_type == RC_FIXQP) { // RC_FIXQP 其压缩损失比较小 + memset(&fixQp, 0, sizeof(S_venc_rc_fixQp)); + fixQp.IQp = 20; + fixQp.PQp = 20; + fixQp.BQp = 20; + config.rc_param = &fixQp; + } else { + memset(&vbr, 0, sizeof(S_venc_rc_vbr)); + vbr.max_bitrate = 100 * 1024; + vbr.stats_time = 1; + config.rc_param = &vbr; + } + int status = rw_mpp__venc_start(m_d->encodeChannel, &config, -1); if (status != 0) { LOG(error) << "rw_mpp__venc_start() failed, status: " << status; @@ -140,6 +158,7 @@ void VideoInput::setPacketHandler(const PacketHandler &hanlder) { } void VideoInputPrivate::processImage(S_mpp_img &image, S_mpp_img &detectImage, uint64_t frameIndex) { + // algorithm.detect(detectImage.nv21); } void VideoInputPrivate::processFrame(S_mpp_img &image) {