Refactor project structure
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,111 @@
|
||||
#ifndef ANSFLV_H
|
||||
#define ANSFLV_H
|
||||
#define ANSFLV_API __declspec(dllexport)
|
||||
#include <iostream>
|
||||
#include "ANSLicense.h"
|
||||
#include "LabVIEWHeader/extcode.h"
|
||||
#include <vector>
|
||||
#include "sys_inc.h"
|
||||
#include "http_flv_cln.h"
|
||||
#include "hqueue.h"
|
||||
#include "http.h"
|
||||
#include "http_parse.h"
|
||||
#include "video_decoder.h"
|
||||
#include "audio_decoder.h"
|
||||
#include "http_flv_player.h"
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
namespace ANSCENTER
|
||||
{
|
||||
class ANSFLV_API ANSFLVClient
|
||||
{
|
||||
protected:
|
||||
std::unique_ptr<CHttpFlvPlayer> _playerClient = std::make_unique<CHttpFlvPlayer>();
|
||||
|
||||
std::string _username;
|
||||
std::string _password;
|
||||
std::string _url;
|
||||
bool _useFullURL;
|
||||
int _imageRotateDeg = 0;
|
||||
int _displayWidth = 0; // 0 = no resize (return original resolution)
|
||||
int _displayHeight = 0;
|
||||
cv::Mat _pLastFrame;
|
||||
std::string _lastJpegImage;
|
||||
int _imageWidth, _imageHeight;
|
||||
int64_t _pts;
|
||||
bool _isPlaying;
|
||||
std::recursive_mutex _mutex;
|
||||
public:
|
||||
ANSFLVClient();
|
||||
~ANSFLVClient() noexcept;
|
||||
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
|
||||
[[nodiscard]] bool Init(std::string licenseKey, std::string username, std::string password, std::string url);
|
||||
[[nodiscard]] bool Setup();
|
||||
[[nodiscard]] bool Reconnect();
|
||||
[[nodiscard]] bool Start();
|
||||
[[nodiscard]] bool IsPaused();
|
||||
[[nodiscard]] bool IsPlaying();
|
||||
[[nodiscard]] bool IsRecording();
|
||||
[[nodiscard]] bool Stop();
|
||||
[[nodiscard]] bool Pause();
|
||||
void Destroy();
|
||||
void EnableAudio(bool status);
|
||||
void SetAudioVolume(int volume);
|
||||
bool areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2);
|
||||
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
|
||||
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
|
||||
[[nodiscard]] std::string GetJpegImage(int& width, int& height, int64_t& pts);
|
||||
void SetImageRotate(int mode) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_imageRotateDeg = mode;
|
||||
if (mode > 360) _imageRotateDeg = 360;
|
||||
};
|
||||
void SetBBox(cv::Rect bbox);
|
||||
void SetCrop(bool crop);
|
||||
static void SetMaxHWDecoders(int maxDecoders);
|
||||
static int AutoConfigureHWDecoders(int maxPerGpuOverride = 0);
|
||||
void SetHWDecoding(int hwMode, int preferredGpu = -1);
|
||||
bool IsHWDecodingActive();
|
||||
int GetHWDecodingGpuIndex();
|
||||
void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize)
|
||||
void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display)
|
||||
AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free)
|
||||
AVFrame* GetCudaHWFrame(); // Returns CUDA HW frame (device ptrs) for zero-copy inference
|
||||
bool IsCudaHWAccel(); // true when decoder uses CUDA (NV12 stays in GPU VRAM)
|
||||
public:
|
||||
void CheckLicense();
|
||||
SPDLogger& _logger = SPDLogger::GetInstance("ANSFLV", false);
|
||||
bool _licenseValid{ false };
|
||||
std::string _licenseKey;
|
||||
//std::once_flag licenseOnceFlag;
|
||||
|
||||
};
|
||||
}
|
||||
extern "C" __declspec(dllexport) int CreateANSFLVHandle(ANSCENTER::ANSFLVClient * *Handle, const char* licenseKey, const char* username, const char* password, const char* url);
|
||||
extern "C" __declspec(dllexport) int ReleaseANSFLVHandle(ANSCENTER::ANSFLVClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int GetFLVImage(ANSCENTER::ANSFLVClient * *Handle, int& width, int& height, int64_t & timeStamp, LStrHandle jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetFLVStrImage(ANSCENTER::ANSFLVClient * *Handle, int& width, int& height, int64_t & timeStamp, std::string & jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetFLVCVImage(ANSCENTER::ANSFLVClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
|
||||
|
||||
extern "C" __declspec(dllexport) int StartFLV(ANSCENTER::ANSFLVClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int ReconnectFLV(ANSCENTER::ANSFLVClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int StopFLV(ANSCENTER::ANSFLVClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int PauseFLV(ANSCENTER::ANSFLVClient** Handle);
|
||||
extern "C" __declspec(dllexport) int IsFLVPaused(ANSCENTER::ANSFLVClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int IsFLVRunning(ANSCENTER::ANSFLVClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int IsFLVRecording(ANSCENTER::ANSFLVClient * *Handle);
|
||||
extern "C" __declspec(dllexport) void SetFLVAudioVolume(ANSCENTER::ANSFLVClient * *Handle, int volume);
|
||||
extern "C" __declspec(dllexport) void EnableFLVAudioVolume(ANSCENTER::ANSFLVClient * *Handle, int status);
|
||||
extern "C" __declspec(dllexport) void SetFLVImageRotation(ANSCENTER::ANSFLVClient * *Handle, double rotationAngle);
|
||||
extern "C" __declspec(dllexport) int SetBBoxFLV(ANSCENTER::ANSFLVClient** Handle, int x, int y, int width, int height);
|
||||
extern "C" __declspec(dllexport) int SetCropFlagFLV(ANSCENTER::ANSFLVClient** Handle, int cropFlag);
|
||||
extern "C" __declspec(dllexport) void SetFLVMaxHWDecoders(int maxDecoders);
|
||||
extern "C" __declspec(dllexport) int AutoConfigureFLVHWDecoders(int maxPerGpuOverride);
|
||||
extern "C" __declspec(dllexport) void SetFLVHWDecoding(ANSCENTER::ANSFLVClient** Handle, int hwMode, int preferredGpu = -1);
|
||||
extern "C" __declspec(dllexport) int IsFLVHWDecodingActive(ANSCENTER::ANSFLVClient** Handle);
|
||||
extern "C" __declspec(dllexport) int GetFLVHWDecodingGpuIndex(ANSCENTER::ANSFLVClient** Handle);
|
||||
extern "C" __declspec(dllexport) void SetFLVImageQuality(ANSCENTER::ANSFLVClient** Handle, int mode);
|
||||
extern "C" __declspec(dllexport) void SetFLVDisplayResolution(ANSCENTER::ANSFLVClient** Handle, int width, int height);
|
||||
#endif
|
||||
@@ -0,0 +1,861 @@
|
||||
#include "ANSFilePlayer.h"
|
||||
#include "ANSMatRegistry.h"
|
||||
#include "ANSGpuFrameOps.h"
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
#include <libswscale/swscale.h>
|
||||
#include <libavutil/imgutils.h>
|
||||
#include <libavutil/frame.h>
|
||||
#include "media_codec.h"
|
||||
static bool ansfileplayerLicenceValid = false;
|
||||
// Global once_flag to protect license checking
|
||||
static std::once_flag ansfileplayerLicenseOnceFlag;
|
||||
namespace ANSCENTER {
|
||||
#define CHECK_CUDA(call) do { \
|
||||
cudaError_t err = call; \
|
||||
if (err != cudaSuccess) { \
|
||||
std::cout<<"CUDA error: " + std::string(cudaGetErrorString(err)); \
|
||||
} \
|
||||
} while (0)
|
||||
#define CHECK_NVJPEG(call) do { \
|
||||
nvjpegStatus_t status = call; \
|
||||
if (status != NVJPEG_STATUS_SUCCESS) { \
|
||||
std::cout <<"nvJPEG error: " + std::to_string(status); \
|
||||
} \
|
||||
} while (0)
|
||||
ANSFILEPLAYER::ANSFILEPLAYER() {
|
||||
_url = "";
|
||||
_imageRotateDeg = 0;
|
||||
_imageWidth = 0;
|
||||
_imageHeight = 0;
|
||||
_pts = 0;
|
||||
_isPlaying = false;
|
||||
_lastJpegImage = "";
|
||||
}
|
||||
ANSFILEPLAYER::~ANSFILEPLAYER() noexcept {
|
||||
try {
|
||||
Destroy();
|
||||
}
|
||||
catch (...) {}
|
||||
}
|
||||
void ANSFILEPLAYER::Destroy() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
_url = "";
|
||||
_imageRotateDeg = 0;
|
||||
_isPlaying = false;
|
||||
_lastJpegImage = "";
|
||||
_pLastFrame.release();
|
||||
if (_playerClient) {
|
||||
_playerClient->close();
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogError("ANSFILEPLAYER::Destroy. Exception:", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
catch (...) {
|
||||
_logger.LogError("ANSFILEPLAYER::Destroy.", "Unknown exception", __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
void ANSFILEPLAYER::CheckLicense() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
_licenseValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(_licenseKey, 1007, "ANSCV");//Default productId=1005
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger.LogFatal("ANSFILEPLAYER::CheckLicense. Error:", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
bool ANSFILEPLAYER::Init(std::string licenseKey, std::string url) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_licenseKey = licenseKey;
|
||||
CheckLicense();
|
||||
if (!_licenseValid) {
|
||||
this->_logger.LogError("ANSFILEPLAYER::Init.", "Invalid license", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
//network_init();
|
||||
//sys_buf_init(200);
|
||||
//rtsp_parse_buf_init(200);
|
||||
//http_msg_buf_init(200);
|
||||
_url = url;
|
||||
return Setup();
|
||||
}
|
||||
void ANSFILEPLAYER::SetBBox(cv::Rect bbox) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_playerClient->setBbox(bbox);
|
||||
}
|
||||
void ANSFILEPLAYER::SetCrop(bool crop) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_playerClient->setCrop(crop);
|
||||
}
|
||||
bool ANSFILEPLAYER::Setup() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
return _playerClient->open(_url);
|
||||
}
|
||||
bool ANSFILEPLAYER::Reconnect() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_playerClient->close();
|
||||
Setup();
|
||||
return Start();
|
||||
}
|
||||
bool ANSFILEPLAYER::Start() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_isPlaying = _playerClient->play();
|
||||
return _isPlaying;
|
||||
}
|
||||
bool ANSFILEPLAYER::Stop() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
if (_playerClient->pause()) {
|
||||
_isPlaying = false;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool ANSFILEPLAYER::IsPaused() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
return _playerClient->isPaused();
|
||||
}
|
||||
bool ANSFILEPLAYER::IsPlaying() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
return _playerClient->isPlaying();
|
||||
}
|
||||
bool ANSFILEPLAYER::IsRecording() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
return _playerClient->isRecording();
|
||||
}
|
||||
std::string ANSFILEPLAYER::GetJpegImage(int& width, int& height, int64_t& pts) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
// If the player is playing, process the frame
|
||||
if (_isPlaying) {
|
||||
// Get a new frame from the player client
|
||||
_lastJpegImage = _playerClient->getJpegImage(width, height, pts);
|
||||
// Update internal state variables
|
||||
_pts = pts;
|
||||
_imageWidth = width;
|
||||
_imageHeight = height;
|
||||
// Return the frame
|
||||
return _lastJpegImage;
|
||||
}
|
||||
// If the player is not playing, return the last known frame
|
||||
else {
|
||||
width = _imageWidth;
|
||||
height = _imageHeight;
|
||||
pts = _pts;
|
||||
return _lastJpegImage;
|
||||
}
|
||||
}
|
||||
cv::Mat ANSFILEPLAYER::GetImage(int& width, int& height, int64_t& pts) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
if (!_isPlaying) {
|
||||
width = _imageWidth;
|
||||
height = _imageHeight;
|
||||
pts = _pts;
|
||||
return _pLastFrame;
|
||||
}
|
||||
|
||||
_pLastFrame.release();
|
||||
int imgW = 0, imgH = 0;
|
||||
_pLastFrame = _playerClient->getImage(imgW, imgH, pts);
|
||||
|
||||
if (_pLastFrame.empty()) {
|
||||
width = _imageWidth;
|
||||
height = _imageHeight;
|
||||
pts = _pts;
|
||||
return _pLastFrame;
|
||||
}
|
||||
|
||||
cv::Mat result = _pLastFrame;
|
||||
|
||||
// Apply rotation if needed
|
||||
if (_imageRotateDeg >= 1 && _imageRotateDeg != 0) {
|
||||
cv::Point2f center(imgW / 2.0f, imgH / 2.0f);
|
||||
cv::Mat rotMat = cv::getRotationMatrix2D(center, _imageRotateDeg, 1.0);
|
||||
cv::Mat rotated;
|
||||
cv::warpAffine(result, rotated, rotMat, result.size(),
|
||||
cv::INTER_CUBIC, cv::BORDER_CONSTANT, cv::Scalar());
|
||||
result = rotated;
|
||||
}
|
||||
|
||||
// Store full-res for inference (before display resize)
|
||||
_inferenceImage = result;
|
||||
|
||||
// Resize for display if display resolution is set
|
||||
if (_displayWidth > 0 && _displayHeight > 0 &&
|
||||
(result.cols != _displayWidth || result.rows != _displayHeight)) {
|
||||
cv::Mat displayResult;
|
||||
cv::resize(result, displayResult, cv::Size(_displayWidth, _displayHeight),
|
||||
0, 0, cv::INTER_LINEAR);
|
||||
result = displayResult;
|
||||
}
|
||||
|
||||
_pts = pts;
|
||||
width = result.cols;
|
||||
height = result.rows;
|
||||
_imageWidth = width;
|
||||
_imageHeight = height;
|
||||
return result;
|
||||
}
|
||||
void ANSFILEPLAYER::SetDisplayResolution(int width, int height) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_displayWidth = width;
|
||||
_displayHeight = height;
|
||||
}
|
||||
cv::Mat ANSFILEPLAYER::GetInferenceImage() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
return _inferenceImage;
|
||||
}
|
||||
void ANSFILEPLAYER::EnableAudio(bool status) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_playerClient->enableAudio(status);
|
||||
}
|
||||
void ANSFILEPLAYER::SetAudioVolume(int volume) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_playerClient->setVolume(volume);
|
||||
}
|
||||
std::string ANSFILEPLAYER::MatToBinaryData(const cv::Mat& image) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
if (!image.empty()) {
|
||||
if ((image.data != nullptr) && (image.u != nullptr)) {
|
||||
try {
|
||||
std::vector<uchar> imageData;
|
||||
bool success = cv::imencode(".jpg", image, imageData);
|
||||
if (!success) {
|
||||
this->_logger.LogError("ANSFILEPLAYER::MatToBinaryData. Error:", "Failed to encode the image.", __FILE__, __LINE__);
|
||||
return "";
|
||||
}
|
||||
std::string binaryData(imageData.begin(), imageData.end());
|
||||
return binaryData;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogFatal("ANSFILEPLAYER::MatToBinaryData. Error:", e.what(), __FILE__, __LINE__);
|
||||
return "";
|
||||
}
|
||||
catch (...) {
|
||||
this->_logger.LogFatal("ANSFILEPLAYER::MatToBinaryData. Error:", "Caught unknown exception!", __FILE__, __LINE__);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
else return "";
|
||||
}
|
||||
else return "";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static void VerifyGlobalANSFPLicense(const std::string& licenseKey) {
|
||||
try {
|
||||
ansfileplayerLicenceValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, 1007, "ANSCV");//Default productId=1005
|
||||
if (!ansfileplayerLicenceValid) { // we also support ANSTS license
|
||||
ansfileplayerLicenceValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, 1003, "ANSVIS");//Default productId=1003 (ANSVIS)
|
||||
}
|
||||
if (!ansfileplayerLicenceValid) { // we also support ANSTS license
|
||||
ansfileplayerLicenceValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, 1008, "ANSTS");//Default productId=1008 (ANSTS)
|
||||
}
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
ansfileplayerLicenceValid = false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) int CreateANSFilePlayerHandle(ANSCENTER::ANSFILEPLAYER** Handle, const char* licenseKey, const char* url) {
|
||||
if (!Handle || !licenseKey || !url) return -1;
|
||||
try {
|
||||
auto ptr = std::make_unique<ANSCENTER::ANSFILEPLAYER>();
|
||||
bool result = ptr->Init(licenseKey, url);
|
||||
if (result) {
|
||||
*Handle = ptr.release();
|
||||
extern void anscv_unregister_handle(void*);
|
||||
extern void anscv_register_handle(void*, void(*)(void*));
|
||||
anscv_register_handle(*Handle, [](void* p) {
|
||||
auto* h = static_cast<ANSCENTER::ANSFILEPLAYER*>(p);
|
||||
try { h->Stop(); } catch (...) {}
|
||||
try { h->Destroy(); } catch (...) {}
|
||||
try { delete h; } catch (...) {}
|
||||
});
|
||||
return 1;
|
||||
}
|
||||
*Handle = nullptr;
|
||||
return 0;
|
||||
}
|
||||
catch (...) { return 0; }
|
||||
}
|
||||
extern "C" __declspec(dllexport) int ReleaseANSFilePlayerHandle(ANSCENTER::ANSFILEPLAYER** Handle) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
extern void anscv_unregister_handle(void*);
|
||||
anscv_unregister_handle(*Handle);
|
||||
// Destructor calls Destroy() — no need to call it explicitly (avoids double-destroy)
|
||||
std::unique_ptr<ANSCENTER::ANSFILEPLAYER> ptr(*Handle);
|
||||
*Handle = nullptr;
|
||||
return 0;
|
||||
}
|
||||
catch (...) {
|
||||
if (Handle) *Handle = nullptr;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int GetFilePlayerStrImage(ANSCENTER::ANSFILEPLAYER** Handle, int& width, int& height, int64_t& timeStamp, std::string& jpegImage) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
jpegImage = (*Handle)->GetJpegStringImage(width, height, timeStamp);
|
||||
if (!jpegImage.empty()) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error getting image data from FilePlayer client: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int GetFilePlayerImage(ANSCENTER::ANSFILEPLAYER** Handle, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
std::string jpegString = (*Handle)->GetJpegStringImage(width, height, timeStamp);
|
||||
int size = jpegString.length();
|
||||
if (size > 0) {
|
||||
MgErr error;
|
||||
// Resize the jpegImage handle to hold the image data
|
||||
error = DSSetHandleSize(jpegImage, sizeof(int32) + size * sizeof(uChar));
|
||||
// Check if resizing the handle was successful
|
||||
if (error == noErr) {
|
||||
// Set the size of the image in the handle
|
||||
(*jpegImage)->cnt = size;
|
||||
// Use memcpy to copy the data from the std::string to the LStrHandle's str buffer
|
||||
memcpy((*jpegImage)->str, jpegString.c_str(), size);
|
||||
// Return success
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
// Return failure if there was an error in resizing the handle
|
||||
std::cerr << "Error resizing jpegImage handle: " << error << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// If the JPEG image string is empty, return failure
|
||||
std::cerr << "No image data retrieved from FilePlayer client." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error getting image data from FilePlayer client: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int GetFilePlayerCVImage(ANSCENTER::ANSFILEPLAYER** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image) {
|
||||
if (!Handle || !(*Handle) || !image) {
|
||||
std::cerr << "Error: Invalid input parameters in GetFilePlayerCVImage." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
try {
|
||||
cv::Mat img = (*Handle)->GetImage(width, height, timeStamp);
|
||||
if (img.empty()) {
|
||||
return 0;
|
||||
}
|
||||
anscv_mat_replace(image, std::move(img));
|
||||
|
||||
// Attach NV12/CUDA frames to registry for inference zero-copy (same as ANSRTSP/ANSVideoPlayer).
|
||||
// CFilePlayer inherits getNV12Frame()/getCudaHWFrame() from CVideoPlayer.
|
||||
int gpuIdx = (*Handle)->_playerClient->getHWDecodingGpuIndex();
|
||||
AVFrame* cudaHW = (*Handle)->_playerClient->getCudaHWFrame();
|
||||
if (cudaHW) {
|
||||
AVFrame* cpuNV12 = (*Handle)->_playerClient->getNV12Frame();
|
||||
gpu_frame_attach_cuda(*image, cudaHW, gpuIdx, timeStamp, cpuNV12);
|
||||
} else {
|
||||
AVFrame* nv12 = (*Handle)->_playerClient->getNV12Frame();
|
||||
if (nv12) {
|
||||
gpu_frame_attach(*image, nv12, gpuIdx, timeStamp);
|
||||
} else {
|
||||
// CPU-only fallback: attach full-res BGR clone for ANSRTYOLO
|
||||
cv::Mat infImg = (*Handle)->GetInferenceImage();
|
||||
if (!infImg.empty() && infImg.data != (*image)->data) {
|
||||
(*Handle)->_inferenceClonePrev = (*Handle)->_inferenceCloneCurr;
|
||||
(*Handle)->_inferenceCloneCurr = infImg.clone();
|
||||
|
||||
GpuFrameData data{};
|
||||
data.avframe = nullptr;
|
||||
data.yPlane = (*Handle)->_inferenceCloneCurr.data;
|
||||
data.uvPlane = nullptr;
|
||||
data.yLinesize = static_cast<int>((*Handle)->_inferenceCloneCurr.step[0]);
|
||||
data.uvLinesize = 0;
|
||||
data.width = (*Handle)->_inferenceCloneCurr.cols;
|
||||
data.height = (*Handle)->_inferenceCloneCurr.rows;
|
||||
data.pixelFormat = 1000; // ANSCV_PIX_FMT_BGR24
|
||||
data.gpuIndex = -1;
|
||||
data.pts = timeStamp;
|
||||
data.isCudaDevicePtr = false;
|
||||
data.cpuAvframe = nullptr;
|
||||
data.cpuYPlane = nullptr;
|
||||
data.cpuUvPlane = nullptr;
|
||||
data.cpuYLinesize = 0;
|
||||
data.cpuUvLinesize = 0;
|
||||
|
||||
ANSGpuFrameRegistry::instance().attach(*image, std::move(data));
|
||||
auto pending = ANSGpuFrameRegistry::instance().drain_pending();
|
||||
for (void* p : pending) {
|
||||
AVFrame* stale = static_cast<AVFrame*>(p);
|
||||
av_frame_free(&stale);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
catch (const cv::Exception& e) {
|
||||
std::cerr << "OpenCV exception in GetFilePlayerCVImage: " << e.what() << std::endl;
|
||||
return -2;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Exception in GetFilePlayerCVImage: " << e.what() << std::endl;
|
||||
return -2;
|
||||
}
|
||||
catch (...) {
|
||||
std::cerr << "Unknown exception in GetFilePlayerCVImage" << std::endl;
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) int StartFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
bool result = (*Handle)->Start();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error starting file player: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int ReconnectFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
bool result = (*Handle)->Reconnect();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error reconnecting file player: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int StopFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
bool result = (*Handle)->Stop();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error stopping file player: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int IsFilePlayerPaused(ANSCENTER::ANSFILEPLAYER** Handle) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
bool result = (*Handle)->IsPaused();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error checking if file player is paused: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int IsFilePlayerRunning(ANSCENTER::ANSFILEPLAYER** Handle) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
bool result = (*Handle)->IsPlaying();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error checking if file player is running: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int IsFilePlayerRecording(ANSCENTER::ANSFILEPLAYER** Handle) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
bool result = (*Handle)->IsRecording();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error checking if file player is recording: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) void SetFilePlayerAudioVolume(ANSCENTER::ANSFILEPLAYER** Handle, int volume)
|
||||
{
|
||||
if (Handle == nullptr || *Handle == nullptr) return;
|
||||
try {
|
||||
(*Handle)->SetAudioVolume(volume);
|
||||
}
|
||||
catch (...) { }
|
||||
}
|
||||
extern "C" __declspec(dllexport) void EnableFilePlayerAudioVolume(ANSCENTER::ANSFILEPLAYER** Handle, int status)
|
||||
{
|
||||
if (Handle == nullptr || *Handle == nullptr) return;
|
||||
try {
|
||||
bool audioStatus = false;
|
||||
if (status == 1)audioStatus = true;
|
||||
(*Handle)->EnableAudio(audioStatus);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error enabling audio for file player: " << e.what() << std::endl;
|
||||
}
|
||||
catch (...) { }
|
||||
}
|
||||
extern "C" __declspec(dllexport) void SetFilePlayerImageRotation(ANSCENTER::ANSFILEPLAYER** Handle, double rotationAngle) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return;
|
||||
try {
|
||||
(*Handle)->SetImageRotate(rotationAngle);
|
||||
}
|
||||
catch (...) { }
|
||||
}
|
||||
extern "C" __declspec(dllexport) int SetBBoxFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle, int x, int y, int width, int height) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
cv::Rect bbox(x, y, width, height);
|
||||
(*Handle)->SetBBox(bbox);
|
||||
return 1;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error setting bounding box for file player: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int SetCropFlagFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle, int cropFlag) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
bool crop = false;
|
||||
if (cropFlag == 1)crop = true;
|
||||
(*Handle)->SetCrop(crop);
|
||||
return 1;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error setting crop flag for file player: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// V2 functions: accept uint64_t handleVal by value (LabVIEW-safe)
|
||||
// ============================================================================
|
||||
|
||||
extern "C" __declspec(dllexport) int GetFilePlayerImage_V2(uint64_t handleVal, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage) {
|
||||
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
|
||||
if (!h) return -1;
|
||||
try {
|
||||
std::string jpegString = h->GetJpegStringImage(width, height, timeStamp);
|
||||
int size = jpegString.length();
|
||||
if (size > 0) {
|
||||
MgErr error;
|
||||
error = DSSetHandleSize(jpegImage, sizeof(int32) + size * sizeof(uChar));
|
||||
if (error == noErr) {
|
||||
(*jpegImage)->cnt = size;
|
||||
memcpy((*jpegImage)->str, jpegString.c_str(), size);
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
std::cerr << "Error resizing jpegImage handle: " << error << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
std::cerr << "No image data retrieved from FilePlayer client." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error getting image data from FilePlayer client: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) int GetFilePlayerCVImage_V2(uint64_t handleVal, int& width, int& height, int64_t& timeStamp, cv::Mat** image) {
|
||||
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
|
||||
if (!h || !image) return -1;
|
||||
try {
|
||||
cv::Mat img = h->GetImage(width, height, timeStamp);
|
||||
if (img.empty()) {
|
||||
return 0;
|
||||
}
|
||||
anscv_mat_replace(image, std::move(img));
|
||||
|
||||
// NV12/CUDA registry attach (same as V1)
|
||||
int gpuIdx = h->_playerClient->getHWDecodingGpuIndex();
|
||||
AVFrame* cudaHW = h->_playerClient->getCudaHWFrame();
|
||||
if (cudaHW) {
|
||||
AVFrame* cpuNV12 = h->_playerClient->getNV12Frame();
|
||||
gpu_frame_attach_cuda(*image, cudaHW, gpuIdx, timeStamp, cpuNV12);
|
||||
} else {
|
||||
AVFrame* nv12 = h->_playerClient->getNV12Frame();
|
||||
if (nv12) {
|
||||
gpu_frame_attach(*image, nv12, gpuIdx, timeStamp);
|
||||
} else {
|
||||
cv::Mat infImg = h->GetInferenceImage();
|
||||
if (!infImg.empty() && infImg.data != (*image)->data) {
|
||||
h->_inferenceClonePrev = h->_inferenceCloneCurr;
|
||||
h->_inferenceCloneCurr = infImg.clone();
|
||||
|
||||
GpuFrameData data{};
|
||||
data.avframe = nullptr;
|
||||
data.yPlane = h->_inferenceCloneCurr.data;
|
||||
data.uvPlane = nullptr;
|
||||
data.yLinesize = static_cast<int>(h->_inferenceCloneCurr.step[0]);
|
||||
data.uvLinesize = 0;
|
||||
data.width = h->_inferenceCloneCurr.cols;
|
||||
data.height = h->_inferenceCloneCurr.rows;
|
||||
data.pixelFormat = 1000;
|
||||
data.gpuIndex = -1;
|
||||
data.pts = timeStamp;
|
||||
data.isCudaDevicePtr = false;
|
||||
data.cpuAvframe = nullptr;
|
||||
data.cpuYPlane = nullptr;
|
||||
data.cpuUvPlane = nullptr;
|
||||
data.cpuYLinesize = 0;
|
||||
data.cpuUvLinesize = 0;
|
||||
|
||||
ANSGpuFrameRegistry::instance().attach(*image, std::move(data));
|
||||
auto pending = ANSGpuFrameRegistry::instance().drain_pending();
|
||||
for (void* p : pending) {
|
||||
AVFrame* stale = static_cast<AVFrame*>(p);
|
||||
av_frame_free(&stale);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
catch (const cv::Exception& e) {
|
||||
std::cerr << "OpenCV exception in GetFilePlayerCVImage_V2: " << e.what() << std::endl;
|
||||
return -2;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Exception in GetFilePlayerCVImage_V2: " << e.what() << std::endl;
|
||||
return -2;
|
||||
}
|
||||
catch (...) {
|
||||
std::cerr << "Unknown exception in GetFilePlayerCVImage_V2" << std::endl;
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) int StartFilePlayer_V2(uint64_t handleVal) {
|
||||
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
|
||||
if (!h) return -1;
|
||||
try {
|
||||
bool result = h->Start();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error starting file player: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) int ReconnectFilePlayer_V2(uint64_t handleVal) {
|
||||
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
|
||||
if (!h) return -1;
|
||||
try {
|
||||
bool result = h->Reconnect();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error reconnecting file player: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) int StopFilePlayer_V2(uint64_t handleVal) {
|
||||
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
|
||||
if (!h) return -1;
|
||||
try {
|
||||
bool result = h->Stop();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error stopping file player: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) int IsFilePlayerPaused_V2(uint64_t handleVal) {
|
||||
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
|
||||
if (!h) return -1;
|
||||
try {
|
||||
bool result = h->IsPaused();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error checking if file player is paused: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) int IsFilePlayerRunning_V2(uint64_t handleVal) {
|
||||
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
|
||||
if (!h) return -1;
|
||||
try {
|
||||
bool result = h->IsPlaying();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error checking if file player is running: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) int IsFilePlayerRecording_V2(uint64_t handleVal) {
|
||||
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
|
||||
if (!h) return -1;
|
||||
try {
|
||||
bool result = h->IsRecording();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error checking if file player is recording: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) void SetFilePlayerAudioVolume_V2(uint64_t handleVal, int volume) {
|
||||
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
|
||||
if (!h) return;
|
||||
try {
|
||||
h->SetAudioVolume(volume);
|
||||
}
|
||||
catch (...) { }
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) void EnableFilePlayerAudioVolume_V2(uint64_t handleVal, int status) {
|
||||
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
|
||||
if (!h) return;
|
||||
try {
|
||||
bool audioStatus = false;
|
||||
if (status == 1) audioStatus = true;
|
||||
h->EnableAudio(audioStatus);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error enabling audio for file player: " << e.what() << std::endl;
|
||||
}
|
||||
catch (...) { }
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) void SetFilePlayerImageRotation_V2(uint64_t handleVal, double rotationAngle) {
|
||||
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
|
||||
if (!h) return;
|
||||
try {
|
||||
h->SetImageRotate(rotationAngle);
|
||||
}
|
||||
catch (...) { }
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) int SetBBoxFilePlayer_V2(uint64_t handleVal, int x, int y, int width, int height) {
|
||||
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
|
||||
if (!h) return -1;
|
||||
try {
|
||||
cv::Rect bbox(x, y, width, height);
|
||||
h->SetBBox(bbox);
|
||||
return 1;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error setting bounding box for file player: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) int SetCropFlagFilePlayer_V2(uint64_t handleVal, int cropFlag) {
|
||||
auto* h = reinterpret_cast<ANSCENTER::ANSFILEPLAYER*>(handleVal);
|
||||
if (!h) return -1;
|
||||
try {
|
||||
bool crop = false;
|
||||
if (cropFlag == 1) crop = true;
|
||||
h->SetCrop(crop);
|
||||
return 1;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error setting crop flag for file player: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) void SetFilePlayerDisplayResolution(ANSCENTER::ANSFILEPLAYER** Handle, int width, int height) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return;
|
||||
try {
|
||||
(*Handle)->SetDisplayResolution(width, height);
|
||||
} catch (...) { }
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
#ifndef ANSFILEPLAYER_H
|
||||
#define ANSFILEPLAYER_H
|
||||
#define ANSFILEPLAYER_API __declspec(dllexport)
|
||||
#include <iostream>
|
||||
#include <cstdint>
|
||||
#include "ANSLicense.h"
|
||||
#include "LabVIEWHeader/extcode.h"
|
||||
#include <vector>
|
||||
#include "sys_inc.h"
|
||||
#include "rtsp_cln.h"
|
||||
#include "hqueue.h"
|
||||
#include "http.h"
|
||||
#include "http_parse.h"
|
||||
#include "video_decoder.h"
|
||||
#include "audio_decoder.h"
|
||||
#include "file_player.h"
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <atomic>
|
||||
#include <turbojpeg.h>
|
||||
|
||||
namespace ANSCENTER
|
||||
{
|
||||
class ANSFILEPLAYER_API ANSFILEPLAYER
|
||||
{
|
||||
public:
|
||||
std::unique_ptr<CFilePlayer> _playerClient = std::make_unique<CFilePlayer>();
|
||||
protected:
|
||||
std::string _url;
|
||||
double _imageRotateDeg = 0;
|
||||
cv::Mat _pLastFrame;
|
||||
std::string _lastJpegImage;
|
||||
int _imageWidth = 0, _imageHeight = 0;
|
||||
int64_t _pts = 0;
|
||||
bool _isPlaying = false;
|
||||
std::recursive_mutex _mutex;
|
||||
|
||||
// Display/inference split (same pattern as ANSVideoPlayer)
|
||||
int _displayWidth = 0;
|
||||
int _displayHeight = 0;
|
||||
cv::Mat _inferenceImage; // full-res, no display resize
|
||||
public:
|
||||
// Public: accessed by extern "C" GetFilePlayerCVImage for NV12/CUDA registry attachment
|
||||
cv::Mat _inferenceCloneCurr; // current clone for registry (keeps data alive)
|
||||
cv::Mat _inferenceClonePrev; // previous clone (keeps data alive until next frame)
|
||||
|
||||
ANSFILEPLAYER();
|
||||
~ANSFILEPLAYER() noexcept;
|
||||
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
|
||||
[[nodiscard]] bool Setup();
|
||||
[[nodiscard]] bool Reconnect();
|
||||
[[nodiscard]] bool Start();
|
||||
void SetBBox(cv::Rect bbox);
|
||||
void SetCrop(bool crop);
|
||||
[[nodiscard]] bool IsPaused();
|
||||
[[nodiscard]] bool IsPlaying();
|
||||
[[nodiscard]] bool IsRecording();
|
||||
[[nodiscard]] bool Stop();
|
||||
void Destroy();
|
||||
void EnableAudio(bool status);
|
||||
void SetAudioVolume(int volume);
|
||||
void SetImageRotate(double mode) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_imageRotateDeg = mode;
|
||||
if (mode > 360) _imageRotateDeg = 360;
|
||||
};
|
||||
void SetDisplayResolution(int width, int height);
|
||||
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
|
||||
[[nodiscard]] cv::Mat GetInferenceImage();
|
||||
[[nodiscard]] std::string GetJpegImage(int& width, int& height, int64_t& pts);
|
||||
[[nodiscard]] std::string GetJpegStringImage(int& width, int& height, int64_t& pts) {
|
||||
return GetJpegImage(width, height, pts);
|
||||
}
|
||||
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
|
||||
public:
|
||||
void CheckLicense();
|
||||
SPDLogger& _logger = SPDLogger::GetInstance("ANSFilePlayer", false);
|
||||
bool _licenseValid{ false };
|
||||
std::string _licenseKey;
|
||||
};
|
||||
}
|
||||
extern "C" __declspec(dllexport) int CreateANSFilePlayerHandle(ANSCENTER::ANSFILEPLAYER* *Handle, const char* licenseKey, const char* url);
|
||||
extern "C" __declspec(dllexport) int ReleaseANSFilePlayerHandle(ANSCENTER::ANSFILEPLAYER* *Handle);
|
||||
extern "C" __declspec(dllexport) int GetFilePlayerImage(ANSCENTER::ANSFILEPLAYER* *Handle, int& width, int& height, int64_t & timeStamp, LStrHandle jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetFilePlayerStrImage(ANSCENTER::ANSFILEPLAYER* *Handle, int& width, int& height, int64_t & timeStamp, std::string & jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetFilePlayerCVImage(ANSCENTER::ANSFILEPLAYER** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
|
||||
extern "C" __declspec(dllexport) int StartFilePlayer(ANSCENTER::ANSFILEPLAYER* *Handle);
|
||||
extern "C" __declspec(dllexport) int ReconnectFilePlayer(ANSCENTER::ANSFILEPLAYER* *Handle);
|
||||
extern "C" __declspec(dllexport) int StopFilePlayer(ANSCENTER::ANSFILEPLAYER* *Handle);
|
||||
extern "C" __declspec(dllexport) int IsFilePlayerPaused(ANSCENTER::ANSFILEPLAYER* *Handle);
|
||||
extern "C" __declspec(dllexport) int IsFilePlayerRunning(ANSCENTER::ANSFILEPLAYER* *Handle);
|
||||
extern "C" __declspec(dllexport) int IsFilePlayerRecording(ANSCENTER::ANSFILEPLAYER* *Handle);
|
||||
extern "C" __declspec(dllexport) void SetFilePlayerAudioVolume(ANSCENTER::ANSFILEPLAYER* *Handle, int volume);
|
||||
extern "C" __declspec(dllexport) void EnableFilePlayerAudioVolume(ANSCENTER::ANSFILEPLAYER* *Handle, int status);
|
||||
extern "C" __declspec(dllexport) void SetFilePlayerImageRotation(ANSCENTER::ANSFILEPLAYER* *Handle, double rotationAngle);
|
||||
extern "C" __declspec(dllexport) int SetBBoxFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle, int x, int y, int width, int height);
|
||||
extern "C" __declspec(dllexport) int SetCropFlagFilePlayer(ANSCENTER::ANSFILEPLAYER** Handle, int cropFlag);
|
||||
extern "C" __declspec(dllexport) void SetFilePlayerDisplayResolution(ANSCENTER::ANSFILEPLAYER** Handle, int width, int height);
|
||||
|
||||
// V2 functions: accept uint64_t handleVal by value (LabVIEW-safe)
|
||||
extern "C" __declspec(dllexport) int GetFilePlayerImage_V2(uint64_t handleVal, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetFilePlayerCVImage_V2(uint64_t handleVal, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
|
||||
extern "C" __declspec(dllexport) int StartFilePlayer_V2(uint64_t handleVal);
|
||||
extern "C" __declspec(dllexport) int ReconnectFilePlayer_V2(uint64_t handleVal);
|
||||
extern "C" __declspec(dllexport) int StopFilePlayer_V2(uint64_t handleVal);
|
||||
extern "C" __declspec(dllexport) int IsFilePlayerPaused_V2(uint64_t handleVal);
|
||||
extern "C" __declspec(dllexport) int IsFilePlayerRunning_V2(uint64_t handleVal);
|
||||
extern "C" __declspec(dllexport) int IsFilePlayerRecording_V2(uint64_t handleVal);
|
||||
extern "C" __declspec(dllexport) void SetFilePlayerAudioVolume_V2(uint64_t handleVal, int volume);
|
||||
extern "C" __declspec(dllexport) void EnableFilePlayerAudioVolume_V2(uint64_t handleVal, int status);
|
||||
extern "C" __declspec(dllexport) void SetFilePlayerImageRotation_V2(uint64_t handleVal, double rotationAngle);
|
||||
extern "C" __declspec(dllexport) int SetBBoxFilePlayer_V2(uint64_t handleVal, int x, int y, int width, int height);
|
||||
extern "C" __declspec(dllexport) int SetCropFlagFilePlayer_V2(uint64_t handleVal, int cropFlag);
|
||||
#endif
|
||||
@@ -0,0 +1,762 @@
|
||||
#include "ANSFilePlayer_CV.h"
|
||||
#include "ANSMatRegistry.h"
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
#include <libswscale/swscale.h>
|
||||
#include <libavutil/imgutils.h>
|
||||
#include <libavutil/frame.h>
|
||||
#include "media_codec.h"
|
||||
static bool ansfileplayerLicenceValid = false;
|
||||
// Global once_flag to protect license checking
|
||||
static std::once_flag ansfileplayerLicenseOnceFlag;
|
||||
namespace ANSCENTER {
|
||||
#define CHECK_CUDA(call) do { \
|
||||
cudaError_t err = call; \
|
||||
if (err != cudaSuccess) { \
|
||||
std::cout<<"CUDA error: " + std::string(cudaGetErrorString(err)); \
|
||||
} \
|
||||
} while (0)
|
||||
#define CHECK_NVJPEG(call) do { \
|
||||
nvjpegStatus_t status = call; \
|
||||
if (status != NVJPEG_STATUS_SUCCESS) { \
|
||||
std::cout <<"nvJPEG error: " + std::to_string(status); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
ANSFILEPLAYER_CV::ANSFILEPLAYER_CV() :_stopThread(std::make_shared<std::atomic<bool>>(false))
|
||||
{
|
||||
_resWidth = 0;
|
||||
_resHeight = 0;
|
||||
m_bPaused = false;
|
||||
_crop = false;
|
||||
_isPlaying = false;
|
||||
_lastJpegImage = "";
|
||||
_bbox = cv::Rect(0, 0, 0, 0);
|
||||
_jpegCompressor = nullptr;
|
||||
_previousPTS = 0;
|
||||
_totalFrames = 0;
|
||||
}
|
||||
ANSFILEPLAYER_CV::~ANSFILEPLAYER_CV() noexcept {
|
||||
try {
|
||||
StopTimerThread();
|
||||
Destroy();
|
||||
}
|
||||
catch (...) {}
|
||||
}
|
||||
void ANSFILEPLAYER_CV::Destroy() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
_previousImage.release();
|
||||
_lastJpegImage = "";
|
||||
_isPlaying = false;
|
||||
_resWidth = 0;
|
||||
_resHeight = 0;
|
||||
_currentFrame = 0;
|
||||
_previousPTS = 0;
|
||||
if (cap.isOpened()) {
|
||||
cap.release();
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogError("ANSFILEPLAYER_CV::Destroy. Exception:", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
catch (...) {
|
||||
_logger.LogError("ANSFILEPLAYER_CV::Destroy.", "Unknown exception", __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
|
||||
static void VerifyGlobalANSFPLicense(const std::string& licenseKey) {
|
||||
try {
|
||||
ansfileplayerLicenceValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, 1007, "ANSCV");//Default productId=1005
|
||||
if (!ansfileplayerLicenceValid) { // we also support ANSTS license
|
||||
ansfileplayerLicenceValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, 1003, "ANSVIS");//Default productId=1003 (ANSVIS)
|
||||
}
|
||||
if (!ansfileplayerLicenceValid) { // we also support ANSTS license
|
||||
ansfileplayerLicenceValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, 1008, "ANSTS");//Default productId=1008 (ANSTS)
|
||||
}
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
ansfileplayerLicenceValid = false;
|
||||
}
|
||||
}
|
||||
void ANSFILEPLAYER_CV::CheckLicense() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
// Check once globally
|
||||
std::call_once(ansfileplayerLicenseOnceFlag, [this]() {
|
||||
VerifyGlobalANSFPLicense(_licenseKey);
|
||||
});
|
||||
|
||||
// Update this instance's local license flag
|
||||
_licenseValid = ansfileplayerLicenceValid;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogFatal("ANSFILEPLAYER_CV::CheckLicense. Error:", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool ANSFILEPLAYER_CV::Init(std::string licenseKey, std::string url) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_licenseKey = licenseKey;
|
||||
CheckLicense();
|
||||
if (!_licenseValid) {
|
||||
_logger.LogError("ANSFILEPLAYER_CV::Init.", "Invalid license", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
_url = url;
|
||||
return Setup();
|
||||
}
|
||||
bool ANSFILEPLAYER_CV::Setup() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_currentFrame = 0;
|
||||
if (cap.isOpened()) return true;
|
||||
cap.open(_url, cv::CAP_FFMPEG); // USe FFMPEG for better codec support
|
||||
if (!cap.isOpened()) {
|
||||
cap.open(_url, cv::CAP_ANY);
|
||||
if (!cap.isOpened())return false;
|
||||
}
|
||||
try {
|
||||
_resWidth = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_WIDTH));
|
||||
_resHeight = static_cast<int>(cap.get(cv::CAP_PROP_FRAME_HEIGHT));
|
||||
_totalFrames = static_cast<int64_t>(cap.get(cv::CAP_PROP_FRAME_COUNT));
|
||||
_fps = cap.get(cv::CAP_PROP_FPS);
|
||||
cap.set(cv::CAP_PROP_POS_FRAMES, _currentFrame);
|
||||
if (_fps > 0) {
|
||||
if (_timerThread && _timerThread->joinable()) return true; // Exit if the thread is already running
|
||||
int interval_ms = static_cast<int>(1000 / _fps);
|
||||
*_stopThread = false;
|
||||
_timerThread = std::make_shared<std::thread>(&ANSFILEPLAYER_CV::StartTimer, this, _stopThread, interval_ms);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger.LogError("ANSFILEPLAYER_CV::Setup:", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
void ANSFILEPLAYER_CV::StartTimer(std::shared_ptr<std::atomic<bool>> stopFlag, int interval_ms) {
|
||||
while (!stopFlag->load()) {
|
||||
std::unique_lock<std::mutex> lock(_cvMutex);
|
||||
// Wait for the specified interval or until stopFlag is set
|
||||
if (_cv.wait_for(lock, std::chrono::milliseconds(interval_ms), [&]() {
|
||||
return stopFlag->load();
|
||||
})) {
|
||||
break; // Exit the loop if stopFlag is true
|
||||
}
|
||||
lock.unlock(); // Unlock to avoid deadlocks in further operations
|
||||
int delayMs = 0;
|
||||
int actualSleepTime = interval_ms;
|
||||
{
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
auto start = std::chrono::system_clock::now();
|
||||
if (_isPlaying) {
|
||||
try {
|
||||
_currentFrame = static_cast<int64_t>(cap.get(cv::CAP_PROP_POS_FRAMES));
|
||||
// Add check for _stopThread before blocking operations
|
||||
if (!cap.grab() || _currentFrame >= _totalFrames - 1) {
|
||||
if (cap.isOpened()) {
|
||||
cap.release();
|
||||
cap.open(_url, cv::CAP_FFMPEG); // USe FFMPEG for better codec support
|
||||
if (!cap.isOpened()) {
|
||||
cap.open(_url, cv::CAP_ANY);
|
||||
}
|
||||
}
|
||||
if (cap.isOpened()) {
|
||||
cap.set(cv::CAP_PROP_POS_FRAMES, 0);
|
||||
_currentFrame = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogError("ANSFILEPLAYER_CV::StartTimer. Exception occurred:", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
auto end = std::chrono::system_clock::now();
|
||||
delayMs = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
|
||||
// Calculate actual sleep time based on any delay introduced
|
||||
actualSleepTime = interval_ms - delayMs;
|
||||
if (actualSleepTime < 1) actualSleepTime = 1;
|
||||
}
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(actualSleepTime));
|
||||
}
|
||||
}
|
||||
void ANSFILEPLAYER_CV::StopTimerThread() {
|
||||
*_stopThread = true; // Signal to stop the thread
|
||||
_cv.notify_all(); // Notify the condition variable to unblock the thread
|
||||
if (_timerThread && _timerThread->joinable()) {
|
||||
_timerThread->join(); // Join the timer thread to ensure cleanup
|
||||
}
|
||||
}
|
||||
bool ANSFILEPLAYER_CV::Reconnect() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
_currentFrame = 0;
|
||||
if (cap.isOpened()) {
|
||||
cap.release();
|
||||
}
|
||||
Setup();
|
||||
_isPlaying = cap.isOpened() && cap.grab();
|
||||
return _isPlaying;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogError("ANSFILEPLAYER_CV::Reconnect. Exception occurred:", e.what(), __FILE__, __LINE__);
|
||||
_currentFrame = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
bool ANSFILEPLAYER_CV::Start() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
if (!cap.isOpened()) {
|
||||
cap.open(_url, cv::CAP_FFMPEG);
|
||||
if (!cap.isOpened()) {
|
||||
cap.open(_url, cv::CAP_ANY);
|
||||
}
|
||||
if (!cap.isOpened()) {
|
||||
this->_logger.LogError("ANSFILEPLAYER_CV::Start. Exception occurred:", "Failed to open video source: ", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
if (!cap.set(cv::CAP_PROP_POS_FRAMES, _currentFrame)) {
|
||||
this->_logger.LogError("ANSFILEPLAYER_CV::Start. Exception occurred:", "Warning: Unable to seek to frame", __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogError("ANSFILEPLAYER_CV::Start. Exception occurred:", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
_isPlaying = cap.isOpened() && cap.grab();
|
||||
return _isPlaying;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogError("ANSFILEPLAYER_CV::Start. Exception occurred:", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool ANSFILEPLAYER_CV::Stop() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
if (cap.isOpened()) {
|
||||
try {
|
||||
double frame_pos = cap.get(cv::CAP_PROP_POS_FRAMES);
|
||||
if (frame_pos >= 0) {
|
||||
_currentFrame = static_cast<int64_t>(frame_pos);
|
||||
}
|
||||
else {
|
||||
_currentFrame = 0;
|
||||
this->_logger.LogError("ANSFILEPLAYER_CV::Stop. Exception occurred:", "Unable to retrieve current frame position", __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogError("ANSFILEPLAYER_CV::Stop. Exception occurred:", e.what(), __FILE__, __LINE__);
|
||||
_currentFrame = 0;
|
||||
}
|
||||
cap.release();
|
||||
}
|
||||
_isPlaying = false;
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogError("ANSFILEPLAYER_CV::Stop. Exception occurred:", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
void ANSFILEPLAYER_CV::SetBBox(cv::Rect bbox) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_bbox = bbox;
|
||||
}
|
||||
void ANSFILEPLAYER_CV::SetCrop(bool crop) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_crop = crop;
|
||||
}
|
||||
bool ANSFILEPLAYER_CV::IsPaused() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
return !cap.isOpened();
|
||||
}
|
||||
bool ANSFILEPLAYER_CV::IsPlaying() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
return cap.isOpened();
|
||||
}
|
||||
bool ANSFILEPLAYER_CV::IsRecording() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
return false;// do not support recording for webcam
|
||||
}
|
||||
|
||||
|
||||
cv::Mat ANSFILEPLAYER_CV::GetImage(int& width, int& height, int64_t& pts) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
|
||||
if (!_isPlaying) {
|
||||
if (!_previousImage.empty()) {
|
||||
width = _previousImage.cols;
|
||||
height = _previousImage.rows;
|
||||
pts = _previousPTS;
|
||||
return _previousImage;
|
||||
}
|
||||
return cv::Mat();
|
||||
}
|
||||
|
||||
try {
|
||||
if (!cap.isOpened()) {
|
||||
if (!_previousImage.empty()) {
|
||||
width = _previousImage.cols;
|
||||
height = _previousImage.rows;
|
||||
pts = _previousPTS;
|
||||
return _previousImage;
|
||||
}
|
||||
return cv::Mat();
|
||||
}
|
||||
|
||||
cv::Mat frame;
|
||||
if (!cap.read(frame) || frame.empty()) {
|
||||
// Return last good frame if available
|
||||
if (!_previousImage.empty()) {
|
||||
width = _previousImage.cols;
|
||||
height = _previousImage.rows;
|
||||
pts = _previousPTS;
|
||||
return _previousImage;
|
||||
}
|
||||
return cv::Mat();
|
||||
}
|
||||
|
||||
cv::Mat result;
|
||||
|
||||
// Apply cropping if enabled
|
||||
if (_crop) {
|
||||
// Validate and clamp crop region
|
||||
_bbox.x = std::clamp(_bbox.x, 0, frame.cols - 1);
|
||||
_bbox.y = std::clamp(_bbox.y, 0, frame.rows - 1);
|
||||
_bbox.width = std::min(_bbox.width, frame.cols - _bbox.x);
|
||||
_bbox.height = std::min(_bbox.height, frame.rows - _bbox.y);
|
||||
|
||||
if (_bbox.width > 0 && _bbox.height > 0) {
|
||||
// CRITICAL: Clone to avoid dangling reference
|
||||
result = frame(_bbox).clone();
|
||||
}
|
||||
else {
|
||||
result = frame.clone();
|
||||
}
|
||||
}
|
||||
else {
|
||||
result = frame.clone();
|
||||
}
|
||||
|
||||
// Apply rotation if specified
|
||||
if (_imageRotateDeg > 0) {
|
||||
// Fast path for 90-degree rotations
|
||||
if (std::abs(_imageRotateDeg - 90.0) < 0.01) {
|
||||
cv::rotate(result, result, cv::ROTATE_90_CLOCKWISE);
|
||||
}
|
||||
else if (std::abs(_imageRotateDeg - 180.0) < 0.01) {
|
||||
cv::rotate(result, result, cv::ROTATE_180);
|
||||
}
|
||||
else if (std::abs(_imageRotateDeg - 270.0) < 0.01) {
|
||||
cv::rotate(result, result, cv::ROTATE_90_COUNTERCLOCKWISE);
|
||||
}
|
||||
else {
|
||||
// Arbitrary angle rotation
|
||||
const cv::Point2f center(result.cols / 2.0f, result.rows / 2.0f);
|
||||
cv::Mat rotationMatrix = cv::getRotationMatrix2D(center, _imageRotateDeg, 1.0);
|
||||
|
||||
cv::Mat rotated;
|
||||
// Use INTER_LINEAR instead of INTER_CUBIC (2-3x faster, minimal quality loss)
|
||||
cv::warpAffine(result, rotated, rotationMatrix, result.size(),
|
||||
cv::INTER_LINEAR, cv::BORDER_CONSTANT, cv::Scalar());
|
||||
result = rotated;
|
||||
}
|
||||
}
|
||||
|
||||
// Update PTS (presentation timestamp)
|
||||
if (_previousPTS < INT64_MAX) {
|
||||
_previousPTS++;
|
||||
}
|
||||
else {
|
||||
_previousPTS = 0;
|
||||
}
|
||||
|
||||
// Update cached frame
|
||||
_previousImage = result;
|
||||
|
||||
// Set output parameters
|
||||
width = result.cols;
|
||||
height = result.rows;
|
||||
pts = _previousPTS;
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (const cv::Exception& e) {
|
||||
this->_logger.LogError("ANSFILEPLAYER_CV::GetImage. OpenCV exception:", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogError("ANSFILEPLAYER_CV::GetImage. Exception:", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
catch (...) {
|
||||
this->_logger.LogError("ANSFILEPLAYER_CV::GetImage. Unknown exception", "", __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
return cv::Mat();
|
||||
}
|
||||
|
||||
void ANSFILEPLAYER_CV::EnableAudio(bool status) {
|
||||
// please support audio enable for webcam
|
||||
}
|
||||
void ANSFILEPLAYER_CV::SetAudioVolume(int volume) {
|
||||
// support audio volumne
|
||||
}
|
||||
std::string ANSFILEPLAYER_CV::encodeJpegString(const cv::Mat& img, int quality) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
if (!_isPlaying) return _lastJpegImage;
|
||||
unsigned char* jpegBuf = nullptr;
|
||||
tjhandle jpegCompressor = nullptr;
|
||||
try {
|
||||
// Initialize TurboJPEG compressor
|
||||
jpegCompressor = tjInitCompress();
|
||||
if (!jpegCompressor) {
|
||||
this->_logger.LogError("Failed to initialize TurboJPEG compressor.", tjGetErrorStr(), __FILE__, __LINE__);
|
||||
return _lastJpegImage;
|
||||
}
|
||||
|
||||
int maxBufferSize = img.cols * img.rows * 3;
|
||||
jpegBuf = tjAlloc(maxBufferSize);
|
||||
if (!jpegBuf) {
|
||||
this->_logger.LogError("Failed to allocate memory for JPEG buffer.", tjGetErrorStr(), __FILE__, __LINE__);
|
||||
tjDestroy(jpegCompressor);
|
||||
return _lastJpegImage;
|
||||
}
|
||||
|
||||
long unsigned int jpegSize = maxBufferSize;
|
||||
int subsamp = TJSAMP_444;
|
||||
int pixelFormat = img.channels() == 3 ? TJPF_BGR : TJPF_GRAY;
|
||||
|
||||
// Compress the image
|
||||
if (tjCompress2(jpegCompressor, img.data, img.cols, 0, img.rows, pixelFormat,
|
||||
&jpegBuf, &jpegSize, subsamp, quality, TJFLAG_FASTDCT) != 0) {
|
||||
this->_logger.LogError("Compression error:", tjGetErrorStr(), __FILE__, __LINE__);
|
||||
tjFree(jpegBuf);
|
||||
tjDestroy(jpegCompressor);
|
||||
return _lastJpegImage;
|
||||
}
|
||||
|
||||
// Create string from JPEG buffer
|
||||
std::string jpegString(reinterpret_cast<char*>(jpegBuf), jpegSize);
|
||||
_lastJpegImage = jpegString;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogError("Exception occurred:", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
// Cleanup resources
|
||||
if (jpegBuf) tjFree(jpegBuf);
|
||||
if (jpegCompressor) tjDestroy(jpegCompressor);
|
||||
|
||||
return _lastJpegImage;
|
||||
}
|
||||
std::string ANSFILEPLAYER_CV::MatToBinaryData(const cv::Mat& image) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
// Check if the image is empty or has invalid data
|
||||
if (!_isPlaying) return _lastJpegImage;
|
||||
if (image.empty() || !image.data || !image.u) {
|
||||
return _lastJpegImage;
|
||||
}
|
||||
try {
|
||||
// Encode the image to a memory buffer
|
||||
return encodeJpegString(image, 85);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogFatal("ANSFILEPLAYER_CV::MatToBinaryData. Exception occurred:", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
catch (...) {
|
||||
this->_logger.LogFatal("ANSFILEPLAYER_CV::MatToBinaryData.", "Unknown exception occurred.", __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
// Return an empty string in case of failure
|
||||
return _lastJpegImage;
|
||||
}
|
||||
std::string ANSFILEPLAYER_CV::GetJpegStringImage(int& width, int& height, int64_t& pts) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
if (!_isPlaying) return _lastJpegImage;
|
||||
try {
|
||||
cv::Mat image = GetImage(width, height, pts);
|
||||
std::string jpegString = MatToBinaryData(image);
|
||||
image.release();
|
||||
return jpegString;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogError("ANSFILEPLAYER_CV::GetJpegStringImage. Exception occurred:", e.what(), __FILE__, __LINE__);
|
||||
return _lastJpegImage;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) int CreateANSFileCVPlayerHandle(ANSCENTER::ANSFILEPLAYER_CV** Handle, const char* licenseKey, const char* url) {
|
||||
if (!Handle || !licenseKey || !url) return -1;
|
||||
try {
|
||||
auto ptr = std::make_unique<ANSCENTER::ANSFILEPLAYER_CV>();
|
||||
bool result = ptr->Init(licenseKey, url);
|
||||
if (result) {
|
||||
*Handle = ptr.release();
|
||||
extern void anscv_unregister_handle(void*);
|
||||
extern void anscv_register_handle(void*, void(*)(void*));
|
||||
anscv_register_handle(*Handle, [](void* p) {
|
||||
auto* h = static_cast<ANSCENTER::ANSFILEPLAYER_CV*>(p);
|
||||
try { h->Stop(); } catch (...) {}
|
||||
try { h->Destroy(); } catch (...) {}
|
||||
try { delete h; } catch (...) {}
|
||||
});
|
||||
return 1;
|
||||
}
|
||||
*Handle = nullptr;
|
||||
return 0;
|
||||
}
|
||||
catch (...) { return 0; }
|
||||
}
|
||||
extern "C" __declspec(dllexport) int ReleaseANSFileCVPlayerHandle(ANSCENTER::ANSFILEPLAYER_CV** Handle) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
extern void anscv_unregister_handle(void*);
|
||||
anscv_unregister_handle(*Handle);
|
||||
// Destructor calls Destroy() � no need to call it explicitly (avoids double-destroy)
|
||||
std::unique_ptr<ANSCENTER::ANSFILEPLAYER_CV> ptr(*Handle);
|
||||
*Handle = nullptr;
|
||||
return 0;
|
||||
}
|
||||
catch (...) {
|
||||
if (Handle) *Handle = nullptr;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int GetFileCVPlayerStrImage(ANSCENTER::ANSFILEPLAYER_CV** Handle, int& width, int& height, int64_t& timeStamp, std::string& jpegImage) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
jpegImage = (*Handle)->GetJpegStringImage(width, height, timeStamp);
|
||||
if (!jpegImage.empty()) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error getting image data from FilePlayer client: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int GetFileCVPlayerImage(ANSCENTER::ANSFILEPLAYER_CV** Handle, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
std::string jpegString = (*Handle)->GetJpegStringImage(width, height, timeStamp);
|
||||
int size = jpegString.length();
|
||||
if (size > 0) {
|
||||
MgErr error;
|
||||
// Resize the jpegImage handle to hold the image data
|
||||
error = DSSetHandleSize(jpegImage, sizeof(int32) + size * sizeof(uChar));
|
||||
// Check if resizing the handle was successful
|
||||
if (error == noErr) {
|
||||
// Set the size of the image in the handle
|
||||
(*jpegImage)->cnt = size;
|
||||
// Use memcpy to copy the data from the std::string to the LStrHandle's str buffer
|
||||
memcpy((*jpegImage)->str, jpegString.c_str(), size);
|
||||
// Return success
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
// Return failure if there was an error in resizing the handle
|
||||
std::cerr << "Error resizing jpegImage handle: " << error << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// If the JPEG image string is empty, return failure
|
||||
std::cerr << "No image data retrieved from FilePlayer client." << std::endl;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error getting image data from FilePlayer client: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int GetFileCVPlayerCVImage(ANSCENTER::ANSFILEPLAYER_CV** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image) {
|
||||
if (!Handle || !(*Handle) || !image) {
|
||||
std::cerr << "Error: Invalid input parameters in GetFilePlayerCVImage." << std::endl;
|
||||
return -1;
|
||||
}
|
||||
try {
|
||||
cv::Mat img = (*Handle)->GetImage(width, height, timeStamp);
|
||||
if (img.empty()) {
|
||||
return 0; // No valid image retrieved
|
||||
}
|
||||
// anscv_mat_replace has its own internal registry_mutex � no global mutex needed
|
||||
anscv_mat_replace(image, std::move(img));
|
||||
|
||||
return 1; // Success
|
||||
}
|
||||
catch (const cv::Exception& e) {
|
||||
std::cerr << "OpenCV exception in GetFilePlayerCVImage: " << e.what() << std::endl;
|
||||
return -2;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Exception in GetFilePlayerCVImage: " << e.what() << std::endl;
|
||||
return -2;
|
||||
}
|
||||
catch (...) {
|
||||
std::cerr << "Unknown exception in GetFilePlayerCVImage" << std::endl;
|
||||
return -2;
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" __declspec(dllexport) int StartFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
bool result = (*Handle)->Start();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error starting file player: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int ReconnectFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
bool result = (*Handle)->Reconnect();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error reconnecting file player: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int StopFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
bool result = (*Handle)->Stop();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error stopping file player: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int IsFileCVPlayerPaused(ANSCENTER::ANSFILEPLAYER_CV** Handle) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
bool result = (*Handle)->IsPaused();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error checking if file player is paused: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int IsFileCVPlayerRunning(ANSCENTER::ANSFILEPLAYER_CV** Handle) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
bool result = (*Handle)->IsPlaying();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error checking if file player is running: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int IsFileCVPlayerRecording(ANSCENTER::ANSFILEPLAYER_CV** Handle) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
bool result = (*Handle)->IsRecording();
|
||||
if (result) return 1;
|
||||
else return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error checking if file player is recording: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) void SetFileCVPlayerAudioVolume(ANSCENTER::ANSFILEPLAYER_CV** Handle, int volume)
|
||||
{
|
||||
if (Handle == nullptr || *Handle == nullptr) return;
|
||||
try {
|
||||
(*Handle)->SetAudioVolume(volume);
|
||||
}
|
||||
catch (...) {}
|
||||
}
|
||||
extern "C" __declspec(dllexport) void EnableFileCVPlayerAudioVolume(ANSCENTER::ANSFILEPLAYER_CV** Handle, int status)
|
||||
{
|
||||
if (Handle == nullptr || *Handle == nullptr) return;
|
||||
try {
|
||||
bool audioStatus = false;
|
||||
if (status == 1)audioStatus = true;
|
||||
(*Handle)->EnableAudio(audioStatus);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error enabling audio for file player: " << e.what() << std::endl;
|
||||
}
|
||||
catch (...) {}
|
||||
}
|
||||
extern "C" __declspec(dllexport) void SetFileCVPlayerImageRotation(ANSCENTER::ANSFILEPLAYER_CV** Handle, double rotationAngle) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return;
|
||||
try {
|
||||
(*Handle)->SetImageRotate(rotationAngle);
|
||||
}
|
||||
catch (...) {}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int SetBBoxFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle, int x, int y, int width, int height) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
cv::Rect bbox(x, y, width, height);
|
||||
(*Handle)->SetBBox(bbox);
|
||||
return 1;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error setting bounding box for file player: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
extern "C" __declspec(dllexport) int SetCropFlagFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle, int cropFlag) {
|
||||
if (Handle == nullptr || *Handle == nullptr) return -1;
|
||||
try {
|
||||
bool crop = false;
|
||||
if (cropFlag == 1)crop = true;
|
||||
(*Handle)->SetCrop(crop);
|
||||
return 1;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "Error setting crop flag for file player: " << e.what() << std::endl;
|
||||
return -1;
|
||||
}
|
||||
catch (...) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
#ifndef ANSFILEPLAYER_H
|
||||
#define ANSFILEPLAYER_H
|
||||
#define ANSFILEPLAYER_API __declspec(dllexport)
|
||||
#include <iostream>
|
||||
#include <cstdint>
|
||||
#include "ANSLicense.h"
|
||||
#include "LabVIEWHeader/extcode.h"
|
||||
#include <vector>
|
||||
#include "sys_inc.h"
|
||||
#include "rtsp_cln.h"
|
||||
#include "hqueue.h"
|
||||
#include "http.h"
|
||||
#include "http_parse.h"
|
||||
#include "video_decoder.h"
|
||||
#include "audio_decoder.h"
|
||||
#include "file_player.h"
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <atomic>
|
||||
#include <turbojpeg.h>
|
||||
|
||||
namespace ANSCENTER
|
||||
{
|
||||
class ANSFILEPLAYER_API ANSFILEPLAYER_CV
|
||||
{
|
||||
protected:
|
||||
cv::VideoCapture cap;
|
||||
int _resWidth;
|
||||
int _resHeight;
|
||||
int64_t _totalFrames;
|
||||
int64_t _currentFrame;
|
||||
int64_t _previousPTS;
|
||||
double _fps;
|
||||
bool _isPlaying;
|
||||
std::string _lastJpegImage;
|
||||
//int64_t _previousPTS;
|
||||
cv::Mat _previousImage;
|
||||
bool m_bPaused;
|
||||
int _imageRotateDeg = 0;
|
||||
std::string _url;
|
||||
cv::Rect _bbox;
|
||||
bool _crop;
|
||||
std::recursive_mutex _mutex;
|
||||
std::shared_ptr<std::atomic<bool>> _stopThread;
|
||||
std::shared_ptr<std::thread> _timerThread;
|
||||
std::mutex _cvMutex;
|
||||
std::condition_variable _cv;
|
||||
//EngineType engineType;
|
||||
//// Initialize nvJPEG structures
|
||||
//nvjpegHandle_t nv_handle;
|
||||
//nvjpegEncoderState_t nv_enc_state;
|
||||
//nvjpegEncoderParams_t nv_enc_params;
|
||||
//cudaStream_t stream;
|
||||
tjhandle _jpegCompressor;
|
||||
|
||||
public:
|
||||
ANSFILEPLAYER_CV();
|
||||
~ANSFILEPLAYER_CV() noexcept;
|
||||
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
|
||||
[[nodiscard]] bool Setup();
|
||||
[[nodiscard]] bool Reconnect();
|
||||
[[nodiscard]] bool Start();
|
||||
[[nodiscard]] bool IsPaused();
|
||||
[[nodiscard]] bool IsPlaying();
|
||||
[[nodiscard]] bool IsRecording();
|
||||
void SetBBox(cv::Rect bbox);
|
||||
void SetCrop(bool crop);
|
||||
[[nodiscard]] bool Stop();
|
||||
void Destroy();
|
||||
void EnableAudio(bool status);
|
||||
void SetAudioVolume(int volume);
|
||||
void SetImageRotate(int mode) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_imageRotateDeg = mode;
|
||||
if (mode > 360) _imageRotateDeg = 360;
|
||||
};
|
||||
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
|
||||
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
|
||||
[[nodiscard]] std::string GetJpegStringImage(int& width, int& height, int64_t& pts);
|
||||
public:
|
||||
void CheckLicense();
|
||||
SPDLogger& _logger = SPDLogger::GetInstance("ANSFilePlayer_CV", false);
|
||||
bool _licenseValid{ false };
|
||||
//std::once_flag licenseOnceFlag;
|
||||
std::string _licenseKey;
|
||||
private:
|
||||
std::string encodeJpegString(const cv::Mat& img, int quality = 80);
|
||||
//std::string encodeMatToJpegWithNvJPEG(const cv::Mat& inputMat, int quality = 80);
|
||||
//void uploadPlanarBGRToGPU(const cv::Mat& inputMat, unsigned char** data);
|
||||
void StartTimer(std::shared_ptr<std::atomic<bool>> stopFlag, int interval_ms);
|
||||
void StopTimerThread();
|
||||
};
|
||||
}
|
||||
extern "C" __declspec(dllexport) int CreateANSFileCVPlayerHandle(ANSCENTER::ANSFILEPLAYER_CV** Handle, const char* licenseKey, const char* url);
|
||||
extern "C" __declspec(dllexport) int ReleaseANSFileCVPlayerHandle(ANSCENTER::ANSFILEPLAYER_CV** Handle);
|
||||
extern "C" __declspec(dllexport) int GetFileCVPlayerImage(ANSCENTER::ANSFILEPLAYER_CV** Handle, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetFileCVPlayerStrImage(ANSCENTER::ANSFILEPLAYER_CV** Handle, int& width, int& height, int64_t& timeStamp, std::string& jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetFileCVPlayerCVImage(ANSCENTER::ANSFILEPLAYER_CV** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
|
||||
extern "C" __declspec(dllexport) int StartFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle);
|
||||
extern "C" __declspec(dllexport) int ReconnectFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle);
|
||||
extern "C" __declspec(dllexport) int StopFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle);
|
||||
extern "C" __declspec(dllexport) int IsFileCVPlayerPaused(ANSCENTER::ANSFILEPLAYER_CV** Handle);
|
||||
extern "C" __declspec(dllexport) int IsFileCVPlayerRunning(ANSCENTER::ANSFILEPLAYER_CV** Handle);
|
||||
extern "C" __declspec(dllexport) int IsFileCVPlayerRecording(ANSCENTER::ANSFILEPLAYER_CV** Handle);
|
||||
extern "C" __declspec(dllexport) void SetFileCVPlayerAudioVolume(ANSCENTER::ANSFILEPLAYER_CV** Handle, int volume);
|
||||
extern "C" __declspec(dllexport) void EnableFileCVPlayerAudioVolume(ANSCENTER::ANSFILEPLAYER_CV** Handle, int status);
|
||||
extern "C" __declspec(dllexport) void SetFileCVPlayerImageRotation(ANSCENTER::ANSFILEPLAYER_CV** Handle, double rotationAngle);
|
||||
extern "C" __declspec(dllexport) int SetBBoxFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle, int x, int y, int width, int height);
|
||||
extern "C" __declspec(dllexport) int SetCropFlagFileCVPlayer(ANSCENTER::ANSFILEPLAYER_CV** Handle, int cropFlag);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,212 @@
|
||||
#pragma once
|
||||
// ANSGpuFrameOps.h — FFmpeg-aware convenience functions for ANSGpuFrameRegistry.
|
||||
//
|
||||
// This header requires FFmpeg headers (libavutil/frame.h) and provides
|
||||
// typed attach/invalidate/remove operations that handle av_frame_clone/free.
|
||||
//
|
||||
// NEW DESIGN: Instead of storing AVFrame* references (which lock NVDEC surfaces),
|
||||
// we snapshot the CPU NV12 planes into malloc'd buffers and release the AVFrames
|
||||
// immediately. This prevents decoder surface pool exhaustion when many clones
|
||||
// hold references to the same frame.
|
||||
//
|
||||
// Include this in ANSCV/ANSRTSP (which link FFmpeg). For projects without
|
||||
// FFmpeg (ANSODEngine), include ANSGpuFrameRegistry.h directly and use
|
||||
// gpu_frame_lookup() + the GpuFrameData plane pointers.
|
||||
|
||||
#include "ANSGpuFrameRegistry.h"
|
||||
|
||||
extern "C" {
|
||||
#include "libavutil/frame.h"
|
||||
}
|
||||
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace anscv_gpu_ops {
|
||||
namespace detail {
|
||||
|
||||
// Snapshot NV12 Y and UV planes from an AVFrame into malloc'd buffers.
|
||||
// Returns true on success. Caller owns the output buffers.
|
||||
inline bool snapshotNV12Planes(const AVFrame* nv12,
|
||||
uint8_t*& outY, int& outYLinesize,
|
||||
uint8_t*& outUV, int& outUVLinesize,
|
||||
int& outWidth, int& outHeight) {
|
||||
if (!nv12 || !nv12->data[0] || !nv12->data[1])
|
||||
return false;
|
||||
|
||||
outWidth = nv12->width;
|
||||
outHeight = nv12->height;
|
||||
outYLinesize = nv12->width; // Packed (no alignment padding)
|
||||
outUVLinesize = nv12->width; // UV interleaved: width bytes per row
|
||||
|
||||
size_t yBytes = static_cast<size_t>(outYLinesize) * outHeight;
|
||||
size_t uvBytes = static_cast<size_t>(outUVLinesize) * (outHeight / 2);
|
||||
|
||||
outY = static_cast<uint8_t*>(std::malloc(yBytes));
|
||||
outUV = static_cast<uint8_t*>(std::malloc(uvBytes));
|
||||
|
||||
if (!outY || !outUV) {
|
||||
std::free(outY);
|
||||
std::free(outUV);
|
||||
outY = nullptr;
|
||||
outUV = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Copy line-by-line (source may have padding via linesize > width)
|
||||
const int srcYLinesize = nv12->linesize[0];
|
||||
const int srcUVLinesize = nv12->linesize[1];
|
||||
|
||||
for (int row = 0; row < outHeight; ++row) {
|
||||
std::memcpy(outY + row * outYLinesize,
|
||||
nv12->data[0] + row * srcYLinesize,
|
||||
outWidth);
|
||||
}
|
||||
for (int row = 0; row < outHeight / 2; ++row) {
|
||||
std::memcpy(outUV + row * outUVLinesize,
|
||||
nv12->data[1] + row * srcUVLinesize,
|
||||
outWidth);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
} // namespace anscv_gpu_ops
|
||||
|
||||
// Attach NV12/YUV frame keyed by cv::Mat* pointer.
|
||||
// Snapshots CPU NV12 planes into owned malloc'd buffers, then releases the AVFrame.
|
||||
// TAKES OWNERSHIP of nv12 — caller must NOT av_frame_free after this call.
|
||||
inline void gpu_frame_attach(cv::Mat* mat, AVFrame* nv12, int gpuIdx, int64_t pts) {
|
||||
if (!mat || !nv12) return;
|
||||
|
||||
GpuFrameData data{};
|
||||
data.gpuIndex = gpuIdx;
|
||||
data.pts = pts;
|
||||
data.pixelFormat = nv12->format;
|
||||
data.width = nv12->width;
|
||||
data.height = nv12->height;
|
||||
|
||||
// Snapshot NV12 planes to owned buffers
|
||||
bool ok = anscv_gpu_ops::detail::snapshotNV12Planes(
|
||||
nv12,
|
||||
data.cpuYPlane, data.cpuYLinesize,
|
||||
data.cpuUvPlane, data.cpuUvLinesize,
|
||||
data.width, data.height);
|
||||
|
||||
// Keep legacy pointers for backward compat during transition
|
||||
data.yPlane = data.cpuYPlane;
|
||||
data.uvPlane = data.cpuUvPlane;
|
||||
data.yLinesize = data.cpuYLinesize;
|
||||
data.uvLinesize = data.cpuUvLinesize;
|
||||
|
||||
// Store AVFrame for legacy cleanup (will be freed by drain_pending)
|
||||
data.avframe = nv12;
|
||||
|
||||
void* old = ANSGpuFrameRegistry::instance().attach(mat, std::move(data));
|
||||
if (old) {
|
||||
AVFrame* oldFrame = static_cast<AVFrame*>(old);
|
||||
av_frame_free(&oldFrame);
|
||||
}
|
||||
|
||||
// Free stale entries evicted by TTL or previous attach
|
||||
auto pending = ANSGpuFrameRegistry::instance().drain_pending();
|
||||
for (void* p : pending) {
|
||||
AVFrame* stale = static_cast<AVFrame*>(p);
|
||||
av_frame_free(&stale);
|
||||
}
|
||||
}
|
||||
|
||||
// Attach CUDA HW frame — keeps CUDA device pointers for zero-copy inference.
|
||||
// TAKES OWNERSHIP of cudaFrame AND cpuNV12 — caller must NOT av_frame_free after.
|
||||
//
|
||||
// Primary path: yPlane/uvPlane point to CUDA device pointers from the cloned
|
||||
// AVFrame (data[0]/data[1]). The cloned AVFrame keeps the NVDEC surface alive
|
||||
// until gpu_frame_remove() is called after inference. With 4 cameras each
|
||||
// holding ~1 surface, this uses 4 of NVDEC's 25-32 surface pool — safe.
|
||||
//
|
||||
// Fallback: cpuYPlane/cpuUvPlane hold CPU-side NV12 snapshot for cross-GPU
|
||||
// inference (when decode GPU != inference GPU, CUDA device ptrs aren't
|
||||
// accessible from another GPU context).
|
||||
inline void gpu_frame_attach_cuda(cv::Mat* mat, AVFrame* cudaFrame, int gpuIdx, int64_t pts,
|
||||
AVFrame* cpuNV12 = nullptr) {
|
||||
if (!mat || !cudaFrame) return;
|
||||
|
||||
GpuFrameData data{};
|
||||
data.gpuIndex = gpuIdx;
|
||||
data.pts = pts;
|
||||
data.width = cudaFrame->width;
|
||||
data.height = cudaFrame->height;
|
||||
data.pixelFormat = 23; // AV_PIX_FMT_NV12 — the underlying sw_format
|
||||
|
||||
// Primary: CUDA device pointers from NVDEC (zero-copy on same GPU)
|
||||
data.isCudaDevicePtr = true;
|
||||
data.yPlane = cudaFrame->data[0]; // CUDA device ptr: Y plane
|
||||
data.uvPlane = cudaFrame->data[1]; // CUDA device ptr: UV plane
|
||||
data.yLinesize = cudaFrame->linesize[0];
|
||||
data.uvLinesize = cudaFrame->linesize[1];
|
||||
|
||||
// Fallback: snapshot CPU NV12 for cross-GPU inference
|
||||
if (cpuNV12) {
|
||||
anscv_gpu_ops::detail::snapshotNV12Planes(
|
||||
cpuNV12,
|
||||
data.cpuYPlane, data.cpuYLinesize,
|
||||
data.cpuUvPlane, data.cpuUvLinesize,
|
||||
data.width, data.height);
|
||||
}
|
||||
|
||||
// Store AVFrames for cleanup (cudaFrame keeps NVDEC surface alive)
|
||||
data.avframe = cudaFrame;
|
||||
data.cpuAvframe = cpuNV12;
|
||||
|
||||
void* old = ANSGpuFrameRegistry::instance().attach(mat, std::move(data));
|
||||
if (old) {
|
||||
AVFrame* oldFrame = static_cast<AVFrame*>(old);
|
||||
av_frame_free(&oldFrame);
|
||||
}
|
||||
|
||||
auto pending = ANSGpuFrameRegistry::instance().drain_pending();
|
||||
for (void* p : pending) {
|
||||
AVFrame* stale = static_cast<AVFrame*>(p);
|
||||
av_frame_free(&stale);
|
||||
}
|
||||
}
|
||||
|
||||
// Release entry by cv::Mat* and free any returned AVFrames. Safe if not in map (no-op).
|
||||
inline void gpu_frame_remove(cv::Mat* mat) {
|
||||
if (!mat) return;
|
||||
|
||||
ANSGpuFrameRegistry::instance().release(mat);
|
||||
|
||||
// Free any AVFrames that became pending from this release or prior eviction
|
||||
auto pending = ANSGpuFrameRegistry::instance().drain_pending();
|
||||
for (void* p : pending) {
|
||||
AVFrame* stale = static_cast<AVFrame*>(p);
|
||||
av_frame_free(&stale);
|
||||
}
|
||||
|
||||
// Free any GPU device pointers that became pending
|
||||
auto gpuPending = gpu_frame_drain_gpu_pending();
|
||||
// NOTE: cudaFree requires CUDA context — caller must be on a CUDA-capable thread.
|
||||
// If not, these will leak. In practice, gpu_frame_remove is called from ANSCV
|
||||
// camera threads which do have CUDA context.
|
||||
// For safety, we skip cudaFree here and let NV12PreprocessHelper handle it.
|
||||
// The GPU pointers are tracked in the budget and will be accounted for.
|
||||
(void)gpuPending;
|
||||
}
|
||||
|
||||
// Alias for remove — used in ANSCV mutating functions to drop stale GPU data.
|
||||
inline void gpu_frame_invalidate(cv::Mat* mat) {
|
||||
gpu_frame_remove(mat);
|
||||
}
|
||||
|
||||
// Run TTL eviction + drain pending. Call periodically from camera threads.
|
||||
inline void gpu_frame_evict_stale() {
|
||||
ANSGpuFrameRegistry::instance().evictStaleFrames();
|
||||
|
||||
auto pending = ANSGpuFrameRegistry::instance().drain_pending();
|
||||
for (void* p : pending) {
|
||||
AVFrame* stale = static_cast<AVFrame*>(p);
|
||||
av_frame_free(&stale);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
// ANSGpuFrameRegistry.cpp — Process-wide singleton, compiled into ANSCV.dll.
|
||||
//
|
||||
// On Windows, header-only singletons (static local in inline function) create
|
||||
// separate instances in each DLL. Since gpu_frame_attach() runs in ANSCV.dll
|
||||
// and gpu_frame_lookup() runs in ANSODEngine.dll, we need a single shared
|
||||
// instance. This file:
|
||||
// 1. Defines resolveProcessWide() which owns the canonical singleton.
|
||||
// 2. Exports a C function so other DLLs can find it via GetProcAddress.
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define NOMINMAX
|
||||
#include <windows.h>
|
||||
#include "ANSGpuFrameRegistry.h"
|
||||
|
||||
// ANSCV.dll owns the process-wide singleton.
|
||||
ANSGpuFrameRegistry* ANSGpuFrameRegistry::resolveProcessWide() {
|
||||
static ANSGpuFrameRegistry reg;
|
||||
return ®
|
||||
}
|
||||
|
||||
// Exported so other DLLs (ANSODEngine, etc.) can find this instance at runtime.
|
||||
extern "C" __declspec(dllexport)
|
||||
ANSGpuFrameRegistry* ANSGpuFrameRegistry_GetInstance() {
|
||||
return &ANSGpuFrameRegistry::instance();
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,111 @@
|
||||
#ifndef ANSMJPEG_H
|
||||
#define ANSMJPEG_H
|
||||
#define ANSMJPEG_API __declspec(dllexport)
|
||||
#include <iostream>
|
||||
#include "ANSLicense.h"
|
||||
#include "LabVIEWHeader/extcode.h"
|
||||
#include <vector>
|
||||
#include "sys_inc.h"
|
||||
#include "http_mjpeg_cln.h"
|
||||
#include "hqueue.h"
|
||||
#include "http.h"
|
||||
#include "http_parse.h"
|
||||
#include "video_decoder.h"
|
||||
#include "audio_decoder.h"
|
||||
#include "http_mjpeg_player.h"
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
namespace ANSCENTER
|
||||
{
|
||||
class ANSMJPEG_API ANSMJPEGClient
|
||||
{
|
||||
protected:
|
||||
std::unique_ptr<CHttpMjpegPlayer> _playerClient = std::make_unique<CHttpMjpegPlayer>();
|
||||
std::string _username;
|
||||
std::string _password;
|
||||
std::string _url;
|
||||
bool _useFullURL;
|
||||
int _imageRotateDeg = 0;
|
||||
int _displayWidth = 0; // 0 = no resize (return original resolution)
|
||||
int _displayHeight = 0;
|
||||
cv::Mat _pLastFrame;
|
||||
std::string _lastJpegImage;
|
||||
int _imageWidth, _imageHeight;
|
||||
int64_t _pts;
|
||||
bool _isPlaying;
|
||||
std::recursive_mutex _mutex;
|
||||
public:
|
||||
ANSMJPEGClient();
|
||||
~ANSMJPEGClient() noexcept;
|
||||
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
|
||||
[[nodiscard]] bool Init(std::string licenseKey, std::string username, std::string password, std::string url);
|
||||
[[nodiscard]] bool Setup();
|
||||
[[nodiscard]] bool Reconnect();
|
||||
[[nodiscard]] bool Start();
|
||||
[[nodiscard]] bool Pause();
|
||||
[[nodiscard]] bool IsPaused();
|
||||
[[nodiscard]] bool IsPlaying();
|
||||
[[nodiscard]] bool IsRecording();
|
||||
[[nodiscard]] bool Stop();
|
||||
void Destroy();
|
||||
void EnableAudio(bool status);
|
||||
void SetAudioVolume(int volume);
|
||||
bool areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2);
|
||||
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
|
||||
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
|
||||
[[nodiscard]] std::string GetJpegImage(int& width, int& height, int64_t& pts);
|
||||
void SetImageRotate(int mode) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_imageRotateDeg = mode;
|
||||
if (mode > 360) _imageRotateDeg = 360;
|
||||
};
|
||||
void SetBBox(cv::Rect bbox);
|
||||
void SetCrop(bool crop);
|
||||
static void SetMaxHWDecoders(int maxDecoders);
|
||||
static int AutoConfigureHWDecoders(int maxPerGpuOverride = 0);
|
||||
void SetHWDecoding(int hwMode, int preferredGpu = -1);
|
||||
bool IsHWDecodingActive();
|
||||
int GetHWDecodingGpuIndex();
|
||||
void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize)
|
||||
void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display)
|
||||
AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free)
|
||||
AVFrame* GetCudaHWFrame(); // Returns CUDA HW frame (device ptrs) for zero-copy inference
|
||||
bool IsCudaHWAccel(); // true when decoder uses CUDA (NV12 stays in GPU VRAM)
|
||||
public:
|
||||
void CheckLicense();
|
||||
SPDLogger& _logger = SPDLogger::GetInstance("ANSMJPEG", false);
|
||||
bool _licenseValid{ false };
|
||||
std::string _licenseKey;
|
||||
//std::once_flag licenseOnceFlag;
|
||||
|
||||
};
|
||||
}
|
||||
extern "C" __declspec(dllexport) int CreateANSMJPEGHandle(ANSCENTER::ANSMJPEGClient * *Handle, const char* licenseKey, const char* username, const char* password, const char* url);
|
||||
extern "C" __declspec(dllexport) int ReleaseANSMJPEGHandle(ANSCENTER::ANSMJPEGClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int GetMJPEGImage(ANSCENTER::ANSMJPEGClient * *Handle, int& width, int& height, int64_t & timeStamp, LStrHandle jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetMJPEGStrImage(ANSCENTER::ANSMJPEGClient * *Handle, int& width, int& height, int64_t & timeStamp, std::string & jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetMJPEGCVImage(ANSCENTER::ANSMJPEGClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
|
||||
|
||||
extern "C" __declspec(dllexport) int StartMJPEG(ANSCENTER::ANSMJPEGClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int ReconnectMJPEG(ANSCENTER::ANSMJPEGClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int StopMJPEG(ANSCENTER::ANSMJPEGClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int PauseMJPEG(ANSCENTER::ANSMJPEGClient** Handle);
|
||||
|
||||
extern "C" __declspec(dllexport) int IsMJPEGPaused(ANSCENTER::ANSMJPEGClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int IsMJPEGRunning(ANSCENTER::ANSMJPEGClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int IsMJPEGRecording(ANSCENTER::ANSMJPEGClient * *Handle);
|
||||
extern "C" __declspec(dllexport) void SetMJPEGAudioVolume(ANSCENTER::ANSMJPEGClient * *Handle, int volume);
|
||||
extern "C" __declspec(dllexport) void EnableMJPEGAudioVolume(ANSCENTER::ANSMJPEGClient * *Handle, int status);
|
||||
extern "C" __declspec(dllexport) void SetMJPEGImageRotation(ANSCENTER::ANSMJPEGClient * *Handle, double rotationAngle);
|
||||
extern "C" __declspec(dllexport) int SetBBoxMJPEG(ANSCENTER::ANSMJPEGClient** Handle, int x, int y, int width, int height);
|
||||
extern "C" __declspec(dllexport) int SetCropFlagMJPEG(ANSCENTER::ANSMJPEGClient** Handle, int cropFlag);
|
||||
extern "C" __declspec(dllexport) void SetMJPEGMaxHWDecoders(int maxDecoders);
|
||||
extern "C" __declspec(dllexport) int AutoConfigureMJPEGHWDecoders(int maxPerGpuOverride);
|
||||
extern "C" __declspec(dllexport) void SetMJPEGHWDecoding(ANSCENTER::ANSMJPEGClient** Handle, int hwMode, int preferredGpu = -1);
|
||||
extern "C" __declspec(dllexport) int IsMJPEGHWDecodingActive(ANSCENTER::ANSMJPEGClient** Handle);
|
||||
extern "C" __declspec(dllexport) int GetMJPEGHWDecodingGpuIndex(ANSCENTER::ANSMJPEGClient** Handle);
|
||||
extern "C" __declspec(dllexport) void SetMJPEGImageQuality(ANSCENTER::ANSMJPEGClient** Handle, int mode);
|
||||
extern "C" __declspec(dllexport) void SetMJPEGDisplayResolution(ANSCENTER::ANSMJPEGClient** Handle, int width, int height);
|
||||
#endif
|
||||
@@ -0,0 +1,116 @@
|
||||
#pragma once
|
||||
// ANSMatRegistry.h — Thread-safe cv::Mat* pointer registry.
|
||||
// Prevents double-free crashes when LabVIEW calls ReleaseImage on a pointer
|
||||
// that a stream source (RTSP, VideoPlayer, etc.) has already freed and replaced.
|
||||
//
|
||||
// Every cv::Mat* allocated through these functions is tracked in a global set.
|
||||
// Deletion only proceeds if the pointer is still in the set, guaranteeing each
|
||||
// pointer is freed exactly once regardless of which thread calls delete first.
|
||||
//
|
||||
// Usage:
|
||||
// Allocate: cv::Mat* p = anscv_mat_new(std::move(img));
|
||||
// Free: anscv_mat_delete(&p); // safe, no-op if already freed
|
||||
// Swap: anscv_mat_replace(&p, std::move(newImg)); // atomic old-delete + new-alloc
|
||||
|
||||
#include <opencv2/core/mat.hpp>
|
||||
#include <mutex>
|
||||
#include <unordered_set>
|
||||
#include "ANSGpuFrameOps.h"
|
||||
|
||||
namespace anscv {
|
||||
namespace detail {
|
||||
|
||||
inline std::mutex& registry_mutex() {
|
||||
static std::mutex m;
|
||||
return m;
|
||||
}
|
||||
|
||||
inline std::unordered_set<cv::Mat*>& registry() {
|
||||
static std::unordered_set<cv::Mat*> s;
|
||||
return s;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
} // namespace anscv
|
||||
|
||||
// Allocate a new cv::Mat on the heap and register it.
|
||||
inline cv::Mat* anscv_mat_new(cv::Mat&& src) {
|
||||
cv::Mat* p = new cv::Mat(std::move(src));
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(anscv::detail::registry_mutex());
|
||||
anscv::detail::registry().insert(p);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
inline cv::Mat* anscv_mat_new(const cv::Mat& src) {
|
||||
cv::Mat* p = new cv::Mat(src);
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(anscv::detail::registry_mutex());
|
||||
anscv::detail::registry().insert(p);
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
// Delete a cv::Mat* only if it is still in the registry.
|
||||
// Returns true if deleted, false if pointer was null or already freed.
|
||||
// Thread-safe: the pointer is removed from the registry AND nulled AND deleted
|
||||
// all inside the lock, so no other thread can race on the same pointer.
|
||||
inline bool anscv_mat_delete(cv::Mat** pp) {
|
||||
if (!pp || !(*pp))
|
||||
return false;
|
||||
cv::Mat* toDelete = nullptr;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(anscv::detail::registry_mutex());
|
||||
// Re-check under lock — another thread may have nulled *pp
|
||||
if (!*pp)
|
||||
return false;
|
||||
auto& reg = anscv::detail::registry();
|
||||
auto it = reg.find(*pp);
|
||||
if (it == reg.end()) {
|
||||
// Not in registry — already freed by another thread.
|
||||
*pp = nullptr;
|
||||
return false;
|
||||
}
|
||||
toDelete = *pp;
|
||||
reg.erase(it);
|
||||
*pp = nullptr; // Null the caller's pointer while still under lock
|
||||
}
|
||||
// Release GPU frame data (refcount--, frees NV12+GPU cache when refcount=0)
|
||||
gpu_frame_remove(toDelete);
|
||||
// Safe to delete outside lock: pointer is removed from registry and
|
||||
// *pp is nullptr, so no other thread can reach toDelete.
|
||||
delete toDelete;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Atomic replace: delete old image (if still registered), allocate new, assign to *pp.
|
||||
// The old pointer is only deleted if it is in the registry (prevents double-free
|
||||
// when LabVIEW already freed it via anscv_mat_delete).
|
||||
inline void anscv_mat_replace(cv::Mat** pp, cv::Mat&& newImg) {
|
||||
cv::Mat* newPtr = new cv::Mat(std::move(newImg));
|
||||
cv::Mat* toDelete = nullptr;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(anscv::detail::registry_mutex());
|
||||
auto& reg = anscv::detail::registry();
|
||||
if (pp && *pp) {
|
||||
auto it = reg.find(*pp);
|
||||
if (it != reg.end()) {
|
||||
toDelete = *pp;
|
||||
reg.erase(it);
|
||||
}
|
||||
// If not in registry, another thread already freed it — skip delete
|
||||
}
|
||||
*pp = newPtr;
|
||||
reg.insert(newPtr);
|
||||
}
|
||||
if (toDelete) {
|
||||
// Release GPU frame ref for old Mat (safe with refcounting —
|
||||
// only frees NV12/GPU cache when refcount reaches 0)
|
||||
gpu_frame_remove(toDelete);
|
||||
delete toDelete;
|
||||
}
|
||||
|
||||
// Periodically run TTL eviction (piggybacked on camera thread activity)
|
||||
gpu_frame_evict_stale();
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,164 @@
|
||||
#ifndef ANSOPENCV_H
|
||||
#define ANSOPENCV_H
|
||||
#define ANSOPENCV_API __declspec(dllexport)
|
||||
#include <iostream>
|
||||
#include "ANSLicense.h"
|
||||
#include "LabVIEWHeader/extcode.h"
|
||||
#include <vector>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
#define MUTEX_TIMEOUT_MS 45000
|
||||
namespace fs = std::filesystem;
|
||||
namespace ANSCENTER
|
||||
{
|
||||
std::string CompressJpegToString(const cv::Mat& image, int quality);
|
||||
struct DetectionObject
|
||||
{
|
||||
int classId{ 0 };
|
||||
int trackId{ 0 };
|
||||
std::string className{};
|
||||
float confidence{ 0.0 };
|
||||
cv::Rect box{};
|
||||
cv::Mat mask{}; //Json string mask ="point1.x,point1.y,...."
|
||||
std::vector<float> kps{}; // Pose exsimate keypoint
|
||||
std::string extraInfo; // More information such as facial recognition
|
||||
};
|
||||
|
||||
class TurboJpegCompressor {
|
||||
public:
|
||||
TurboJpegCompressor();
|
||||
~TurboJpegCompressor() noexcept;
|
||||
// Delete copy constructor and assignment operator
|
||||
TurboJpegCompressor(const TurboJpegCompressor&) = delete;
|
||||
TurboJpegCompressor& operator=(const TurboJpegCompressor&) = delete;
|
||||
// Your original logic with minimal optimizations
|
||||
[[nodiscard]] std::string compress(const cv::Mat& image, int quality);
|
||||
private:
|
||||
void* _handle = nullptr;
|
||||
unsigned char* _buffer = nullptr;
|
||||
unsigned long _bufferSize = 0;
|
||||
};
|
||||
/// <summary>
|
||||
/// // ANSOPENCV class provides various image processing functionalities using OpenCV and ANS Center SDK.
|
||||
/// </summary>
|
||||
class ANSOPENCV_API ANSOPENCV
|
||||
{
|
||||
public:
|
||||
[[nodiscard]] bool Init(std::string licenseKey);
|
||||
void ImageResize(const cv::Mat& inputFrame, int width, int height, cv::Mat& outputFrame);
|
||||
void ImageResizeWithRatio(const cv::Mat& inputFrame, int width, cv::Mat& outputFrame);
|
||||
[[nodiscard]] cv::Mat BlurObjects(const cv::Mat& image, const std::vector<cv::Rect>& objects);
|
||||
[[nodiscard]] cv::Mat BlurBackground(const cv::Mat& image, const std::vector<cv::Rect>& objects);
|
||||
[[nodiscard]] cv::Mat ToGray(const cv::Mat& image);
|
||||
[[nodiscard]] cv::Mat ImageDenoise(const cv::Mat& image);
|
||||
[[nodiscard]] cv::Mat ImageRepair(const cv::Mat& image);
|
||||
[[nodiscard]] cv::Mat ImageCrop(const cv::Mat& image,const cv::Rect& ROI, int originalImageSize = 0);
|
||||
[[nodiscard]] cv::Mat ImageResizeV2(const cv::Mat& image, int resizeWidth, int orginalImageSize = 0);
|
||||
[[nodiscard]] std::string QRDecoder(const cv::Mat& image);
|
||||
[[nodiscard]] std::string QRDecoderWithBBox(const cv::Mat& image, int maxImageSize, const std::vector<cv::Rect>& bBox);
|
||||
|
||||
[[nodiscard]] std::string MatToBase64(const cv::Mat& image);
|
||||
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
|
||||
[[nodiscard]] std::vector<cv::Rect> GetBoundingBoxes(std::string strBBoxes);
|
||||
[[nodiscard]] std::string VectorDetectionToJsonString(const std::vector<DetectionObject>& dets);
|
||||
[[nodiscard]] std::string PatternMatches(cv::Mat& image, cv::Mat& templateImage, double threshold);
|
||||
[[nodiscard]] cv::Mat ImageDarkEnhancement(const cv::Mat& img, double brightnessScaleFactor=1.5);
|
||||
[[nodiscard]] cv::Mat ImageContrastEnhancement(const cv::Mat& img);
|
||||
[[nodiscard]] cv::Mat ImageWhiteBalance(const cv::Mat& img);
|
||||
|
||||
[[nodiscard]] cv::Mat RotateImage(const cv::Mat& image, double angle);
|
||||
[[nodiscard]] cv::Mat FlipImage(const cv::Mat& image, int flipCode);//flipCode = 0: Vertical flip (around the x-axis). flipCode = 1: Horizontal flip (around the y-axis). flipCode = -1: Both axes, flipping the image horizontally and vertically.
|
||||
[[nodiscard]] cv::Mat ShiftImage(const cv::Mat& image, int shiftX, int shiftY);
|
||||
[[nodiscard]] cv::Mat AddGaussianNoise(const cv::Mat& image, double mean, double stddev);
|
||||
[[nodiscard]] cv::Mat AddSaltAndPepperNoise(const cv::Mat& image, double amount);
|
||||
[[nodiscard]] cv::Mat AddSpeckleNoise(const cv::Mat& image, double stddev);
|
||||
|
||||
static void InitCameraNetwork();
|
||||
static void DeinitCameraNetwork();
|
||||
|
||||
static cv::Mat resizeImageToFit(const cv::Mat& inputImage, int maxWidth, int maxHeight, int& newWidth, int& newHeight);
|
||||
static bool resizeImage(cv::Mat& inputImage, int resizeWidth, int orginalImageSize=0);
|
||||
static bool cropImage(cv::Mat& inputImage, const cv::Rect& resizeROI, int originalImageSize=0);
|
||||
static bool ImagesToMP4(const std::string& imageFolder, const std::string& outputVideoPath, int targetDurationSec);
|
||||
|
||||
private:
|
||||
void CheckLicense();
|
||||
double CalculateIoU(const cv::Rect& box1, const cv::Rect& box2);
|
||||
void NonMaximumSuppression(std::vector<DetectionObject>& detectedObjects, double iouThreshold);
|
||||
std::string EncodeJpegString(const cv::Mat& img, int quality);
|
||||
SPDLogger& _logger = SPDLogger::GetInstance("ANSCV", false);
|
||||
std::string _licenseKey;
|
||||
std::recursive_mutex _mutex;
|
||||
|
||||
//std::once_flag licenseOnceFlag; // For one-time license check
|
||||
bool _licenseValid = false;
|
||||
public:
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
extern "C" __declspec(dllexport) int CreateANSCVHandle(ANSCENTER::ANSOPENCV **Handle, const char* licenseKey);
|
||||
extern "C" __declspec(dllexport) int ReleaseANSCVHandle(ANSCENTER::ANSOPENCV **Handle);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageResize(ANSCENTER::ANSOPENCV * *Handle,unsigned char* inputImage, unsigned int bufferLength, int width, int height, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageResizeWithRatio(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, int width,LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageToBase64(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageToGray(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageDenoise(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageRepair(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageAutoWhiteBalance(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageBrightEnhance(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, double brightnessScaleFactor, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageContrastEnhance(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageCrop(ANSCENTER::ANSOPENCV** Handle, unsigned char* inputImage, unsigned int bufferLength, int x, int y, int width, int height, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_GetImageSize(ANSCENTER::ANSOPENCV** Handle, unsigned char* inputImage, unsigned int bufferLength, LStrHandle imageSize);
|
||||
extern "C" __declspec(dllexport) int ANSCV_GetImageSizeFromImageFile(ANSCENTER::ANSOPENCV** Handle, const char* imageFilePath, LStrHandle imageSize);
|
||||
|
||||
extern "C" __declspec(dllexport) int ANSCV_BlurObjects(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, const char* strBboxes, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_BlurBackground(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, const char* strBboxes, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_QRDecoder(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, LStrHandle detectedQRText);
|
||||
extern "C" __declspec(dllexport) int ANSCV_QRDecoderCV(ANSCENTER::ANSOPENCV** Handle, const cv::Mat &image, std::string& detectedQRText);
|
||||
|
||||
extern "C" __declspec(dllexport) int ANSCV_PatternMatchs(ANSCENTER::ANSOPENCV * *Handle, unsigned char* inputImage, unsigned int bufferLength, const char* templateFilePath, double theshold, LStrHandle detectedMatchedLocations);
|
||||
|
||||
|
||||
extern "C" __declspec(dllexport) int ANSCV_RotateImage(ANSCENTER::ANSOPENCV** Handle, unsigned char* inputImage, unsigned int bufferLength, double angle, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_FlipImage(ANSCENTER::ANSOPENCV** Handle, unsigned char* inputImage, unsigned int bufferLength, int flipCode, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ShiftImage(ANSCENTER::ANSOPENCV** Handle, unsigned char* inputImage, unsigned int bufferLength, int shiftX, int shiftY, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_AddGaussianNoise(ANSCENTER::ANSOPENCV** Handle, unsigned char* inputImage, unsigned int bufferLength, double mean, double stddev, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_AddSaltAndPepperNoise(ANSCENTER::ANSOPENCV** Handle, unsigned char* inputImage, unsigned int bufferLength, double amount, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_AddSpeckleNoise(ANSCENTER::ANSOPENCV** Handle, unsigned char* inputImage, unsigned int bufferLength, double stddev, LStrHandle outputImage);
|
||||
|
||||
extern "C" __declspec(dllexport) void ANSCV_InitCameraResource();
|
||||
extern "C" __declspec(dllexport) void ANSCV_FreeCameraResource();
|
||||
extern "C" __declspec(dllexport) int ANSCV_ResizeImage_Static(unsigned char* inputImage, unsigned int bufferLength, int width,int height, int& newWidth, int&newHeight, LStrHandle outputImage);
|
||||
|
||||
|
||||
// Image reference management functions
|
||||
extern "C" __declspec(dllexport) int ANSCV_CloneImage_S(cv::Mat **imageIn, cv::Mat** imageOut);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ReleaseImage_S(cv::Mat** imageIn);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ReSizeImage_S(cv::Mat** imageIn, int width, int originalImageSize = 0);
|
||||
extern "C" __declspec(dllexport) int ANSCV_CropImage_S(cv::Mat** imageIn, int x, int y, int width, int height, int originalImageSize=0);
|
||||
extern "C" __declspec(dllexport) int ANSCV_GetImage_S(cv::Mat** imageIn, int width, int quality, int& newWidth, int& newHeight, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_GetImage_CPP(cv::Mat** imageIn, int width, int quality, int& newWidth, int& newHeight, std::string& outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_GetImageAndRemoveImgRef_S(cv::Mat** imageIn, int width, int quality, int& newWidth, int& newHeight, LStrHandle outputImage);
|
||||
extern "C" __declspec(dllexport) int ANSCV_GetImageInfo_S(cv::Mat** imageIn, int &width, int& height);
|
||||
extern "C" __declspec(dllexport) int ANSCV_CreateImageFromJpegString_S(unsigned char* inputImage, unsigned int bufferLength,cv::Mat** image);
|
||||
extern "C" __declspec(dllexport) int ANSCV_CreateImageFromFile_S(const char* imageFilePath, cv::Mat** image);
|
||||
// Pre-process image functions
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageAutoWhiteBalance_S(cv::Mat** imageIn);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageBrightEnhance_S(cv::Mat** imageIn, double brightnessScaleFactor);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageContrastEnhance_S(cv::Mat** imageIn);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageDenoise_S(cv::Mat** imageIn);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageRepair_S(cv::Mat** imageIn);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageToGray_S(cv::Mat** imageIn);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageRotate_S(cv::Mat** imageIn, double angle);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageFlip_S(cv::Mat** imageIn, int flipCode);
|
||||
// Post-process image functions
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageBlurObjects_S(cv::Mat** imageIn, const char* strBboxes);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageBlurBackground_S(cv::Mat** imageIn, const char* strBboxes);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImageQRDecoder_S(cv::Mat** imageIn, int maxImageWidth, const char* strBboxes, LStrHandle detectedQRText);
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImagePatternMatchs_S(cv::Mat** imageIn, const char* templateFilePath, double theshold, LStrHandle detectedMatchedLocations);
|
||||
|
||||
|
||||
extern "C" __declspec(dllexport) int ANSCV_ImagesToMP4_S(const char* imageFolder, const char* outputVideoPath, int targetDurationSec);
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,110 @@
|
||||
#ifndef ANSRTMP_H
|
||||
#define ANSRTMP_H
|
||||
#define ANSRTMP_API __declspec(dllexport)
|
||||
#include <iostream>
|
||||
#include "ANSLicense.h"
|
||||
#include "LabVIEWHeader/extcode.h"
|
||||
#include <vector>
|
||||
#include "sys_inc.h"
|
||||
#include "rtmp_cln.h"
|
||||
#include "hqueue.h"
|
||||
#include "http.h"
|
||||
#include "http_parse.h"
|
||||
#include "video_decoder.h"
|
||||
#include "audio_decoder.h"
|
||||
#include "rtmp_player.h"
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
namespace ANSCENTER
|
||||
{
|
||||
class ANSRTMP_API ANSRTMPClient
|
||||
{
|
||||
protected:
|
||||
std::unique_ptr<CRtmpPlayer> _playerClient = std::make_unique<CRtmpPlayer>();
|
||||
|
||||
std::string _username;
|
||||
std::string _password;
|
||||
std::string _url;
|
||||
bool _useFullURL;
|
||||
int _imageRotateDeg = 0;
|
||||
int _displayWidth = 0; // 0 = no resize (return original resolution)
|
||||
int _displayHeight = 0;
|
||||
cv::Mat _pLastFrame;
|
||||
std::string _lastJpegImage;
|
||||
int _imageWidth, _imageHeight;
|
||||
int64_t _pts;
|
||||
bool _isPlaying;
|
||||
std::recursive_mutex _mutex;
|
||||
public:
|
||||
ANSRTMPClient();
|
||||
~ANSRTMPClient() noexcept;
|
||||
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
|
||||
[[nodiscard]] bool Init(std::string licenseKey, std::string username, std::string password, std::string url);
|
||||
[[nodiscard]] bool Setup();
|
||||
[[nodiscard]] bool Reconnect();
|
||||
[[nodiscard]] bool Start();
|
||||
[[nodiscard]] bool IsPaused();
|
||||
[[nodiscard]] bool IsPlaying();
|
||||
[[nodiscard]] bool IsRecording();
|
||||
[[nodiscard]] bool Stop();
|
||||
[[nodiscard]] bool Pause();
|
||||
void Destroy();
|
||||
void EnableAudio(bool status);
|
||||
void SetAudioVolume(int volume);
|
||||
bool areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2);
|
||||
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
|
||||
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
|
||||
[[nodiscard]] std::string GetJpegImage(int& width, int& height, int64_t& pts);
|
||||
void SetImageRotate(int mode) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_imageRotateDeg = mode;
|
||||
if (mode > 360) _imageRotateDeg = 360;
|
||||
};
|
||||
void SetBBox(cv::Rect bbox);
|
||||
void SetCrop(bool crop);
|
||||
static void SetMaxHWDecoders(int maxDecoders);
|
||||
static int AutoConfigureHWDecoders(int maxPerGpuOverride = 0);
|
||||
void SetHWDecoding(int hwMode, int preferredGpu = -1);
|
||||
bool IsHWDecodingActive();
|
||||
int GetHWDecodingGpuIndex();
|
||||
void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize)
|
||||
void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display)
|
||||
AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free)
|
||||
AVFrame* GetCudaHWFrame(); // Returns CUDA HW frame (device ptrs) for zero-copy inference
|
||||
bool IsCudaHWAccel(); // true when decoder uses CUDA (NV12 stays in GPU VRAM)
|
||||
public:
|
||||
void CheckLicense();
|
||||
SPDLogger& _logger = SPDLogger::GetInstance("ANSRTMP", false);
|
||||
bool _licenseValid{ false };
|
||||
std::string _licenseKey;
|
||||
//std::once_flag licenseOnceFlag;
|
||||
|
||||
};
|
||||
}
|
||||
extern "C" __declspec(dllexport) int CreateANSRTMPHandle(ANSCENTER::ANSRTMPClient * *Handle, const char* licenseKey, const char* username, const char* password, const char* url);
|
||||
extern "C" __declspec(dllexport) int ReleaseANSRTMPHandle(ANSCENTER::ANSRTMPClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int GetRTMPImage(ANSCENTER::ANSRTMPClient * *Handle, int& width, int& height, int64_t & timeStamp, LStrHandle jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetRTMPtrImage(ANSCENTER::ANSRTMPClient * *Handle, int& width, int& height, int64_t & timeStamp, std::string & jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetRTMPCVImage(ANSCENTER::ANSRTMPClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
|
||||
extern "C" __declspec(dllexport) int StartRTMP(ANSCENTER::ANSRTMPClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int ReconnectRTMP(ANSCENTER::ANSRTMPClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int StopRTMP(ANSCENTER::ANSRTMPClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int PauseRTMP(ANSCENTER::ANSRTMPClient** Handle);
|
||||
extern "C" __declspec(dllexport) int IsRTMPPaused(ANSCENTER::ANSRTMPClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int IsRTMPRunning(ANSCENTER::ANSRTMPClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int IsRTMPRecording(ANSCENTER::ANSRTMPClient * *Handle);
|
||||
extern "C" __declspec(dllexport) void SetRTMPAudioVolume(ANSCENTER::ANSRTMPClient * *Handle, int volume);
|
||||
extern "C" __declspec(dllexport) void EnableRTMPAudioVolume(ANSCENTER::ANSRTMPClient * *Handle, int status);
|
||||
extern "C" __declspec(dllexport) void SetRTMPImageRotation(ANSCENTER::ANSRTMPClient * *Handle, double rotationAngle);
|
||||
extern "C" __declspec(dllexport) int SetBBoxRTMP(ANSCENTER::ANSRTMPClient** Handle, int x, int y, int width, int height);
|
||||
extern "C" __declspec(dllexport) int SetCropFlagRTMP(ANSCENTER::ANSRTMPClient** Handle, int cropFlag);
|
||||
extern "C" __declspec(dllexport) void SetRTMPMaxHWDecoders(int maxDecoders);
|
||||
extern "C" __declspec(dllexport) int AutoConfigureRTMPHWDecoders(int maxPerGpuOverride);
|
||||
extern "C" __declspec(dllexport) void SetRTMPHWDecoding(ANSCENTER::ANSRTMPClient** Handle, int hwMode, int preferredGpu = -1);
|
||||
extern "C" __declspec(dllexport) int IsRTMPHWDecodingActive(ANSCENTER::ANSRTMPClient** Handle);
|
||||
extern "C" __declspec(dllexport) int GetRTMPHWDecodingGpuIndex(ANSCENTER::ANSRTMPClient** Handle);
|
||||
extern "C" __declspec(dllexport) void SetRTMPImageQuality(ANSCENTER::ANSRTMPClient** Handle, int mode);
|
||||
extern "C" __declspec(dllexport) void SetRTMPDisplayResolution(ANSCENTER::ANSRTMPClient** Handle, int width, int height);
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,111 @@
|
||||
#ifndef ANSRTSP_H
|
||||
#define ANSRTSP_H
|
||||
#define ANSRTSP_API __declspec(dllexport)
|
||||
#include <iostream>
|
||||
#include "ANSLicense.h"
|
||||
#include "LabVIEWHeader/extcode.h"
|
||||
#include <vector>
|
||||
#include "sys_inc.h"
|
||||
#include "rtsp_cln.h"
|
||||
#include "hqueue.h"
|
||||
#include "http.h"
|
||||
#include "http_parse.h"
|
||||
#include "video_decoder.h"
|
||||
#include "audio_decoder.h"
|
||||
#include "rtsp_player.h"
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
namespace ANSCENTER
|
||||
{
|
||||
class ANSRTSP_API ANSRTSPClient
|
||||
{
|
||||
protected:
|
||||
std::unique_ptr<CRtspPlayer> _playerClient = std::make_unique<CRtspPlayer>();
|
||||
|
||||
std::string _username;
|
||||
std::string _password;
|
||||
std::string _url;
|
||||
bool _useFullURL;
|
||||
int _imageRotateDeg = 0;
|
||||
int _displayWidth = 0; // 0 = no resize (return original resolution)
|
||||
int _displayHeight = 0;
|
||||
cv::Mat _pLastFrame;
|
||||
std::string _lastJpegImage;
|
||||
int _imageWidth,_imageHeight;
|
||||
int64_t _pts;
|
||||
bool _isPlaying;
|
||||
std::recursive_mutex _mutex;
|
||||
public:
|
||||
ANSRTSPClient();
|
||||
~ANSRTSPClient() noexcept;
|
||||
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
|
||||
[[nodiscard]] bool Init(std::string licenseKey, std::string username, std::string password, std::string url);
|
||||
[[nodiscard]] bool Setup();
|
||||
[[nodiscard]] bool Reconnect();
|
||||
[[nodiscard]] bool Start();
|
||||
[[nodiscard]] bool IsPaused();
|
||||
[[nodiscard]] bool IsPlaying();
|
||||
[[nodiscard]] bool IsRecording();
|
||||
[[nodiscard]] bool Stop();
|
||||
[[nodiscard]] bool Pause();
|
||||
void Destroy();
|
||||
void SetBBox(cv::Rect bbox);
|
||||
void SetCrop(bool crop);
|
||||
void EnableAudio(bool status);
|
||||
void SetAudioVolume(int volume);
|
||||
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
|
||||
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
|
||||
[[nodiscard]] std::string GetJpegImage(int& width, int& height, int64_t& pts);
|
||||
bool areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2);
|
||||
void SetImageRotate(int mode) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_imageRotateDeg = mode;
|
||||
if (mode > 360) _imageRotateDeg = 360;
|
||||
};
|
||||
static void SetMaxHWDecoders(int maxDecoders);
|
||||
static int AutoConfigureHWDecoders(int maxPerGpuOverride = 0);
|
||||
void SetHWDecoding(int hwMode, int preferredGpu = -1);
|
||||
bool IsHWDecodingActive();
|
||||
int GetHWDecodingGpuIndex();
|
||||
void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize)
|
||||
void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display)
|
||||
AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free)
|
||||
AVFrame* GetCudaHWFrame(); // Returns CUDA HW frame (device ptrs) for zero-copy inference
|
||||
bool IsCudaHWAccel(); // true when decoder uses CUDA (NV12 stays in GPU VRAM)
|
||||
public:
|
||||
void CheckLicense();
|
||||
SPDLogger& _logger = SPDLogger::GetInstance("ANSRTSP", false);
|
||||
bool _licenseValid{ false };
|
||||
std::string _licenseKey;
|
||||
//std::once_flag licenseOnceFlag;
|
||||
|
||||
};
|
||||
}
|
||||
extern "C" __declspec(dllexport) int CreateANSRTSPHandle(ANSCENTER::ANSRTSPClient * *Handle, const char* licenseKey, const char* username, const char* password, const char* url);
|
||||
extern "C" __declspec(dllexport) int ReleaseANSRTSPHandle(ANSCENTER::ANSRTSPClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int GetRTSPImage(ANSCENTER::ANSRTSPClient **Handle, int &width, int &height, int64_t& timeStamp, LStrHandle jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetRTSPStrImage(ANSCENTER::ANSRTSPClient * *Handle, int& width, int& height, int64_t & timeStamp, std::string& jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetRTSPCVImage(ANSCENTER::ANSRTSPClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
|
||||
|
||||
extern "C" __declspec(dllexport) int StartRTSP(ANSCENTER::ANSRTSPClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int ReconnectRTSP(ANSCENTER::ANSRTSPClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int StopRTSP(ANSCENTER::ANSRTSPClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int PauseRTSP(ANSCENTER::ANSRTSPClient** Handle);
|
||||
extern "C" __declspec(dllexport) int IsRTSPPaused(ANSCENTER::ANSRTSPClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int IsRTSPRunning(ANSCENTER::ANSRTSPClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int IsRTSPRecording(ANSCENTER::ANSRTSPClient * *Handle);
|
||||
extern "C" __declspec(dllexport) void SetRTSPAudioVolume(ANSCENTER::ANSRTSPClient * *Handle, int volume);
|
||||
extern "C" __declspec(dllexport) void EnableRTSPAudioVolume(ANSCENTER::ANSRTSPClient * *Handle, int status);
|
||||
extern "C" __declspec(dllexport) void SetRTSPImageRotation(ANSCENTER::ANSRTSPClient * *Handle, double rotationAngle);
|
||||
extern "C" __declspec(dllexport) int SetBBoxRTSP(ANSCENTER::ANSRTSPClient** Handle, int x, int y, int width, int height);
|
||||
extern "C" __declspec(dllexport) int SetCropFlagRTSP(ANSCENTER::ANSRTSPClient** Handle, int cropFlag);
|
||||
extern "C" __declspec(dllexport) void SetMaxHWDecoders(int maxDecoders);
|
||||
extern "C" __declspec(dllexport) int AutoConfigureHWDecoders(int maxPerGpuOverride);
|
||||
extern "C" __declspec(dllexport) void SetRTSPHWDecoding(ANSCENTER::ANSRTSPClient** Handle, int hwMode, int preferredGpu = -1);
|
||||
extern "C" __declspec(dllexport) int IsRTSPHWDecodingActive(ANSCENTER::ANSRTSPClient** Handle);
|
||||
extern "C" __declspec(dllexport) int GetRTSPHWDecodingGpuIndex(ANSCENTER::ANSRTSPClient** Handle);
|
||||
extern "C" __declspec(dllexport) void SetRTSPImageQuality(ANSCENTER::ANSRTSPClient** Handle, int mode);
|
||||
extern "C" __declspec(dllexport) void SetRTSPDisplayResolution(ANSCENTER::ANSRTSPClient** Handle, int width, int height);
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,110 @@
|
||||
#ifndef ANSSRT_H
|
||||
#define ANSSRT_H
|
||||
#define ANSSRT_API __declspec(dllexport)
|
||||
#include <iostream>
|
||||
#include "ANSLicense.h"
|
||||
#include "LabVIEWHeader/extcode.h"
|
||||
#include <vector>
|
||||
#include "sys_inc.h"
|
||||
#include "srt_cln.h"
|
||||
#include "hqueue.h"
|
||||
#include "http.h"
|
||||
#include "http_parse.h"
|
||||
#include "video_decoder.h"
|
||||
#include "audio_decoder.h"
|
||||
#include "srt_player.h"
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
namespace ANSCENTER
|
||||
{
|
||||
class ANSSRT_API ANSSRTClient
|
||||
{
|
||||
protected:
|
||||
std::unique_ptr<CSrtPlayer> _playerClient = std::make_unique<CSrtPlayer>();
|
||||
std::string _username;
|
||||
std::string _password;
|
||||
std::string _url;
|
||||
bool _useFullURL;
|
||||
int _imageRotateDeg = 0;
|
||||
int _displayWidth = 0; // 0 = no resize (return original resolution)
|
||||
int _displayHeight = 0;
|
||||
cv::Mat _pLastFrame;
|
||||
std::string _lastJpegImage;
|
||||
int _imageWidth, _imageHeight;
|
||||
int64_t _pts;
|
||||
bool _isPlaying;
|
||||
std::recursive_mutex _mutex;
|
||||
public:
|
||||
ANSSRTClient();
|
||||
~ANSSRTClient() noexcept;
|
||||
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
|
||||
[[nodiscard]] bool Init(std::string licenseKey, std::string username, std::string password, std::string url);
|
||||
[[nodiscard]] bool Setup();
|
||||
[[nodiscard]] bool Reconnect();
|
||||
[[nodiscard]] bool Start();
|
||||
[[nodiscard]] bool IsPaused();
|
||||
[[nodiscard]] bool IsPlaying();
|
||||
[[nodiscard]] bool IsRecording();
|
||||
[[nodiscard]] bool Stop();
|
||||
[[nodiscard]] bool Pause();
|
||||
void Destroy();
|
||||
void SetBBox(cv::Rect bbox);
|
||||
void SetCrop(bool crop);
|
||||
void EnableAudio(bool status);
|
||||
void SetAudioVolume(int volume);
|
||||
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
|
||||
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
|
||||
[[nodiscard]] std::string GetJpegImage(int& width, int& height, int64_t& pts);
|
||||
bool areImagesIdentical(const cv::Mat& img1, const cv::Mat& img2);
|
||||
void SetImageRotate(int mode) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_imageRotateDeg = mode;
|
||||
if (mode > 360) _imageRotateDeg = 360;
|
||||
};
|
||||
static void SetMaxHWDecoders(int maxDecoders);
|
||||
static int AutoConfigureHWDecoders(int maxPerGpuOverride = 0);
|
||||
void SetHWDecoding(int hwMode, int preferredGpu = -1);
|
||||
bool IsHWDecodingActive();
|
||||
int GetHWDecodingGpuIndex();
|
||||
void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize)
|
||||
void SetImageQuality(int mode); // 0=fast (AI), 1=quality (display)
|
||||
AVFrame* GetNV12Frame(); // Returns cloned NV12 frame for GPU fast-path (caller must av_frame_free)
|
||||
AVFrame* GetCudaHWFrame(); // Returns CUDA HW frame (device ptrs) for zero-copy inference
|
||||
bool IsCudaHWAccel(); // true when decoder uses CUDA (NV12 stays in GPU VRAM)
|
||||
public:
|
||||
void CheckLicense();
|
||||
SPDLogger& _logger = SPDLogger::GetInstance("ANSSRT", false);
|
||||
bool _licenseValid{ false };
|
||||
std::string _licenseKey;
|
||||
//std::once_flag licenseOnceFlag;
|
||||
|
||||
};
|
||||
}
|
||||
extern "C" __declspec(dllexport) int CreateANSSRTHandle(ANSCENTER::ANSSRTClient * *Handle, const char* licenseKey, const char* username, const char* password, const char* url);
|
||||
extern "C" __declspec(dllexport) int ReleaseANSSRTHandle(ANSCENTER::ANSSRTClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int GetSRTImage(ANSCENTER::ANSSRTClient * *Handle, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetSRTStrImage(ANSCENTER::ANSSRTClient * *Handle, int& width, int& height, int64_t & timeStamp, std::string & jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetSRTCVImage(ANSCENTER::ANSSRTClient** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
|
||||
|
||||
extern "C" __declspec(dllexport) int StartSRT(ANSCENTER::ANSSRTClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int ReconnectSRT(ANSCENTER::ANSSRTClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int StopSRT(ANSCENTER::ANSSRTClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int PauseSRT(ANSCENTER::ANSSRTClient** Handle);
|
||||
extern "C" __declspec(dllexport) int IsSRTPaused(ANSCENTER::ANSSRTClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int IsSRTRunning(ANSCENTER::ANSSRTClient * *Handle);
|
||||
extern "C" __declspec(dllexport) int IsSRTRecording(ANSCENTER::ANSSRTClient * *Handle);
|
||||
extern "C" __declspec(dllexport) void SetSRTAudioVolume(ANSCENTER::ANSSRTClient * *Handle, int volume);
|
||||
extern "C" __declspec(dllexport) void EnableSRTAudioVolume(ANSCENTER::ANSSRTClient * *Handle, int status);
|
||||
extern "C" __declspec(dllexport) void SetSRTImageRotation(ANSCENTER::ANSSRTClient * *Handle, double rotationAngle);
|
||||
extern "C" __declspec(dllexport) int SetBBoxSRT(ANSCENTER::ANSSRTClient** Handle, int x, int y, int width, int height);
|
||||
extern "C" __declspec(dllexport) int SetCropFlagSRT(ANSCENTER::ANSSRTClient** Handle, int cropFlag);
|
||||
extern "C" __declspec(dllexport) void SetSRTMaxHWDecoders(int maxDecoders);
|
||||
extern "C" __declspec(dllexport) int AutoConfigureSRTHWDecoders(int maxPerGpuOverride);
|
||||
extern "C" __declspec(dllexport) void SetSRTHWDecoding(ANSCENTER::ANSSRTClient** Handle, int hwMode, int preferredGpu = -1);
|
||||
extern "C" __declspec(dllexport) int IsSRTHWDecodingActive(ANSCENTER::ANSSRTClient** Handle);
|
||||
extern "C" __declspec(dllexport) int GetSRTHWDecodingGpuIndex(ANSCENTER::ANSSRTClient** Handle);
|
||||
extern "C" __declspec(dllexport) void SetSRTImageQuality(ANSCENTER::ANSSRTClient** Handle, int mode);
|
||||
extern "C" __declspec(dllexport) void SetSRTDisplayResolution(ANSCENTER::ANSSRTClient** Handle, int width, int height);
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,110 @@
|
||||
#ifndef ANSVIDEOPLAYER_H
|
||||
#define ANSVIDEOPLAYER_H
|
||||
#define ANSVIDEOPLAYER_API __declspec(dllexport)
|
||||
#include <iostream>
|
||||
#include "ANSLicense.h"
|
||||
#include "LabVIEWHeader/extcode.h"
|
||||
#include <vector>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <thread>
|
||||
#include <chrono>
|
||||
#include <atomic>
|
||||
#include <turbojpeg.h>
|
||||
#include "file_player.h" // CFilePlayer (FFmpeg HW decode: CUDA/D3D11VA/DXVA2)
|
||||
#include "video_decoder.h" // HW_DECODING_AUTO, HWDecoderPool
|
||||
|
||||
namespace ANSCENTER
|
||||
{
|
||||
class ANSVIDEOPLAYER_API ANSVIDEOPLAYER
|
||||
{
|
||||
protected:
|
||||
cv::VideoCapture cap;
|
||||
int _resWidth;
|
||||
int _resHeight;
|
||||
int64_t _totalFrames;
|
||||
int64_t _currentFrame;
|
||||
int64_t _previousPTS;
|
||||
double _fps;
|
||||
bool _isPlaying;
|
||||
std::string _lastJpegImage;
|
||||
cv::Mat _previousImage;
|
||||
bool m_bPaused;
|
||||
int _imageRotateDeg = 0;
|
||||
std::string _url;
|
||||
cv::Rect _bbox;
|
||||
bool _crop;
|
||||
std::recursive_mutex _mutex;
|
||||
std::mutex _cvMutex;
|
||||
std::condition_variable _cv;
|
||||
tjhandle _jpegCompressor;
|
||||
|
||||
public:
|
||||
// --- HW decode state (CFilePlayer composition) ---
|
||||
// Public: accessed by extern "C" GetVideoPlayerCVImage for NV12/CUDA registry attachment
|
||||
std::unique_ptr<CFilePlayer> _hwPlayer; // HW decode path (nullptr = CV fallback)
|
||||
bool _hwDecodeActive = false; // true when _hwPlayer init succeeded
|
||||
int _hwGpuIndex = -1; // GPU index for registry (-1 = CPU only)
|
||||
bool _hwCudaAccel = false; // true = NVIDIA CUDA zero-copy available
|
||||
bool _hwEOF = false; // true when video reached end of file
|
||||
int64_t _hwFrameCount = 0; // frame counter for EOF detection
|
||||
int64_t _hwLastPts = 0; // last video PTS for EOF wrap detection
|
||||
ANSVIDEOPLAYER();
|
||||
~ANSVIDEOPLAYER() noexcept;
|
||||
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
|
||||
[[nodiscard]] bool Setup();
|
||||
[[nodiscard]] bool Reconnect();
|
||||
[[nodiscard]] bool Start();
|
||||
[[nodiscard]] bool IsPaused();
|
||||
[[nodiscard]] bool IsPlaying();
|
||||
[[nodiscard]] bool IsRecording();
|
||||
void SetBBox(cv::Rect bbox);
|
||||
void SetCrop(bool crop);
|
||||
[[nodiscard]] bool Stop();
|
||||
void Destroy();
|
||||
void EnableAudio(bool status);
|
||||
void SetAudioVolume(int volume);
|
||||
void SetImageRotate(int mode) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_imageRotateDeg = mode;
|
||||
if (mode > 360) _imageRotateDeg = 360;
|
||||
};
|
||||
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
|
||||
[[nodiscard]] cv::Mat GetInferenceImage(); // Returns full-res frame for AI inference (no resize/rotation)
|
||||
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
|
||||
[[nodiscard]] std::string GetJpegStringImage(int& width, int& height, int64_t& pts);
|
||||
void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize)
|
||||
public:
|
||||
void CheckLicense();
|
||||
SPDLogger& _logger = SPDLogger::GetInstance("ANSVideoPlayer", false);
|
||||
bool _licenseValid{ false };
|
||||
//std::once_flag licenseOnceFlag;
|
||||
std::string _licenseKey;
|
||||
cv::Mat _inferenceCloneCurr; // Heap-cloned full-res (keeps data alive for registry)
|
||||
cv::Mat _inferenceClonePrev; // Previous clone (keeps data alive during ANSRTYOLO read)
|
||||
private:
|
||||
std::string encodeJpegString(const cv::Mat& img, int quality = 80);
|
||||
int _displayWidth = 0; // 0 = no resize (return original)
|
||||
int _displayHeight = 0;
|
||||
cv::Mat _inferenceImage; // Full-res frame for AI inference
|
||||
};
|
||||
}
|
||||
extern "C" __declspec(dllexport) int CreateANSVideoPlayerHandle(ANSCENTER::ANSVIDEOPLAYER** Handle, const char* licenseKey, const char* url);
|
||||
extern "C" __declspec(dllexport) int ReleaseANSVideoPlayerHandle(ANSCENTER::ANSVIDEOPLAYER** Handle);
|
||||
extern "C" __declspec(dllexport) int GetVideoPlayerImage(ANSCENTER::ANSVIDEOPLAYER** Handle, int& width, int& height, int64_t& timeStamp, LStrHandle jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetVideoPlayerStrImage(ANSCENTER::ANSVIDEOPLAYER** Handle, int& width, int& height, int64_t& timeStamp, std::string& jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetVideoPlayerCVImage(ANSCENTER::ANSVIDEOPLAYER** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
|
||||
extern "C" __declspec(dllexport) int StartVideoPlayer(ANSCENTER::ANSVIDEOPLAYER** Handle);
|
||||
extern "C" __declspec(dllexport) int ReconnectVideoPlayer(ANSCENTER::ANSVIDEOPLAYER** Handle);
|
||||
extern "C" __declspec(dllexport) int StopVideoPlayer(ANSCENTER::ANSVIDEOPLAYER** Handle);
|
||||
extern "C" __declspec(dllexport) int IsVideoPlayerPaused(ANSCENTER::ANSVIDEOPLAYER** Handle);
|
||||
extern "C" __declspec(dllexport) int IsVidedoPlayerRunning(ANSCENTER::ANSVIDEOPLAYER** Handle);
|
||||
extern "C" __declspec(dllexport) int IsVideoPlayerRecording(ANSCENTER::ANSVIDEOPLAYER** Handle);
|
||||
extern "C" __declspec(dllexport) void SetVideoPlayerAudioVolume(ANSCENTER::ANSVIDEOPLAYER** Handle, int volume);
|
||||
extern "C" __declspec(dllexport) void EnableVideoPlayerAudioVolume(ANSCENTER::ANSVIDEOPLAYER** Handle, int status);
|
||||
extern "C" __declspec(dllexport) void SetVideoPlayerImageRotation(ANSCENTER::ANSVIDEOPLAYER** Handle, double rotationAngle);
|
||||
extern "C" __declspec(dllexport) int SetBBoxVideoPlayer(ANSCENTER::ANSVIDEOPLAYER** Handle, int x, int y, int width, int height);
|
||||
extern "C" __declspec(dllexport) int SetCropFlagVideoPlayer(ANSCENTER::ANSVIDEOPLAYER** Handle, int cropFlag);
|
||||
extern "C" __declspec(dllexport) void SetVideoPlayerDisplayResolution(ANSCENTER::ANSVIDEOPLAYER** Handle, int width, int height);
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,140 @@
|
||||
#ifndef ANSWEBCAM_H
|
||||
#define ANSWEBCAM_H
|
||||
#define ANSWEBCAM_API __declspec(dllexport)
|
||||
#include <iostream>
|
||||
#include "ANSLicense.h"
|
||||
#include "LabVIEWHeader/extcode.h"
|
||||
#include <vector>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <dshow.h>
|
||||
#include <mutex>
|
||||
#include <nvjpeg.h>
|
||||
#include <cuda_runtime.h>
|
||||
#include <turbojpeg.h>
|
||||
#pragma comment(lib, "strmiids.lib")
|
||||
#pragma comment(lib, "ole32.lib")
|
||||
|
||||
namespace ANSCENTER
|
||||
{
|
||||
struct Resolution {
|
||||
int width;
|
||||
int height;
|
||||
|
||||
Resolution(int w, int h) : width(w), height(h) {}
|
||||
|
||||
int getArea() const {
|
||||
return width * height;
|
||||
}
|
||||
|
||||
bool operator<(const Resolution& other) const {
|
||||
return getArea() < other.getArea();
|
||||
}
|
||||
};
|
||||
class ANSWEBCAM_API ANSWEBCAMPlayer
|
||||
{
|
||||
protected:
|
||||
// Add this structure definition in the private section
|
||||
cv::VideoCapture cap;
|
||||
int _deviceId;
|
||||
int _resWidth;
|
||||
int _resHeight;
|
||||
int _savedResWidth = 0; // Saved resolution for stop/start restore
|
||||
int _savedResHeight = 0;
|
||||
bool _enableMaxResolution = false; // false = Full HD default, true = max resolution
|
||||
int64_t _previousPTS;
|
||||
cv::Mat _previousImage;
|
||||
bool m_bPaused;
|
||||
std::string _lastJpegImage;
|
||||
bool _isPlaying;
|
||||
std::vector<std::string> _cameraNames;
|
||||
int _imageRotateDeg = 0;
|
||||
std::recursive_mutex _mutex;
|
||||
cv::Rect _bbox;
|
||||
bool _crop;
|
||||
EngineType engineType;
|
||||
// Initialize nvJPEG structures
|
||||
nvjpegHandle_t nv_handle;
|
||||
nvjpegEncoderState_t nv_enc_state;
|
||||
nvjpegEncoderParams_t nv_enc_params;
|
||||
cudaStream_t stream;
|
||||
tjhandle _jpegCompressor;
|
||||
public:
|
||||
ANSWEBCAMPlayer();
|
||||
~ANSWEBCAMPlayer() noexcept;
|
||||
[[nodiscard]] bool Init(std::string licenseKey, std::string url);
|
||||
[[nodiscard]] bool Setup();
|
||||
[[nodiscard]] bool Reconnect();
|
||||
[[nodiscard]] bool Start();
|
||||
[[nodiscard]] bool IsPaused();
|
||||
[[nodiscard]] bool IsPlaying();
|
||||
[[nodiscard]] bool IsRecording();
|
||||
[[nodiscard]] bool Stop();
|
||||
void Destroy();
|
||||
void EnableAudio(bool status);
|
||||
void SetAudioVolume(int volume);
|
||||
[[nodiscard]] cv::Mat GetImage(int& width, int& height, int64_t& pts);
|
||||
[[nodiscard]] std::string MatToBinaryData(const cv::Mat& image);
|
||||
[[nodiscard]] std::string GetJpegStringImage(int& width, int& height, int64_t& pts);
|
||||
[[nodiscard]] static std::string VectorToCommaSeparatedString(const std::vector<std::string>& inputVector);
|
||||
[[nodiscard]] static std::vector<std::string> ScanWebcamDevices();
|
||||
[[nodiscard]] static std::string GetSupportedResolutions(int deviceId);
|
||||
static std::mutex mtx;
|
||||
void SetImageRotate(int mode) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_imageRotateDeg = mode;
|
||||
if (mode > 360) _imageRotateDeg = 360;
|
||||
};
|
||||
void SetBBox(cv::Rect bbox);
|
||||
void SetCrop(bool crop);
|
||||
void SetDisplayResolution(int width, int height); // Set display output size; 0,0 = original (no resize)
|
||||
[[nodiscard]] cv::Mat GetInferenceImage(); // Returns full-res frame for AI inference (no resize)
|
||||
cv::Mat _inferenceCloneCurr; // Heap-cloned full-res (keeps data alive for registry)
|
||||
cv::Mat _inferenceClonePrev; // Previous clone (keeps data alive during ANSRTYOLO read)
|
||||
public:
|
||||
void CheckLicense();
|
||||
SPDLogger& _logger = SPDLogger::GetInstance("ANSWebcamPlayer", false);
|
||||
bool _licenseValid{ false };
|
||||
std::string _licenseKey;
|
||||
|
||||
// Resolution management methods
|
||||
std::vector<Resolution> GetAvailableResolutions(int deviceId);
|
||||
bool SetBestResolution(int deviceId);
|
||||
bool SetPreferredResolution(int deviceId);
|
||||
bool SetResolution(int width, int height);
|
||||
void SetEnableMaxResolution(bool enable);
|
||||
std::string GetAvailableResolutionsString(int deviceId);
|
||||
private:
|
||||
std::string encodeJpegString(const cv::Mat& img, int quality = 80);
|
||||
std::string encodeMatToJpegWithNvJPEG(const cv::Mat& inputMat, int quality = 80);
|
||||
void uploadPlanarBGRToGPU(const cv::Mat& inputMat, unsigned char** data);
|
||||
int _displayWidth = 0; // 0 = no resize (return original)
|
||||
int _displayHeight = 0;
|
||||
cv::Mat _inferenceImage; // Full-res frame for AI inference
|
||||
};
|
||||
std::mutex ANSWEBCAMPlayer::mtx; // Definition of the static mutex
|
||||
}
|
||||
extern "C" __declspec(dllexport) int CreateANSWebcamPlayerHandle(ANSCENTER::ANSWEBCAMPlayer * *Handle, const char* licenseKey, const char* url);
|
||||
extern "C" __declspec(dllexport) int CreateANSWebcamPlayerWithMaxResoHandle(ANSCENTER::ANSWEBCAMPlayer * *Handle, const char* licenseKey, const char* url);
|
||||
extern "C" __declspec(dllexport) int ReleaseANSWebcamPlayerHandle(ANSCENTER::ANSWEBCAMPlayer * *Handle);
|
||||
extern "C" __declspec(dllexport) int ScanANSWebcamPlayer(LStrHandle cameraNames);
|
||||
extern "C" __declspec(dllexport) int ScanStrANSWebcamPlayer(std::vector<std::string> &cameraNameList);
|
||||
extern "C" __declspec(dllexport) int GetWebcamImage(ANSCENTER::ANSWEBCAMPlayer * *Handle, int& width, int& height, int64_t & timeStamp, LStrHandle jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetWebcamStrImage(ANSCENTER::ANSWEBCAMPlayer * *Handle, int& width, int& height, int64_t & timeStamp, std::string & jpegImage);
|
||||
extern "C" __declspec(dllexport) int GetWebcamCVImage(ANSCENTER::ANSWEBCAMPlayer** Handle, int& width, int& height, int64_t& timeStamp, cv::Mat** image);
|
||||
extern "C" __declspec(dllexport) int StartWebcamPlayer(ANSCENTER::ANSWEBCAMPlayer * *Handle);
|
||||
extern "C" __declspec(dllexport) int ReconnectWebcamPlayer(ANSCENTER::ANSWEBCAMPlayer * *Handle);
|
||||
extern "C" __declspec(dllexport) int StopWebcamPlayer(ANSCENTER::ANSWEBCAMPlayer * *Handle);
|
||||
extern "C" __declspec(dllexport) int IsWebcamPlayerPaused(ANSCENTER::ANSWEBCAMPlayer * *Handle);
|
||||
extern "C" __declspec(dllexport) int IsWebcamPlayerRunning(ANSCENTER::ANSWEBCAMPlayer * *Handle);
|
||||
extern "C" __declspec(dllexport) int IsWebcamPlayerRecording(ANSCENTER::ANSWEBCAMPlayer * *Handle);
|
||||
extern "C" __declspec(dllexport) void SetWebcamPlayerAudioVolume(ANSCENTER::ANSWEBCAMPlayer * *Handle, int volume);
|
||||
extern "C" __declspec(dllexport) void EnableWebcamPlayerAudioVolume(ANSCENTER::ANSWEBCAMPlayer * *Handle, int status);
|
||||
extern "C" __declspec(dllexport) void SetWebcamImageRotation(ANSCENTER::ANSWEBCAMPlayer * *Handle, double rotationAngle);
|
||||
extern "C" __declspec(dllexport) int ScanSupportedResolutions(std::string cameraName,std::vector<std::string> cameraNameList, std::string &supportedResolution);
|
||||
extern "C" __declspec(dllexport) int SetBBoxANSWebcamPlayer(ANSCENTER::ANSWEBCAMPlayer** Handle, int x, int y, int width, int height);
|
||||
extern "C" __declspec(dllexport) int SetCropFlagANSWebcamPlayer(ANSCENTER::ANSWEBCAMPlayer** Handle, int cropFlag);
|
||||
extern "C" __declspec(dllexport) void SetWebcamDisplayResolution(ANSCENTER::ANSWEBCAMPlayer** Handle, int width, int height);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,121 @@
|
||||
# ANSCV — Computer Vision / Media DLL (video capture, RTSP, FFmpeg)
|
||||
file(GLOB ANSCV_HEADERS "*.h")
|
||||
file(GLOB ANSCV_SOURCES "*.cpp")
|
||||
# Exclude old VideoPlayer.cpp — superseded by MediaClient/media/video_player.cpp
|
||||
list(REMOVE_ITEM ANSCV_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/VideoPlayer.cpp")
|
||||
|
||||
# MediaClient sources compiled into ANSCV (from original vcxproj)
|
||||
set(MEDIACLIENT_SOURCES
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/audio_capture.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/audio_capture_win.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/audio_decoder.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/audio_encoder.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/audio_play.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/audio_play_win.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/avcodec_mutex.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/avi_write.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/file_player.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/http_flv_player.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/http_mjpeg_player.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/media_codec.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/media_parse.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/media_util.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/rtmp_player.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/rtsp_player.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/srt_player.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/video_decoder.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media/video_player.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/rtp/h264_rtp_rx.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/rtp/h264_util.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/rtp/h265_rtp_rx.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/rtp/h265_util.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/rtp/mjpeg_rtp_rx.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/rtp/mjpeg_tables.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/rtp/ts_parser.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/rtsp/rtsp_backchannel.cpp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/rtsp/rtsp_cln.cpp
|
||||
)
|
||||
|
||||
add_library(ANSCV SHARED ${ANSCV_HEADERS} ${ANSCV_SOURCES} ${MEDIACLIENT_SOURCES})
|
||||
|
||||
# MediaClient sources skip precompiled headers (they don't include ANSCV's pch.h)
|
||||
set_source_files_properties(${MEDIACLIENT_SOURCES} PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
|
||||
|
||||
target_include_directories(ANSCV PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
|
||||
# MediaClient includes (referenced from original ANLS)
|
||||
target_include_directories(ANSCV PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/MediaClient
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/media
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/rtsp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/rtp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/http
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/directx
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/directx/include
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/ffmpeg/include
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/openssl/include
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/libsrt/include
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/bm
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/rtmp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/librtmp
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/srt
|
||||
${SHARED_INCLUDE_DIR}
|
||||
)
|
||||
|
||||
target_link_libraries(ANSCV
|
||||
PRIVATE ANSLicensingSystem
|
||||
PRIVATE labview
|
||||
PRIVATE spdlog_dep
|
||||
PRIVATE ANSLibsLoader
|
||||
PRIVATE opencv
|
||||
PRIVATE ffmpeg
|
||||
PRIVATE turbojpeg
|
||||
PRIVATE zxing
|
||||
PRIVATE zlib_dep
|
||||
PRIVATE CUDA::cudart_static
|
||||
PRIVATE CUDA::cublasLt
|
||||
PRIVATE CUDA::nvjpeg
|
||||
)
|
||||
|
||||
# Platform-specific libs
|
||||
if(WIN32)
|
||||
target_link_directories(ANSCV PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/directx/lib/x64
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/openssl/lib/x64
|
||||
${CMAKE_SOURCE_DIR}/MediaClient/libsrt/lib/x64
|
||||
)
|
||||
# Prebuilt MediaClient protocol libraries (built from MediaClient VS project)
|
||||
target_link_directories(ANSCV PRIVATE "${ANLS_ROOT}/MediaClient/x64/Release")
|
||||
target_link_libraries(ANSCV PRIVATE
|
||||
RtspClientLibrary HttpFlvClientLibrary HttpMjpegClientLibrary
|
||||
RtmpClientLibrary SrtClientLibrary
|
||||
d3d9 d3dx9 dxva2
|
||||
libcrypto libssl srt
|
||||
${WIN_COMMON_LIBS}
|
||||
)
|
||||
else()
|
||||
target_link_libraries(ANSCV PRIVATE ${UNIX_COMMON_LIBS})
|
||||
endif()
|
||||
|
||||
target_compile_definitions(ANSCV PRIVATE
|
||||
ANSCV_EXPORTS
|
||||
_USRDLL
|
||||
HT_STATIC
|
||||
HTTPS
|
||||
BACKCHANNEL
|
||||
METADATA
|
||||
REPLAY
|
||||
OVER_HTTP
|
||||
OVER_WEBSOCKET
|
||||
NOMINMAX
|
||||
)
|
||||
|
||||
if(WIN32)
|
||||
target_compile_definitions(ANSCV PRIVATE UNICODE _UNICODE __WINDOWS_OS__)
|
||||
else()
|
||||
target_compile_definitions(ANSCV PRIVATE __LINUX_OS__)
|
||||
endif()
|
||||
|
||||
target_precompile_headers(ANSCV PRIVATE pch.h)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,199 @@
|
||||
/***************************************************************************************
|
||||
*
|
||||
* IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
|
||||
*
|
||||
* By downloading, copying, installing or using the software you agree to this license.
|
||||
* If you do not agree to this license, do not download, install,
|
||||
* copy or use the software.
|
||||
*
|
||||
* Copyright (C) 2014-2024, Happytimesoft Corporation, all rights reserved.
|
||||
*
|
||||
* Redistribution and use in binary forms, with or without modification, are permitted.
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
|
||||
* language governing permissions and limitations under the License.
|
||||
*
|
||||
****************************************************************************************/
|
||||
|
||||
#ifndef VIDEO_PLAYER_H
|
||||
#define VIDEO_PLAYER_H
|
||||
|
||||
#include "sys_inc.h"
|
||||
#include "hqueue.h"
|
||||
#include "video_decoder.h"
|
||||
#include "audio_decoder.h"
|
||||
#include "audio_play.h"
|
||||
#include "avi_write.h"
|
||||
#include "media_util.h"
|
||||
#include <list>
|
||||
#include <string>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint32 SyncTimestamp;
|
||||
struct timeval SyncTime;
|
||||
} HTCLOCK;
|
||||
|
||||
typedef std::list<AVFrame*> FRAMELIST;
|
||||
|
||||
class CVideoPlayer
|
||||
{
|
||||
|
||||
public:
|
||||
CVideoPlayer();
|
||||
virtual ~CVideoPlayer();
|
||||
virtual BOOL open(std::string fileName);
|
||||
virtual BOOL play() = 0;
|
||||
virtual void stop() = 0;
|
||||
virtual BOOL pause() = 0;
|
||||
virtual void close();
|
||||
virtual BOOL seek(int pos) { return FALSE; }
|
||||
virtual BOOL isPlaying() { return m_bPlaying; }
|
||||
virtual BOOL isPaused() { return m_bPaused; }
|
||||
virtual void setVolume(int volume);
|
||||
virtual void snapshot(int videofmt);
|
||||
virtual BOOL record(std::string baseName);
|
||||
virtual void stopRecord();
|
||||
virtual BOOL isRecording() { return m_bRecording; }
|
||||
virtual BOOL onRecord() { return FALSE; }
|
||||
virtual int getVideoClock() { return 1000; }
|
||||
virtual int getAudioClock() { return 1000; }
|
||||
virtual void setWindowSize(int size);
|
||||
virtual int64 getElapse() { return 0; }
|
||||
virtual int64 getDuration() { return 0; }
|
||||
virtual int getVideoCodec() { return m_nVideoCodec; }
|
||||
virtual int getAudioCodec() { return m_nAudioCodec; }
|
||||
|
||||
virtual void setAuthInfo(std::string acct, std::string pass) { m_acct = acct; m_pass = pass; }
|
||||
virtual void setRenderMode(int mode) { m_nRenderMode = mode; }
|
||||
virtual void setHWDecoding(int mode) { m_nHWDecoding = mode; }
|
||||
virtual void setRtpMulticast(BOOL flag) {}
|
||||
virtual void setRtpOverUdp(BOOL flag) {}
|
||||
|
||||
#ifdef OVER_HTTP
|
||||
virtual void setRtspOverHttp(int flag, int port) {}
|
||||
#endif
|
||||
|
||||
#ifdef OVER_WEBSOCKET
|
||||
virtual void setRtspOverWs(int flag, int port) {}
|
||||
#endif
|
||||
|
||||
#ifdef BACKCHANNEL
|
||||
virtual int getBCFlag() { return 0; }
|
||||
virtual void setBCFlag(int flag) {}
|
||||
virtual int getBCDataFlag() { return 0; }
|
||||
virtual void setBCDataFlag(int flag) {}
|
||||
virtual void setAudioDevice(int index) {}
|
||||
#endif
|
||||
|
||||
#ifdef REPLAY
|
||||
virtual int getReplayFlag() { return 0; }
|
||||
virtual void setReplayFlag(int flag) {}
|
||||
virtual void setScale(double scale) {}
|
||||
virtual void setRateControlFlag(int flag) {}
|
||||
virtual void setImmediateFlag(int flag) {}
|
||||
virtual void setFramesFlag(int flag, int interval) {}
|
||||
virtual void setReplayRange(time_t start, time_t end) {}
|
||||
#endif
|
||||
|
||||
BOOL openVideo(int codec, uint8* extradata = NULL, int extradata_size = 0);
|
||||
BOOL openVideo(enum AVCodecID codec, uint8* extradata = NULL, int extradata_size = 0);
|
||||
void closeVideo();
|
||||
BOOL openAudio(int codec, int samplerate, int channels, int bitpersample = 0);
|
||||
BOOL openAudio(enum AVCodecID codec, int samplerate, int channels, int bitpersample = 0);
|
||||
void closeAudio();
|
||||
|
||||
void playVideo(uint8* data, int len, uint32 ts, uint16 seq);
|
||||
void playAudio(uint8* data, int len, uint32 ts, uint16 seq);
|
||||
|
||||
void recordVideo(uint8* data, int len, uint32 ts, uint16 seq);
|
||||
void recordAudio(uint8* data, int len, uint32 ts, uint16 seq);
|
||||
|
||||
int getVideoWidth();
|
||||
int getVideoHeight();
|
||||
double getFrameRate();
|
||||
int getSampleRate() { return m_nSampleRate; }
|
||||
int getChannel() { return m_nChannel; }
|
||||
|
||||
void onVideoFrame(AVFrame* frame);
|
||||
void onAudioFrame(AVFrame* frame);
|
||||
void audioPlayThread();
|
||||
void videoPlayThread();
|
||||
|
||||
//signals:
|
||||
// void notify(int);
|
||||
// void snapshoted(AVFrame*);
|
||||
|
||||
protected:
|
||||
void updateClock(HTCLOCK* clock, uint32 ts, int frequency);
|
||||
BOOL initFrame(AVFrame*& frame, int width, int height, AVPixelFormat pixfmt);
|
||||
//BOOL initVideoRender(int width, int height);
|
||||
BOOL doSnapshot(AVFrame* srcframe);
|
||||
AVFrame* convertFrame(AVFrame* srcframe, AVFrame* dstframe, BOOL updown);
|
||||
void recordVideoEx(uint8* data, int len, uint32 ts, uint16 seq);
|
||||
BOOL recordSwitchCheck();
|
||||
void recordFileSwitch();
|
||||
|
||||
protected:
|
||||
BOOL m_bVideoInited;
|
||||
BOOL m_bAudioInited;
|
||||
CVideoDecoder* m_pVideoDecoder;
|
||||
CAudioDecoder* m_pAudioDecoder;
|
||||
//CVideoRender* m_pVideoRender;
|
||||
CAudioPlay* m_pAudioPlay;
|
||||
|
||||
BOOL m_bPlaying;
|
||||
BOOL m_bPaused;
|
||||
|
||||
std::string m_acct;
|
||||
std::string m_pass;
|
||||
std::string m_sFileName;
|
||||
std::string m_sBaseName;
|
||||
//WId m_nVideoWnd;
|
||||
int m_size;
|
||||
BOOL m_bSizeChanged;
|
||||
int m_nRenderMode;
|
||||
int m_nHWDecoding;
|
||||
AVPixelFormat m_nDstVideoFmt;
|
||||
BOOL m_bUpdown;
|
||||
BOOL m_bSnapshot;
|
||||
int m_nSnapVideoFmt;
|
||||
|
||||
H26XParamSets m_h26XParamSets;
|
||||
|
||||
int m_nVideoCodec;
|
||||
int m_nAudioCodec;
|
||||
int m_nSampleRate;
|
||||
int m_nChannel;
|
||||
int m_nBitPerSample;
|
||||
|
||||
AVFrame* m_pSnapFrame;
|
||||
AVFrame* m_pRenderFrame;
|
||||
|
||||
BOOL m_bRecording;
|
||||
BOOL m_bNalFlag;
|
||||
AVICTX* m_pAviCtx;
|
||||
void* m_pRecordMutex;
|
||||
|
||||
HTCLOCK m_audioClock;
|
||||
void* m_pAudioListMutex;
|
||||
FRAMELIST m_audioFrameList;
|
||||
BOOL m_audioPlayFlag;
|
||||
pthread_t m_audioPlayThread;
|
||||
|
||||
HTCLOCK m_videoClock;
|
||||
void* m_pVideoListMutex;
|
||||
FRAMELIST m_videoFrameList;
|
||||
BOOL m_videoPlayFlag;
|
||||
pthread_t m_videoPlayThread;
|
||||
|
||||
uint64 m_nLastAudioPts;
|
||||
time_t m_lastAudioTS;
|
||||
};
|
||||
|
||||
#endif // end of VIDEO_PLAYER_H
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
// dllmain.cpp : Defines the entry point for the DLL application.
|
||||
#include "pch.h"
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
|
||||
// ── Global handle registry for shutdown cleanup ──────────────────────────
|
||||
// Streaming handle creators register a stop-callback on creation.
|
||||
// DLL_PROCESS_DETACH invokes all callbacks so threads exit before DLL unmaps.
|
||||
|
||||
static std::mutex& shutdownMutex() {
|
||||
static std::mutex m;
|
||||
return m;
|
||||
}
|
||||
|
||||
static std::unordered_map<void*, std::function<void()>>& shutdownRegistry() {
|
||||
static std::unordered_map<void*, std::function<void()>> r;
|
||||
return r;
|
||||
}
|
||||
|
||||
// Called by Create*Handle functions to register a cleanup callback
|
||||
extern "C" void anscv_register_handle(void* handle, void(*stopFn)(void*)) {
|
||||
if (!handle || !stopFn) return;
|
||||
std::lock_guard<std::mutex> lk(shutdownMutex());
|
||||
shutdownRegistry()[handle] = [=]() { stopFn(handle); };
|
||||
}
|
||||
|
||||
// Called by Release*Handle functions to unregister (handle properly cleaned up)
|
||||
extern "C" void anscv_unregister_handle(void* handle) {
|
||||
if (!handle) return;
|
||||
std::lock_guard<std::mutex> lk(shutdownMutex());
|
||||
shutdownRegistry().erase(handle);
|
||||
}
|
||||
|
||||
// Stop all leaked handles
|
||||
static void stopAllHandles() {
|
||||
std::unordered_map<void*, std::function<void()>> copy;
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(shutdownMutex());
|
||||
copy.swap(shutdownRegistry());
|
||||
}
|
||||
for (auto& [ptr, fn] : copy) {
|
||||
try { fn(); } catch (...) {}
|
||||
}
|
||||
}
|
||||
|
||||
BOOL APIENTRY DllMain( HMODULE hModule,
|
||||
DWORD ul_reason_for_call,
|
||||
LPVOID lpReserved
|
||||
) noexcept
|
||||
{
|
||||
switch (ul_reason_for_call)
|
||||
{
|
||||
case DLL_PROCESS_ATTACH:
|
||||
// Pin the DLL so it is never unmapped from the process address space.
|
||||
// ANSCV creates background threads (RTSP receive, timers) that may
|
||||
// still be running when LabVIEW's CLR/COM shutdown unloads the DLL.
|
||||
// If the DLL is unmapped while threads are executing its code, the
|
||||
// threads crash with an access violation at <Unloaded_ANSCV.dll>.
|
||||
// Pinning keeps the code pages mapped; the OS kills all threads when
|
||||
// the process exits, so this is safe and is Microsoft's recommended
|
||||
// pattern for DLLs that own threads.
|
||||
{
|
||||
HMODULE hSelf = nullptr;
|
||||
GetModuleHandleExW(
|
||||
GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
|
||||
GET_MODULE_HANDLE_EX_FLAG_PIN,
|
||||
reinterpret_cast<LPCWSTR>(&DllMain),
|
||||
&hSelf);
|
||||
}
|
||||
break;
|
||||
case DLL_THREAD_ATTACH:
|
||||
case DLL_THREAD_DETACH:
|
||||
break;
|
||||
case DLL_PROCESS_DETACH:
|
||||
// CRITICAL: Do NOT call stopAllHandles() here.
|
||||
//
|
||||
// DllMain holds the OS loader lock (LdrpLoaderLock). Destroying
|
||||
// streaming handles (CRtspPlayer → CVideoPlayer → CWAudioPlay)
|
||||
// calls dsound!CThread::Terminate which waits for the DirectSound
|
||||
// admin thread to exit. That thread needs the loader lock to shut
|
||||
// down → classic deadlock. (Confirmed by crash dump analysis:
|
||||
// Thread 0 holds LdrpLoaderLock and waits for dsound thread 331,
|
||||
// which is blocked on LdrpLoaderLock.)
|
||||
//
|
||||
// If lpReserved is non-NULL the process is terminating — the OS
|
||||
// will tear down all threads and reclaim resources; cleanup is
|
||||
// unnecessary and dangerous. If lpReserved is NULL (dynamic
|
||||
// FreeLibrary), LabVIEW should have called Release*Handle first.
|
||||
// Log leaked handles for diagnostics but do NOT stop them here.
|
||||
if (lpReserved == nullptr) {
|
||||
// Dynamic unload — warn about leaked handles (non-blocking)
|
||||
try {
|
||||
std::lock_guard<std::mutex> lk(shutdownMutex());
|
||||
if (!shutdownRegistry().empty()) {
|
||||
std::cerr << "[ANSCV] WARNING: " << shutdownRegistry().size()
|
||||
<< " streaming handle(s) leaked at DLL unload."
|
||||
<< " Call Release*Handle before FreeLibrary." << std::endl;
|
||||
}
|
||||
} catch (...) {}
|
||||
}
|
||||
break;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
|
||||
#define NOMINMAX // Prevent windows.h from defining min/max macros
|
||||
// which break std::min / std::max (C2589)
|
||||
// Windows Header Files
|
||||
#include <windows.h>
|
||||
@@ -0,0 +1,5 @@
|
||||
// pch.cpp: source file corresponding to the pre-compiled header
|
||||
|
||||
#include "pch.h"
|
||||
|
||||
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
|
||||
@@ -0,0 +1,25 @@
|
||||
// pch.h: This is a precompiled header file.
|
||||
// Files listed below are compiled only once, improving build performance for future builds.
|
||||
// This also affects IntelliSense performance, including code completion and many code browsing features.
|
||||
// However, files listed here are ALL re-compiled if any one of them is updated between builds.
|
||||
// Do not add files here that you will be updating frequently as this negates the performance advantage.
|
||||
|
||||
#ifndef PCH_H
|
||||
#define PCH_H
|
||||
|
||||
// add headers that you want to pre-compile here
|
||||
#include "framework.h"
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
// Legacy code uses bare min/max (without std:: prefix).
|
||||
// NOMINMAX prevents windows.h from defining min/max macros,
|
||||
// so pull them in from <algorithm> instead.
|
||||
using std::min;
|
||||
using std::max;
|
||||
|
||||
#endif //PCH_H
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,352 @@
|
||||
#ifndef ANSFR_H
|
||||
#define ANSFR_H
|
||||
#define ANSFR_API __declspec(dllexport)
|
||||
#pragma once
|
||||
#include "ANSFRCommon.h"
|
||||
#include "FaceDatabase.h"
|
||||
#include <shared_mutex>
|
||||
#include <atomic>
|
||||
#define MAX_FACE_CHECKER_FRAMES 20
|
||||
#define FACE_CONFIDENT_LEVEL 5
|
||||
|
||||
namespace ANSCENTER
|
||||
{
|
||||
struct FaceDetection {
|
||||
FaceResultObject faceObject; // Bounding box
|
||||
int faceConfLevel; // face confident level, if face is detected then it will increase otherwise, it will be recreased.
|
||||
};
|
||||
|
||||
class FaceChecker {
|
||||
public:
|
||||
FaceChecker(int maxFrames = MAX_FACE_CHECKER_FRAMES, float minScore = 0.7f)
|
||||
: maxFrames(maxFrames), minScore(minScore) {}
|
||||
|
||||
// Add detected faces from a new frame
|
||||
std::vector<FaceResultObject> ValidateDetectedFaces(const std::vector<FaceResultObject>& faces);
|
||||
std::vector<FaceResultObject> UniqueFaces(const std::vector<FaceResultObject>& faces);
|
||||
|
||||
private:
|
||||
int maxFrames;
|
||||
float minScore;
|
||||
std::recursive_mutex _mutex;
|
||||
std::unordered_map<std::string, std::vector<std::vector<FaceDetection>>> frameBufferMap; // Keyed by cameraId
|
||||
};
|
||||
/////
|
||||
|
||||
// Fix #11: ScopedTimer now logs via SPDLogger instead of being a dead no-op
|
||||
class ScopedTimer {
|
||||
public:
|
||||
explicit ScopedTimer(const std::string& operation_name)
|
||||
: _name(operation_name)
|
||||
, _start(std::chrono::steady_clock::now())
|
||||
{
|
||||
}
|
||||
|
||||
~ScopedTimer() noexcept {
|
||||
try {
|
||||
auto end = std::chrono::steady_clock::now();
|
||||
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - _start).count();
|
||||
ANSCENTER::SPDLogger::GetInstance("ANSFR").LogDebug(
|
||||
"ScopedTimer", _name + " took " + std::to_string(ms) + " ms",
|
||||
__FILE__, __LINE__);
|
||||
}
|
||||
catch (...) {} // Never throw from destructor
|
||||
}
|
||||
|
||||
private:
|
||||
std::string _name;
|
||||
std::chrono::steady_clock::time_point _start;
|
||||
};
|
||||
class ANSFR_API ANSFacialRecognition
|
||||
{
|
||||
public:
|
||||
[[nodiscard]] int Initialize(const std::string & licenseKey,
|
||||
const std::string& configFile,
|
||||
const std::string& databaseFilePath,
|
||||
const std::string& recogniserFilePath,
|
||||
const std::string& detectorFilePath="",
|
||||
int precisionType=0,
|
||||
float knownPersonThreshold=0.35,
|
||||
bool enableAgeGender=true,
|
||||
bool enableFaceEmotions=true,
|
||||
bool enableHeadPose=true,
|
||||
int minFaceSize=30,
|
||||
float faceDetectorScoreThreshold=0.5,
|
||||
bool faceliveness=true,
|
||||
bool antiSpoofing=true);
|
||||
[[nodiscard]] bool LoadEngine();
|
||||
int InsertUser(const std::string& userCode,const std::string& userName);
|
||||
int UpdateUser(int userId, const std::string& userCode, const std::string& userName);
|
||||
int DeleteUser(int userId);
|
||||
int DeleteUsers(const std::vector<int>& userIds);
|
||||
int InsertFace(int userId, const cv::Mat& image);
|
||||
[[nodiscard]] std::vector<int> InsertMultipleFaces(int userId, const cv::Mat& image);
|
||||
[[nodiscard]] int CheckFace(const cv::Mat& image);// return 0 if face is not valid otherwise 1
|
||||
int DeleteFace(int faceId);
|
||||
int DeleteFacesByUser(int userId);
|
||||
int GetUser(int userId, std::string& userRecord);
|
||||
int GetUsers(std::string& userRecords, std::vector<int>&userIds);
|
||||
int GetFace(int faceId, std::string& faceRecord);
|
||||
int GetFaces(int userId, std::string& faceRecords);
|
||||
void CheckLicense();
|
||||
bool Reload();
|
||||
bool UpdateUserDictionary();
|
||||
bool UpdateParameters(float knownPersonThreshold = 0.35,bool enableAgeGender = true,bool enableFaceEmotions = true,bool enableHeadPose = true, int minFaceSize=112, float faceDetectorScoreThreshold=0.5, bool faceliveness=true, bool antiSpoof=true, bool removeFakeFace=false);
|
||||
bool GetFaceParameters(float& knownPersonThreshold, bool& enableAgeGender, bool& enableFaceEmotions, bool& enableHeadPose, int &minFaceSize, float& faceDetectorScoreThreshold, bool& faceliveness, bool& antiSpoof, bool& removeFakeFace);
|
||||
|
||||
|
||||
bool UpdateFaceQueue(int queueSize, int faceThresholdSize, bool enableFaceQueue);
|
||||
bool GetFaceQueue(int& queueSize, int& faceThresholdSize, bool& enableFaceQueue);
|
||||
[[nodiscard]] std::vector<FaceResultObject> Recognize(const cv::Mat& frame); // Expect to return on 1 object
|
||||
[[nodiscard]] std::vector<FaceResultObject> Inference(const cv::Mat& input);
|
||||
[[nodiscard]] std::vector<FaceResultObject> Detect(const cv::Mat& input);
|
||||
[[nodiscard]] std::vector<Object> FaceDetect(const cv::Mat& input);
|
||||
|
||||
[[nodiscard]] std::vector<FaceResultObject> Recognize(const cv::Mat& frame, const std::string& camera_id); // Expect to return on 1 object
|
||||
[[nodiscard]] std::vector<FaceResultObject> Inference(const cv::Mat& input, const std::string& camera_id);
|
||||
[[nodiscard]] std::vector<FaceResultObject> Detect(const cv::Mat& input, const std::string& camera_id);
|
||||
[[nodiscard]] std::vector<Object> FaceDetect(const cv::Mat& input, const std::string& camera_id);
|
||||
|
||||
[[nodiscard]] std::string FaceObjectsToJsonString(const std::vector<FaceResultObject>& faces);
|
||||
[[nodiscard]] std::string FaceToJsonString(const std::vector<Object>& faces);
|
||||
|
||||
[[nodiscard]] std::string PolygonToString(const std::vector<cv::Point2f>& polygon);
|
||||
[[nodiscard]] std::string KeypointsToString(const std::vector<float>& kps);
|
||||
enum class LogLevel { Debug, Info, Warn, Error, Fatal };
|
||||
void LogThreadSafe(const std::string& function, const std::string& message,
|
||||
LogLevel level = LogLevel::Error);
|
||||
void LogError(const std::string& function,const std::string& message,const std::string& camera_id);
|
||||
void MarkAsUnknown(FaceResultObject& face);
|
||||
ANSFacialRecognition();
|
||||
~ANSFacialRecognition() noexcept;
|
||||
void UnloadEngine();
|
||||
void Destroy();
|
||||
private:
|
||||
int GetUser(int userId, UserRecord& userRecord);
|
||||
int GetUser(int userId, const std::string& userCode,const std::string& userName, UserRecord& userRecord);
|
||||
int GetUsers(std::vector<UserRecord>& userRecords, std::vector<int>& userIds);
|
||||
int GetFace(int faceId, FaceRecord& faceRecord);
|
||||
int GetFaces(int userId, std::vector<FaceRecord>& faceRecords);
|
||||
std::vector<FaceResultObject> UpdateFaceAttributes(const std::vector<FaceResultObject>& resultObjects,
|
||||
const std::string& camera_id = "");
|
||||
std::vector<Object> CheckFaces(const std::vector<Object>& faceObjects,bool checkFaceSize=true);
|
||||
std::string GetOpenVINODevice();
|
||||
ModelConfig CreateDetectorModelConfig();
|
||||
bool InitializeDetector();
|
||||
bool InitializeRecognizer();
|
||||
bool InitializeAntispoofingModel(const std::string& deviceName);
|
||||
bool InitializeAgeGenderModel(const std::string& deviceName);
|
||||
bool InitializeEmotionModel(const std::string& deviceName);
|
||||
bool InitializeHeadPoseModel(const std::string& deviceName);
|
||||
void ensureUniqueUserIdWithHighestConfidence(std::vector<FaceResultObject>& resultObjects);
|
||||
Object GetLargestObject(const std::vector<Object>& objects);
|
||||
bool AreFacesSimilar(const FaceResultObject& face1, const FaceResultObject& face2);
|
||||
size_t GenerateFaceHash(const FaceResultObject& face, const std::vector<FaceResultObject>& detectedObjects);
|
||||
std::vector<FaceResultObject> FindFrequentFaces(const std::deque<std::vector<FaceResultObject>>& faceQueue,
|
||||
const std::vector<FaceResultObject>& detectedObjects, int occurrenceThreshold = 8);
|
||||
protected:
|
||||
bool _licenseValid;
|
||||
std::string _licenseKey;
|
||||
std::string _detectorFilePath;
|
||||
std::string _recognizerFilePath;
|
||||
std::string _recognizerModelFolder;
|
||||
std::string _databaseFilePath;
|
||||
int _precisionType;
|
||||
std::map<std::string, std::string> _userDict;
|
||||
ANSCENTER::FRConfig _config;
|
||||
std::unique_ptr<FaceDatabase> _db =std::make_unique<FaceDatabase>();
|
||||
EngineType engineType;
|
||||
bool _enableAgeGender;
|
||||
bool _enableFaceEmotions;
|
||||
bool _enableFaceLandmarks;
|
||||
bool _enableHeadPose;
|
||||
bool _enableAntiSpoof;
|
||||
bool _enableFaceliveness;
|
||||
bool _removeFakeFaces;
|
||||
bool _isInitialized{false};
|
||||
int _minFaceSize;
|
||||
int _queueSize; // number of frames to store in the queue
|
||||
int _faceThresholdSize; // number of frames to consider for face recognition
|
||||
bool _enableFaceQueue;
|
||||
double _faceDetectorScoreThreshold; // Face threshold score for face checker
|
||||
int _attributeInterval{ 5 }; // Run slow attributes every N frames (1=every frame)
|
||||
FaceChecker faceChecker;
|
||||
|
||||
|
||||
// Add fine-grained mutexes for shared resources
|
||||
std::mutex _detectionMutex; // Protects face detector
|
||||
std::mutex _recognitionMutex; // Protects face recognizer (Feature/Forward inference only)
|
||||
std::mutex _databaseMutex; // For database operations
|
||||
|
||||
// Double-buffer synchronization: protects atomic swap of recognizer index + userDict
|
||||
// shared_lock for inference (concurrent reads), unique_lock for reload (exclusive write)
|
||||
mutable std::shared_mutex _indexSwapMutex;
|
||||
|
||||
// Reload guard: prevents redundant index rebuilds.
|
||||
// Set to true after any DB mutation; cleared by a successful Reload().
|
||||
// If third-party code calls Reload() after a mutation that already auto-reloaded,
|
||||
// the flag will be false and Reload() returns immediately (no-op).
|
||||
std::atomic<bool> _reloadNeeded{ true };
|
||||
|
||||
|
||||
std::mutex _ageGenderMutex; // For age/gender detector
|
||||
std::mutex _emotionsMutex; // For emotions detector
|
||||
std::mutex _antispoofMutex; // For antispoof detector
|
||||
std::mutex _headPoseMutex; // For head pose detector
|
||||
|
||||
std::mutex _loggerMutex; // Protects logger (if not thread-safe)
|
||||
std::mutex _faceQueueMutex; // NEW: Protects face queue operations
|
||||
|
||||
std::mutex _configMutex; // For configuration
|
||||
std::mutex _cameraMutex; // For camera data
|
||||
|
||||
|
||||
|
||||
//std::unordered_map<std::string, std::deque<std::vector<FaceResultObject>>> _cameraFaceQueues;
|
||||
|
||||
|
||||
std::recursive_mutex _mutex;
|
||||
ANSCENTER::SPDLogger& _logger = ANSCENTER::SPDLogger::GetInstance("ANSFR");
|
||||
std::shared_ptr<ov::Core> core; // Persistent OpenVINO core instance
|
||||
std::unique_ptr<ANSFRBase>_recognizer = nullptr;
|
||||
std::unique_ptr<ANSFDBase>_detector = nullptr;
|
||||
std::unique_ptr<AntispoofingClassifier>_antiSpoofDetector = nullptr;
|
||||
std::unique_ptr<AgeGenderDetection>_ageGenderDetector = nullptr;
|
||||
std::unique_ptr<HeadPoseDetection>_headPoseDetector = nullptr;
|
||||
std::unique_ptr<EmotionsDetection>_emotionsDetector = nullptr;
|
||||
const size_t QUEUE_SIZE = 10;
|
||||
const size_t FACE_THRESHOLD_SIZE = 5;
|
||||
// Cached attribute JSON per tracked face (everything except head pose).
|
||||
// Keyed by trackId from ANSMOT. Updated every _attributeInterval frames.
|
||||
// On non-attribute frames, head pose is run fresh and spliced into this JSON.
|
||||
struct CachedFaceAttributes {
|
||||
std::string attributeJson; // Full JSON from FaceAttributeToJsonString
|
||||
bool isUnknown = false; // Whether face was marked unknown (extreme pose etc.)
|
||||
};
|
||||
|
||||
struct CameraData
|
||||
{
|
||||
std::deque<std::vector<FaceResultObject>> _detectionQueue; // That stores the detection results
|
||||
int attributeFrameCounter = 0; // counts frames for attribute skip logic
|
||||
std::unordered_map<int, CachedFaceAttributes> cachedAttributes; // trackId → cached attrs
|
||||
|
||||
// Adaptive interval state
|
||||
int currentAttributeInterval = 5; // current adaptive interval
|
||||
int stableFrameCount = 0; // frames with no new/lost trackIds
|
||||
int previousTrackIdCount = 0; // trackId count from last frame
|
||||
|
||||
void clear()
|
||||
{
|
||||
for (auto& detectionVector : _detectionQueue)
|
||||
{
|
||||
detectionVector.clear();
|
||||
}
|
||||
_detectionQueue.clear();
|
||||
attributeFrameCounter = 0;
|
||||
cachedAttributes.clear();
|
||||
currentAttributeInterval = 5;
|
||||
stableFrameCount = 0;
|
||||
previousTrackIdCount = 0;
|
||||
}
|
||||
};
|
||||
std::unordered_map<std::string, CameraData> _cameras;
|
||||
CameraData& GetCameraData(const std::string& cameraId) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
|
||||
// Use try_emplace to insert a default CameraData if cameraId does not exist
|
||||
auto [iterator, inserted] = _cameras.try_emplace(cameraId, CameraData{});
|
||||
|
||||
return iterator->second; // Return the reference to CameraData
|
||||
}
|
||||
void EnqueueDetection(const std::vector<FaceResultObject>& detectedObjects, const std::string& cameraId);
|
||||
std::deque<std::vector<FaceResultObject>> DequeueDetection(const std::string& cameraId);
|
||||
std::vector<FaceResultObject> PostProcess(const std::vector<FaceResultObject>& detectedObjects, const std::string& cameraId);
|
||||
std::vector<FaceResultObject> BuildFaceResultObjects(const std::vector<Object>& validFaces, const cv::Mat& originalInput, const std::string& camera_id);
|
||||
void ProcessFaceAttributes(Face& face, const cv::Mat& mask);
|
||||
// Validation helper methods
|
||||
bool ValidateInitialization();
|
||||
bool ValidateInput(const cv::Mat& input);
|
||||
bool ValidateComponents();
|
||||
// Frame preparation
|
||||
cv::Mat PrepareInputFrame(const cv::Mat& input);
|
||||
// Pipeline stage methods with timing
|
||||
std::vector<Object> DetectFaces(const cv::Mat& frame, const std::string& camera_id);
|
||||
std::vector<FaceResultObject> RecognizeFaces(const cv::Mat& frame, const std::vector<Object>& validFaces);
|
||||
};
|
||||
|
||||
};
|
||||
extern "C" ANSFR_API int CreateANSRFHandle(ANSCENTER::ANSFacialRecognition * *Handle,
|
||||
const char* licenseKey,
|
||||
const char* configFilePath,
|
||||
const char* databaseFilePath,
|
||||
const char* recogniserFilePath,
|
||||
const char* detectorFilePath,
|
||||
int precisionType=0,
|
||||
float knownPersonThreshold=0.35,
|
||||
int enableAgeGender=1,
|
||||
int enableFaceEmotions=1,
|
||||
int enableHeadPose=1,
|
||||
int minFaceSize=30,
|
||||
float faceDetectorThreshold=0.5,
|
||||
int enableFaceLiveness=1,
|
||||
int enableAntiSpoofing=1);
|
||||
extern "C" ANSFR_API int LoadANSRFEngine(ANSCENTER::ANSFacialRecognition * *Handle);
|
||||
extern "C" ANSFR_API int ReleaseANSRFHandle(ANSCENTER::ANSFacialRecognition * *Handle);
|
||||
extern "C" ANSFR_API std::string RunANSRFDetector(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_string, unsigned int bufferLength);
|
||||
extern "C" ANSFR_API std::string RunANSRFDetectorBinary(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_bytes, unsigned int width, unsigned int height);
|
||||
extern "C" ANSFR_API std::string RunANSRFInference(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_string, unsigned int bufferLength);
|
||||
extern "C" ANSFR_API std::string RunANSRFInferenceBinary(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_bytes, unsigned int width, unsigned int height);
|
||||
extern "C" ANSFR_API std::string RunANSRFRecognition(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_string, unsigned int bufferLength);
|
||||
extern "C" ANSFR_API std::string RunANSRFRecognitionBinary(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_bytes, unsigned int width, unsigned int height);
|
||||
extern "C" ANSFR_API std::string RunANSRFFaceDetector(ANSCENTER::ANSFacialRecognition** Handle, unsigned char* jpeg_bytes, unsigned int width, unsigned int height);
|
||||
|
||||
|
||||
//// For LabVIEW API
|
||||
extern "C" ANSFR_API int RunDetector_LV(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_string, unsigned int bufferLength, LStrHandle detectionResult);
|
||||
extern "C" ANSFR_API int RunInference_LV(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_string, unsigned int bufferLength, LStrHandle detectionResult);
|
||||
extern "C" ANSFR_API int RunRecognition_LV(ANSCENTER::ANSFacialRecognition * *Handle, unsigned char* jpeg_string, unsigned int bufferLength, LStrHandle detectionResult);
|
||||
|
||||
|
||||
extern "C" ANSFR_API int RunDetectorWithCamId_LV(ANSCENTER::ANSFacialRecognition** Handle, unsigned char* jpeg_string, unsigned int bufferLength, const char* cameraId,LStrHandle detectionResult);
|
||||
extern "C" ANSFR_API int RunInferenceWithCamId_LV(ANSCENTER::ANSFacialRecognition** Handle, unsigned char* jpeg_string, unsigned int bufferLength, const char* cameraId,LStrHandle detectionResult);
|
||||
extern "C" ANSFR_API int RunRecognitionWithCamId_LV(ANSCENTER::ANSFacialRecognition** Handle, unsigned char* jpeg_string, unsigned int bufferLength, const char* cameraId,LStrHandle detectionResult);
|
||||
extern "C" ANSFR_API int RunFaceDetection_LV(ANSCENTER::ANSFacialRecognition** Handle, unsigned char* jpeg_string, unsigned int bufferLength, const char* cameraId, LStrHandle detectionResult);
|
||||
|
||||
extern "C" ANSFR_API int RunInferenceComplete_LV(ANSCENTER::ANSFacialRecognition** Handle, cv::Mat** cvImage, const char* cameraId, int getJpegString, int jpegImageSize, LStrHandle detectionResult, LStrHandle imageStr);
|
||||
extern "C" ANSFR_API int RunFaceDetectionComplete_LV(ANSCENTER::ANSFacialRecognition** Handle, cv::Mat** cvImage, const char* cameraId, int getJpegString, int jpegImageSize,LStrHandle detectionResult, LStrHandle imageStr);
|
||||
extern "C" ANSFR_API int RunFaceRecogniserComplete_LV(ANSCENTER::ANSFacialRecognition** Handle, cv::Mat** cvImage, const char* cameraId, int getJpegString, int jpegImageSize,LStrHandle detectionResult, LStrHandle imageStr);
|
||||
|
||||
|
||||
// User management APIs
|
||||
extern "C" ANSFR_API int InsertUser(ANSCENTER::ANSFacialRecognition * *Handle, const char* userCode, const char* userName);
|
||||
extern "C" ANSFR_API int UpdateUser(ANSCENTER::ANSFacialRecognition * *Handle, int userId, const char* userCode, const char* userName);
|
||||
extern "C" ANSFR_API int DeleteUser(ANSCENTER::ANSFacialRecognition * *Handle, int userId);
|
||||
extern "C" ANSFR_API int DeleteUsers(ANSCENTER::ANSFacialRecognition * *Handle, int* userIds, int count);
|
||||
extern "C" ANSFR_API int InsertFace(ANSCENTER::ANSFacialRecognition * *Handle, int userId, unsigned char* jpeg_string, unsigned int bufferLength);
|
||||
extern "C" ANSFR_API int InsertFaces(ANSCENTER::ANSFacialRecognition** Handle, int userId, unsigned char* jpeg_string, unsigned int bufferLength, LStrHandle faceIds);
|
||||
extern "C" ANSFR_API int CheckFaceEmbedding(ANSCENTER::ANSFacialRecognition** Handle, unsigned char* jpeg_string, unsigned int bufferLength);
|
||||
|
||||
extern "C" ANSFR_API int InsertFaceBinary(ANSCENTER::ANSFacialRecognition * *Handle, int userId, unsigned char* jpeg_bytes, unsigned int width, unsigned int height);
|
||||
extern "C" ANSFR_API int DeleteFace(ANSCENTER::ANSFacialRecognition * *Handle, int faceId); // How to we know the faceId?
|
||||
extern "C" ANSFR_API int Reload(ANSCENTER::ANSFacialRecognition * *Handle);
|
||||
extern "C" ANSFR_API int UpdateParameters(ANSCENTER::ANSFacialRecognition * *Handle, float knownPersonThreshold, int enableAgeGender, int enableFaceEmotions, int enableHeadPose, int minFaceSize, float faceDetectorThreshold, int faceliveness, int antiSpoof, int removeFakeFaces);
|
||||
extern "C" ANSFR_API int GetParamters(ANSCENTER::ANSFacialRecognition** Handle, LStrHandle faceParams);
|
||||
extern "C" ANSFR_API int UpdateFaceQueue(ANSCENTER::ANSFacialRecognition** Handle, int queueSize, int numKnownFaceInQueue, int enableFaceQueue);
|
||||
extern "C" ANSFR_API int GetFaceQueue(ANSCENTER::ANSFacialRecognition** Handle, LStrHandle faceQueue);
|
||||
|
||||
|
||||
|
||||
extern "C" ANSFR_API int GetUserString(ANSCENTER::ANSFacialRecognition * *Handle, int userId, std::string& userRecord);
|
||||
extern "C" ANSFR_API int GetUsersString(ANSCENTER::ANSFacialRecognition * *Handle, std::string & userRecords, std::vector<int>& userIds);
|
||||
extern "C" ANSFR_API int GetFaceString(ANSCENTER::ANSFacialRecognition * *Handle, int faceId, std::string & faceRecord);
|
||||
extern "C" ANSFR_API int GetFacesString(ANSCENTER::ANSFacialRecognition * *Handle, int userId, std::string & faceRecords);
|
||||
|
||||
// For LabVIEW
|
||||
extern "C" ANSFR_API int GetUser(ANSCENTER::ANSFacialRecognition * *Handle,int userId, LStrHandle userRecord);
|
||||
extern "C" ANSFR_API int GetUsers(ANSCENTER::ANSFacialRecognition * *Handle,LStrHandle userRecords);
|
||||
extern "C" ANSFR_API int GetFace(ANSCENTER::ANSFacialRecognition * *Handle,int faceId, LStrHandle faceRecord);
|
||||
extern "C" ANSFR_API int GetFaces(ANSCENTER::ANSFacialRecognition * *Handle,int userId, LStrHandle faceRecords);
|
||||
extern "C" ANSFR_API int DeleteFacesByUser(ANSCENTER::ANSFacialRecognition * *Handle,int userId);
|
||||
extern "C" ANSFR_API double BlurCalculation(unsigned char* jpeg_string, unsigned int bufferLength);
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,370 @@
|
||||
#ifndef ANSFRCOMMON_H
|
||||
#define ANSFRCOMMON_H
|
||||
#define ANSF_API __declspec(dllexport)
|
||||
|
||||
#pragma once
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include "NvInfer.h"
|
||||
//#include <cublasLt.h>
|
||||
#include "ANSLicense.h"
|
||||
#include "LabVIEWHeader/extcode.h"
|
||||
#include "Utility.h"
|
||||
#include "macros.h"
|
||||
#include "ANSEngineCommon.h"
|
||||
#include "openvino/openvino.hpp"
|
||||
using namespace nvinfer1;
|
||||
#define CUDACHECK(status) \
|
||||
do\
|
||||
{\
|
||||
auto ret = (status);\
|
||||
if (ret != 0)\
|
||||
{\
|
||||
std::cerr << "Cuda failure: " << ret << std::endl;\
|
||||
abort();\
|
||||
}\
|
||||
} while (0)
|
||||
|
||||
namespace ANSCENTER {
|
||||
|
||||
struct UserRecord {
|
||||
int UserId;
|
||||
std::string UserCode;
|
||||
std::string UserName;
|
||||
std::vector<int> FaceIds;
|
||||
};
|
||||
struct FaceRecord {
|
||||
int FaceId;
|
||||
int UserId;
|
||||
std::string ImagePath;
|
||||
};
|
||||
|
||||
struct alignas(float) Detection {
|
||||
float bbox[4]; //x1 y1 x2 y2
|
||||
float class_confidence;
|
||||
float landmark[10];
|
||||
};
|
||||
|
||||
struct CroppedFace {
|
||||
cv::Mat face;
|
||||
cv::Mat faceMat;
|
||||
int x1, y1, x2, y2;
|
||||
};
|
||||
|
||||
struct Bbox {
|
||||
int x1, y1, x2, y2;
|
||||
float score;
|
||||
};
|
||||
|
||||
struct Paths {
|
||||
std::string absPath;
|
||||
std::string className;
|
||||
};
|
||||
struct AnchorBox {
|
||||
float cx;
|
||||
float cy;
|
||||
float sx;
|
||||
float sy;
|
||||
};
|
||||
|
||||
struct FRConfig {
|
||||
std::string _databasePath;
|
||||
int _videoFrameWidth;
|
||||
int _videoFrameHeight;
|
||||
std::string _detEngineFile;
|
||||
std::vector<int> _detInputShape;
|
||||
std::string _detInputName;
|
||||
std::vector<std::string> _detOutputNames;
|
||||
int _detMaxBatchSize;
|
||||
float _detThresholdNMS;
|
||||
float _detThresholdBbox;
|
||||
std::vector<int> _recInputShape;
|
||||
int _recOutputDim;
|
||||
std::string _recEngineFile;
|
||||
int _maxFacesPerScene;
|
||||
float _knownPersonThreshold;
|
||||
int _recMaxBatchSize;
|
||||
std::string _recInputName;
|
||||
std::string _recOutputName;
|
||||
bool _gen;
|
||||
std::string _gen_imgSource;
|
||||
bool _gen_imgIsCropped;
|
||||
bool _apiImageIsCropped;
|
||||
};
|
||||
|
||||
|
||||
struct HeadPoseResults {
|
||||
float angle_r;
|
||||
float angle_p;
|
||||
float angle_y;
|
||||
};
|
||||
|
||||
struct AgeGenderResults {
|
||||
float age;
|
||||
float maleProb;
|
||||
};
|
||||
//// Class for face attribute
|
||||
class ANSF_API Face {
|
||||
public:
|
||||
//using Ptr = std::shared_ptr<Face>;
|
||||
explicit Face(size_t id, cv::Rect& location);
|
||||
void updateAge(float value);
|
||||
void updateGender(float value);
|
||||
void updateEmotions(std::map<std::string, float> values);
|
||||
void updateHeadPose(HeadPoseResults values);
|
||||
void updateLandmarks(std::vector<float> values);
|
||||
void updateRealFaceConfidence(float value);
|
||||
void updateFaceLiveness(int value);
|
||||
|
||||
int getAge();
|
||||
bool isMale();
|
||||
bool isReal();
|
||||
float getAntispoofingScore();
|
||||
int getFaceLiveness();
|
||||
std::map<std::string, float> getEmotions();
|
||||
std::pair<std::string, float> getMainEmotion();
|
||||
HeadPoseResults getHeadPose();
|
||||
const std::vector<float>& getLandmarks();
|
||||
size_t getId();
|
||||
|
||||
void ageGenderEnable(bool value);
|
||||
void emotionsEnable(bool value);
|
||||
void headPoseEnable(bool value);
|
||||
void landmarksEnable(bool value);
|
||||
void antispoofingEnable(bool value);
|
||||
void faceLivenessEnable(bool value);
|
||||
|
||||
bool isAgeGenderEnabled();
|
||||
bool isEmotionsEnabled();
|
||||
bool isHeadPoseEnabled();
|
||||
bool isLandmarksEnabled();
|
||||
bool isAntispoofingEnabled();
|
||||
bool isFaceLivenessEnabled();
|
||||
|
||||
public:
|
||||
cv::Rect _location;
|
||||
float _intensity_mean;
|
||||
|
||||
private:
|
||||
size_t _id;
|
||||
float _age;
|
||||
float _maleScore;
|
||||
float _femaleScore;
|
||||
std::map<std::string, float> _emotions;
|
||||
HeadPoseResults _headPose;
|
||||
|
||||
std::vector<float> _landmarks;
|
||||
float _realFaceConfidence;
|
||||
int _faceLiveness;
|
||||
bool _isAgeGenderEnabled;
|
||||
bool _isEmotionsEnabled;
|
||||
bool _isHeadPoseEnabled;
|
||||
bool _isLandmarksEnabled;
|
||||
bool _isAntispoofingEnabled;
|
||||
bool _isFaceLivenessEnabled;
|
||||
};
|
||||
|
||||
class ANSF_API BaseDetection {
|
||||
public:
|
||||
BaseDetection(const std::string& pathToModel, bool doRawOutputMessages);
|
||||
virtual ~BaseDetection() = default;
|
||||
virtual std::shared_ptr<ov::Model> read(const ov::Core& core) = 0;
|
||||
bool enabled() const;
|
||||
ov::InferRequest request;
|
||||
ov::Tensor inTensor;
|
||||
std::string pathToModel;
|
||||
ov::Shape inShape;
|
||||
const bool doRawOutputMessages;
|
||||
};
|
||||
|
||||
class ANSF_API HeadPoseDetection :public BaseDetection {
|
||||
public:
|
||||
std::recursive_mutex _mutex;
|
||||
std::string outputAngleR;
|
||||
std::string outputAngleP;
|
||||
std::string outputAngleY;
|
||||
std::string _modelFilePath;
|
||||
//size_t enquedFaces;
|
||||
cv::Mat cameraMatrix;
|
||||
HeadPoseDetection(const std::string& pathToModel, bool doRawOutputMessages);
|
||||
virtual std::shared_ptr<ov::Model> read(const ov::Core& core) override;
|
||||
HeadPoseResults runInfer(const cv::Mat& frame);
|
||||
|
||||
//void submitRequest();
|
||||
//void enqueue(const cv::Mat& face);
|
||||
//HeadPoseResults getResult(int idx);
|
||||
//std::vector<HeadPoseResults> getAllResults();
|
||||
};
|
||||
|
||||
class ANSF_API AgeGenderDetection :public BaseDetection {
|
||||
public:
|
||||
std::recursive_mutex _mutex;
|
||||
std::string outputAge;
|
||||
std::string outputGender;
|
||||
//size_t enquedFaces;
|
||||
std::string _modelFilePath;
|
||||
AgeGenderDetection(const std::string& pathToModel,
|
||||
bool doRawOutputMessages);
|
||||
|
||||
virtual std::shared_ptr<ov::Model> read(const ov::Core& core) override;
|
||||
AgeGenderResults runInfer(const cv::Mat& frame);
|
||||
//void submitRequest();
|
||||
//void enqueue(const cv::Mat& face);
|
||||
//AgeGenderResults getResult(int idx);
|
||||
//std::vector<AgeGenderResults> getAllResults();
|
||||
|
||||
};
|
||||
|
||||
class ANSF_API EmotionsDetection : public BaseDetection {
|
||||
public:
|
||||
std::recursive_mutex _mutex;
|
||||
//size_t enquedFaces;
|
||||
std::string _modelFilePath;
|
||||
const std::vector<std::string> emotionsVec = { "neutral", "happy", "sad", "surprise", "anger" };
|
||||
|
||||
EmotionsDetection(const std::string& pathToModel,
|
||||
bool doRawOutputMessages);
|
||||
|
||||
virtual std::shared_ptr<ov::Model> read(const ov::Core& core) override;
|
||||
std::map<std::string, float> runInfer(const cv::Mat& frame);
|
||||
//void submitRequest();
|
||||
//void enqueue(const cv::Mat& face);
|
||||
//std::map<std::string, float> getResult(int idx);
|
||||
//std::vector<std::map<std::string, float>> getAllResults();
|
||||
};
|
||||
|
||||
class FacialLandmarksDetection : public BaseDetection {
|
||||
public:
|
||||
//size_t enquedFaces;
|
||||
std::vector<std::vector<float>> landmarks_results;
|
||||
std::vector<cv::Rect> faces_bounding_boxes;
|
||||
std::string _modelFilePath;
|
||||
std::recursive_mutex _mutex;
|
||||
FacialLandmarksDetection(const std::string& pathToModel,
|
||||
bool doRawOutputMessages);
|
||||
virtual std::shared_ptr<ov::Model> read(const ov::Core& core) override;
|
||||
std::vector<float> runInfer(const cv::Mat& frame);
|
||||
|
||||
//void submitRequest();
|
||||
//void enqueue(const cv::Mat& face);
|
||||
//std::vector<float> getResult(int idx);
|
||||
|
||||
};
|
||||
|
||||
class ANSF_API AntispoofingClassifier : public BaseDetection {
|
||||
public:
|
||||
std::recursive_mutex _mutex;
|
||||
//size_t enquedFaces;
|
||||
std::string _modelFilePath;
|
||||
AntispoofingClassifier(const std::string& pathToModel,bool doRawOutputMessages);
|
||||
virtual std::shared_ptr<ov::Model> read(const ov::Core& core) override;
|
||||
float runInfer(const cv::Mat& frame);
|
||||
//void submitRequest();
|
||||
//void enqueue(const cv::Mat& frame);
|
||||
//float getResult(int idx);
|
||||
};
|
||||
class ANSFRHelper {
|
||||
public:
|
||||
static unsigned char* CVMatToBytes(cv::Mat image, unsigned int& bufferLengh);
|
||||
static void GetCroppedFaces(const cv::Mat& input, std::vector<Object>& outputBbox, int resize_w, int resize_h, std::vector<struct CroppedFace>& croppedFaces);
|
||||
static void GetFilePaths(std::string rootPath, std::vector<struct Paths>& paths);
|
||||
static bool LoadConfigFile(std::string configFile, FRConfig& config);
|
||||
static std::string StringCurrentDateTime(); // Return current datetime in string format
|
||||
static std::string FaceObjectsToJsonString(const std::vector<FaceResultObject>& faces);
|
||||
static cv::Mat PreprocessImg(cv::Mat& img, int inputW, int inputH);
|
||||
static int ReadFilesInDir(const char* pDirName, std::vector<std::string>& fileNames);
|
||||
static cv::Rect GetRectAdaptLandmark(cv::Mat& img, int inputW, int inputH, float bBox[4], float lmk[10]);
|
||||
static float IOU(float lbox[4], float rbox[4]);
|
||||
static bool CMP(const Detection& a, const Detection& b);
|
||||
static void NMS(std::vector<Detection>& res, float* output, float nms_thresh = 0.4);
|
||||
static std::map<std::string, Weights> LoadWeights(const std::string file);
|
||||
static Weights GetWeights(std::map<std::string, Weights>& weightMap, std::string key);
|
||||
static IScaleLayer* AddBatchNorm2D(INetworkDefinition* network, std::map<std::string, Weights>& weightMap, ITensor& input, std::string lName, float eps);
|
||||
static std::string UserRecordToJsonString(const UserRecord userRecord);
|
||||
static std::string UserRecordsToJsonString(const std::vector<UserRecord> userRecords);
|
||||
static std::string FaceRecordToJsonString(const FaceRecord faceRecord);
|
||||
static std::string FaceRecordsToJsonString(const std::vector<FaceRecord> faceRecords);
|
||||
static std::string FaceAttributeToJsonString(Face face);
|
||||
};
|
||||
float calcIoU(cv::Rect& src, cv::Rect& dst);
|
||||
float calcMean(const cv::Mat& src);
|
||||
Face matchFace(cv::Rect rect, std::list<Face>& faces);
|
||||
|
||||
|
||||
// Class to extend TensorRT logger
|
||||
/* class TRTLogger : public nvinfer1::ILogger {
|
||||
public:
|
||||
void log(Severity severity, const char* msg) noexcept override {
|
||||
if (severity <= Severity::kWARNING) {
|
||||
std::cout << "TENSORRT_ENGINE:" << msg << std::endl;
|
||||
}
|
||||
}
|
||||
void LogDebug(const std::string source, std::string message) {
|
||||
std::cout << source << ":" << message << std::endl;
|
||||
}
|
||||
void LogInfo(const std::string source, std::string message) {
|
||||
std::cout << source << ":" << message << std::endl;
|
||||
}
|
||||
void LogError(const std::string source, std::string message) {
|
||||
std::cout << source << ":" << message << std::endl;
|
||||
}
|
||||
void LogFatal(const std::string source, std::string message) {
|
||||
std::cout << source << ":" << message << std::endl;
|
||||
}
|
||||
~TRTLogger() {
|
||||
}
|
||||
};
|
||||
class MatMul {
|
||||
public:
|
||||
MatMul();
|
||||
~MatMul();
|
||||
void Init(float* knownEmbeds, int numRow, int numCol);
|
||||
void Calculate(float* embeds, int embedCount, float* outputs);
|
||||
|
||||
private:
|
||||
cudaDataType_t cudaDataType = CUDA_R_32F;
|
||||
cublasComputeType_t computeType = CUBLAS_COMPUTE_32F;
|
||||
cublasLtHandle_t ltHandle;
|
||||
cublasOperation_t transa = CUBLAS_OP_T;
|
||||
cublasOperation_t transb = CUBLAS_OP_N;
|
||||
void* workspace;
|
||||
const size_t workspaceSize = 1024 * 1024 * 4;
|
||||
cudaStream_t stream;
|
||||
float* dA, * dB, * dC;
|
||||
const float alpha = 1, beta = 0;
|
||||
int m, n, k, lda, ldb, ldc;
|
||||
cublasLtMatmulDesc_t operationDesc = NULL;
|
||||
cublasLtMatrixLayout_t Adesc = NULL;
|
||||
cublasLtMatmulPreference_t preference = NULL;
|
||||
};
|
||||
class Int8EntropyCalibrator2 : public nvinfer1::IInt8EntropyCalibrator2
|
||||
{
|
||||
public:
|
||||
Int8EntropyCalibrator2(int batchsize, int input_w, int input_h, const char* img_dir, const char* calib_table_name, const char* input_blob_name, bool read_cache = true);
|
||||
|
||||
virtual ~Int8EntropyCalibrator2();
|
||||
int getBatchSize() const TRT_NOEXCEPT override;
|
||||
bool getBatch(void* bindings[], const char* names[], int nbBindings) TRT_NOEXCEPT override;
|
||||
const void* readCalibrationCache(size_t& length) TRT_NOEXCEPT override;
|
||||
void writeCalibrationCache(const void* cache, size_t length) TRT_NOEXCEPT override;
|
||||
|
||||
private:
|
||||
int batchsize_;
|
||||
int input_w_;
|
||||
int input_h_;
|
||||
int img_idx_;
|
||||
std::string img_dir_;
|
||||
std::vector<std::string> img_files_;
|
||||
size_t input_count_;
|
||||
std::string calib_table_name_;
|
||||
const char* input_blob_name_;
|
||||
bool read_cache_;
|
||||
void* device_input_;
|
||||
std::vector<char> calib_cache_;
|
||||
};*/
|
||||
}
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,130 @@
|
||||
#ifndef ANSFACERECOGNISER_H
|
||||
#define ANSFACERECOGNISER_H
|
||||
#pragma once
|
||||
#include "ANSFRCommon.h"
|
||||
#include "hnswlib/hnswlib.h"
|
||||
#include "cnn.hpp"
|
||||
#include "face_reid.hpp"
|
||||
#include "openvino/openvino.hpp"
|
||||
#include <faiss/IndexFlat.h>
|
||||
#include <faiss/IndexIDMap.h>
|
||||
#include <faiss/gpu/GpuIndexFlat.h>
|
||||
#include <faiss/gpu/StandardGpuResources.h>
|
||||
#include <unordered_map>
|
||||
#include "engine.h"
|
||||
#include "engine/EnginePoolManager.h"
|
||||
#include "ONNXEngine.h"
|
||||
#define USE_ONNX_ENGINE
|
||||
//#define CPU_MODE
|
||||
//#define USE_CPU_BATCH_MODE
|
||||
namespace ANSCENTER {
|
||||
class ANSFaceRecognizer : public ANSFRBase {
|
||||
public:
|
||||
virtual bool Initialize(std::string licenseKey,
|
||||
ModelConfig modelConfig,
|
||||
const std::string& modelZipFilePath,
|
||||
const std::string& modelZipPassword,
|
||||
std::string& labelMap) override;
|
||||
|
||||
virtual bool LoadModel(const std::string& modelZipFilePath,
|
||||
const std::string& modelZipPassword) override;
|
||||
|
||||
bool OptimizeModel(bool fp16, std::string& optimizedModelFolder);
|
||||
|
||||
// Single face feature
|
||||
std::vector<float> Feature(const cv::Mat& image,const ANSCENTER::Object& bBox);
|
||||
|
||||
// Full pipeline: embeddings -> FAISS search -> results
|
||||
std::vector<FaceResultObject> Match(const cv::Mat& input, const std::vector<ANSCENTER::Object>& bBox, const std::map<std::string, std::string>& userDict);
|
||||
|
||||
cv::Mat GetCropFace(const cv::Mat& input, const ANSCENTER::Object& bBox);
|
||||
|
||||
void Init();
|
||||
void AddEmbedding(const std::string& className, float embedding[]);
|
||||
void AddEmbedding(const std::string& className, const std::vector<float>& embedding);
|
||||
|
||||
// Double-buffer support: atomically swap FAISS index + mapping
|
||||
void SwapIndex(std::shared_ptr<faiss::IndexIDMap> newIndex,
|
||||
std::unordered_map<faiss::idx_t, std::string>&& newFaceIdToUserId);
|
||||
|
||||
int GetEmbeddingSize() const { return FACE_EMBEDDING_SIZE; }
|
||||
std::shared_ptr<faiss::gpu::StandardGpuResources> GetGpuResources() const { return m_gpuResources; }
|
||||
|
||||
bool UpdateParamater(double knownPersonThreshold) {
|
||||
_modelConfig.unknownPersonThreshold = knownPersonThreshold;
|
||||
m_knownPersonThresh = _modelConfig.unknownPersonThreshold;
|
||||
return true;
|
||||
}
|
||||
|
||||
~ANSFaceRecognizer();
|
||||
bool Destroy();
|
||||
|
||||
// L2-normalize a vector in-place (public — used by ANSFR::Reload)
|
||||
static void L2NormalizeInPlace(std::vector<float>& vec);
|
||||
|
||||
private:
|
||||
bool LoadEngine(const std::string& xmlModelPath, bool engineOptimisation = true);
|
||||
|
||||
// Batched forward: one embedding per Object.mask (caller must hold _mutex)
|
||||
std::vector<std::vector<float>> ForwardUnlocked(const cv::Mat& input,const std::vector<ANSCENTER::Object>& outputBbox);
|
||||
|
||||
// FAISS search (caller must hold _mutex)
|
||||
std::tuple<std::vector<std::string>, std::vector<float>>
|
||||
SearchForFacesUnlocked(const std::vector<std::vector<float>>& detectedEmbeddings);
|
||||
|
||||
std::string GetOpenVINODevice();
|
||||
|
||||
// Single-face GPU inference
|
||||
std::vector<float> RunArcFace(const cv::Mat& input);
|
||||
|
||||
// Batched GPU inference
|
||||
std::vector<std::vector<float>> RunArcFaceBatch(
|
||||
const std::vector<cv::Mat>& faceROIs,
|
||||
const std::vector<cv::cuda::GpuMat>& gpuFaceROIs = {});
|
||||
|
||||
protected:
|
||||
const int GPU_FACE_WIDTH = 112;
|
||||
const int GPU_FACE_HEIGHT = 112;
|
||||
const int FACE_EMBEDDING_SIZE = 512;
|
||||
|
||||
std::unordered_map<faiss::idx_t, std::string> _faceIdToUserId;
|
||||
faiss::idx_t _nextFaceId = 0; // Sequential ID counter for backward-compat AddEmbedding
|
||||
ModelConfig _modelConfig;
|
||||
std::string _modelFilePath;
|
||||
std::string _landmarkModelFilePath;
|
||||
|
||||
ANSCENTER::Options m_options;
|
||||
const std::array<float, 3> SUB_VALS{ 0.5f, 0.5f, 0.5f };
|
||||
const std::array<float, 3> DIV_VALS{ 0.5f, 0.5f, 0.5f };
|
||||
const bool NORMALIZE = true;
|
||||
|
||||
std::recursive_mutex _mutex;
|
||||
float m_knownPersonThresh = 0.35f;
|
||||
EngineType engineType;
|
||||
|
||||
#ifdef USE_ONNX_ENGINE
|
||||
std::unique_ptr<GlintArcFace> faceRecognizer = nullptr;
|
||||
const int CPU_FACE_WIDTH = 112;
|
||||
const int CPU_FACE_HEIGHT = 112;
|
||||
#else
|
||||
std::unique_ptr<VectorCNN> faceRecognizer = nullptr; // OpenVINO
|
||||
const int CPU_FACE_WIDTH = 160;
|
||||
const int CPU_FACE_HEIGHT = 160;
|
||||
#endif
|
||||
// Pooled GPU buffers to avoid per-frame allocation (Fix #8)
|
||||
cv::cuda::Stream m_gpuStream;
|
||||
cv::cuda::GpuMat m_gpuImg;
|
||||
cv::cuda::GpuMat m_gpuResized;
|
||||
cv::cuda::GpuMat m_gpuRgb;
|
||||
|
||||
std::shared_ptr<Engine<float>> m_trtEngine = nullptr; // NVIDIA TensorRT
|
||||
EnginePoolManager<float>::PoolKey m_poolKey;
|
||||
bool m_usingSharedPool = false;
|
||||
int m_maxSlotsPerGpu{ -1 }; // -1 = elastic mode (on-demand slots, auto-cleanup)
|
||||
void SetMaxSlotsPerGpu(int n) override { m_maxSlotsPerGpu = n; }
|
||||
std::shared_ptr<faiss::IndexIDMap> faiss_index;
|
||||
std::shared_ptr<faiss::gpu::StandardGpuResources> m_gpuResources;
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,23 @@
|
||||
// ANSGpuFrameRegistry.cpp — Cross-DLL singleton resolver for ANSFR.dll.
|
||||
//
|
||||
// Finds the canonical ANSGpuFrameRegistry instance exported by ANSCV.dll
|
||||
// via GetProcAddress. No link dependency on ANSCV.lib needed.
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define NOMINMAX
|
||||
#include <windows.h>
|
||||
#include "ANSGpuFrameRegistry.h"
|
||||
|
||||
ANSGpuFrameRegistry* ANSGpuFrameRegistry::resolveProcessWide() {
|
||||
// ANSCV.dll is always loaded before inference starts (it provides RTSP).
|
||||
HMODULE hMod = GetModuleHandleA("ANSCV.dll");
|
||||
if (hMod) {
|
||||
typedef ANSGpuFrameRegistry* (*GetInstanceFn)();
|
||||
auto fn = reinterpret_cast<GetInstanceFn>(
|
||||
GetProcAddress(hMod, "ANSGpuFrameRegistry_GetInstance"));
|
||||
if (fn) return fn();
|
||||
}
|
||||
// Fallback: local instance (unit tests without ANSCV.dll).
|
||||
static ANSGpuFrameRegistry local;
|
||||
return &local;
|
||||
}
|
||||
@@ -0,0 +1,692 @@
|
||||
#include "ARCFaceRT.h"
|
||||
#include "NvOnnxParser.h"
|
||||
|
||||
namespace ANSCENTER {
|
||||
bool ArcFace::Initialize(std::string licenseKey,
|
||||
ModelConfig modelConfig,
|
||||
const std::string& modelZipFilePath,
|
||||
const std::string& modelZipPassword,
|
||||
std::string& labelMap) {
|
||||
bool result = ANSFRBase::Initialize(licenseKey, modelConfig, modelZipFilePath, modelZipPassword, labelMap);
|
||||
if (!result) return false;
|
||||
|
||||
try {
|
||||
_modelConfig = modelConfig;
|
||||
_modelConfig.modelType = ModelType::FACERECOGNIZE;
|
||||
_modelConfig.detectionType = DetectionType::FACERECOGNIZER;
|
||||
|
||||
m_knownPersonThresh = _modelConfig.unknownPersonThreshold;
|
||||
if (m_knownPersonThresh == 0.0f) m_knownPersonThresh = 0.35f;
|
||||
|
||||
std::string onnxfile50 = CreateFilePath(_modelFolder, "ansfacerecognizer50.onnx");
|
||||
if (std::filesystem::exists(onnxfile50)) {
|
||||
_modelFilePath = onnxfile50;
|
||||
_logger.LogDebug("ArcFace::Initialize. Loading arcface50 weight", _modelFilePath, __FILE__, __LINE__);
|
||||
}
|
||||
else {
|
||||
std::string onnxfile = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
|
||||
if (std::filesystem::exists(onnxfile)) {
|
||||
_modelFilePath = onnxfile;
|
||||
_logger.LogDebug("ArcFace::Initialize. Loading arcface weight", _modelFilePath, __FILE__, __LINE__);
|
||||
}
|
||||
else {
|
||||
_logger.LogError("ArcFace::Initialize. Model arcface.onnx file does not exist", _modelFilePath, __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Configure engine with batch support
|
||||
m_options.precision = ANSCENTER::Precision::FP32;
|
||||
m_options.optBatchSize = 8; // expected typical batch
|
||||
m_options.maxBatchSize = 32; // maximum number of faces per frame you want
|
||||
m_options.calibrationBatchSize = 8;
|
||||
m_options.deviceIndex = 0;
|
||||
m_trtEngine.UpdateOptions(m_options);
|
||||
|
||||
if (FileExist(_modelFilePath)) {
|
||||
bool succ = m_trtEngine.buildLoadNetwork(_modelFilePath, SUB_VALS, DIV_VALS, NORMALIZE);
|
||||
if (!succ) {
|
||||
_logger.LogError("ArcFace::Initialize. Unable to build or load TensorRT engine.",
|
||||
_modelFilePath, __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
_logger.LogError("ArcFace::Initialize. Model file does not exist",
|
||||
_modelFilePath, __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
Init();
|
||||
_isInitialized = true;
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ArcFace::Initialize", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ArcFace::LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword) {
|
||||
try {
|
||||
bool result = ANSFRBase::LoadModel(modelZipFilePath, modelZipPassword);
|
||||
if (!result) return false;
|
||||
|
||||
std::string onnxfile50 = CreateFilePath(_modelFolder, "ansfacerecognizer50.onnx");
|
||||
if (std::filesystem::exists(onnxfile50)) {
|
||||
_modelFilePath = onnxfile50;
|
||||
_logger.LogDebug("ArcFace::LoadModel. Loading arcface50 weight", _modelFilePath, __FILE__, __LINE__);
|
||||
}
|
||||
else {
|
||||
std::string onnxfile = CreateFilePath(_modelFolder, "ansfacerecognizer.onnx");
|
||||
if (std::filesystem::exists(onnxfile)) {
|
||||
_modelFilePath = onnxfile;
|
||||
_logger.LogDebug("ArcFace::LoadModel. Loading arcface weight", _modelFilePath, __FILE__, __LINE__);
|
||||
}
|
||||
else {
|
||||
_logger.LogError("ArcFace::LoadModel. Model arcface.onnx file does not exist",
|
||||
_modelFilePath, __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ArcFace50::LoadModel", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ArcFace::OptimizeModel(bool fp16, std::string& optimizedModelFolder) {
|
||||
if (!FileExist(_modelFilePath)) {
|
||||
optimizedModelFolder = "";
|
||||
return false;
|
||||
}
|
||||
optimizedModelFolder = GetParentFolder(_modelFilePath);
|
||||
|
||||
m_options.optBatchSize = 8;
|
||||
m_options.maxBatchSize = 32;
|
||||
m_options.engineFileDir = optimizedModelFolder;
|
||||
m_options.precision = Precision::FP32;
|
||||
|
||||
Engine<float> engine(m_options);
|
||||
|
||||
auto succ = engine.buildWithRetry(_modelFilePath, SUB_VALS, DIV_VALS, NORMALIZE);
|
||||
if (!succ) {
|
||||
const std::string errMsg =
|
||||
"Error: Unable to build the TensorRT engine. Try increasing TensorRT log severity to kVERBOSE.";
|
||||
_logger.LogError("ArcFace::OptimizeModel", errMsg, __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<float> ArcFace::Feature(const cv::Mat& image, const ANSCENTER::Object& bBox) {
|
||||
std::vector<float> embedding;
|
||||
|
||||
// Early validation before locking
|
||||
if (image.empty()) {
|
||||
return embedding;
|
||||
}
|
||||
|
||||
if (image.cols < 10 || image.rows < 10) {
|
||||
return embedding;
|
||||
}
|
||||
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
|
||||
try {
|
||||
return RunArcFace(bBox.mask);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ArcFace::Feature", e.what(), __FILE__, __LINE__);
|
||||
return std::vector<float>();
|
||||
}
|
||||
}
|
||||
std::vector<FaceResultObject> ArcFace::Match(
|
||||
const cv::Mat& input,
|
||||
const std::vector<ANSCENTER::Object>& bBox,
|
||||
const std::map<std::string, std::string>& userDict) {
|
||||
|
||||
std::vector<FaceResultObject> resultObjects;
|
||||
|
||||
// Early validation before locking
|
||||
if (input.empty()) {
|
||||
return resultObjects;
|
||||
}
|
||||
|
||||
if (input.cols < 10 || input.rows < 10) {
|
||||
return resultObjects;
|
||||
}
|
||||
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
|
||||
if (!_isInitialized) {
|
||||
_logger.LogError("ArcFace::Match", "Model is not initialized", __FILE__, __LINE__);
|
||||
return resultObjects;
|
||||
}
|
||||
|
||||
try {
|
||||
// Get embeddings
|
||||
std::vector<std::vector<float>> detectedEmbeddings = Forward(input, bBox);
|
||||
|
||||
// Search for matches
|
||||
std::vector<std::string> names;
|
||||
std::vector<float> sims;
|
||||
std::tie(names, sims) = SearchForFaces(detectedEmbeddings);
|
||||
|
||||
if (names.empty()) {
|
||||
_logger.LogError("ArcFace::Match", "No face is match", __FILE__, __LINE__);
|
||||
return resultObjects;
|
||||
}
|
||||
|
||||
// Pre-reserve result space
|
||||
const size_t resultCount = std::min(names.size(), bBox.size());
|
||||
resultObjects.reserve(resultCount);
|
||||
|
||||
// Build result objects
|
||||
for (size_t i = 0; i < resultCount; ++i) {
|
||||
FaceResultObject resultObject;
|
||||
|
||||
// Determine if face is known or unknown
|
||||
const bool isUnknown = (sims[i] > m_knownPersonThresh);
|
||||
|
||||
if (isUnknown) {
|
||||
resultObject.isUnknown = true;
|
||||
resultObject.userId = "0";
|
||||
resultObject.userName = "Unknown";
|
||||
resultObject.confidence = 1.0f;
|
||||
}
|
||||
else {
|
||||
resultObject.isUnknown = false;
|
||||
resultObject.userId = names[i];
|
||||
|
||||
// Safe map lookup with fallback
|
||||
auto it = userDict.find(names[i]);
|
||||
resultObject.userName = (it != userDict.end()) ? it->second : names[i];
|
||||
|
||||
resultObject.confidence = 1.0f - sims[i];
|
||||
}
|
||||
|
||||
resultObject.similarity = sims[i];
|
||||
|
||||
// Copy bounding box and additional data
|
||||
resultObject.box = bBox[i].box;
|
||||
resultObject.mask = bBox[i].mask;
|
||||
resultObject.cameraId = bBox[i].cameraId;
|
||||
resultObject.trackId = bBox[i].trackId;
|
||||
resultObject.polygon = bBox[i].polygon;
|
||||
resultObject.kps = bBox[i].kps;
|
||||
resultObject.extraInformation = bBox[i].extraInfo;
|
||||
|
||||
resultObjects.push_back(std::move(resultObject));
|
||||
}
|
||||
|
||||
return resultObjects;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ArcFace::Match", e.what(), __FILE__, __LINE__);
|
||||
return resultObjects;
|
||||
}
|
||||
}
|
||||
|
||||
cv::Mat ArcFace::GetCropFace(const cv::Mat& input, const ANSCENTER::Object& bBox) {
|
||||
try {
|
||||
std::vector<ANSCENTER::Object> outputBbox;
|
||||
outputBbox.reserve(1);
|
||||
outputBbox.push_back(bBox);
|
||||
|
||||
std::vector<CroppedFace> crFaces;
|
||||
ANSFRHelper::GetCroppedFaces(input, outputBbox, 112, 112, crFaces);
|
||||
|
||||
if (crFaces.empty()) {
|
||||
return cv::Mat();
|
||||
}
|
||||
|
||||
return crFaces[0].faceMat;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ArcFace::GetCropFace", e.what(), __FILE__, __LINE__);
|
||||
return cv::Mat();
|
||||
}
|
||||
}
|
||||
|
||||
bool ArcFace::LoadEngine(const std::string onnxModelPath, bool engineOptimisation) {
|
||||
try {
|
||||
if (!FileExist(onnxModelPath)) {
|
||||
_logger.LogError("ArcFace::LoadEngine", "Cannot find the raw ONNX model file.", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_options.precision = ANSCENTER::Precision::FP32;
|
||||
m_options.optBatchSize = 8;
|
||||
m_options.maxBatchSize = 32;
|
||||
m_options.calibrationBatchSize = 8;
|
||||
m_options.deviceIndex = 0;
|
||||
m_trtEngine.UpdateOptions(m_options);
|
||||
|
||||
if (FileExist(onnxModelPath)) {
|
||||
bool succ = m_trtEngine.buildLoadNetwork(onnxModelPath, SUB_VALS, DIV_VALS, NORMALIZE);
|
||||
if (!succ) {
|
||||
_logger.LogError("ArcFace::LoadEngine. Unable to build or load TensorRT engine.",
|
||||
onnxModelPath, __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
_logger.LogError("ArcFace::LoadEngine. Model file does not exist",
|
||||
onnxModelPath, __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ArcFace::LoadEngine", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<float> ArcFace::RunArcFace(const cv::Mat& inputImage) {
|
||||
std::vector<float> embedding;
|
||||
|
||||
// Early validation before locking
|
||||
if (inputImage.empty()) {
|
||||
_logger.LogError("ArcFace::RunArcFace", "Input image is empty", __FILE__, __LINE__);
|
||||
return embedding;
|
||||
}
|
||||
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
|
||||
try {
|
||||
if (!_isInitialized) {
|
||||
_logger.LogError("ArcFace::RunArcFace", "Model is not initialized", __FILE__, __LINE__);
|
||||
return embedding;
|
||||
}
|
||||
|
||||
// GPU preprocessing pipeline
|
||||
cv::cuda::Stream stream;
|
||||
cv::cuda::GpuMat d_img;
|
||||
|
||||
// Upload to GPU
|
||||
d_img.upload(inputImage, stream);
|
||||
|
||||
// Handle grayscale conversion on GPU
|
||||
if (inputImage.channels() == 1) {
|
||||
cv::cuda::GpuMat d_bgr;
|
||||
cv::cuda::cvtColor(d_img, d_bgr, cv::COLOR_GRAY2BGR, 0, stream);
|
||||
d_img = d_bgr;
|
||||
}
|
||||
|
||||
// Resize on GPU if needed
|
||||
if (inputImage.cols != FACE_WIDTH || inputImage.rows != FACE_HEIGHT) {
|
||||
cv::cuda::GpuMat d_resized;
|
||||
cv::cuda::resize(d_img, d_resized, cv::Size(FACE_WIDTH, FACE_HEIGHT),
|
||||
0, 0, cv::INTER_LINEAR, stream);
|
||||
d_img = d_resized;
|
||||
}
|
||||
|
||||
// BGR to RGB conversion on GPU
|
||||
cv::cuda::GpuMat d_rgb;
|
||||
cv::cuda::cvtColor(d_img, d_rgb, cv::COLOR_BGR2RGB, 0, stream);
|
||||
|
||||
// Prepare inference inputs
|
||||
std::vector<cv::cuda::GpuMat> inputVec;
|
||||
inputVec.emplace_back(std::move(d_rgb));
|
||||
|
||||
std::vector<std::vector<cv::cuda::GpuMat>> inputs;
|
||||
inputs.emplace_back(std::move(inputVec));
|
||||
|
||||
// Run inference
|
||||
std::vector<std::vector<std::vector<float>>> featureVectors;
|
||||
bool succ = m_trtEngine.runInference(inputs, featureVectors);
|
||||
|
||||
stream.waitForCompletion();
|
||||
|
||||
if (!succ) {
|
||||
_logger.LogError("ArcFace::RunArcFace", "Failed to run inference.", __FILE__, __LINE__);
|
||||
return embedding;
|
||||
}
|
||||
|
||||
if (!featureVectors.empty() && !featureVectors[0].empty()) {
|
||||
embedding = std::move(featureVectors[0][0]);
|
||||
}
|
||||
|
||||
return embedding;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ArcFace::RunArcFace", e.what(), __FILE__, __LINE__);
|
||||
return embedding;
|
||||
}
|
||||
}
|
||||
std::vector<std::vector<float>> ArcFace::RunArcFaceBatch(const std::vector<cv::Mat>& faceROIs) {
|
||||
std::vector<std::vector<float>> embeddings;
|
||||
|
||||
try {
|
||||
if (!_isInitialized) {
|
||||
_logger.LogError("ArcFace::RunArcFaceBatch", "Model is not initialized", __FILE__, __LINE__);
|
||||
return embeddings;
|
||||
}
|
||||
|
||||
if (faceROIs.empty()) {
|
||||
return embeddings;
|
||||
}
|
||||
|
||||
if (faceROIs.size() > static_cast<size_t>(m_options.maxBatchSize)) {
|
||||
_logger.LogError("ArcFace::RunArcFaceBatch",
|
||||
"Batch size exceeds maxBatchSize", __FILE__, __LINE__);
|
||||
return embeddings;
|
||||
}
|
||||
|
||||
const auto& inputDims = m_trtEngine.getInputDims();
|
||||
if (inputDims.empty() || inputDims[0].nbDims < 3) {
|
||||
_logger.LogError("ArcFace::RunArcFaceBatch",
|
||||
"Invalid engine input dims", __FILE__, __LINE__);
|
||||
return embeddings;
|
||||
}
|
||||
|
||||
// Pre-reserve embeddings space
|
||||
embeddings.reserve(faceROIs.size());
|
||||
|
||||
// GPU preprocessing pipeline
|
||||
cv::cuda::Stream stream;
|
||||
std::vector<cv::cuda::GpuMat> batchGpu;
|
||||
batchGpu.reserve(faceROIs.size());
|
||||
|
||||
const cv::Size targetSize(FACE_WIDTH, FACE_HEIGHT);
|
||||
|
||||
for (size_t i = 0; i < faceROIs.size(); ++i) {
|
||||
const cv::Mat& roi = faceROIs[i];
|
||||
|
||||
if (roi.empty()) {
|
||||
_logger.LogWarn("ArcFace::RunArcFaceBatch",
|
||||
"Empty ROI at index " + std::to_string(i) + ", skipping",
|
||||
__FILE__, __LINE__);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Upload to GPU
|
||||
cv::cuda::GpuMat d_img;
|
||||
d_img.upload(roi, stream);
|
||||
|
||||
// Handle grayscale conversion on GPU
|
||||
if (roi.channels() == 1) {
|
||||
cv::cuda::GpuMat d_bgr;
|
||||
cv::cuda::cvtColor(d_img, d_bgr, cv::COLOR_GRAY2BGR, 0, stream);
|
||||
d_img = d_bgr;
|
||||
}
|
||||
|
||||
// Resize on GPU if needed
|
||||
if (roi.cols != FACE_WIDTH || roi.rows != FACE_HEIGHT) {
|
||||
cv::cuda::GpuMat d_resized;
|
||||
cv::cuda::resize(d_img, d_resized, targetSize, 0, 0, cv::INTER_LINEAR, stream);
|
||||
d_img = d_resized;
|
||||
}
|
||||
|
||||
// BGR to RGB conversion on GPU
|
||||
cv::cuda::GpuMat d_rgb;
|
||||
cv::cuda::cvtColor(d_img, d_rgb, cv::COLOR_BGR2RGB, 0, stream);
|
||||
|
||||
batchGpu.emplace_back(std::move(d_rgb));
|
||||
}
|
||||
|
||||
if (batchGpu.empty()) {
|
||||
return embeddings;
|
||||
}
|
||||
|
||||
// Prepare inference inputs
|
||||
std::vector<std::vector<cv::cuda::GpuMat>> inputs;
|
||||
inputs.reserve(1);
|
||||
inputs.emplace_back(std::move(batchGpu));
|
||||
|
||||
// Run inference
|
||||
std::vector<std::vector<std::vector<float>>> featureVectors;
|
||||
bool succ = m_trtEngine.runInference(inputs, featureVectors);
|
||||
|
||||
stream.waitForCompletion();
|
||||
|
||||
if (!succ) {
|
||||
_logger.LogError("ArcFace::RunArcFaceBatch", "runInference failed", __FILE__, __LINE__);
|
||||
return embeddings;
|
||||
}
|
||||
|
||||
if (featureVectors.empty() || featureVectors[0].empty()) {
|
||||
_logger.LogError("ArcFace::RunArcFaceBatch", "Empty featureVectors", __FILE__, __LINE__);
|
||||
return embeddings;
|
||||
}
|
||||
|
||||
embeddings = std::move(featureVectors[0]);
|
||||
|
||||
return embeddings;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ArcFace::RunArcFaceBatch", e.what(), __FILE__, __LINE__);
|
||||
return embeddings;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::vector<float>> ArcFace::Forward(const cv::Mat& input,const std::vector<ANSCENTER::Object>& outputBbox)
|
||||
{
|
||||
std::vector<std::vector<float>> detectedEmbeddings;
|
||||
|
||||
// Early validation before locking
|
||||
if (input.empty()) {
|
||||
_logger.LogError("ArcFace::Forward",
|
||||
"Input image is empty", __FILE__, __LINE__);
|
||||
return detectedEmbeddings;
|
||||
}
|
||||
|
||||
if (outputBbox.empty()) {
|
||||
return detectedEmbeddings;
|
||||
}
|
||||
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
|
||||
try {
|
||||
// Pre-reserve output space
|
||||
detectedEmbeddings.reserve(outputBbox.size());
|
||||
|
||||
// Collect valid face ROIs
|
||||
std::vector<cv::Mat> faceROIs;
|
||||
faceROIs.reserve(outputBbox.size());
|
||||
|
||||
for (const auto& obj : outputBbox) {
|
||||
if (!obj.mask.empty()) {
|
||||
faceROIs.push_back(obj.mask);
|
||||
}
|
||||
}
|
||||
|
||||
if (faceROIs.empty()) {
|
||||
return detectedEmbeddings;
|
||||
}
|
||||
|
||||
// Run batch inference
|
||||
detectedEmbeddings = RunArcFaceBatch(faceROIs);
|
||||
|
||||
return detectedEmbeddings;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ArcFace::Forward", e.what(), __FILE__, __LINE__);
|
||||
return detectedEmbeddings;
|
||||
}
|
||||
}
|
||||
|
||||
std::tuple<std::vector<std::string>, std::vector<float>>
|
||||
ArcFace::SearchForFaces(const std::vector<std::vector<float>>& detectedEmbeddings) {
|
||||
std::vector<std::string> detectedUsers;
|
||||
std::vector<float> simValues;
|
||||
|
||||
// Early exit before locking
|
||||
if (detectedEmbeddings.empty()) {
|
||||
return std::make_tuple(detectedUsers, simValues);
|
||||
}
|
||||
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
|
||||
try {
|
||||
// Pre-reserve output space
|
||||
detectedUsers.reserve(detectedEmbeddings.size());
|
||||
simValues.reserve(detectedEmbeddings.size());
|
||||
|
||||
if (!classNames.empty() && faiss_index && faiss_index->ntotal > 0) {
|
||||
// Determine k based on database size
|
||||
const int k = std::min(3, static_cast<int>(faiss_index->ntotal));
|
||||
|
||||
// Pre-allocate search buffers (reuse across iterations)
|
||||
std::vector<faiss::idx_t> indices(k);
|
||||
std::vector<float> distances(k);
|
||||
std::vector<float> matchEmbedding(faiss_index->d);
|
||||
|
||||
for (const auto& embedding : detectedEmbeddings) {
|
||||
if (embedding.size() != FACE_EMBEDDING_SIZE) {
|
||||
detectedUsers.push_back("0");
|
||||
simValues.push_back(1.0f);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Search FAISS index
|
||||
faiss_index->search(1, embedding.data(), k,
|
||||
distances.data(), indices.data());
|
||||
|
||||
// Find best match (minimum distance for L2)
|
||||
auto min_it = std::min_element(distances.begin(), distances.end());
|
||||
int best_index = static_cast<int>(std::distance(distances.begin(), min_it));
|
||||
|
||||
// Validate index
|
||||
faiss::idx_t id = indices[best_index];
|
||||
if (id < 0 || id >= static_cast<faiss::idx_t>(classNames.size())) {
|
||||
detectedUsers.push_back("0");
|
||||
simValues.push_back(1.0f);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Reconstruct embedding and compute similarity
|
||||
faiss_index->reconstruct(id, matchEmbedding.data());
|
||||
float cosine = CosineSimilarity(embedding, matchEmbedding, false);
|
||||
float similarity = 1.0f - cosine;
|
||||
|
||||
detectedUsers.push_back(classNames[id]);
|
||||
simValues.push_back(std::abs(similarity));
|
||||
}
|
||||
}
|
||||
else {
|
||||
detectedUsers.assign(detectedEmbeddings.size(), "0");
|
||||
simValues.assign(detectedEmbeddings.size(), 1.0f);
|
||||
}
|
||||
|
||||
return std::make_tuple(std::move(detectedUsers), std::move(simValues));
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ArcFace::SearchForFaces", e.what(), __FILE__, __LINE__);
|
||||
|
||||
// Return appropriate sized vectors on error
|
||||
detectedUsers.assign(detectedEmbeddings.size(), "0");
|
||||
simValues.assign(detectedEmbeddings.size(), 1.0f);
|
||||
return std::make_tuple(detectedUsers, simValues);
|
||||
}
|
||||
}
|
||||
void ArcFace::Init() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
|
||||
try {
|
||||
classNames.clear();
|
||||
|
||||
if (faiss_index) {
|
||||
faiss_index->reset();
|
||||
}
|
||||
else {
|
||||
faiss_index = std::make_unique<faiss::IndexFlatL2>(FACE_EMBEDDING_SIZE);
|
||||
}
|
||||
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_isInitialized = false;
|
||||
_logger.LogFatal("ArcFace::Init", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
ArcFace::~ArcFace() {
|
||||
try {
|
||||
Destroy();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
// Log but don't throw - exceptions in destructors are dangerous
|
||||
_logger.LogError("ArcFace::~ArcFace", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
catch (...) {
|
||||
// Catch all exceptions to prevent std::terminate
|
||||
_logger.LogError("ArcFace::~ArcFace", "Unknown exception during destruction", __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
bool ArcFace::Destroy() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
|
||||
try {
|
||||
classNames.clear();
|
||||
|
||||
if (faiss_index) {
|
||||
faiss_index->reset();
|
||||
faiss_index.reset();
|
||||
}
|
||||
|
||||
_isInitialized = false;
|
||||
|
||||
m_trtEngine.clearGpuBuffers();
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ArcFace::Destroy", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
void ArcFace::AddEmbedding(const std::string& className, float embedding[]) {
|
||||
// Validate input before locking
|
||||
if (!embedding) {
|
||||
_logger.LogError("ArcFace::AddEmbedding",
|
||||
"Null embedding pointer.", __FILE__, __LINE__);
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
|
||||
try {
|
||||
if (!faiss_index) {
|
||||
_logger.LogError("ArcFace::AddEmbedding",
|
||||
"FAISS index is not initialized.", __FILE__, __LINE__);
|
||||
return;
|
||||
}
|
||||
|
||||
// Direct add without intermediate vector copy
|
||||
classNames.push_back(className);
|
||||
faiss_index->add(1, embedding);
|
||||
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ArcFace::AddEmbedding", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
void ArcFace::AddEmbedding(const std::string& className, const std::vector<float>& embedding) {
|
||||
// Early validation before locking
|
||||
if (embedding.size() != FACE_EMBEDDING_SIZE) {
|
||||
_logger.LogError("ArcFace::AddEmbedding",
|
||||
"Embedding size does not match expected output dimension of 512.", __FILE__, __LINE__);
|
||||
return;
|
||||
}
|
||||
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
|
||||
try {
|
||||
if (!faiss_index) {
|
||||
_logger.LogError("ArcFace::AddEmbedding",
|
||||
"FAISS index is not initialized.", __FILE__, __LINE__);
|
||||
return;
|
||||
}
|
||||
|
||||
classNames.push_back(className);
|
||||
faiss_index->add(1, embedding.data());
|
||||
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ArcFace::AddEmbedding", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
#ifndef ARCFACERT_H
|
||||
#define ARCFACERT_H
|
||||
#pragma once
|
||||
#include "ANSFRCommon.h"
|
||||
//#include "fastdeploy/vision.h"
|
||||
#include "hnswlib/hnswlib.h"
|
||||
#include <faiss/IndexFlat.h>
|
||||
#include "engine.h"
|
||||
|
||||
namespace ANSCENTER {
|
||||
|
||||
class ArcFace : public ANSFRBase {
|
||||
public:
|
||||
virtual bool Initialize(std::string licenseKey,
|
||||
ModelConfig modelConfig,
|
||||
const std::string& modelZipFilePath,
|
||||
const std::string& modelZipPassword,
|
||||
std::string& labelMap) override;
|
||||
|
||||
virtual bool LoadModel(const std::string& modelZipFilePath,
|
||||
const std::string& modelZipPassword) override;
|
||||
|
||||
bool OptimizeModel(bool fp16, std::string& optimizedModelFolder);
|
||||
|
||||
// Single face feature (uses RunArcFace)
|
||||
std::vector<float> Feature(const cv::Mat& image, const ANSCENTER::Object& bBox);
|
||||
|
||||
// Main entry: run recognition for all faces in bBox
|
||||
std::vector<FaceResultObject> Match(const cv::Mat& input,
|
||||
const std::vector<ANSCENTER::Object>& bBox,
|
||||
const std::map<std::string, std::string>& userDict);
|
||||
|
||||
cv::Mat GetCropFace(const cv::Mat& input, const ANSCENTER::Object& bBox);
|
||||
|
||||
void Init();
|
||||
|
||||
void AddEmbedding(const std::string& className, float embedding[]);
|
||||
void AddEmbedding(const std::string& className, const std::vector<float>& embedding);
|
||||
|
||||
bool UpdateParamater(double knownPersonThreshold) {
|
||||
_modelConfig.unknownPersonThreshold = knownPersonThreshold;
|
||||
m_knownPersonThresh = _modelConfig.unknownPersonThreshold;
|
||||
return true;
|
||||
}
|
||||
|
||||
~ArcFace();
|
||||
bool Destroy();
|
||||
|
||||
private:
|
||||
bool LoadEngine(const std::string onnxModelPath, bool engineOptimisation = true);
|
||||
|
||||
// Batched forward: one embedding per Object.mask
|
||||
std::vector<std::vector<float>> Forward(const cv::Mat& input,
|
||||
const std::vector<ANSCENTER::Object>& outputBbox);
|
||||
|
||||
// Search embeddings in FAISS index
|
||||
std::tuple<std::vector<std::string>, std::vector<float>>
|
||||
SearchForFaces(const std::vector<std::vector<float>>& detectedEmbeddings);
|
||||
|
||||
// Single-face inference (kept for Feature)
|
||||
std::vector<float> RunArcFace(const cv::Mat& input);
|
||||
|
||||
// New: batched inference helper
|
||||
std::vector<std::vector<float>> RunArcFaceBatch(const std::vector<cv::Mat>& faceROIs);
|
||||
|
||||
protected:
|
||||
std::vector<std::string> classNames;
|
||||
ModelConfig _modelConfig;
|
||||
std::string _modelFilePath;
|
||||
float m_knownPersonThresh = 0.35f;
|
||||
|
||||
const int FACE_WIDTH = 112;
|
||||
const int FACE_HEIGHT = 112;
|
||||
const int FACE_EMBEDDING_SIZE = 512;
|
||||
|
||||
std::unique_ptr<faiss::IndexFlatL2> faiss_index;
|
||||
ANSCENTER::Options m_options;
|
||||
std::recursive_mutex _mutex;
|
||||
Engine<float> m_trtEngine;
|
||||
|
||||
float m_ratio = 1.0f;
|
||||
float m_imgWidth = 0.0f;
|
||||
float m_imgHeight = 0.0f;
|
||||
|
||||
const std::array<float, 3> SUB_VALS{ 0.5f, 0.5f, 0.5f };
|
||||
const std::array<float, 3> DIV_VALS{ 0.5f, 0.5f, 0.5f };
|
||||
const bool NORMALIZE = true;
|
||||
};
|
||||
// Using FastDeploy Enging to perform facial recognition
|
||||
// class ArcFace :public ANSFRBase {
|
||||
// public:
|
||||
// virtual bool Initialize(std::string licenseKey, ModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, std::string& labelMap) override;
|
||||
// virtual bool LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword)override;
|
||||
// bool OptimizeModel(bool fp16, std::string& optimizedModelFolder);
|
||||
// std::vector<float> Feature(const cv::Mat& input, ANSCENTER::Object bBox); // Run inference and get embedding information from a cropped image
|
||||
// std::vector<FaceResultObject> Match(const cv::Mat& input, std::vector<ANSCENTER::Object> bBox, std::map<std::string, std::string>userDict); // Run inference and get embedding information from a cropped image (the first bbox)
|
||||
// cv::Mat GetCropFace(const cv::Mat& input, ANSCENTER::Object bBox);
|
||||
// void Init();
|
||||
// void AddEmbedding(const std::string& className, float embedding[]);
|
||||
// void AddEmbedding(const std::string& className, const std::vector<float>& embedding);
|
||||
// bool UpdateParamater(double knownPersonThreshold) {
|
||||
// _modelConfig.unknownPersonThreshold = knownPersonThreshold;
|
||||
// _modelConfig.unknownPersonThreshold = knownPersonThreshold;
|
||||
// m_knownPersonThresh = _modelConfig.unknownPersonThreshold;
|
||||
// return true;
|
||||
// }
|
||||
//~ArcFace();
|
||||
// bool Destroy();
|
||||
// private:
|
||||
// bool LoadEngine(const std::string engineFile, bool engineOptimisation = true);
|
||||
// std::vector<std::vector<float>> Forward(const cv::Mat& input, std::vector<ANSCENTER::Object> outputBbox);
|
||||
// std::tuple<std::vector<std::string>, std::vector<float>> SearchForFaces(const std::vector<std::vector<float>>& detectedEmbeddings);
|
||||
//std::vector<float> RunArcFace(const cv::Mat& input);
|
||||
// protected:
|
||||
// std::vector<std::string> classNames;
|
||||
// ModelConfig _modelConfig;
|
||||
// std::string _modelFilePath;
|
||||
// float m_knownPersonThresh;
|
||||
// const int FACE_WIDTH = 112;
|
||||
// const int FACE_HEIGHT = 112;
|
||||
// const int FACE_EMBEDDING_SIZE = 512;
|
||||
// std::unique_ptr<faiss::IndexFlatL2> faiss_index; // Use shared_ptr
|
||||
// ANSCENTER::Options m_options;
|
||||
// std::recursive_mutex _mutex;
|
||||
// Engine<float> m_trtEngine;
|
||||
// float m_ratio = 1;
|
||||
// float m_imgWidth = 0;
|
||||
// float m_imgHeight = 0;
|
||||
// const std::array<float, 3> SUB_VALS{ 0.5f, 0.5f, 0.5f };
|
||||
// const std::array<float, 3> DIV_VALS{ 0.5f, 0.5f, 0.5f };
|
||||
// const bool NORMALIZE = true;
|
||||
// };
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,78 @@
|
||||
# ANSFR — Face Recognition DLL (ONNX + TensorRT + FAISS + SQLite)
|
||||
# Explicit source list from original vcxproj
|
||||
set(ANSFR_SOURCES
|
||||
ANSFR.cpp
|
||||
ANSFRCommon.cpp
|
||||
ANSFaceRecognizer.cpp
|
||||
ANSGpuFrameRegistry.cpp
|
||||
FaceDatabase.cpp
|
||||
FaceNet.cpp
|
||||
dllmain.cpp
|
||||
pch.cpp
|
||||
sqlite3.c
|
||||
# Engine sources compiled into ANSFR
|
||||
${CMAKE_SOURCE_DIR}/engines/ONNXEngine/ONNXEngine.cpp
|
||||
${CMAKE_SOURCE_DIR}/engines/OpenVINOEngine/src/faceapp/cnn.cpp
|
||||
${CMAKE_SOURCE_DIR}/engines/OpenVINOEngine/src/utils/args_helper.cpp
|
||||
${CMAKE_SOURCE_DIR}/engines/OpenVINOEngine/src/utils/common.cpp
|
||||
)
|
||||
file(GLOB ANSFR_HEADERS "*.h")
|
||||
|
||||
add_library(ANSFR SHARED ${ANSFR_SOURCES} ${ANSFR_HEADERS})
|
||||
|
||||
target_include_directories(ANSFR PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
|
||||
target_include_directories(ANSFR PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/engines/ONNXEngine
|
||||
${CMAKE_SOURCE_DIR}/engines/OpenVINOEngine/include
|
||||
${CMAKE_SOURCE_DIR}/engines/TensorRTAPI/include
|
||||
${CMAKE_SOURCE_DIR}/modules/ANSODEngine
|
||||
${SHARED_INCLUDE_DIR}
|
||||
)
|
||||
|
||||
target_include_directories(ANSFR PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/modules/ANSMOT
|
||||
${CMAKE_SOURCE_DIR}/engines/OpenVINOEngine/include/faceapp
|
||||
${ANSLIBS_DIR}/faiss
|
||||
)
|
||||
|
||||
target_link_libraries(ANSFR
|
||||
PUBLIC ANSLibsLoader
|
||||
PRIVATE ANSODEngine
|
||||
PRIVATE ANSLicensingSystem
|
||||
PRIVATE labview
|
||||
PRIVATE spdlog_dep
|
||||
PRIVATE opencv
|
||||
PRIVATE onnxruntime
|
||||
PRIVATE tensorrt
|
||||
PRIVATE openvino
|
||||
PRIVATE faiss
|
||||
PRIVATE mkl
|
||||
PRIVATE boost
|
||||
PRIVATE ANSMOT
|
||||
PRIVATE CUDA::cudart_static
|
||||
PRIVATE CUDA::cublas
|
||||
PRIVATE CUDA::cublasLt
|
||||
PRIVATE nvinfer_10.lib
|
||||
PRIVATE nvonnxparser_10.lib
|
||||
)
|
||||
|
||||
# Boost asio needs full boost install path
|
||||
target_include_directories(ANSFR PRIVATE ${RESEARCH_DIR}/boost_1_88_0)
|
||||
|
||||
target_compile_definitions(ANSFR PRIVATE UNICODE _UNICODE
|
||||
ANSFR_EXPORTS
|
||||
ENGINE_EXPORTS
|
||||
ORT_API_MANUAL_INIT
|
||||
_USRDLL
|
||||
)
|
||||
|
||||
target_precompile_headers(ANSFR PRIVATE pch.h)
|
||||
|
||||
# sqlite3.c is pure C — skip precompiled headers and force C language
|
||||
set_source_files_properties(sqlite3.c PROPERTIES
|
||||
SKIP_PRECOMPILE_HEADERS ON
|
||||
LANGUAGE C
|
||||
)
|
||||
@@ -0,0 +1,909 @@
|
||||
#include "FaceDatabase.h"
|
||||
namespace ANSCENTER {
|
||||
|
||||
FaceDatabase::FaceDatabase() {
|
||||
m_db = nullptr;
|
||||
m_embedding_dim = 0;
|
||||
}
|
||||
bool FaceDatabase::Initialize(std::string path, int embedding_dim, std::string dataFolder) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
try {
|
||||
sqlite3_stmt* stmt;
|
||||
_dataFolder = dataFolder;
|
||||
m_embedding_dim = embedding_dim;
|
||||
std::string sql;
|
||||
int rc;
|
||||
int isNotEmpty = 0;
|
||||
//this->_logger.LogDebug("FaceDatabase::Initialize. Database path: ", path, __FILE__, __LINE__);
|
||||
std::string faceDatabaseFolder = GetParentFolder(path);
|
||||
if (!FolderExist(faceDatabaseFolder)) fs::create_directory(faceDatabaseFolder);
|
||||
std::string imageDataFolder = CreateFilePath(faceDatabaseFolder, "data");
|
||||
if (!FolderExist(imageDataFolder)) fs::create_directory(imageDataFolder);
|
||||
|
||||
rc = sqlite3_open(path.c_str(), &m_db);
|
||||
if (rc) {
|
||||
_logger.LogError("FaceDatabase::Initialize", std::string("Can't open database: ") + sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
}
|
||||
else {
|
||||
_logger.LogDebug("FaceDatabase::Initialize", "Opened database successfully", __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
// Fix #2: Enable WAL mode for concurrent read/write from separate processes
|
||||
sqlite3_exec(m_db, "PRAGMA journal_mode=WAL;", nullptr, nullptr, nullptr);
|
||||
// Fix #2: 5-second busy timeout for multi-process contention
|
||||
sqlite3_exec(m_db, "PRAGMA busy_timeout=5000;", nullptr, nullptr, nullptr);
|
||||
// Fix #2: Enable foreign key enforcement
|
||||
sqlite3_exec(m_db, "PRAGMA foreign_keys=ON;", nullptr, nullptr, nullptr);
|
||||
|
||||
// Prepare
|
||||
sql = "SELECT name FROM sqlite_master;";
|
||||
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::Initialize. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
// Step
|
||||
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||
isNotEmpty = 1;
|
||||
break;
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
if (isNotEmpty) {
|
||||
//this->_logger.LogDebug("FaceDatabase::Initialize.", "Database is not empty", __FILE__, __LINE__);
|
||||
}
|
||||
else {
|
||||
//this->_logger.LogDebug("FaceDatabase::Initialize.", "Empty Database, initiating...", __FILE__, __LINE__);
|
||||
// Prepare
|
||||
sql = "CREATE TABLE IF NOT EXISTS USER ( \
|
||||
ID INTEGER PRIMARY KEY AUTOINCREMENT, \
|
||||
CODE TEXT, \
|
||||
NAME TEXT, \
|
||||
UNIQUE(ID, CODE)\
|
||||
)";
|
||||
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::Initialize. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
// Step
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
|
||||
// this->_logger.LogError("FaceDatabase::Initialize. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
}
|
||||
else {
|
||||
// this->_logger.LogDebug("FaceDatabase::Initialize.", "Table `USER` created successfully", __FILE__, __LINE__);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
// Prepare
|
||||
sql = "CREATE TABLE IF NOT EXISTS FACE ( \
|
||||
ID INTEGER PRIMARY KEY AUTOINCREMENT, \
|
||||
USER INTEGER, \
|
||||
IMG_PATH TEXT, \
|
||||
EMBEDDING BLOB, \
|
||||
UNIQUE(ID, USER), \
|
||||
FOREIGN KEY(USER) REFERENCES USER(ID) \
|
||||
)";
|
||||
|
||||
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::Initialize. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
// Step
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
|
||||
// this->_logger.LogError("FaceDatabase::Initialize. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
}
|
||||
else {
|
||||
// this->_logger.LogDebug("FaceDatabase::Initialize", "Table `FACE` created successfully.", __FILE__, __LINE__);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
//this->_logger.LogFatal("FaceDatabase::Initialize", e.what(), __FILE__, __LINE__);
|
||||
//sqlite3_finalize(stmt);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
int FaceDatabase::InsertUser(std::string userId, std::string userName) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
std::string sql;
|
||||
sqlite3_stmt* stmt;
|
||||
int rc;
|
||||
int resultCode = 0;
|
||||
// Prepare
|
||||
sql = "INSERT INTO USER (CODE, NAME) VALUES (?, ?);";
|
||||
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
|
||||
try {
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::InsertUser. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
return -2;
|
||||
}
|
||||
// Binding
|
||||
rc = sqlite3_bind_text(stmt, 1, userId.c_str(), int(userId.length()), SQLITE_STATIC);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::InsertUser. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -3;
|
||||
}
|
||||
rc = sqlite3_bind_text(stmt, 2, userName.c_str(), int(userName.length()), SQLITE_STATIC);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::InsertUser. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -3;
|
||||
}
|
||||
|
||||
// Step
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
|
||||
//this->_logger.LogError("FaceDatabase::InsertUser. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -4;
|
||||
}
|
||||
if (rc == SQLITE_DONE) {
|
||||
// this->_logger.LogDebug("FaceDatabase::InsertUser:", "Record successfully created", __FILE__, __LINE__);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
int id = (int)sqlite3_last_insert_rowid(m_db);
|
||||
resultCode = id;
|
||||
return resultCode;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
//this->_logger.LogFatal("FaceDatabase::InsertUser", e.what(), __FILE__, __LINE__);
|
||||
sqlite3_finalize(stmt);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
int FaceDatabase::UpdateUser(int userId, std::string newUserCode, std::string newUserName) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
std::string sql;
|
||||
sqlite3_stmt* stmt;
|
||||
int resultCode = 0;
|
||||
int rc;
|
||||
// Prepare
|
||||
// UPDATE USER SET CODE ="0003", NAME= "Biden" WHERE ID =1
|
||||
sql = "UPDATE USER SET CODE=?, NAME=? WHERE ID=?;";
|
||||
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
|
||||
try {
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::InsertUser. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Binding
|
||||
rc = sqlite3_bind_text(stmt, 1, newUserCode.c_str(), int(newUserCode.length()), SQLITE_STATIC);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::InsertUser. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -2;
|
||||
}
|
||||
|
||||
rc = sqlite3_bind_text(stmt, 2, newUserName.c_str(), int(newUserName.length()), SQLITE_STATIC);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::InsertUser. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -2;
|
||||
}
|
||||
rc = sqlite3_bind_int(stmt, 3, userId);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::InsertUser. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -2;
|
||||
}
|
||||
|
||||
// Step
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
|
||||
//this->_logger.LogError("FaceDatabase::InsertUser. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -3;
|
||||
}
|
||||
if (rc == SQLITE_DONE) {
|
||||
// this->_logger.LogDebug("FaceDatabase::InsertUser:", "Record successfully created", __FILE__, __LINE__);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
resultCode = 1;
|
||||
return resultCode;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
//this->_logger.LogFatal("FaceDatabase::InsertUser", e.what(), __FILE__, __LINE__);
|
||||
sqlite3_finalize(stmt);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
int FaceDatabase::InsertFace(int userId, std::string imgPath, float embedding[]) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
if (!FileExist(imgPath)) return 0;
|
||||
std::string sql;
|
||||
sqlite3_stmt* stmt;
|
||||
int rc;
|
||||
int resultCode = 0;
|
||||
// Prepare
|
||||
sql = "INSERT INTO FACE (USER, IMG_PATH, EMBEDDING) VALUES (?, ?, ?);";
|
||||
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
|
||||
try {
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
//std::cout << "SQL error: " << sqlite3_errmsg(m_db);
|
||||
return -2;
|
||||
}
|
||||
// Binding
|
||||
rc = sqlite3_bind_int(stmt, 1, userId);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::InsertFace. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -3;
|
||||
}
|
||||
rc = sqlite3_bind_text(stmt, 2, imgPath.c_str(), int(imgPath.length()), SQLITE_STATIC);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::InsertFace. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -3;
|
||||
}
|
||||
rc = sqlite3_bind_blob(stmt, 3, embedding, sizeof(float) * m_embedding_dim, SQLITE_STATIC);
|
||||
if (rc != SQLITE_OK) {
|
||||
//::cout << "SQL bind error: " << sqlite3_errmsg(m_db);
|
||||
resultCode = -3;
|
||||
}
|
||||
|
||||
// Step
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
|
||||
//this->_logger.LogError("FaceDatabase::InsertFace. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -4;
|
||||
}
|
||||
if (rc == SQLITE_DONE) {
|
||||
//this->_logger.LogDebug("FaceDatabase::InsertFace.", "Embedding for user created successfully", __FILE__, __LINE__);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
int id = (int)sqlite3_last_insert_rowid(m_db);
|
||||
resultCode = id;
|
||||
return resultCode;
|
||||
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
//this->_logger.LogFatal("FaceDatabase::InsertFace", e.what(), __FILE__, __LINE__);
|
||||
sqlite3_finalize(stmt);
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
||||
int FaceDatabase::DeleteFace(int id) {
|
||||
try {
|
||||
// Fix #4: Single lock scope — GetFaceById + DELETE in one atomic section
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
std::string sql;
|
||||
sqlite3_stmt* stmt;
|
||||
int rc;
|
||||
int userId;
|
||||
int resultCode = 0;
|
||||
std::string faceImagePath;
|
||||
|
||||
// Look up face image path (now inside lock — no TOCTOU race)
|
||||
{
|
||||
sqlite3_stmt* lookupStmt;
|
||||
rc = sqlite3_prepare_v2(m_db, "SELECT USER, IMG_PATH FROM FACE WHERE ID=?;", -1, &lookupStmt, NULL);
|
||||
if (rc == SQLITE_OK) {
|
||||
sqlite3_bind_int(lookupStmt, 1, id);
|
||||
if (sqlite3_step(lookupStmt) == SQLITE_ROW) {
|
||||
userId = sqlite3_column_int(lookupStmt, 0);
|
||||
const char* pathText = reinterpret_cast<const char*>(sqlite3_column_text(lookupStmt, 1));
|
||||
faceImagePath = pathText ? std::string(pathText) : "";
|
||||
}
|
||||
}
|
||||
sqlite3_finalize(lookupStmt);
|
||||
}
|
||||
|
||||
// Delete image file
|
||||
if (!faceImagePath.empty() && FileExist(faceImagePath)) {
|
||||
if (!fs::remove(faceImagePath)) {
|
||||
_logger.LogError("FaceDatabase::DeleteFace", "Failed to delete face image: " + faceImagePath, __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
//_mutex.lock();
|
||||
// Prepare
|
||||
sql = "DELETE FROM FACE WHERE ID=?;";
|
||||
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::DeleteFace. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -1;
|
||||
}
|
||||
|
||||
// Binding
|
||||
rc = sqlite3_bind_int(stmt, 1, id);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::DeleteFace. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -2;
|
||||
}
|
||||
// Step
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
|
||||
//this->_logger.LogError("FaceDatabase::DeleteFace. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -3;
|
||||
}
|
||||
if (rc == SQLITE_DONE) {
|
||||
// this->_logger.LogDebug("FaceDatabase::DeleteFace.", "Embedding for user deleted successfully", __FILE__, __LINE__);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
resultCode = 1;
|
||||
//_mutex.unlock();
|
||||
return resultCode;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
//this->_logger.LogFatal("FaceDatabase::DeleteFace", e.what(), __FILE__, __LINE__);
|
||||
//_mutex.unlock();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
int FaceDatabase::DeleteUser(int userId) {
|
||||
try {
|
||||
std::string sql;
|
||||
sqlite3_stmt* stmt;
|
||||
int resultCode = 0;
|
||||
int rc;
|
||||
|
||||
std::vector<int> faceIds;
|
||||
faceIds.clear();
|
||||
GetFaceIdsByUser(userId, faceIds);
|
||||
int numOfFaces = (int)faceIds.size();
|
||||
//this->_logger.LogDebug("FaceDatabase::DeleteUser. Number of Face for this user", std::to_string(numOfFaces), __FILE__, __LINE__);
|
||||
if (numOfFaces > 0) {
|
||||
for (int i = 0; i < numOfFaces; i++) {
|
||||
if (!DeleteFace(faceIds[i])) {
|
||||
//this->_logger.LogError("FaceDatabase::DeleteUser. Failed to delete face id", std::to_string(numOfFaces), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
}
|
||||
std::string userImageFolderName = "User" + std::to_string(userId);
|
||||
std::string userImageFolder = CreateFilePath(_dataFolder, userImageFolderName);
|
||||
if (FolderExist(userImageFolder)) {
|
||||
if (!DeleteFolder(userImageFolder)) {
|
||||
//this->_logger.LogError("FaceDatabase::DeleteUser. Failed to delete user data folder", userImageFolder, __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
|
||||
// Delete person
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
//_mutex.lock();
|
||||
// Prepare
|
||||
sql = "DELETE FROM USER WHERE ID=?;";
|
||||
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::DeleteUser. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -1;
|
||||
}
|
||||
// Binding
|
||||
rc = sqlite3_bind_int(stmt, 1, userId);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::DeleteUser. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -2;
|
||||
}
|
||||
|
||||
// Step
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
|
||||
//this->_logger.LogError("FaceDatabase::DeleteUser. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -3;
|
||||
}
|
||||
if (rc == SQLITE_DONE) {
|
||||
//this->_logger.LogDebug("FaceDatabase::DeleteUser.", "Deleted person successfully", __FILE__, __LINE__);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
resultCode = 1;
|
||||
//_mutex.unlock();
|
||||
return resultCode;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
//this->_logger.LogFatal("FaceDatabase::DeleteUser", e.what(), __FILE__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
int FaceDatabase::GetUserId(std::string code) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
std::string sql;
|
||||
sqlite3_stmt* stmt;
|
||||
int rc;
|
||||
// Prepare
|
||||
sql = "SELECT ID FROM USER WHERE CODE =?;";
|
||||
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
|
||||
try {
|
||||
|
||||
int resultCode = 0;
|
||||
int userId = -1;
|
||||
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::GetUser. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -1;
|
||||
}
|
||||
|
||||
// Binding
|
||||
rc = sqlite3_bind_text(stmt, 1, code.c_str(), int(code.length()), SQLITE_STATIC);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::GetUser. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -2;
|
||||
}
|
||||
|
||||
// Step
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
|
||||
//this->_logger.LogError("FaceDatabase::GetUser. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -3;
|
||||
}
|
||||
if (rc == SQLITE_ROW) {
|
||||
//this->_logger.LogDebug("FaceDatabase::GetUser.", "Get User information successfully", __FILE__, __LINE__);
|
||||
userId = sqlite3_column_int(stmt, 0);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
resultCode = userId;
|
||||
return resultCode;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
//this->_logger.LogFatal("FaceDatabase::GetUserId", e.what(), __FILE__, __LINE__);
|
||||
sqlite3_finalize(stmt);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
int FaceDatabase::GetUser(int userId, std::string& code, std::string& userName) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
std::string sql;
|
||||
sqlite3_stmt* stmt;
|
||||
int resultCode = 0;
|
||||
userName = "";
|
||||
code = "";
|
||||
int rc;
|
||||
// Prepare
|
||||
sql = "SELECT * FROM USER WHERE ID =?;";
|
||||
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
|
||||
try {
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::GetUser. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -1;
|
||||
}
|
||||
|
||||
// Binding
|
||||
rc = sqlite3_bind_int(stmt, 1, userId);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::GetUser. SQL bind error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -2;
|
||||
}
|
||||
|
||||
// Step
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
|
||||
//this->_logger.LogError("FaceDatabase::GetUser. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -3;
|
||||
}
|
||||
if (rc == SQLITE_ROW) {
|
||||
//this->_logger.LogDebug("FaceDatabase::GetUser.", "Get User information successfully", __FILE__, __LINE__);
|
||||
const char* codeText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
|
||||
const char* nameText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
|
||||
code = codeText ? std::string(codeText) : "";
|
||||
userName = nameText ? std::string(nameText) : "";
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
resultCode = 1;
|
||||
return resultCode;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
//this->_logger.LogFatal("FaceDatabase::GetUserId", e.what(), __FILE__, __LINE__);
|
||||
sqlite3_finalize(stmt);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int FaceDatabase::GetFaceIdsByUser(int userId, std::vector<int>& faceIds)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
faceIds.clear();
|
||||
std::string sql;
|
||||
sqlite3_stmt* stmt;
|
||||
int rc;
|
||||
sql = "SELECT ID FROM FACE WHERE USER=?;";
|
||||
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
|
||||
try {
|
||||
int resultCode = 0;
|
||||
int faceId;
|
||||
if (rc != SQLITE_OK) {
|
||||
_logger.LogError("FaceDatabase::GetFaceIdsByUser", std::string("SQL error: ") + sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -1;
|
||||
}
|
||||
|
||||
// Binding
|
||||
rc = sqlite3_bind_int(stmt, 1, userId);
|
||||
if (rc != SQLITE_OK) {
|
||||
_logger.LogError("FaceDatabase::GetFaceIdsByUser", std::string("SQL bind error: ") + sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -2;
|
||||
}
|
||||
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||
faceId = sqlite3_column_int(stmt, 0);
|
||||
faceIds.push_back(faceId);
|
||||
}
|
||||
if (rc != SQLITE_DONE) {
|
||||
_logger.LogError("FaceDatabase::GetFaceIdsByUser", std::string("SQL step error: ") + sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
resultCode = 1;
|
||||
return resultCode;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
//this->_logger.LogFatal("FaceDatabase::GetFaceIdsByUser", e.what(), __FILE__, __LINE__);
|
||||
sqlite3_finalize(stmt);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
int FaceDatabase::GetUsers(std::vector<int>& ids, std::vector<std::string>& codes, std::vector<std::string>& userNames) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
std::string sql;
|
||||
sqlite3_stmt* stmt;
|
||||
int rc;
|
||||
int userId;
|
||||
std::string userCode;
|
||||
std::string userName;
|
||||
int resutCode = 0;
|
||||
ids.clear();
|
||||
codes.clear();
|
||||
userNames.clear();
|
||||
sql = "SELECT * FROM USER;";
|
||||
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
|
||||
try {
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::GetUsers. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resutCode = -1;
|
||||
}
|
||||
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||
userId = sqlite3_column_int(stmt, 0);
|
||||
const char* codeText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
|
||||
const char* nameText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
|
||||
userCode = codeText ? std::string(codeText) : "";
|
||||
userName = nameText ? std::string(nameText) : "";
|
||||
ids.push_back(userId);
|
||||
codes.push_back(userCode);
|
||||
userNames.push_back(userName);
|
||||
}
|
||||
if (rc != SQLITE_DONE) {
|
||||
//this->_logger.LogError("FaceDatabase::GetUsers. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
resutCode = 1;
|
||||
return resutCode;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
//this->_logger.LogFatal("FaceDatabase::GetUsers", e.what(), __FILE__, __LINE__);
|
||||
sqlite3_finalize(stmt);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
int FaceDatabase::GetFaceById(int faceId, int& userId, std::string& imagePath) {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
std::string sql;
|
||||
sqlite3_stmt* stmt;
|
||||
int rc;
|
||||
int resultCode = 0;
|
||||
userId = -1;
|
||||
imagePath = "";
|
||||
// SELECT *FROM FACE WHERE ID =1
|
||||
sql = "SELECT * FROM FACE WHERE ID =?;";
|
||||
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
|
||||
try {
|
||||
if (rc != SQLITE_OK) {
|
||||
_logger.LogError("FaceDatabase::GetFaceById", std::string("SQL error: ") + sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -1;
|
||||
}
|
||||
// Binding
|
||||
rc = sqlite3_bind_int(stmt, 1, faceId);
|
||||
if (rc != SQLITE_OK) {
|
||||
_logger.LogError("FaceDatabase::GetFaceById", std::string("SQL bind error: ") + sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -2;
|
||||
}
|
||||
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||
userId = sqlite3_column_int(stmt, 1);
|
||||
const char* pathText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
|
||||
imagePath = pathText ? std::string(pathText) : "";
|
||||
}
|
||||
if (rc != SQLITE_DONE) {
|
||||
//this->_logger.LogError("FaceDatabase::GetFaceIds. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
resultCode = 1;
|
||||
return resultCode;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
//this->_logger.LogFatal("FaceDatabase::GetFaceIds", e.what(), __FILE__, __LINE__);
|
||||
sqlite3_finalize(stmt);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
std::map<std::string, std::string> FaceDatabase::GetUserDict() {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
std::map<std::string, std::string> userDict;
|
||||
userDict.clear();
|
||||
std::string sql;
|
||||
sqlite3_stmt* stmt;
|
||||
int rc;
|
||||
std::string userId;
|
||||
std::string userCode;
|
||||
std::string userName;
|
||||
// get user id and name
|
||||
sql = "SELECT * FROM USER;";
|
||||
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
|
||||
try {
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::GetUserDict. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
}
|
||||
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||
const char* idText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 0));
|
||||
const char* codeText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
|
||||
const char* nameText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
|
||||
userId = idText ? std::string(idText) : "";
|
||||
userCode = codeText ? std::string(codeText) : "";
|
||||
userName = nameText ? std::string(nameText) : "";
|
||||
if (!userCode.empty()) {
|
||||
userDict[userId] = userName+"(" + userCode + ")";
|
||||
}
|
||||
else {
|
||||
userDict[userId] = userName;
|
||||
}
|
||||
}
|
||||
if (rc != SQLITE_DONE) {
|
||||
//this->_logger.LogError("FaceDatabase::GetUserDict. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
return userDict;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
//this->_logger.LogFatal("FaceDatabase::GetUserDict", e.what(), __FILE__, __LINE__);
|
||||
sqlite3_finalize(stmt);
|
||||
return userDict;
|
||||
}
|
||||
}
|
||||
int FaceDatabase::GetNumEmbeddings() {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
try {
|
||||
int numEmbeds = 0;
|
||||
std::string sql;
|
||||
sqlite3_stmt* stmt;
|
||||
int rc;
|
||||
int resultCode = 0;
|
||||
// Prepare
|
||||
sql = "SELECT COUNT(*) FROM FACE;";
|
||||
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::GetNumEmbeddings. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -1;
|
||||
}
|
||||
// Step
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
|
||||
//this->_logger.LogError("FaceDatabase::GetNumEmbeddings. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -2;
|
||||
}
|
||||
numEmbeds = sqlite3_column_int(stmt, 0);
|
||||
sqlite3_finalize(stmt);
|
||||
resultCode = numEmbeds;
|
||||
return resultCode;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
//this->_logger.LogFatal("FaceDatabase::GetNumEmbeddings", e.what(), __FILE__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int FaceDatabase::GetEmbeddings(std::unique_ptr<ANSFRBase>& recognizer) {
|
||||
try {
|
||||
std::string sql;
|
||||
sqlite3_stmt* stmt;
|
||||
int rc;
|
||||
int resultCode = 0;
|
||||
std::string userId;
|
||||
int numEmbeds = GetNumEmbeddings();
|
||||
_logger.LogDebug("FaceDatabase::GetEmbeddings", "There are " + std::to_string(numEmbeds) + " embeddings in database", __FILE__, __LINE__);
|
||||
// get embedding
|
||||
//_mutex.lock();
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
sql = "SELECT * FROM FACE;";
|
||||
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::GetEmbeddings. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -1;
|
||||
}
|
||||
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||
const char* userIdText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
|
||||
if (!userIdText) continue;
|
||||
userId = std::string(userIdText);
|
||||
float* embedding = (float*)sqlite3_column_blob(stmt, 3);
|
||||
if (!embedding) continue;
|
||||
recognizer->AddEmbedding(userId, embedding);
|
||||
}
|
||||
if (rc != SQLITE_DONE) {
|
||||
//this->_logger.LogError("FaceDatabase::GetEmbeddings. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
resultCode = 0;
|
||||
//_mutex.unlock();
|
||||
return resultCode;
|
||||
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
//this->_logger.LogFatal("FaceDatabase::GetEmbeddings", e.what(), __FILE__, __LINE__);
|
||||
//_mutex.unlock();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
int FaceDatabase::GetEmbeddings(ANSFRBase& recognizer) {
|
||||
try {
|
||||
std::string sql;
|
||||
sqlite3_stmt* stmt;
|
||||
int rc;
|
||||
int resultCode = 0;
|
||||
std::string userId;
|
||||
int numEmbeds = GetNumEmbeddings();
|
||||
_logger.LogDebug("FaceDatabase::GetEmbeddings", "There are " + std::to_string(numEmbeds) + " embeddings in database", __FILE__, __LINE__);
|
||||
// get embedding
|
||||
//_mutex.lock();
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
sql = "SELECT * FROM FACE;";
|
||||
rc = sqlite3_prepare_v2(m_db, sql.c_str(), -1, &stmt, NULL);
|
||||
if (rc != SQLITE_OK) {
|
||||
//this->_logger.LogError("FaceDatabase::GetEmbeddings. SQL step error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
resultCode = -1;
|
||||
}
|
||||
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||
const char* userIdText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
|
||||
if (!userIdText) continue;
|
||||
userId = std::string(userIdText);
|
||||
float* embedding = (float*)sqlite3_column_blob(stmt, 3);
|
||||
if (!embedding) continue;
|
||||
recognizer.AddEmbedding(userId, embedding);
|
||||
}
|
||||
if (rc != SQLITE_DONE) {
|
||||
//this->_logger.LogError("FaceDatabase::GetEmbeddings. SQL error:", sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
resultCode = 0;
|
||||
//_mutex.unlock();
|
||||
return resultCode;
|
||||
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
//this->_logger.LogFatal("FaceDatabase::GetEmbeddings", e.what(), __FILE__, __LINE__);
|
||||
//_mutex.unlock();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int FaceDatabase::GetAllEmbeddingRecords(std::vector<FaceEmbeddingRecord>& records) {
|
||||
try {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
records.clear();
|
||||
|
||||
int numEmbeds = 0;
|
||||
{
|
||||
// Count for pre-allocation (inline, mutex already held)
|
||||
sqlite3_stmt* countStmt;
|
||||
int rc = sqlite3_prepare_v2(m_db, "SELECT COUNT(*) FROM FACE;", -1, &countStmt, NULL);
|
||||
if (rc == SQLITE_OK && sqlite3_step(countStmt) == SQLITE_ROW) {
|
||||
numEmbeds = sqlite3_column_int(countStmt, 0);
|
||||
}
|
||||
sqlite3_finalize(countStmt);
|
||||
}
|
||||
|
||||
records.reserve(numEmbeds);
|
||||
|
||||
sqlite3_stmt* stmt;
|
||||
int rc = sqlite3_prepare_v2(m_db,
|
||||
"SELECT ID, USER, EMBEDDING FROM FACE;", -1, &stmt, NULL);
|
||||
if (rc != SQLITE_OK) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||
int faceId = sqlite3_column_int(stmt, 0);
|
||||
|
||||
const char* userIdText = reinterpret_cast<const char*>(
|
||||
sqlite3_column_text(stmt, 1));
|
||||
if (!userIdText) continue;
|
||||
|
||||
const float* embData = reinterpret_cast<const float*>(
|
||||
sqlite3_column_blob(stmt, 2));
|
||||
int embBytes = sqlite3_column_bytes(stmt, 2);
|
||||
int embSize = embBytes / static_cast<int>(sizeof(float));
|
||||
if (!embData || embSize <= 0) continue;
|
||||
|
||||
FaceEmbeddingRecord record;
|
||||
record.faceId = faceId;
|
||||
record.userId = std::string(userIdText);
|
||||
record.embedding.assign(embData, embData + embSize);
|
||||
records.push_back(std::move(record));
|
||||
}
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
return 0;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int FaceDatabase::GetUsersWithFaceIds(std::vector<int>& ids, std::vector<std::string>& codes,
|
||||
std::vector<std::string>& userNames, std::vector<std::vector<int>>& faceIdsByUser) {
|
||||
// Fix #12: Single JOIN query replaces N+1 GetUser + GetFaceIdsByUser calls
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
ids.clear();
|
||||
codes.clear();
|
||||
userNames.clear();
|
||||
faceIdsByUser.clear();
|
||||
|
||||
try {
|
||||
sqlite3_stmt* stmt;
|
||||
int rc = sqlite3_prepare_v2(m_db,
|
||||
"SELECT U.ID, U.CODE, U.NAME, F.ID AS FACE_ID "
|
||||
"FROM USER U LEFT JOIN FACE F ON U.ID = F.USER "
|
||||
"ORDER BY U.ID;",
|
||||
-1, &stmt, NULL);
|
||||
if (rc != SQLITE_OK) {
|
||||
_logger.LogError("FaceDatabase::GetUsersWithFaceIds",
|
||||
std::string("SQL error: ") + sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
|
||||
int lastUserId = -1;
|
||||
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||
int userId = sqlite3_column_int(stmt, 0);
|
||||
if (userId != lastUserId) {
|
||||
// New user row
|
||||
const char* codeText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 1));
|
||||
const char* nameText = reinterpret_cast<const char*>(sqlite3_column_text(stmt, 2));
|
||||
ids.push_back(userId);
|
||||
codes.push_back(codeText ? std::string(codeText) : "");
|
||||
userNames.push_back(nameText ? std::string(nameText) : "");
|
||||
faceIdsByUser.emplace_back();
|
||||
lastUserId = userId;
|
||||
}
|
||||
// Append face ID (may be NULL if user has no faces — LEFT JOIN)
|
||||
if (sqlite3_column_type(stmt, 3) != SQLITE_NULL) {
|
||||
faceIdsByUser.back().push_back(sqlite3_column_int(stmt, 3));
|
||||
}
|
||||
}
|
||||
if (rc != SQLITE_DONE) {
|
||||
_logger.LogError("FaceDatabase::GetUsersWithFaceIds",
|
||||
std::string("SQL step error: ") + sqlite3_errmsg(m_db), __FILE__, __LINE__);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
return 1;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogError("FaceDatabase::GetUsersWithFaceIds", e.what(), __FILE__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
FaceDatabase::~FaceDatabase() {
|
||||
try {
|
||||
Destroy();
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
_logger.LogError("FaceDatabase::~FaceDatabase", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool FaceDatabase::Destroy() {
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
try {
|
||||
if (!m_db) return true;
|
||||
int rc = sqlite3_close(m_db);
|
||||
if (rc == SQLITE_OK) {
|
||||
//this->_logger.LogDebug("Database::Destroy", "Success!", __FILE__, __LINE__);
|
||||
m_db = nullptr;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
std::string errorCode = "Cannot close database connection.Error code:" + std::to_string(rc);
|
||||
//this->_logger.LogError("Database::Destroy", errorCode, __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
//this->_logger.LogFatal("FaceDatabase::Destroy", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#ifndef FACEDATABASE_H
|
||||
#define FACEDATABASE_H
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
//#include "ARCFaceRT.h"
|
||||
#include "sqlite3.h"
|
||||
#include <vector>
|
||||
#include "ANSFRCommon.h"
|
||||
|
||||
namespace ANSCENTER {
|
||||
// Structured embedding record for double-buffer reload
|
||||
struct FaceEmbeddingRecord {
|
||||
int faceId; // FACE.ID (unique per face)
|
||||
std::string userId; // FACE.USER (= USER.ID, as string)
|
||||
std::vector<float> embedding;
|
||||
};
|
||||
|
||||
class FaceDatabase {
|
||||
public:
|
||||
FaceDatabase();
|
||||
~FaceDatabase();
|
||||
bool Destroy();
|
||||
bool Initialize(std::string path, int embeddingDim, std::string dataFolder);
|
||||
int InsertUser(std::string code, std::string userName);
|
||||
int UpdateUser(int UserId, std::string newUserCode, std::string newUserName);
|
||||
int InsertFace(int userId, std::string imgPath, float embedding[]);
|
||||
int DeleteFace(int id);
|
||||
int DeleteUser(int UserId);
|
||||
int GetFaceIdsByUser(int userId, std::vector<int>& faceIds);
|
||||
int GetUserId(std::string code);
|
||||
int GetUser(int userId, std::string& code, std::string& userName);
|
||||
int GetUsers(std::vector<int>& ids, std::vector<std::string>& codes, std::vector<std::string>& userNames);
|
||||
// Fix #12: Batch query — returns users with their face IDs in a single query
|
||||
int GetUsersWithFaceIds(std::vector<int>& ids, std::vector<std::string>& codes,
|
||||
std::vector<std::string>& userNames, std::vector<std::vector<int>>& faceIdsByUser);
|
||||
int GetFaceById(int faceId, int& userId, std::string& imagePath);
|
||||
std::map<std::string, std::string> GetUserDict();
|
||||
int GetEmbeddings(ANSFRBase& recognizer);
|
||||
int GetEmbeddings(std::unique_ptr<ANSFRBase>& recognizer);
|
||||
// Returns all face embedding records (FACE.ID + USER.ID + embedding) for double-buffer reload
|
||||
int GetAllEmbeddingRecords(std::vector<FaceEmbeddingRecord>& records);
|
||||
private:
|
||||
sqlite3* m_db;
|
||||
int m_embedding_dim;
|
||||
std::string _dataFolder;
|
||||
std::mutex _mutex;
|
||||
// Fix #10: Logger for database operations
|
||||
ANSCENTER::SPDLogger& _logger = ANSCENTER::SPDLogger::GetInstance("FaceDB");
|
||||
int GetNumEmbeddings();
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,412 @@
|
||||
#include "FaceNet.h"
|
||||
namespace ANSCENTER {
|
||||
std::string ANSFaceNet::GetOpenVINODevice() {
|
||||
ov::Core core;
|
||||
std::vector<std::string> available_devices = core.get_available_devices();
|
||||
// Prioritize devices: NPU > GPU > CPU
|
||||
std::vector<std::string> priority_devices = {"GPU", "CPU" };
|
||||
for (const auto& device : priority_devices) {
|
||||
if (std::find(available_devices.begin(), available_devices.end(), device) != available_devices.end()) {
|
||||
return device; // Return the first available device based on priority
|
||||
}
|
||||
}
|
||||
|
||||
// Default to CPU if no prioritized devices are found
|
||||
return "CPU";
|
||||
}
|
||||
|
||||
bool ANSFaceNet::Initialize(std::string licenseKey,ModelConfig modelConfig,
|
||||
const std::string& modelZipFilePath,const std::string& modelZipPassword,std::string& labelMap)
|
||||
{
|
||||
bool result = ANSFRBase::Initialize(licenseKey, modelConfig, modelZipFilePath, modelZipPassword, labelMap);
|
||||
if (!result) return false;
|
||||
try {
|
||||
_modelConfig = modelConfig;
|
||||
_modelConfig.modelType = ModelType::FACERECOGNIZE;
|
||||
_modelConfig.detectionType = DetectionType::FACERECOGNIZER;
|
||||
|
||||
// We need to get the modelfolder from here
|
||||
std::string faceidModel = CreateFilePath(_modelFolder, "ansfacenet.xml");
|
||||
if (std::filesystem::exists(faceidModel)) {
|
||||
_modelFilePath = faceidModel;
|
||||
this->_logger.LogDebug("ANSFaceNet::Initialize. Loading ansfacenet weight", _modelFilePath, __FILE__, __LINE__);
|
||||
}
|
||||
else {
|
||||
this->_logger.LogError("ANSFaceNet::Initialize. Model ansfacenet.xml file is not exist", _modelFilePath, __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
m_knownPersonThresh = _modelConfig.unknownPersonThreshold;
|
||||
if (m_knownPersonThresh == 0)m_knownPersonThresh = 0.35;
|
||||
|
||||
std::string deviceName = GetOpenVINODevice();
|
||||
ov::Core core;
|
||||
CnnConfig reid_config(_modelFilePath, "Face Re-Identification");
|
||||
reid_config.m_deviceName = deviceName;
|
||||
//core.set_property(deviceName, ov::hint::performance_mode(ov::hint::PerformanceMode::THROUGHPUT));
|
||||
reid_config.m_max_batch_size = 1;
|
||||
reid_config.m_core = core;
|
||||
this->faceRecognizer = std::make_unique<VectorCNN>(reid_config);
|
||||
if (faceRecognizer == nullptr) {
|
||||
this->_logger.LogFatal("ANSFaceNet::Initialize", "Failed to initialize face recognizer model", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
Init();
|
||||
_isInitialized = true;
|
||||
return true;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger.LogFatal("ANSFaceNet::Initialize", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ANSFaceNet::LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword) {
|
||||
try {
|
||||
// We need to get the _modelFolder
|
||||
bool result = ANSFRBase::LoadModel(modelZipFilePath, modelZipPassword);
|
||||
if (!result) return false;
|
||||
// We need to get the modelfolder from here
|
||||
std::string faceidModel = CreateFilePath(_modelFolder, "ansfacenet.xml");
|
||||
if (std::filesystem::exists(faceidModel)) {
|
||||
_modelFilePath = faceidModel;
|
||||
this->_logger.LogDebug("ANSFaceNet::LoadModel. Loading ansfacenet weight", _modelFilePath, __FILE__, __LINE__);
|
||||
}
|
||||
else {
|
||||
this->_logger.LogError("ANSFaceNet::Initialize. Model ansfacenet.xml file is not exist", _modelFilePath, __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger.LogFatal("ArcFace50::LoadModel", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool ANSFaceNet::OptimizeModel(bool fp16, std::string& optimizedModelFolder) {
|
||||
if (!FileExist(_modelFilePath)) {
|
||||
optimizedModelFolder = "";
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
std::vector<float> ANSFaceNet::Feature(const cv::Mat& image, const ANSCENTER::Object& bBox) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
std::vector<float> embeddingResult;
|
||||
try {
|
||||
if (image.empty()) return embeddingResult;
|
||||
if ((image.cols < 10) || (image.rows < 10)) return embeddingResult;
|
||||
std::vector<cv::Mat> embeddings;
|
||||
std::vector<cv::Mat> face_rois;
|
||||
face_rois.clear();
|
||||
embeddings.clear();
|
||||
face_rois.push_back(bBox.mask);
|
||||
faceRecognizer->Compute(face_rois, &embeddings);
|
||||
embeddingResult.assign(embeddings[0].begin<float>(), embeddings[0].end<float>());
|
||||
return embeddingResult;
|
||||
}
|
||||
|
||||
catch (std::exception& e) {
|
||||
std::vector<float> embeddingResult;
|
||||
embeddingResult.clear();
|
||||
this->_logger.LogFatal("ANSFaceNet::Feature", e.what(), __FILE__, __LINE__);
|
||||
return embeddingResult;
|
||||
}
|
||||
}
|
||||
std::vector<FaceResultObject> ANSFaceNet::Match(
|
||||
const cv::Mat& input,
|
||||
const std::vector<ANSCENTER::Object>& bBox,
|
||||
const std::map<std::string, std::string>& userDict) {
|
||||
|
||||
std::vector<FaceResultObject> resultObjects;
|
||||
|
||||
// Early validation before locking
|
||||
if (input.empty()) {
|
||||
return resultObjects;
|
||||
}
|
||||
|
||||
if (input.cols < 10 || input.rows < 10) {
|
||||
return resultObjects;
|
||||
}
|
||||
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
|
||||
if (!_isInitialized) {
|
||||
_logger.LogError("ANSFaceNet::Match", "Model is not initialized", __FILE__, __LINE__);
|
||||
return resultObjects;
|
||||
}
|
||||
|
||||
try {
|
||||
// Convert grayscale to 3-channel BGR if needed
|
||||
cv::Mat processedImage;
|
||||
if (input.channels() == 1) {
|
||||
cv::cvtColor(input, processedImage, cv::COLOR_GRAY2BGR);
|
||||
}
|
||||
else {
|
||||
processedImage = input;
|
||||
}
|
||||
|
||||
// Get embeddings
|
||||
std::vector<std::vector<float>> detectedEmbeddings = Forward(processedImage, bBox);
|
||||
|
||||
// Search for matches
|
||||
std::vector<std::string> names;
|
||||
std::vector<float> sims;
|
||||
std::tie(names, sims) = SearchForFaces(detectedEmbeddings);
|
||||
|
||||
// Check if we got results
|
||||
if (names.empty()) {
|
||||
_logger.LogError("ANSFaceNet::Match", "No face is match", __FILE__, __LINE__);
|
||||
return resultObjects;
|
||||
}
|
||||
|
||||
// Pre-reserve result space
|
||||
const size_t resultCount = std::min(names.size(), bBox.size());
|
||||
resultObjects.reserve(resultCount);
|
||||
|
||||
// Build result objects
|
||||
for (size_t i = 0; i < resultCount; ++i) {
|
||||
FaceResultObject resultObject;
|
||||
|
||||
// Determine if face is known or unknown
|
||||
const bool isUnknown = (sims[i] > m_knownPersonThresh);
|
||||
|
||||
if (isUnknown) {
|
||||
resultObject.isUnknown = true;
|
||||
resultObject.userId = "0";
|
||||
resultObject.userName = "Unknown";
|
||||
resultObject.confidence = 1.0f; // 100% unknown
|
||||
}
|
||||
else {
|
||||
resultObject.isUnknown = false;
|
||||
resultObject.userId = names[i];
|
||||
|
||||
// Safe map lookup
|
||||
auto it = userDict.find(names[i]);
|
||||
resultObject.userName = (it != userDict.end()) ? it->second : names[i];
|
||||
|
||||
resultObject.confidence = 1.0f - sims[i];
|
||||
}
|
||||
|
||||
resultObject.similarity = sims[i];
|
||||
|
||||
// Copy bounding box (no clamping - keeping original logic)
|
||||
resultObject.box.x = bBox[i].box.x;
|
||||
resultObject.box.y = bBox[i].box.y;
|
||||
resultObject.box.width = bBox[i].box.width;
|
||||
resultObject.box.height = bBox[i].box.height;
|
||||
|
||||
// Copy additional data
|
||||
resultObject.mask = bBox[i].mask;
|
||||
resultObject.cameraId = bBox[i].cameraId;
|
||||
resultObject.trackId = bBox[i].trackId;
|
||||
resultObject.polygon = bBox[i].polygon;
|
||||
resultObject.kps = bBox[i].kps;
|
||||
resultObject.extraInformation = bBox[i].extraInfo;
|
||||
|
||||
resultObjects.push_back(std::move(resultObject));
|
||||
}
|
||||
|
||||
return resultObjects;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSFaceNet::Match", e.what(), __FILE__, __LINE__);
|
||||
return resultObjects;
|
||||
}
|
||||
}
|
||||
cv::Mat ANSFaceNet::GetCropFace(const cv::Mat& input, const ANSCENTER::Object& bBox) {
|
||||
try {
|
||||
cv::Mat processedImage;
|
||||
if (input.channels() == 1) {
|
||||
cv::cvtColor(input, processedImage, cv::COLOR_GRAY2BGR);
|
||||
}
|
||||
else {
|
||||
processedImage = input;
|
||||
}
|
||||
std::vector<ANSCENTER::Object> outputBbox;
|
||||
outputBbox.push_back(bBox);
|
||||
std::vector<struct CroppedFace> crFaces;
|
||||
crFaces.clear();
|
||||
ANSFRHelper::GetCroppedFaces(processedImage, outputBbox, FACE_WIDTH, FACE_HEIGHT, crFaces);
|
||||
return crFaces[0].faceMat;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
cv::Mat result;
|
||||
this->_logger.LogFatal("ANSFaceNet::GetCropFace", e.what(), __FILE__, __LINE__);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
bool ANSFaceNet::LoadEngine(const std::string xmlModelPath, bool engineOptimisation) {
|
||||
try {
|
||||
if (!FileExist(xmlModelPath)) {
|
||||
this->_logger.LogError("ANSFaceNet::LoadEngine", "Cannot find the raw XML model file.", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger.LogFatal("ANSFaceNet::LoadEngine", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Private methods, can be replacable
|
||||
void ANSFaceNet::AddEmbedding(const std::string& className, float embedding[]) {
|
||||
//std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
// Check if faiss_index is initialized
|
||||
if (!faiss_index) {
|
||||
this->_logger.LogFatal("ANSFaceNet::AddEmbedding", "Search_index is not initialized.", __FILE__, __LINE__);
|
||||
return;
|
||||
}
|
||||
// Convert embedding array to vector and check size
|
||||
std::vector<float> vec(embedding, embedding + FACE_EMBEDDING_SIZE);
|
||||
if (vec.size() != FACE_EMBEDDING_SIZE) {
|
||||
this->_logger.LogFatal("ANSFaceNet::AddEmbedding", "Embedding size does not match expected output dimension of 512.", __FILE__, __LINE__);
|
||||
return;
|
||||
}
|
||||
// Add class name and get index
|
||||
classNames.push_back(className);
|
||||
|
||||
// Add embedding to faiss_index
|
||||
faiss_index->add(1, vec.data());
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger.LogFatal("ANSFaceNet::AddEmbedding", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
}
|
||||
void ANSFaceNet::AddEmbedding(const std::string& className, const std::vector<float>& embedding) {
|
||||
//std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
// Ensure faiss_index is initialized
|
||||
if (!faiss_index) {
|
||||
this->_logger.LogFatal("ANSFaceNet::AddEmbedding", "Search_index is not initialized.", __FILE__, __LINE__);
|
||||
return;
|
||||
}
|
||||
// Check embedding size
|
||||
if (embedding.size() != FACE_EMBEDDING_SIZE) {
|
||||
this->_logger.LogFatal("ANSFaceNet::AddEmbedding", "Embedding size does not match expected output dimension of 512.", __FILE__, __LINE__);
|
||||
return;
|
||||
}
|
||||
// Add class name
|
||||
classNames.push_back(className);
|
||||
// Add embedding to faiss_index
|
||||
faiss_index->add(1, embedding.data());
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogFatal("ANSFaceNet::AddEmbedding", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
std::vector<std::vector<float>> ANSFaceNet::Forward(const cv::Mat& input, std::vector<ANSCENTER::Object> outputBbox) {
|
||||
//std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
std::vector<std::vector<float>> detectedEmbeddings;
|
||||
try {
|
||||
std::vector<cv::Mat> embeddings;
|
||||
std::vector<cv::Mat> face_rois;
|
||||
detectedEmbeddings.clear();
|
||||
if (outputBbox.size() > 0) {
|
||||
for (int i = 0; i < outputBbox.size(); i++) {
|
||||
face_rois.clear();
|
||||
embeddings.clear();
|
||||
std::vector<float> embeddingRs;
|
||||
face_rois.push_back(outputBbox[i].mask);
|
||||
faceRecognizer->Compute(face_rois, &embeddings);
|
||||
embeddingRs.assign(embeddings[0].begin<float>(), embeddings[0].end<float>());
|
||||
detectedEmbeddings.push_back(embeddingRs);
|
||||
}
|
||||
}
|
||||
return detectedEmbeddings;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger.LogFatal("ANSFaceNet::Forward", e.what(), __FILE__, __LINE__);
|
||||
return detectedEmbeddings;
|
||||
}
|
||||
}
|
||||
std::tuple<std::vector<std::string>, std::vector<float>> ANSFaceNet::SearchForFaces(const std::vector<std::vector<float>>& detectedEmbeddings)
|
||||
{
|
||||
//std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
std::vector<std::string> detectedUsers;
|
||||
std::vector<float> simValues;
|
||||
try {
|
||||
// Check if there are class names available
|
||||
if (!classNames.empty()) {
|
||||
const int k = 3; // Number of nearest neighbors to retrieve
|
||||
// Process each detected embedding
|
||||
for (const auto& embedding : detectedEmbeddings) {
|
||||
// Prepare vectors to hold search results
|
||||
std::vector<faiss::idx_t> indices(k);
|
||||
std::vector<float> distances(k);
|
||||
|
||||
// Perform the search for the k nearest neighbors
|
||||
faiss_index->search(1, embedding.data(), k, distances.data(), indices.data());
|
||||
|
||||
// Find the index with the maximum distance
|
||||
auto min_it = std::min_element(distances.begin(), distances.end());
|
||||
int best_index = std::distance(distances.begin(), min_it);
|
||||
|
||||
// Map the index to the corresponding class name and calculate similarity
|
||||
std::vector<float> matchEmbedding(faiss_index->d);
|
||||
faiss_index->reconstruct(indices[best_index], matchEmbedding.data());
|
||||
float similarity = 1 - CosineSimilarity(embedding, matchEmbedding, false);
|
||||
detectedUsers.push_back(classNames.at(indices[best_index]));
|
||||
simValues.push_back(std::abs(similarity));
|
||||
|
||||
}
|
||||
}
|
||||
else {
|
||||
// If no class names are available, mark all users as "unknown"
|
||||
detectedUsers.assign(detectedEmbeddings.size(), "0");
|
||||
simValues.assign(detectedEmbeddings.size(), 1.0f);
|
||||
}
|
||||
|
||||
return std::make_tuple(detectedUsers, simValues);
|
||||
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
// Log the error and return default values for the failed search
|
||||
detectedUsers.assign(1, "0");
|
||||
simValues.assign(1, 1.0f);
|
||||
this->_logger.LogFatal("ANSFaceNet::SearchForFaces", e.what(), __FILE__, __LINE__);
|
||||
return std::make_tuple(detectedUsers, simValues);
|
||||
}
|
||||
}
|
||||
void ANSFaceNet::Init() {
|
||||
try {
|
||||
classNames.clear();
|
||||
if (faiss_index) {
|
||||
faiss_index->reset();
|
||||
}
|
||||
else {
|
||||
faiss_index = std::make_unique<faiss::IndexFlatL2>(FACE_EMBEDDING_SIZE);
|
||||
}
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger.LogFatal("ANSFaceNet::Init", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
ANSFaceNet::~ANSFaceNet() {
|
||||
try {
|
||||
Destroy();
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger.LogFatal("ANSFaceNet::~ANSFaceNet()", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
bool ANSFaceNet::Destroy() {
|
||||
try {
|
||||
// Clear all resources safely
|
||||
classNames.clear();
|
||||
|
||||
// Reset and release faiss_index
|
||||
if (faiss_index) {
|
||||
faiss_index->reset();
|
||||
faiss_index.reset(); // Release shared pointer ownership
|
||||
}
|
||||
faceRecognizer.reset();
|
||||
return true;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger.LogFatal("ANSFaceNet::Destroy", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#ifndef FACENET_H
|
||||
#define FACENET_H
|
||||
#pragma once
|
||||
#include "ANSFRCommon.h"
|
||||
#include "hnswlib/hnswlib.h"
|
||||
#include "cnn.hpp"
|
||||
#include "face_reid.hpp"
|
||||
#include "openvino/openvino.hpp"
|
||||
#include <faiss/IndexFlat.h>
|
||||
|
||||
namespace ANSCENTER {
|
||||
class ANSFaceNet :public ANSFRBase {
|
||||
public:
|
||||
virtual bool Initialize(std::string licenseKey, ModelConfig modelConfig, const std::string& modelZipFilePath, const std::string& modelZipPassword, std::string& labelMap) override;
|
||||
virtual bool LoadModel(const std::string& modelZipFilePath, const std::string& modelZipPassword)override;
|
||||
bool OptimizeModel(bool fp16, std::string& optimizedModelFolder);
|
||||
std::vector<float> Feature(const cv::Mat& image, const ANSCENTER::Object& bBox); // Run inference and get embedding information from a cropped image
|
||||
std::vector<FaceResultObject> Match(const cv::Mat& input, const std::vector<ANSCENTER::Object>& bBox, const std::map<std::string, std::string>& userDict); // Run inference and get embedding information from a cropped image (the first bbox)
|
||||
cv::Mat GetCropFace(const cv::Mat& input, const ANSCENTER::Object& bBox);
|
||||
void Init();
|
||||
void AddEmbedding(const std::string& className, float embedding[]);
|
||||
void AddEmbedding(const std::string& className, const std::vector<float>& embedding);
|
||||
bool UpdateParamater(double knownPersonThreshold) {
|
||||
_modelConfig.unknownPersonThreshold = knownPersonThreshold;
|
||||
m_knownPersonThresh = _modelConfig.unknownPersonThreshold;
|
||||
return true;
|
||||
}
|
||||
~ANSFaceNet();
|
||||
bool Destroy();
|
||||
private:
|
||||
bool LoadEngine(const std::string engineFile, bool engineOptimisation = true);
|
||||
std::vector<std::vector<float>> Forward(const cv::Mat& input, std::vector<ANSCENTER::Object> outputBbox);
|
||||
std::tuple<std::vector<std::string>, std::vector<float>> SearchForFaces(const std::vector<std::vector<float>>& detectedEmbeddings);
|
||||
std::string GetOpenVINODevice();
|
||||
protected:
|
||||
std::vector<std::string> classNames;
|
||||
ModelConfig _modelConfig;
|
||||
std::string _modelFilePath;
|
||||
ov::Core core;
|
||||
float m_knownPersonThresh;
|
||||
const int FACE_WIDTH = 116;
|
||||
const int FACE_HEIGHT = 116;
|
||||
const int FACE_EMBEDDING_SIZE = 512;
|
||||
std::unique_ptr<VectorCNN>faceRecognizer = nullptr;
|
||||
std::unique_ptr<faiss::IndexFlatL2> faiss_index; // Use shared_ptr
|
||||
std::recursive_mutex _mutex;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,366 @@
|
||||
#include "RetinaFaceTRT.h"
|
||||
// This is standalone Retina Face detector using TensorRT (We will not use as it is not inherit the ANSFD class)
|
||||
namespace ANSCENTER {
|
||||
RetinaFaceTRT::RetinaFaceTRT()
|
||||
{
|
||||
m_outputBbox.clear();
|
||||
}
|
||||
bool RetinaFaceTRT::Initialize(const std::string engineFile,
|
||||
int frameWidth,
|
||||
int frameHeight,
|
||||
std::string inputName,
|
||||
std::vector<std::string> outputNames,
|
||||
std::vector<int> inputShape,
|
||||
int maxBatchSize,
|
||||
int maxFacesPerScene,
|
||||
float nms_threshold,
|
||||
float bbox_threshold)
|
||||
{
|
||||
try
|
||||
{
|
||||
assert(inputShape.size() == 3);
|
||||
m_INPUT_C = static_cast<const int>(inputShape[0]);
|
||||
m_INPUT_H = static_cast<const int>(inputShape[1]);
|
||||
m_INPUT_W = static_cast<const int>(inputShape[2]);
|
||||
m_INPUT_SIZE = static_cast<const int>(m_INPUT_C * m_INPUT_H * m_INPUT_W * sizeof(float));
|
||||
m_OUTPUT_SIZE_BASE = static_cast<const int>((m_INPUT_H / 8 * m_INPUT_W / 8 + m_INPUT_H / 16 * m_INPUT_W / 16 + m_INPUT_H / 32 * m_INPUT_W / 32) * 2);
|
||||
m_output0 = new float[m_OUTPUT_SIZE_BASE * 4];
|
||||
m_output1 = new float[m_OUTPUT_SIZE_BASE * 2];
|
||||
m_maxBatchSize = static_cast<const int>(maxBatchSize);
|
||||
m_maxFacesPerScene = static_cast<const int>(maxFacesPerScene);
|
||||
m_nms_threshold = static_cast<const float>(nms_threshold);
|
||||
m_bbox_threshold = static_cast<const float>(bbox_threshold);
|
||||
// load engine from .engine file
|
||||
LoadEngine(engineFile);
|
||||
|
||||
// create stream and pre-allocate GPU buffers memory
|
||||
PreInference(inputName, outputNames);
|
||||
return true;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger.LogFatal("RetinaFace::Initialize", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
void RetinaFaceTRT::LoadEngine(const std::string engineFile)
|
||||
{
|
||||
try {
|
||||
if (FileExist(engineFile)) {
|
||||
this->_logger.LogDebug("RetinaFace::LoadEngine", "Loading RetinaFace Engine...", __FILE__, __LINE__);
|
||||
std::vector<char> trtModelStream_;
|
||||
size_t size{ 0 };
|
||||
|
||||
std::ifstream file(engineFile, std::ios::binary);
|
||||
if (file.good()) {
|
||||
file.seekg(0, file.end);
|
||||
size = file.tellg();
|
||||
file.seekg(0, file.beg);
|
||||
trtModelStream_.resize(size);
|
||||
file.read(trtModelStream_.data(), size);
|
||||
file.close();
|
||||
}
|
||||
nvinfer1::IRuntime* runtime = nvinfer1::createInferRuntime(m_logger);
|
||||
assert(runtime != nullptr);
|
||||
m_engine = runtime->deserializeCudaEngine(trtModelStream_.data(), size);
|
||||
assert(m_engine != nullptr);
|
||||
m_context = m_engine->createExecutionContext();
|
||||
assert(m_context != nullptr);
|
||||
}
|
||||
else {
|
||||
this->_logger.LogError("RetinaFace::LoadEngine", "Cant find engine file", __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger.LogFatal("RetinaFace::Initialize", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void RetinaFaceTRT::PreInference(std::string inputName, std::vector<std::string> outputNames) {
|
||||
try {
|
||||
/*
|
||||
Does not make landmark head as we do not use face alignment.
|
||||
*/
|
||||
// Assert
|
||||
assert(outputNames.size() == 2);
|
||||
|
||||
#if NV_TENSORRT_MAJOR >= 10
|
||||
// TRT 10+: use named tensor API
|
||||
assert(m_engine->getNbIOTensors() == 3);
|
||||
|
||||
// Look up tensor indices by name
|
||||
for (int i = 0; i < m_engine->getNbIOTensors(); ++i) {
|
||||
const char* name = m_engine->getIOTensorName(i);
|
||||
if (name == inputName) inputIndex = i;
|
||||
else if (name == outputNames[0]) outputIndex0 = i;
|
||||
else if (name == outputNames[1]) outputIndex1 = i;
|
||||
}
|
||||
#else
|
||||
// TRT 8.x: use binding API
|
||||
assert(m_engine->getNbBindings() == 3);
|
||||
inputIndex = m_engine->getBindingIndex(inputName.c_str());
|
||||
outputIndex0 = m_engine->getBindingIndex(outputNames[0].c_str());
|
||||
outputIndex1 = m_engine->getBindingIndex(outputNames[1].c_str());
|
||||
#endif
|
||||
|
||||
// Create GPU buffers on device
|
||||
ANSFRHelper::CheckCudaStatus(cudaMalloc(&buffers[inputIndex], m_maxBatchSize * m_INPUT_SIZE));
|
||||
ANSFRHelper::CheckCudaStatus(cudaMalloc(&buffers[outputIndex0], m_maxBatchSize * m_OUTPUT_SIZE_BASE * 4 * sizeof(float)));
|
||||
ANSFRHelper::CheckCudaStatus(cudaMalloc(&buffers[outputIndex1], m_maxBatchSize * m_OUTPUT_SIZE_BASE * 2 * sizeof(float)));
|
||||
|
||||
#if NV_TENSORRT_MAJOR >= 10
|
||||
// TRT 10+: bind tensor addresses
|
||||
m_context->setTensorAddress(inputName.c_str(), buffers[inputIndex]);
|
||||
m_context->setTensorAddress(outputNames[0].c_str(), buffers[outputIndex0]);
|
||||
m_context->setTensorAddress(outputNames[1].c_str(), buffers[outputIndex1]);
|
||||
#endif
|
||||
|
||||
// Create stream
|
||||
ANSFRHelper::CheckCudaStatus(cudaStreamCreate(&stream));
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger.LogFatal("RetinaFace::PreInference", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void RetinaFaceTRT::PreProcess(cv::Mat& img) {
|
||||
try {
|
||||
// Release input vector
|
||||
m_input.release();
|
||||
|
||||
// Resize
|
||||
float w, h, x, y;
|
||||
if (m_scale_h > m_scale_w) {
|
||||
w = float(m_INPUT_W);
|
||||
h = float(m_scale_w * img.rows);
|
||||
x = 0;
|
||||
y = float((m_INPUT_H - h) / 2);
|
||||
}
|
||||
else {
|
||||
w = float(m_scale_h * img.cols);
|
||||
h = float(m_INPUT_H);
|
||||
x = float((m_INPUT_W - w) / 2);
|
||||
y = 0;
|
||||
}
|
||||
cv::Mat re((int)h, (int)w, CV_8UC3);
|
||||
cv::resize(img, re, re.size(), 0, 0, cv::INTER_LINEAR);
|
||||
cv::Mat out((int)m_INPUT_H, (int)m_INPUT_W, CV_8UC3, cv::Scalar(128, 128, 128));
|
||||
re.copyTo(out(cv::Rect(int(x), int(y), re.cols, re.rows)));
|
||||
|
||||
|
||||
// Normalize
|
||||
out.convertTo(out, CV_32F);
|
||||
out = out - cv::Scalar(104, 117, 123);
|
||||
std::vector<cv::Mat> temp;
|
||||
cv::split(out, temp);
|
||||
for (int i = 0; i < temp.size(); i++) {
|
||||
m_input.push_back(temp[i]);
|
||||
}
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger.LogFatal("RetinaFace::PreProcess", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void RetinaFaceTRT::RunInference(float* input, float* output0, float* output1) {
|
||||
try {
|
||||
// DMA input batch data to device, infer on the batch asynchronously, and DMA output back to host
|
||||
ANSCENTER::ANSFRHelper::CheckCudaStatus(cudaMemcpyAsync(buffers[inputIndex], input, m_maxBatchSize * m_INPUT_SIZE, cudaMemcpyHostToDevice, stream));
|
||||
#if NV_TENSORRT_MAJOR >= 10
|
||||
m_context->enqueueV3(stream);
|
||||
#else
|
||||
m_context->enqueueV2(buffers, stream, nullptr);
|
||||
#endif
|
||||
ANSFRHelper::CheckCudaStatus(cudaMemcpyAsync(output0, buffers[outputIndex0], m_maxBatchSize * m_OUTPUT_SIZE_BASE * 4 * sizeof(float), cudaMemcpyDeviceToHost, stream));
|
||||
ANSFRHelper::CheckCudaStatus(cudaMemcpyAsync(output1, buffers[outputIndex1], m_maxBatchSize * m_OUTPUT_SIZE_BASE * 2 * sizeof(float), cudaMemcpyDeviceToHost, stream));
|
||||
cudaStreamSynchronize(stream);
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger.LogFatal("RetinaFace::RunInference", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<struct Bbox> RetinaFaceTRT::FindFace(cv::Mat& img) {
|
||||
try {
|
||||
m_outputBbox.clear();
|
||||
int out_rows = img.rows;
|
||||
int out_cols = img.cols;
|
||||
m_frameWidth = static_cast<const int>(out_cols);
|
||||
m_frameHeight = static_cast<const int>(out_rows);
|
||||
m_scale_h = (float)(m_INPUT_H) / float(m_frameHeight);
|
||||
m_scale_w = (float)(m_INPUT_W) / float(m_frameWidth);
|
||||
PreProcess(img);
|
||||
RunInference((float*)m_input.ptr<float>(0), m_output0, m_output1);
|
||||
PostProcessing(m_output0, m_output1);
|
||||
return m_outputBbox;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
m_outputBbox.clear();
|
||||
this->_logger.LogFatal("RetinaFace::FindFace", e.what(), __FILE__, __LINE__);
|
||||
return m_outputBbox;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void RetinaFaceTRT::PostProcessing(float* bbox, float* conf) {
|
||||
try {
|
||||
m_outputBbox.clear();
|
||||
std::vector<AnchorBox> anchor;
|
||||
CreateAnchorRetinaface(anchor, m_INPUT_W, m_INPUT_H);
|
||||
|
||||
for (int i = 0; i < anchor.size(); ++i) {
|
||||
if (*(conf + 1) > m_bbox_threshold) {
|
||||
AnchorBox tmp = anchor[i];
|
||||
AnchorBox tmp1;
|
||||
Bbox result{};
|
||||
|
||||
// decode bbox (y - W; x - H)
|
||||
tmp1.cx = float(tmp.cx + *bbox * 0.1 * tmp.sx);
|
||||
tmp1.cy = float(tmp.cy + *(bbox + 1) * 0.1 * tmp.sy);
|
||||
tmp1.sx = float(tmp.sx * exp(*(bbox + 2) * 0.2));
|
||||
tmp1.sy = float(tmp.sy * exp(*(bbox + 3) * 0.2));
|
||||
|
||||
result.y1 = int((tmp1.cx - tmp1.sx / 2) * m_INPUT_W);
|
||||
result.x1 = int((tmp1.cy - tmp1.sy / 2) * m_INPUT_H);
|
||||
result.y2 = int((tmp1.cx + tmp1.sx / 2) * m_INPUT_W);
|
||||
result.x2 = int((tmp1.cy + tmp1.sy / 2) * m_INPUT_H);
|
||||
|
||||
// rescale to original size
|
||||
if (m_scale_h > m_scale_w) {
|
||||
result.y1 = int(result.y1 / m_scale_w);
|
||||
result.y2 = int(result.y2 / m_scale_w);
|
||||
result.x1 = int((result.x1 - (m_INPUT_H - m_scale_w * m_frameHeight) / 2) / m_scale_w);
|
||||
result.x2 = int((result.x2 - (m_INPUT_H - m_scale_w * m_frameHeight) / 2) / m_scale_w);
|
||||
}
|
||||
else {
|
||||
result.y1 = int((result.y1 - (m_INPUT_W - m_scale_h * m_frameWidth) / 2) / m_scale_h);
|
||||
result.y2 = int((result.y2 - (m_INPUT_W - m_scale_h * m_frameWidth) / 2) / m_scale_h);
|
||||
result.x1 = int(result.x1 / m_scale_h);
|
||||
result.x2 = int(result.x2 / m_scale_h);
|
||||
}
|
||||
|
||||
// Clip object box coordinates to network resolution
|
||||
result.y1 = CLIP(result.y1, 0, m_frameWidth - 1);
|
||||
result.x1 = CLIP(result.x1, 0, m_frameHeight - 1);
|
||||
result.y2 = CLIP(result.y2, 0, m_frameWidth - 1);
|
||||
result.x2 = CLIP(result.x2, 0, m_frameHeight - 1);
|
||||
|
||||
// Get confidence
|
||||
result.score = *(conf + 1);
|
||||
|
||||
// Push to result vector
|
||||
m_outputBbox.push_back(result);
|
||||
}
|
||||
bbox += 4;
|
||||
conf += 2;
|
||||
}
|
||||
std::sort(m_outputBbox.begin(), m_outputBbox.end(), MCMP);
|
||||
NMS(m_outputBbox, m_nms_threshold);
|
||||
if (m_outputBbox.size() > m_maxFacesPerScene)
|
||||
m_outputBbox.resize(m_maxFacesPerScene);
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
m_outputBbox.clear();
|
||||
this->_logger.LogFatal("RetinaFace::PostProcessing", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
|
||||
void RetinaFaceTRT::CreateAnchorRetinaface(std::vector<AnchorBox>& anchor, int w, int h) {
|
||||
try {
|
||||
anchor.clear();
|
||||
std::vector<std::vector<int>> feature_map(3), min_sizes(3);
|
||||
float steps[] = { 8, 16, 32 };
|
||||
for (int i = 0; i < feature_map.size(); ++i) {
|
||||
feature_map[i].push_back(int(ceil(h / steps[i])));
|
||||
feature_map[i].push_back(int(ceil(w / steps[i])));
|
||||
}
|
||||
std::vector<int> minsize1 = { 10, 20 };
|
||||
min_sizes[0] = minsize1;
|
||||
std::vector<int> minsize2 = { 32, 64 };
|
||||
min_sizes[1] = minsize2;
|
||||
std::vector<int> minsize3 = { 128, 256 };
|
||||
min_sizes[2] = minsize3;
|
||||
|
||||
for (int k = 0; k < feature_map.size(); ++k) {
|
||||
std::vector<int> min_size = min_sizes[k];
|
||||
for (int i = 0; i < feature_map[k][0]; ++i) {
|
||||
for (int j = 0; j < feature_map[k][1]; ++j) {
|
||||
for (int l = 0; l < min_size.size(); ++l) {
|
||||
float s_kx = float(min_size[l] * 1.0 / w);
|
||||
float s_ky = float(min_size[l] * 1.0 / h);
|
||||
float cx = float((j + 0.5) * steps[k] / w);
|
||||
float cy = float((i + 0.5) * steps[k] / h);
|
||||
AnchorBox axil = { cx, cy, s_kx, s_ky };
|
||||
anchor.push_back(axil);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
m_outputBbox.clear();
|
||||
this->_logger.LogFatal("RetinaFace::CreateAnchorRetinaface", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
inline bool RetinaFaceTRT::MCMP(Bbox a, Bbox b) {
|
||||
if (a.score > b.score)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void RetinaFaceTRT::NMS(std::vector<Bbox>& input_boxes, float NMS_THRESH) {
|
||||
try {
|
||||
std::vector<float> vArea(input_boxes.size());
|
||||
for (int i = 0; i < int(input_boxes.size()); ++i) {
|
||||
vArea[i] = float((input_boxes.at(i).x2 - input_boxes.at(i).x1 + 1) * (input_boxes.at(i).y2 - input_boxes.at(i).y1 + 1));
|
||||
}
|
||||
for (int i = 0; i < int(input_boxes.size()); ++i) {
|
||||
for (int j = i + 1; j < int(input_boxes.size());) {
|
||||
float xx1 = (float)(std::max(input_boxes[i].x1, input_boxes[j].x1));
|
||||
float yy1 = (float)(std::max(input_boxes[i].y1, input_boxes[j].y1));
|
||||
float xx2 = (float)(std::min(input_boxes[i].x2, input_boxes[j].x2));
|
||||
float yy2 = (float)(std::min(input_boxes[i].y2, input_boxes[j].y2));
|
||||
float w = (float)(std::max(float(0), xx2 - xx1 + 1));
|
||||
float h = (float)(std::max(float(0), yy2 - yy1 + 1));
|
||||
float inter = (float)(w * h);
|
||||
float ovr = (float)(inter / (vArea[i] + vArea[j] - inter));
|
||||
if (ovr >= NMS_THRESH) {
|
||||
input_boxes.erase(input_boxes.begin() + j);
|
||||
vArea.erase(vArea.begin() + j);
|
||||
}
|
||||
else {
|
||||
j++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
m_outputBbox.clear();
|
||||
this->_logger.LogFatal("RetinaFace::NMS", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
|
||||
RetinaFaceTRT::~RetinaFaceTRT() {
|
||||
try {
|
||||
// Release stream and buffers
|
||||
ANSFRHelper::CheckCudaStatus(cudaStreamDestroy(stream));
|
||||
ANSFRHelper::CheckCudaStatus(cudaFree(buffers[inputIndex]));
|
||||
ANSFRHelper::CheckCudaStatus(cudaFree(buffers[outputIndex0]));
|
||||
ANSFRHelper::CheckCudaStatus(cudaFree(buffers[outputIndex1]));
|
||||
// checkCudaStatus(cudaFree(buffers[outputIndex2]));
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
m_outputBbox.clear();
|
||||
this->_logger.LogFatal("RetinaFace::~RetinaFace", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#ifndef RETINAFACE_H
|
||||
#define RETINAFACE_H
|
||||
#pragma once
|
||||
#include "ANSFRCommon.h"
|
||||
#define CLIP(a, b, c) (MAX(MIN(a, c), b)) // MIN, MAX defined in opencv
|
||||
namespace ANSCENTER {
|
||||
// Retina will be FD subclass
|
||||
class RetinaFaceTRT {
|
||||
public:
|
||||
RetinaFaceTRT();
|
||||
~RetinaFaceTRT();
|
||||
std::vector<struct Bbox> FindFace(cv::Mat& img);
|
||||
bool Initialize(const std::string engineFile,
|
||||
int frameWidth,
|
||||
int frameHeight,
|
||||
std::string inputName,
|
||||
std::vector<std::string> outputNames,
|
||||
std::vector<int> inputShape,
|
||||
int maxBatchSize,
|
||||
int maxFacesPerScene,
|
||||
float nms_threshold,
|
||||
float bbox_threshold);
|
||||
|
||||
private:
|
||||
ANSCENTER::SPDLogger& _logger = ANSCENTER::SPDLogger::GetInstance("ANSFD", true);
|
||||
TRTLogger m_logger;
|
||||
|
||||
int m_frameWidth, m_frameHeight, m_INPUT_C, m_INPUT_H, m_INPUT_W, m_INPUT_SIZE, m_OUTPUT_SIZE_BASE, m_maxBatchSize, m_maxFacesPerScene;
|
||||
float m_nms_threshold, m_bbox_threshold, m_scale_h, m_scale_w;
|
||||
cv::Mat m_input;
|
||||
float* m_output0, * m_output1;
|
||||
std::vector<struct Bbox> m_outputBbox;
|
||||
|
||||
nvinfer1::ICudaEngine* m_engine;
|
||||
nvinfer1::IExecutionContext* m_context;
|
||||
cudaStream_t stream;
|
||||
void* buffers[3];
|
||||
int inputIndex, outputIndex0, outputIndex1;
|
||||
private:
|
||||
void LoadEngine(const std::string engineFile);
|
||||
void PreInference(std::string inputNames, std::vector<std::string> outputNames);
|
||||
void RunInference(float* input, float* output0, float* output1);
|
||||
void PreProcess(cv::Mat& img);
|
||||
void PostProcessing(float* bbox, float* conf);
|
||||
void CreateAnchorRetinaface(std::vector<AnchorBox>& anchor, int w, int h);
|
||||
static inline bool MCMP(Bbox a, Bbox b);
|
||||
void NMS(std::vector<Bbox>& input_boxes, float NMS_THRESH);
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,84 @@
|
||||
#include"WebClient.h"
|
||||
namespace ANSCENTER {
|
||||
#include "webclient.h"
|
||||
|
||||
WebSocketClient::WebSocketClient(std::string host, std::string port, std::string url) {
|
||||
// Look up the domain name
|
||||
tcp::resolver resolver{ m_ioc };
|
||||
auto const results = resolver.resolve(host, port);
|
||||
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
auto ep = net::connect(m_ws.next_layer(), results);
|
||||
|
||||
// Update the host_ string. This will provide the value of the
|
||||
// Host HTTP header during the WebSocket handshake.
|
||||
// See https://tools.ietf.org/html/rfc7230#section-5.4
|
||||
host += ':' + std::to_string(ep.port());
|
||||
|
||||
// Perform the websocket handshake
|
||||
m_ws.handshake(host, url);
|
||||
}
|
||||
|
||||
std::string WebSocketClient::Send(std::string s) {
|
||||
// Clear read buffer
|
||||
m_buffer.clear();
|
||||
|
||||
// Send the message
|
||||
m_ws.write(net::buffer(s));
|
||||
|
||||
// Read a message into our buffer
|
||||
m_ws.read(m_buffer);
|
||||
return beast::buffers_to_string(m_buffer.data());
|
||||
}
|
||||
|
||||
WebSocketClient::~WebSocketClient() {
|
||||
// Close the WebSocket connection
|
||||
m_ws.close(websocket::close_code::normal);
|
||||
}
|
||||
|
||||
HttpClient::HttpClient(std::string host, std::string port, std::string url) {
|
||||
// Look up the domain name
|
||||
tcp::resolver resolver{ m_ioc };
|
||||
m_results = resolver.resolve(host, port);
|
||||
|
||||
// Set up an HTTP POST request message
|
||||
m_req.method(beast::http::verb::post);
|
||||
m_req.target(url);
|
||||
m_req.set(http::field::host, host);
|
||||
m_req.set(http::field::content_type, "application/json");
|
||||
}
|
||||
|
||||
std::string HttpClient::Send(std::string s) {
|
||||
// Make the connection on the IP address we get from a lookup
|
||||
m_stream.connect(m_results);
|
||||
|
||||
// Clear read buffer
|
||||
// m_buffer.clear();
|
||||
|
||||
// Clear response
|
||||
m_res = {};
|
||||
|
||||
// Prepare response
|
||||
m_req.body() = s;
|
||||
m_req.prepare_payload();
|
||||
|
||||
// Send the HTTP request to the remote host
|
||||
http::write(m_stream, m_req);
|
||||
|
||||
// Receive the HTTP response
|
||||
http::read(m_stream, m_buffer, m_res);
|
||||
|
||||
// Gracefully close the socket
|
||||
beast::error_code ec;
|
||||
m_stream.socket().shutdown(tcp::socket::shutdown_both, ec);
|
||||
|
||||
// not_connected happens sometimes, so don't bother reporting it.
|
||||
if (ec && ec != beast::errc::not_connected)
|
||||
throw beast::system_error{ ec };
|
||||
|
||||
// If we get here then the connection is closed gracefully
|
||||
return beast::buffers_to_string(m_res.body().data());
|
||||
}
|
||||
|
||||
HttpClient::~HttpClient() {}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
#ifndef WEBCLIENT_H
|
||||
#define WEBCLIENT_H
|
||||
#pragma once
|
||||
#include <boost/asio/connect.hpp>
|
||||
#include <boost/asio/ip/tcp.hpp>
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/http.hpp>
|
||||
#include <boost/beast/websocket.hpp>
|
||||
|
||||
namespace beast = boost::beast; // from <boost/beast.hpp>
|
||||
namespace http = beast::http; // from <boost/beast/http.hpp>
|
||||
namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
|
||||
namespace net = boost::asio; // from <boost/asio.hpp>
|
||||
using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
|
||||
|
||||
namespace ANSCENTER {
|
||||
class WebSocketClient {
|
||||
public:
|
||||
WebSocketClient(std::string host, std::string port, std::string url);
|
||||
~WebSocketClient();
|
||||
std::string Send(std::string s);
|
||||
|
||||
private:
|
||||
net::io_context m_ioc; // The io_context is required for all I/O
|
||||
websocket::stream<tcp::socket> m_ws{ m_ioc }; // perform our I/O
|
||||
beast::flat_buffer m_buffer;
|
||||
};
|
||||
|
||||
class HttpClient {
|
||||
public:
|
||||
HttpClient(std::string host, std::string port, std::string url);
|
||||
~HttpClient();
|
||||
std::string Send(std::string s);
|
||||
|
||||
private:
|
||||
net::io_context m_ioc; // The io_context is required for all I/O
|
||||
beast::tcp_stream m_stream{ m_ioc }; // perform our I/O
|
||||
beast::flat_buffer m_buffer;
|
||||
http::request<http::string_body> m_req;
|
||||
http::response<http::dynamic_body> m_res;
|
||||
tcp::resolver::results_type m_results;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,246 @@
|
||||
#include "decode.h"
|
||||
#include "stdio.h"
|
||||
#include "device_launch_parameters.h"
|
||||
|
||||
|
||||
namespace nvinfer1
|
||||
{
|
||||
DecodePlugin::DecodePlugin()
|
||||
{
|
||||
}
|
||||
|
||||
DecodePlugin::~DecodePlugin()
|
||||
{
|
||||
}
|
||||
|
||||
// create the plugin at runtime from a byte stream
|
||||
DecodePlugin::DecodePlugin(const void* data, size_t length)
|
||||
{
|
||||
}
|
||||
|
||||
void DecodePlugin::serialize(void* buffer) const TRT_NOEXCEPT
|
||||
{
|
||||
}
|
||||
|
||||
size_t DecodePlugin::getSerializationSize() const TRT_NOEXCEPT
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
int DecodePlugin::initialize() TRT_NOEXCEPT
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
Dims DecodePlugin::getOutputDimensions(int index, const Dims* inputs, int nbInputDims) TRT_NOEXCEPT
|
||||
{
|
||||
//output the result to channel
|
||||
int totalCount = 0;
|
||||
totalCount += decodeplugin::INPUT_H / 8 * decodeplugin::INPUT_W / 8 * 2 * sizeof(decodeplugin::Detection) / sizeof(float);
|
||||
totalCount += decodeplugin::INPUT_H / 16 * decodeplugin::INPUT_W / 16 * 2 * sizeof(decodeplugin::Detection) / sizeof(float);
|
||||
totalCount += decodeplugin::INPUT_H / 32 * decodeplugin::INPUT_W / 32 * 2 * sizeof(decodeplugin::Detection) / sizeof(float);
|
||||
|
||||
return Dims3(totalCount + 1, 1, 1);
|
||||
}
|
||||
|
||||
// Set plugin namespace
|
||||
void DecodePlugin::setPluginNamespace(const char* pluginNamespace) TRT_NOEXCEPT
|
||||
{
|
||||
mPluginNamespace = pluginNamespace;
|
||||
}
|
||||
|
||||
const char* DecodePlugin::getPluginNamespace() const TRT_NOEXCEPT
|
||||
{
|
||||
return mPluginNamespace;
|
||||
}
|
||||
|
||||
// Return the DataType of the plugin output at the requested index
|
||||
DataType DecodePlugin::getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const TRT_NOEXCEPT
|
||||
{
|
||||
return DataType::kFLOAT;
|
||||
}
|
||||
|
||||
// Return true if output tensor is broadcast across a batch.
|
||||
bool DecodePlugin::isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const TRT_NOEXCEPT
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Return true if plugin can use input that is broadcast across batch without replication.
|
||||
bool DecodePlugin::canBroadcastInputAcrossBatch(int inputIndex) const TRT_NOEXCEPT
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void DecodePlugin::configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) TRT_NOEXCEPT
|
||||
{
|
||||
}
|
||||
|
||||
// Attach the plugin object to an execution context and grant the plugin the access to some context resource.
|
||||
void DecodePlugin::attachToContext(cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) TRT_NOEXCEPT
|
||||
{
|
||||
}
|
||||
|
||||
// Detach the plugin object from its execution context.
|
||||
void DecodePlugin::detachFromContext() TRT_NOEXCEPT {}
|
||||
|
||||
const char* DecodePlugin::getPluginType() const TRT_NOEXCEPT
|
||||
{
|
||||
return "Decode_TRT";
|
||||
}
|
||||
|
||||
const char* DecodePlugin::getPluginVersion() const TRT_NOEXCEPT
|
||||
{
|
||||
return "1";
|
||||
}
|
||||
|
||||
void DecodePlugin::destroy() TRT_NOEXCEPT
|
||||
{
|
||||
delete this;
|
||||
}
|
||||
|
||||
// Clone the plugin
|
||||
IPluginV2IOExt* DecodePlugin::clone() const TRT_NOEXCEPT
|
||||
{
|
||||
DecodePlugin* p = new DecodePlugin();
|
||||
p->setPluginNamespace(mPluginNamespace);
|
||||
return p;
|
||||
}
|
||||
|
||||
__device__ float Logist(float data) { return 1. / (1. + expf(-data)); };
|
||||
|
||||
__global__ void CalDetection(const float* input, float* output, int num_elem, int step, int anchor, int output_elem) {
|
||||
|
||||
int idx = threadIdx.x + blockDim.x * blockIdx.x;
|
||||
if (idx >= num_elem) return;
|
||||
|
||||
int h = decodeplugin::INPUT_H / step;
|
||||
int w = decodeplugin::INPUT_W / step;
|
||||
int total_grid = h * w;
|
||||
int bn_idx = idx / total_grid;
|
||||
idx = idx - bn_idx * total_grid;
|
||||
int y = idx / w;
|
||||
int x = idx % w;
|
||||
const float* cur_input = input + bn_idx * (4 + 2 + 10) * 2 * total_grid;
|
||||
const float* bbox_reg = &cur_input[0];
|
||||
const float* cls_reg = &cur_input[2 * 4 * total_grid];
|
||||
const float* lmk_reg = &cur_input[2 * 4 * total_grid + 2 * 2 * total_grid];
|
||||
|
||||
for (int k = 0; k < 2; ++k) {
|
||||
float conf1 = cls_reg[idx + k * total_grid * 2];
|
||||
float conf2 = cls_reg[idx + k * total_grid * 2 + total_grid];
|
||||
conf2 = expf(conf2) / (expf(conf1) + expf(conf2));
|
||||
if (conf2 <= 0.02) continue;
|
||||
|
||||
float* res_count = output + bn_idx * output_elem;
|
||||
int count = (int)atomicAdd(res_count, 1);
|
||||
char* data = (char*)res_count + sizeof(float) + count * sizeof(decodeplugin::Detection);
|
||||
decodeplugin::Detection* det = (decodeplugin::Detection*)(data);
|
||||
|
||||
float prior[4];
|
||||
prior[0] = ((float)x + 0.5) / w;
|
||||
prior[1] = ((float)y + 0.5) / h;
|
||||
prior[2] = (float)anchor * (k + 1) / decodeplugin::INPUT_W;
|
||||
prior[3] = (float)anchor * (k + 1) / decodeplugin::INPUT_H;
|
||||
|
||||
//Location
|
||||
det->bbox[0] = prior[0] + bbox_reg[idx + k * total_grid * 4] * 0.1 * prior[2];
|
||||
det->bbox[1] = prior[1] + bbox_reg[idx + k * total_grid * 4 + total_grid] * 0.1 * prior[3];
|
||||
det->bbox[2] = prior[2] * expf(bbox_reg[idx + k * total_grid * 4 + total_grid * 2] * 0.2);
|
||||
det->bbox[3] = prior[3] * expf(bbox_reg[idx + k * total_grid * 4 + total_grid * 3] * 0.2);
|
||||
det->bbox[0] -= det->bbox[2] / 2;
|
||||
det->bbox[1] -= det->bbox[3] / 2;
|
||||
det->bbox[2] += det->bbox[0];
|
||||
det->bbox[3] += det->bbox[1];
|
||||
det->bbox[0] *= decodeplugin::INPUT_W;
|
||||
det->bbox[1] *= decodeplugin::INPUT_H;
|
||||
det->bbox[2] *= decodeplugin::INPUT_W;
|
||||
det->bbox[3] *= decodeplugin::INPUT_H;
|
||||
det->class_confidence = conf2;
|
||||
for (int i = 0; i < 10; i += 2) {
|
||||
det->landmark[i] = prior[0] + lmk_reg[idx + k * total_grid * 10 + total_grid * i] * 0.1 * prior[2];
|
||||
det->landmark[i + 1] = prior[1] + lmk_reg[idx + k * total_grid * 10 + total_grid * (i + 1)] * 0.1 * prior[3];
|
||||
det->landmark[i] *= decodeplugin::INPUT_W;
|
||||
det->landmark[i + 1] *= decodeplugin::INPUT_H;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void DecodePlugin::forwardGpu(const float* const* inputs, float* output, cudaStream_t stream, int batchSize)
|
||||
{
|
||||
int num_elem = 0;
|
||||
int base_step = 8;
|
||||
int base_anchor = 16;
|
||||
int thread_count;
|
||||
|
||||
int totalCount = 1;
|
||||
totalCount += decodeplugin::INPUT_H / 8 * decodeplugin::INPUT_W / 8 * 2 * sizeof(decodeplugin::Detection) / sizeof(float);
|
||||
totalCount += decodeplugin::INPUT_H / 16 * decodeplugin::INPUT_W / 16 * 2 * sizeof(decodeplugin::Detection) / sizeof(float);
|
||||
totalCount += decodeplugin::INPUT_H / 32 * decodeplugin::INPUT_W / 32 * 2 * sizeof(decodeplugin::Detection) / sizeof(float);
|
||||
for (int idx = 0; idx < batchSize; ++idx) {
|
||||
cudaMemsetAsync(output + idx * totalCount, 0, sizeof(float), stream);
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < 3; ++i)
|
||||
{
|
||||
num_elem = batchSize * decodeplugin::INPUT_H / base_step * decodeplugin::INPUT_W / base_step;
|
||||
thread_count = (num_elem < thread_count_) ? num_elem : thread_count_;
|
||||
CalDetection << < (num_elem + thread_count - 1) / thread_count, thread_count, 0, stream >> >
|
||||
(inputs[i], output, num_elem, base_step, base_anchor, totalCount);
|
||||
|
||||
base_step *= 2;
|
||||
base_anchor *= 4;
|
||||
}
|
||||
}
|
||||
|
||||
int DecodePlugin::enqueue(int batchSize, const void* const* inputs, void* TRT_CONST_ENQUEUE* outputs, void* workspace, cudaStream_t stream) TRT_NOEXCEPT
|
||||
{
|
||||
//GPU
|
||||
//CUDA_CHECK(cudaStreamSynchronize(stream));
|
||||
forwardGpu((const float* const*)inputs, (float*)outputs[0], stream, batchSize);
|
||||
return 0;
|
||||
};
|
||||
|
||||
PluginFieldCollection DecodePluginCreator::mFC{};
|
||||
std::vector<PluginField> DecodePluginCreator::mPluginAttributes;
|
||||
|
||||
DecodePluginCreator::DecodePluginCreator()
|
||||
{
|
||||
mPluginAttributes.clear();
|
||||
|
||||
mFC.nbFields = mPluginAttributes.size();
|
||||
mFC.fields = mPluginAttributes.data();
|
||||
}
|
||||
|
||||
const char* DecodePluginCreator::getPluginName() const TRT_NOEXCEPT
|
||||
{
|
||||
return "Decode_TRT";
|
||||
}
|
||||
|
||||
const char* DecodePluginCreator::getPluginVersion() const TRT_NOEXCEPT
|
||||
{
|
||||
return "1";
|
||||
}
|
||||
|
||||
const PluginFieldCollection* DecodePluginCreator::getFieldNames() TRT_NOEXCEPT
|
||||
{
|
||||
return &mFC;
|
||||
}
|
||||
|
||||
IPluginV2IOExt* DecodePluginCreator::createPlugin(const char* name, const PluginFieldCollection* fc) TRT_NOEXCEPT
|
||||
{
|
||||
DecodePlugin* obj = new DecodePlugin();
|
||||
obj->setPluginNamespace(mNamespace.c_str());
|
||||
return obj;
|
||||
}
|
||||
|
||||
IPluginV2IOExt* DecodePluginCreator::deserializePlugin(const char* name, const void* serialData, size_t serialLength) TRT_NOEXCEPT
|
||||
{
|
||||
// This object will be deleted when the network is destroyed, which will
|
||||
// call PReluPlugin::destroy()
|
||||
DecodePlugin* obj = new DecodePlugin(serialData, serialLength);
|
||||
obj->setPluginNamespace(mNamespace.c_str());
|
||||
return obj;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
#ifndef _DECODE_CU_H
|
||||
#define _DECODE_CU_H
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "NvInfer.h"
|
||||
#include "macros.h"
|
||||
|
||||
namespace decodeplugin
|
||||
{
|
||||
struct alignas(float) Detection{
|
||||
float bbox[4]; //x1 y1 x2 y2
|
||||
float class_confidence;
|
||||
float landmark[10];
|
||||
};
|
||||
static const int INPUT_H = 480;
|
||||
static const int INPUT_W = 640;
|
||||
}
|
||||
|
||||
namespace nvinfer1
|
||||
{
|
||||
class DecodePlugin: public IPluginV2IOExt
|
||||
{
|
||||
public:
|
||||
DecodePlugin();
|
||||
DecodePlugin(const void* data, size_t length);
|
||||
|
||||
~DecodePlugin();
|
||||
|
||||
int getNbOutputs() const TRT_NOEXCEPT override
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
Dims getOutputDimensions(int index, const Dims* inputs, int nbInputDims) TRT_NOEXCEPT override;
|
||||
|
||||
int initialize() TRT_NOEXCEPT override;
|
||||
|
||||
virtual void terminate() TRT_NOEXCEPT override {};
|
||||
|
||||
virtual size_t getWorkspaceSize(int maxBatchSize) const TRT_NOEXCEPT override { return 0;}
|
||||
|
||||
virtual int enqueue(int batchSize, const void*const * inputs, void*TRT_CONST_ENQUEUE* outputs, void* workspace, cudaStream_t stream) TRT_NOEXCEPT override;
|
||||
|
||||
virtual size_t getSerializationSize() const TRT_NOEXCEPT override;
|
||||
|
||||
virtual void serialize(void* buffer) const TRT_NOEXCEPT override;
|
||||
|
||||
bool supportsFormatCombination(int pos, const PluginTensorDesc* inOut, int nbInputs, int nbOutputs) const TRT_NOEXCEPT override {
|
||||
return inOut[pos].format == TensorFormat::kLINEAR && inOut[pos].type == DataType::kFLOAT;
|
||||
}
|
||||
|
||||
const char* getPluginType() const TRT_NOEXCEPT override;
|
||||
|
||||
const char* getPluginVersion() const TRT_NOEXCEPT override;
|
||||
|
||||
void destroy() TRT_NOEXCEPT override;
|
||||
|
||||
IPluginV2IOExt* clone() const TRT_NOEXCEPT override;
|
||||
|
||||
void setPluginNamespace(const char* pluginNamespace) TRT_NOEXCEPT override;
|
||||
|
||||
const char* getPluginNamespace() const TRT_NOEXCEPT override;
|
||||
|
||||
DataType getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const TRT_NOEXCEPT override;
|
||||
|
||||
bool isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const TRT_NOEXCEPT override;
|
||||
|
||||
bool canBroadcastInputAcrossBatch(int inputIndex) const TRT_NOEXCEPT override;
|
||||
|
||||
void attachToContext(
|
||||
cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator) TRT_NOEXCEPT override;
|
||||
|
||||
void configurePlugin(const PluginTensorDesc* in, int nbInput, const PluginTensorDesc* out, int nbOutput) TRT_NOEXCEPT override;
|
||||
|
||||
void detachFromContext() TRT_NOEXCEPT override;
|
||||
|
||||
int input_size_;
|
||||
private:
|
||||
void forwardGpu(const float *const * inputs, float* output, cudaStream_t stream, int batchSize = 1);
|
||||
int thread_count_ = 256;
|
||||
const char* mPluginNamespace;
|
||||
};
|
||||
|
||||
class DecodePluginCreator : public IPluginCreator
|
||||
{
|
||||
public:
|
||||
DecodePluginCreator();
|
||||
|
||||
~DecodePluginCreator() override = default;
|
||||
|
||||
const char* getPluginName() const TRT_NOEXCEPT override;
|
||||
|
||||
const char* getPluginVersion() const TRT_NOEXCEPT override;
|
||||
|
||||
const PluginFieldCollection* getFieldNames() TRT_NOEXCEPT override;
|
||||
|
||||
IPluginV2IOExt* createPlugin(const char* name, const PluginFieldCollection* fc) TRT_NOEXCEPT override;
|
||||
|
||||
IPluginV2IOExt* deserializePlugin(const char* name, const void* serialData, size_t serialLength) TRT_NOEXCEPT override;
|
||||
|
||||
void setPluginNamespace(const char* libNamespace) TRT_NOEXCEPT override
|
||||
{
|
||||
mNamespace = libNamespace;
|
||||
}
|
||||
|
||||
const char* getPluginNamespace() const TRT_NOEXCEPT override
|
||||
{
|
||||
return mNamespace.c_str();
|
||||
}
|
||||
|
||||
private:
|
||||
std::string mNamespace;
|
||||
static PluginFieldCollection mFC;
|
||||
static std::vector<PluginField> mPluginAttributes;
|
||||
};
|
||||
REGISTER_TENSORRT_PLUGIN(DecodePluginCreator);
|
||||
};
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
|
||||
#define NOMINMAX // Prevent windows.h from defining min/max macros
|
||||
// which break std::min / std::max (C2589)
|
||||
// Windows Header Files
|
||||
#include <windows.h>
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef __MACROS_H
|
||||
#define __MACROS_H
|
||||
|
||||
#if NV_TENSORRT_MAJOR >= 8
|
||||
#define TRT_NOEXCEPT noexcept
|
||||
#define TRT_CONST_ENQUEUE const
|
||||
#else
|
||||
#define TRT_NOEXCEPT
|
||||
#define TRT_CONST_ENQUEUE
|
||||
#endif
|
||||
|
||||
#endif // __MACROS_H
|
||||
@@ -0,0 +1,5 @@
|
||||
// pch.cpp: source file corresponding to the pre-compiled header
|
||||
|
||||
#include "pch.h"
|
||||
|
||||
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
|
||||
@@ -0,0 +1,18 @@
|
||||
// pch.h: This is a precompiled header file.
|
||||
// Files listed below are compiled only once, improving build performance for future builds.
|
||||
// This also affects IntelliSense performance, including code completion and many code browsing features.
|
||||
// However, files listed here are ALL re-compiled if any one of them is updated between builds.
|
||||
// Do not add files here that you will be updating frequently as this negates the performance advantage.
|
||||
|
||||
#ifndef PCH_H
|
||||
#define PCH_H
|
||||
|
||||
// add headers that you want to pre-compile here
|
||||
#include "framework.h"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
#endif //PCH_H
|
||||
+28831
File diff suppressed because it is too large
Load Diff
+252611
File diff suppressed because it is too large
Load Diff
+13257
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,719 @@
|
||||
/*
|
||||
** 2006 June 7
|
||||
**
|
||||
** The author disclaims copyright to this source code. In place of
|
||||
** a legal notice, here is a blessing:
|
||||
**
|
||||
** May you do good and not evil.
|
||||
** May you find forgiveness for yourself and forgive others.
|
||||
** May you share freely, never taking more than you give.
|
||||
**
|
||||
*************************************************************************
|
||||
** This header file defines the SQLite interface for use by
|
||||
** shared libraries that want to be imported as extensions into
|
||||
** an SQLite instance. Shared libraries that intend to be loaded
|
||||
** as extensions by SQLite should #include this file instead of
|
||||
** sqlite3.h.
|
||||
*/
|
||||
#ifndef SQLITE3EXT_H
|
||||
#define SQLITE3EXT_H
|
||||
#include "sqlite3.h"
|
||||
|
||||
/*
|
||||
** The following structure holds pointers to all of the SQLite API
|
||||
** routines.
|
||||
**
|
||||
** WARNING: In order to maintain backwards compatibility, add new
|
||||
** interfaces to the end of this structure only. If you insert new
|
||||
** interfaces in the middle of this structure, then older different
|
||||
** versions of SQLite will not be able to load each other's shared
|
||||
** libraries!
|
||||
*/
|
||||
struct sqlite3_api_routines {
|
||||
void * (*aggregate_context)(sqlite3_context*,int nBytes);
|
||||
int (*aggregate_count)(sqlite3_context*);
|
||||
int (*bind_blob)(sqlite3_stmt*,int,const void*,int n,void(*)(void*));
|
||||
int (*bind_double)(sqlite3_stmt*,int,double);
|
||||
int (*bind_int)(sqlite3_stmt*,int,int);
|
||||
int (*bind_int64)(sqlite3_stmt*,int,sqlite_int64);
|
||||
int (*bind_null)(sqlite3_stmt*,int);
|
||||
int (*bind_parameter_count)(sqlite3_stmt*);
|
||||
int (*bind_parameter_index)(sqlite3_stmt*,const char*zName);
|
||||
const char * (*bind_parameter_name)(sqlite3_stmt*,int);
|
||||
int (*bind_text)(sqlite3_stmt*,int,const char*,int n,void(*)(void*));
|
||||
int (*bind_text16)(sqlite3_stmt*,int,const void*,int,void(*)(void*));
|
||||
int (*bind_value)(sqlite3_stmt*,int,const sqlite3_value*);
|
||||
int (*busy_handler)(sqlite3*,int(*)(void*,int),void*);
|
||||
int (*busy_timeout)(sqlite3*,int ms);
|
||||
int (*changes)(sqlite3*);
|
||||
int (*close)(sqlite3*);
|
||||
int (*collation_needed)(sqlite3*,void*,void(*)(void*,sqlite3*,
|
||||
int eTextRep,const char*));
|
||||
int (*collation_needed16)(sqlite3*,void*,void(*)(void*,sqlite3*,
|
||||
int eTextRep,const void*));
|
||||
const void * (*column_blob)(sqlite3_stmt*,int iCol);
|
||||
int (*column_bytes)(sqlite3_stmt*,int iCol);
|
||||
int (*column_bytes16)(sqlite3_stmt*,int iCol);
|
||||
int (*column_count)(sqlite3_stmt*pStmt);
|
||||
const char * (*column_database_name)(sqlite3_stmt*,int);
|
||||
const void * (*column_database_name16)(sqlite3_stmt*,int);
|
||||
const char * (*column_decltype)(sqlite3_stmt*,int i);
|
||||
const void * (*column_decltype16)(sqlite3_stmt*,int);
|
||||
double (*column_double)(sqlite3_stmt*,int iCol);
|
||||
int (*column_int)(sqlite3_stmt*,int iCol);
|
||||
sqlite_int64 (*column_int64)(sqlite3_stmt*,int iCol);
|
||||
const char * (*column_name)(sqlite3_stmt*,int);
|
||||
const void * (*column_name16)(sqlite3_stmt*,int);
|
||||
const char * (*column_origin_name)(sqlite3_stmt*,int);
|
||||
const void * (*column_origin_name16)(sqlite3_stmt*,int);
|
||||
const char * (*column_table_name)(sqlite3_stmt*,int);
|
||||
const void * (*column_table_name16)(sqlite3_stmt*,int);
|
||||
const unsigned char * (*column_text)(sqlite3_stmt*,int iCol);
|
||||
const void * (*column_text16)(sqlite3_stmt*,int iCol);
|
||||
int (*column_type)(sqlite3_stmt*,int iCol);
|
||||
sqlite3_value* (*column_value)(sqlite3_stmt*,int iCol);
|
||||
void * (*commit_hook)(sqlite3*,int(*)(void*),void*);
|
||||
int (*complete)(const char*sql);
|
||||
int (*complete16)(const void*sql);
|
||||
int (*create_collation)(sqlite3*,const char*,int,void*,
|
||||
int(*)(void*,int,const void*,int,const void*));
|
||||
int (*create_collation16)(sqlite3*,const void*,int,void*,
|
||||
int(*)(void*,int,const void*,int,const void*));
|
||||
int (*create_function)(sqlite3*,const char*,int,int,void*,
|
||||
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
|
||||
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
|
||||
void (*xFinal)(sqlite3_context*));
|
||||
int (*create_function16)(sqlite3*,const void*,int,int,void*,
|
||||
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
|
||||
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
|
||||
void (*xFinal)(sqlite3_context*));
|
||||
int (*create_module)(sqlite3*,const char*,const sqlite3_module*,void*);
|
||||
int (*data_count)(sqlite3_stmt*pStmt);
|
||||
sqlite3 * (*db_handle)(sqlite3_stmt*);
|
||||
int (*declare_vtab)(sqlite3*,const char*);
|
||||
int (*enable_shared_cache)(int);
|
||||
int (*errcode)(sqlite3*db);
|
||||
const char * (*errmsg)(sqlite3*);
|
||||
const void * (*errmsg16)(sqlite3*);
|
||||
int (*exec)(sqlite3*,const char*,sqlite3_callback,void*,char**);
|
||||
int (*expired)(sqlite3_stmt*);
|
||||
int (*finalize)(sqlite3_stmt*pStmt);
|
||||
void (*free)(void*);
|
||||
void (*free_table)(char**result);
|
||||
int (*get_autocommit)(sqlite3*);
|
||||
void * (*get_auxdata)(sqlite3_context*,int);
|
||||
int (*get_table)(sqlite3*,const char*,char***,int*,int*,char**);
|
||||
int (*global_recover)(void);
|
||||
void (*interruptx)(sqlite3*);
|
||||
sqlite_int64 (*last_insert_rowid)(sqlite3*);
|
||||
const char * (*libversion)(void);
|
||||
int (*libversion_number)(void);
|
||||
void *(*malloc)(int);
|
||||
char * (*mprintf)(const char*,...);
|
||||
int (*open)(const char*,sqlite3**);
|
||||
int (*open16)(const void*,sqlite3**);
|
||||
int (*prepare)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
|
||||
int (*prepare16)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
|
||||
void * (*profile)(sqlite3*,void(*)(void*,const char*,sqlite_uint64),void*);
|
||||
void (*progress_handler)(sqlite3*,int,int(*)(void*),void*);
|
||||
void *(*realloc)(void*,int);
|
||||
int (*reset)(sqlite3_stmt*pStmt);
|
||||
void (*result_blob)(sqlite3_context*,const void*,int,void(*)(void*));
|
||||
void (*result_double)(sqlite3_context*,double);
|
||||
void (*result_error)(sqlite3_context*,const char*,int);
|
||||
void (*result_error16)(sqlite3_context*,const void*,int);
|
||||
void (*result_int)(sqlite3_context*,int);
|
||||
void (*result_int64)(sqlite3_context*,sqlite_int64);
|
||||
void (*result_null)(sqlite3_context*);
|
||||
void (*result_text)(sqlite3_context*,const char*,int,void(*)(void*));
|
||||
void (*result_text16)(sqlite3_context*,const void*,int,void(*)(void*));
|
||||
void (*result_text16be)(sqlite3_context*,const void*,int,void(*)(void*));
|
||||
void (*result_text16le)(sqlite3_context*,const void*,int,void(*)(void*));
|
||||
void (*result_value)(sqlite3_context*,sqlite3_value*);
|
||||
void * (*rollback_hook)(sqlite3*,void(*)(void*),void*);
|
||||
int (*set_authorizer)(sqlite3*,int(*)(void*,int,const char*,const char*,
|
||||
const char*,const char*),void*);
|
||||
void (*set_auxdata)(sqlite3_context*,int,void*,void (*)(void*));
|
||||
char * (*xsnprintf)(int,char*,const char*,...);
|
||||
int (*step)(sqlite3_stmt*);
|
||||
int (*table_column_metadata)(sqlite3*,const char*,const char*,const char*,
|
||||
char const**,char const**,int*,int*,int*);
|
||||
void (*thread_cleanup)(void);
|
||||
int (*total_changes)(sqlite3*);
|
||||
void * (*trace)(sqlite3*,void(*xTrace)(void*,const char*),void*);
|
||||
int (*transfer_bindings)(sqlite3_stmt*,sqlite3_stmt*);
|
||||
void * (*update_hook)(sqlite3*,void(*)(void*,int ,char const*,char const*,
|
||||
sqlite_int64),void*);
|
||||
void * (*user_data)(sqlite3_context*);
|
||||
const void * (*value_blob)(sqlite3_value*);
|
||||
int (*value_bytes)(sqlite3_value*);
|
||||
int (*value_bytes16)(sqlite3_value*);
|
||||
double (*value_double)(sqlite3_value*);
|
||||
int (*value_int)(sqlite3_value*);
|
||||
sqlite_int64 (*value_int64)(sqlite3_value*);
|
||||
int (*value_numeric_type)(sqlite3_value*);
|
||||
const unsigned char * (*value_text)(sqlite3_value*);
|
||||
const void * (*value_text16)(sqlite3_value*);
|
||||
const void * (*value_text16be)(sqlite3_value*);
|
||||
const void * (*value_text16le)(sqlite3_value*);
|
||||
int (*value_type)(sqlite3_value*);
|
||||
char *(*vmprintf)(const char*,va_list);
|
||||
/* Added ??? */
|
||||
int (*overload_function)(sqlite3*, const char *zFuncName, int nArg);
|
||||
/* Added by 3.3.13 */
|
||||
int (*prepare_v2)(sqlite3*,const char*,int,sqlite3_stmt**,const char**);
|
||||
int (*prepare16_v2)(sqlite3*,const void*,int,sqlite3_stmt**,const void**);
|
||||
int (*clear_bindings)(sqlite3_stmt*);
|
||||
/* Added by 3.4.1 */
|
||||
int (*create_module_v2)(sqlite3*,const char*,const sqlite3_module*,void*,
|
||||
void (*xDestroy)(void *));
|
||||
/* Added by 3.5.0 */
|
||||
int (*bind_zeroblob)(sqlite3_stmt*,int,int);
|
||||
int (*blob_bytes)(sqlite3_blob*);
|
||||
int (*blob_close)(sqlite3_blob*);
|
||||
int (*blob_open)(sqlite3*,const char*,const char*,const char*,sqlite3_int64,
|
||||
int,sqlite3_blob**);
|
||||
int (*blob_read)(sqlite3_blob*,void*,int,int);
|
||||
int (*blob_write)(sqlite3_blob*,const void*,int,int);
|
||||
int (*create_collation_v2)(sqlite3*,const char*,int,void*,
|
||||
int(*)(void*,int,const void*,int,const void*),
|
||||
void(*)(void*));
|
||||
int (*file_control)(sqlite3*,const char*,int,void*);
|
||||
sqlite3_int64 (*memory_highwater)(int);
|
||||
sqlite3_int64 (*memory_used)(void);
|
||||
sqlite3_mutex *(*mutex_alloc)(int);
|
||||
void (*mutex_enter)(sqlite3_mutex*);
|
||||
void (*mutex_free)(sqlite3_mutex*);
|
||||
void (*mutex_leave)(sqlite3_mutex*);
|
||||
int (*mutex_try)(sqlite3_mutex*);
|
||||
int (*open_v2)(const char*,sqlite3**,int,const char*);
|
||||
int (*release_memory)(int);
|
||||
void (*result_error_nomem)(sqlite3_context*);
|
||||
void (*result_error_toobig)(sqlite3_context*);
|
||||
int (*sleep)(int);
|
||||
void (*soft_heap_limit)(int);
|
||||
sqlite3_vfs *(*vfs_find)(const char*);
|
||||
int (*vfs_register)(sqlite3_vfs*,int);
|
||||
int (*vfs_unregister)(sqlite3_vfs*);
|
||||
int (*xthreadsafe)(void);
|
||||
void (*result_zeroblob)(sqlite3_context*,int);
|
||||
void (*result_error_code)(sqlite3_context*,int);
|
||||
int (*test_control)(int, ...);
|
||||
void (*randomness)(int,void*);
|
||||
sqlite3 *(*context_db_handle)(sqlite3_context*);
|
||||
int (*extended_result_codes)(sqlite3*,int);
|
||||
int (*limit)(sqlite3*,int,int);
|
||||
sqlite3_stmt *(*next_stmt)(sqlite3*,sqlite3_stmt*);
|
||||
const char *(*sql)(sqlite3_stmt*);
|
||||
int (*status)(int,int*,int*,int);
|
||||
int (*backup_finish)(sqlite3_backup*);
|
||||
sqlite3_backup *(*backup_init)(sqlite3*,const char*,sqlite3*,const char*);
|
||||
int (*backup_pagecount)(sqlite3_backup*);
|
||||
int (*backup_remaining)(sqlite3_backup*);
|
||||
int (*backup_step)(sqlite3_backup*,int);
|
||||
const char *(*compileoption_get)(int);
|
||||
int (*compileoption_used)(const char*);
|
||||
int (*create_function_v2)(sqlite3*,const char*,int,int,void*,
|
||||
void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
|
||||
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
|
||||
void (*xFinal)(sqlite3_context*),
|
||||
void(*xDestroy)(void*));
|
||||
int (*db_config)(sqlite3*,int,...);
|
||||
sqlite3_mutex *(*db_mutex)(sqlite3*);
|
||||
int (*db_status)(sqlite3*,int,int*,int*,int);
|
||||
int (*extended_errcode)(sqlite3*);
|
||||
void (*log)(int,const char*,...);
|
||||
sqlite3_int64 (*soft_heap_limit64)(sqlite3_int64);
|
||||
const char *(*sourceid)(void);
|
||||
int (*stmt_status)(sqlite3_stmt*,int,int);
|
||||
int (*strnicmp)(const char*,const char*,int);
|
||||
int (*unlock_notify)(sqlite3*,void(*)(void**,int),void*);
|
||||
int (*wal_autocheckpoint)(sqlite3*,int);
|
||||
int (*wal_checkpoint)(sqlite3*,const char*);
|
||||
void *(*wal_hook)(sqlite3*,int(*)(void*,sqlite3*,const char*,int),void*);
|
||||
int (*blob_reopen)(sqlite3_blob*,sqlite3_int64);
|
||||
int (*vtab_config)(sqlite3*,int op,...);
|
||||
int (*vtab_on_conflict)(sqlite3*);
|
||||
/* Version 3.7.16 and later */
|
||||
int (*close_v2)(sqlite3*);
|
||||
const char *(*db_filename)(sqlite3*,const char*);
|
||||
int (*db_readonly)(sqlite3*,const char*);
|
||||
int (*db_release_memory)(sqlite3*);
|
||||
const char *(*errstr)(int);
|
||||
int (*stmt_busy)(sqlite3_stmt*);
|
||||
int (*stmt_readonly)(sqlite3_stmt*);
|
||||
int (*stricmp)(const char*,const char*);
|
||||
int (*uri_boolean)(const char*,const char*,int);
|
||||
sqlite3_int64 (*uri_int64)(const char*,const char*,sqlite3_int64);
|
||||
const char *(*uri_parameter)(const char*,const char*);
|
||||
char *(*xvsnprintf)(int,char*,const char*,va_list);
|
||||
int (*wal_checkpoint_v2)(sqlite3*,const char*,int,int*,int*);
|
||||
/* Version 3.8.7 and later */
|
||||
int (*auto_extension)(void(*)(void));
|
||||
int (*bind_blob64)(sqlite3_stmt*,int,const void*,sqlite3_uint64,
|
||||
void(*)(void*));
|
||||
int (*bind_text64)(sqlite3_stmt*,int,const char*,sqlite3_uint64,
|
||||
void(*)(void*),unsigned char);
|
||||
int (*cancel_auto_extension)(void(*)(void));
|
||||
int (*load_extension)(sqlite3*,const char*,const char*,char**);
|
||||
void *(*malloc64)(sqlite3_uint64);
|
||||
sqlite3_uint64 (*msize)(void*);
|
||||
void *(*realloc64)(void*,sqlite3_uint64);
|
||||
void (*reset_auto_extension)(void);
|
||||
void (*result_blob64)(sqlite3_context*,const void*,sqlite3_uint64,
|
||||
void(*)(void*));
|
||||
void (*result_text64)(sqlite3_context*,const char*,sqlite3_uint64,
|
||||
void(*)(void*), unsigned char);
|
||||
int (*strglob)(const char*,const char*);
|
||||
/* Version 3.8.11 and later */
|
||||
sqlite3_value *(*value_dup)(const sqlite3_value*);
|
||||
void (*value_free)(sqlite3_value*);
|
||||
int (*result_zeroblob64)(sqlite3_context*,sqlite3_uint64);
|
||||
int (*bind_zeroblob64)(sqlite3_stmt*, int, sqlite3_uint64);
|
||||
/* Version 3.9.0 and later */
|
||||
unsigned int (*value_subtype)(sqlite3_value*);
|
||||
void (*result_subtype)(sqlite3_context*,unsigned int);
|
||||
/* Version 3.10.0 and later */
|
||||
int (*status64)(int,sqlite3_int64*,sqlite3_int64*,int);
|
||||
int (*strlike)(const char*,const char*,unsigned int);
|
||||
int (*db_cacheflush)(sqlite3*);
|
||||
/* Version 3.12.0 and later */
|
||||
int (*system_errno)(sqlite3*);
|
||||
/* Version 3.14.0 and later */
|
||||
int (*trace_v2)(sqlite3*,unsigned,int(*)(unsigned,void*,void*,void*),void*);
|
||||
char *(*expanded_sql)(sqlite3_stmt*);
|
||||
/* Version 3.18.0 and later */
|
||||
void (*set_last_insert_rowid)(sqlite3*,sqlite3_int64);
|
||||
/* Version 3.20.0 and later */
|
||||
int (*prepare_v3)(sqlite3*,const char*,int,unsigned int,
|
||||
sqlite3_stmt**,const char**);
|
||||
int (*prepare16_v3)(sqlite3*,const void*,int,unsigned int,
|
||||
sqlite3_stmt**,const void**);
|
||||
int (*bind_pointer)(sqlite3_stmt*,int,void*,const char*,void(*)(void*));
|
||||
void (*result_pointer)(sqlite3_context*,void*,const char*,void(*)(void*));
|
||||
void *(*value_pointer)(sqlite3_value*,const char*);
|
||||
int (*vtab_nochange)(sqlite3_context*);
|
||||
int (*value_nochange)(sqlite3_value*);
|
||||
const char *(*vtab_collation)(sqlite3_index_info*,int);
|
||||
/* Version 3.24.0 and later */
|
||||
int (*keyword_count)(void);
|
||||
int (*keyword_name)(int,const char**,int*);
|
||||
int (*keyword_check)(const char*,int);
|
||||
sqlite3_str *(*str_new)(sqlite3*);
|
||||
char *(*str_finish)(sqlite3_str*);
|
||||
void (*str_appendf)(sqlite3_str*, const char *zFormat, ...);
|
||||
void (*str_vappendf)(sqlite3_str*, const char *zFormat, va_list);
|
||||
void (*str_append)(sqlite3_str*, const char *zIn, int N);
|
||||
void (*str_appendall)(sqlite3_str*, const char *zIn);
|
||||
void (*str_appendchar)(sqlite3_str*, int N, char C);
|
||||
void (*str_reset)(sqlite3_str*);
|
||||
int (*str_errcode)(sqlite3_str*);
|
||||
int (*str_length)(sqlite3_str*);
|
||||
char *(*str_value)(sqlite3_str*);
|
||||
/* Version 3.25.0 and later */
|
||||
int (*create_window_function)(sqlite3*,const char*,int,int,void*,
|
||||
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
|
||||
void (*xFinal)(sqlite3_context*),
|
||||
void (*xValue)(sqlite3_context*),
|
||||
void (*xInv)(sqlite3_context*,int,sqlite3_value**),
|
||||
void(*xDestroy)(void*));
|
||||
/* Version 3.26.0 and later */
|
||||
const char *(*normalized_sql)(sqlite3_stmt*);
|
||||
/* Version 3.28.0 and later */
|
||||
int (*stmt_isexplain)(sqlite3_stmt*);
|
||||
int (*value_frombind)(sqlite3_value*);
|
||||
/* Version 3.30.0 and later */
|
||||
int (*drop_modules)(sqlite3*,const char**);
|
||||
/* Version 3.31.0 and later */
|
||||
sqlite3_int64 (*hard_heap_limit64)(sqlite3_int64);
|
||||
const char *(*uri_key)(const char*,int);
|
||||
const char *(*filename_database)(const char*);
|
||||
const char *(*filename_journal)(const char*);
|
||||
const char *(*filename_wal)(const char*);
|
||||
/* Version 3.32.0 and later */
|
||||
const char *(*create_filename)(const char*,const char*,const char*,
|
||||
int,const char**);
|
||||
void (*free_filename)(const char*);
|
||||
sqlite3_file *(*database_file_object)(const char*);
|
||||
/* Version 3.34.0 and later */
|
||||
int (*txn_state)(sqlite3*,const char*);
|
||||
/* Version 3.36.1 and later */
|
||||
sqlite3_int64 (*changes64)(sqlite3*);
|
||||
sqlite3_int64 (*total_changes64)(sqlite3*);
|
||||
/* Version 3.37.0 and later */
|
||||
int (*autovacuum_pages)(sqlite3*,
|
||||
unsigned int(*)(void*,const char*,unsigned int,unsigned int,unsigned int),
|
||||
void*, void(*)(void*));
|
||||
/* Version 3.38.0 and later */
|
||||
int (*error_offset)(sqlite3*);
|
||||
int (*vtab_rhs_value)(sqlite3_index_info*,int,sqlite3_value**);
|
||||
int (*vtab_distinct)(sqlite3_index_info*);
|
||||
int (*vtab_in)(sqlite3_index_info*,int,int);
|
||||
int (*vtab_in_first)(sqlite3_value*,sqlite3_value**);
|
||||
int (*vtab_in_next)(sqlite3_value*,sqlite3_value**);
|
||||
/* Version 3.39.0 and later */
|
||||
int (*deserialize)(sqlite3*,const char*,unsigned char*,
|
||||
sqlite3_int64,sqlite3_int64,unsigned);
|
||||
unsigned char *(*serialize)(sqlite3*,const char *,sqlite3_int64*,
|
||||
unsigned int);
|
||||
const char *(*db_name)(sqlite3*,int);
|
||||
/* Version 3.40.0 and later */
|
||||
int (*value_encoding)(sqlite3_value*);
|
||||
/* Version 3.41.0 and later */
|
||||
int (*is_interrupted)(sqlite3*);
|
||||
/* Version 3.43.0 and later */
|
||||
int (*stmt_explain)(sqlite3_stmt*,int);
|
||||
/* Version 3.44.0 and later */
|
||||
void *(*get_clientdata)(sqlite3*,const char*);
|
||||
int (*set_clientdata)(sqlite3*, const char*, void*, void(*)(void*));
|
||||
};
|
||||
|
||||
/*
|
||||
** This is the function signature used for all extension entry points. It
|
||||
** is also defined in the file "loadext.c".
|
||||
*/
|
||||
typedef int (*sqlite3_loadext_entry)(
|
||||
sqlite3 *db, /* Handle to the database. */
|
||||
char **pzErrMsg, /* Used to set error string on failure. */
|
||||
const sqlite3_api_routines *pThunk /* Extension API function pointers. */
|
||||
);
|
||||
|
||||
/*
|
||||
** The following macros redefine the API routines so that they are
|
||||
** redirected through the global sqlite3_api structure.
|
||||
**
|
||||
** This header file is also used by the loadext.c source file
|
||||
** (part of the main SQLite library - not an extension) so that
|
||||
** it can get access to the sqlite3_api_routines structure
|
||||
** definition. But the main library does not want to redefine
|
||||
** the API. So the redefinition macros are only valid if the
|
||||
** SQLITE_CORE macros is undefined.
|
||||
*/
|
||||
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
|
||||
#define sqlite3_aggregate_context sqlite3_api->aggregate_context
|
||||
#ifndef SQLITE_OMIT_DEPRECATED
|
||||
#define sqlite3_aggregate_count sqlite3_api->aggregate_count
|
||||
#endif
|
||||
#define sqlite3_bind_blob sqlite3_api->bind_blob
|
||||
#define sqlite3_bind_double sqlite3_api->bind_double
|
||||
#define sqlite3_bind_int sqlite3_api->bind_int
|
||||
#define sqlite3_bind_int64 sqlite3_api->bind_int64
|
||||
#define sqlite3_bind_null sqlite3_api->bind_null
|
||||
#define sqlite3_bind_parameter_count sqlite3_api->bind_parameter_count
|
||||
#define sqlite3_bind_parameter_index sqlite3_api->bind_parameter_index
|
||||
#define sqlite3_bind_parameter_name sqlite3_api->bind_parameter_name
|
||||
#define sqlite3_bind_text sqlite3_api->bind_text
|
||||
#define sqlite3_bind_text16 sqlite3_api->bind_text16
|
||||
#define sqlite3_bind_value sqlite3_api->bind_value
|
||||
#define sqlite3_busy_handler sqlite3_api->busy_handler
|
||||
#define sqlite3_busy_timeout sqlite3_api->busy_timeout
|
||||
#define sqlite3_changes sqlite3_api->changes
|
||||
#define sqlite3_close sqlite3_api->close
|
||||
#define sqlite3_collation_needed sqlite3_api->collation_needed
|
||||
#define sqlite3_collation_needed16 sqlite3_api->collation_needed16
|
||||
#define sqlite3_column_blob sqlite3_api->column_blob
|
||||
#define sqlite3_column_bytes sqlite3_api->column_bytes
|
||||
#define sqlite3_column_bytes16 sqlite3_api->column_bytes16
|
||||
#define sqlite3_column_count sqlite3_api->column_count
|
||||
#define sqlite3_column_database_name sqlite3_api->column_database_name
|
||||
#define sqlite3_column_database_name16 sqlite3_api->column_database_name16
|
||||
#define sqlite3_column_decltype sqlite3_api->column_decltype
|
||||
#define sqlite3_column_decltype16 sqlite3_api->column_decltype16
|
||||
#define sqlite3_column_double sqlite3_api->column_double
|
||||
#define sqlite3_column_int sqlite3_api->column_int
|
||||
#define sqlite3_column_int64 sqlite3_api->column_int64
|
||||
#define sqlite3_column_name sqlite3_api->column_name
|
||||
#define sqlite3_column_name16 sqlite3_api->column_name16
|
||||
#define sqlite3_column_origin_name sqlite3_api->column_origin_name
|
||||
#define sqlite3_column_origin_name16 sqlite3_api->column_origin_name16
|
||||
#define sqlite3_column_table_name sqlite3_api->column_table_name
|
||||
#define sqlite3_column_table_name16 sqlite3_api->column_table_name16
|
||||
#define sqlite3_column_text sqlite3_api->column_text
|
||||
#define sqlite3_column_text16 sqlite3_api->column_text16
|
||||
#define sqlite3_column_type sqlite3_api->column_type
|
||||
#define sqlite3_column_value sqlite3_api->column_value
|
||||
#define sqlite3_commit_hook sqlite3_api->commit_hook
|
||||
#define sqlite3_complete sqlite3_api->complete
|
||||
#define sqlite3_complete16 sqlite3_api->complete16
|
||||
#define sqlite3_create_collation sqlite3_api->create_collation
|
||||
#define sqlite3_create_collation16 sqlite3_api->create_collation16
|
||||
#define sqlite3_create_function sqlite3_api->create_function
|
||||
#define sqlite3_create_function16 sqlite3_api->create_function16
|
||||
#define sqlite3_create_module sqlite3_api->create_module
|
||||
#define sqlite3_create_module_v2 sqlite3_api->create_module_v2
|
||||
#define sqlite3_data_count sqlite3_api->data_count
|
||||
#define sqlite3_db_handle sqlite3_api->db_handle
|
||||
#define sqlite3_declare_vtab sqlite3_api->declare_vtab
|
||||
#define sqlite3_enable_shared_cache sqlite3_api->enable_shared_cache
|
||||
#define sqlite3_errcode sqlite3_api->errcode
|
||||
#define sqlite3_errmsg sqlite3_api->errmsg
|
||||
#define sqlite3_errmsg16 sqlite3_api->errmsg16
|
||||
#define sqlite3_exec sqlite3_api->exec
|
||||
#ifndef SQLITE_OMIT_DEPRECATED
|
||||
#define sqlite3_expired sqlite3_api->expired
|
||||
#endif
|
||||
#define sqlite3_finalize sqlite3_api->finalize
|
||||
#define sqlite3_free sqlite3_api->free
|
||||
#define sqlite3_free_table sqlite3_api->free_table
|
||||
#define sqlite3_get_autocommit sqlite3_api->get_autocommit
|
||||
#define sqlite3_get_auxdata sqlite3_api->get_auxdata
|
||||
#define sqlite3_get_table sqlite3_api->get_table
|
||||
#ifndef SQLITE_OMIT_DEPRECATED
|
||||
#define sqlite3_global_recover sqlite3_api->global_recover
|
||||
#endif
|
||||
#define sqlite3_interrupt sqlite3_api->interruptx
|
||||
#define sqlite3_last_insert_rowid sqlite3_api->last_insert_rowid
|
||||
#define sqlite3_libversion sqlite3_api->libversion
|
||||
#define sqlite3_libversion_number sqlite3_api->libversion_number
|
||||
#define sqlite3_malloc sqlite3_api->malloc
|
||||
#define sqlite3_mprintf sqlite3_api->mprintf
|
||||
#define sqlite3_open sqlite3_api->open
|
||||
#define sqlite3_open16 sqlite3_api->open16
|
||||
#define sqlite3_prepare sqlite3_api->prepare
|
||||
#define sqlite3_prepare16 sqlite3_api->prepare16
|
||||
#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
|
||||
#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
|
||||
#define sqlite3_profile sqlite3_api->profile
|
||||
#define sqlite3_progress_handler sqlite3_api->progress_handler
|
||||
#define sqlite3_realloc sqlite3_api->realloc
|
||||
#define sqlite3_reset sqlite3_api->reset
|
||||
#define sqlite3_result_blob sqlite3_api->result_blob
|
||||
#define sqlite3_result_double sqlite3_api->result_double
|
||||
#define sqlite3_result_error sqlite3_api->result_error
|
||||
#define sqlite3_result_error16 sqlite3_api->result_error16
|
||||
#define sqlite3_result_int sqlite3_api->result_int
|
||||
#define sqlite3_result_int64 sqlite3_api->result_int64
|
||||
#define sqlite3_result_null sqlite3_api->result_null
|
||||
#define sqlite3_result_text sqlite3_api->result_text
|
||||
#define sqlite3_result_text16 sqlite3_api->result_text16
|
||||
#define sqlite3_result_text16be sqlite3_api->result_text16be
|
||||
#define sqlite3_result_text16le sqlite3_api->result_text16le
|
||||
#define sqlite3_result_value sqlite3_api->result_value
|
||||
#define sqlite3_rollback_hook sqlite3_api->rollback_hook
|
||||
#define sqlite3_set_authorizer sqlite3_api->set_authorizer
|
||||
#define sqlite3_set_auxdata sqlite3_api->set_auxdata
|
||||
#define sqlite3_snprintf sqlite3_api->xsnprintf
|
||||
#define sqlite3_step sqlite3_api->step
|
||||
#define sqlite3_table_column_metadata sqlite3_api->table_column_metadata
|
||||
#define sqlite3_thread_cleanup sqlite3_api->thread_cleanup
|
||||
#define sqlite3_total_changes sqlite3_api->total_changes
|
||||
#define sqlite3_trace sqlite3_api->trace
|
||||
#ifndef SQLITE_OMIT_DEPRECATED
|
||||
#define sqlite3_transfer_bindings sqlite3_api->transfer_bindings
|
||||
#endif
|
||||
#define sqlite3_update_hook sqlite3_api->update_hook
|
||||
#define sqlite3_user_data sqlite3_api->user_data
|
||||
#define sqlite3_value_blob sqlite3_api->value_blob
|
||||
#define sqlite3_value_bytes sqlite3_api->value_bytes
|
||||
#define sqlite3_value_bytes16 sqlite3_api->value_bytes16
|
||||
#define sqlite3_value_double sqlite3_api->value_double
|
||||
#define sqlite3_value_int sqlite3_api->value_int
|
||||
#define sqlite3_value_int64 sqlite3_api->value_int64
|
||||
#define sqlite3_value_numeric_type sqlite3_api->value_numeric_type
|
||||
#define sqlite3_value_text sqlite3_api->value_text
|
||||
#define sqlite3_value_text16 sqlite3_api->value_text16
|
||||
#define sqlite3_value_text16be sqlite3_api->value_text16be
|
||||
#define sqlite3_value_text16le sqlite3_api->value_text16le
|
||||
#define sqlite3_value_type sqlite3_api->value_type
|
||||
#define sqlite3_vmprintf sqlite3_api->vmprintf
|
||||
#define sqlite3_vsnprintf sqlite3_api->xvsnprintf
|
||||
#define sqlite3_overload_function sqlite3_api->overload_function
|
||||
#define sqlite3_prepare_v2 sqlite3_api->prepare_v2
|
||||
#define sqlite3_prepare16_v2 sqlite3_api->prepare16_v2
|
||||
#define sqlite3_clear_bindings sqlite3_api->clear_bindings
|
||||
#define sqlite3_bind_zeroblob sqlite3_api->bind_zeroblob
|
||||
#define sqlite3_blob_bytes sqlite3_api->blob_bytes
|
||||
#define sqlite3_blob_close sqlite3_api->blob_close
|
||||
#define sqlite3_blob_open sqlite3_api->blob_open
|
||||
#define sqlite3_blob_read sqlite3_api->blob_read
|
||||
#define sqlite3_blob_write sqlite3_api->blob_write
|
||||
#define sqlite3_create_collation_v2 sqlite3_api->create_collation_v2
|
||||
#define sqlite3_file_control sqlite3_api->file_control
|
||||
#define sqlite3_memory_highwater sqlite3_api->memory_highwater
|
||||
#define sqlite3_memory_used sqlite3_api->memory_used
|
||||
#define sqlite3_mutex_alloc sqlite3_api->mutex_alloc
|
||||
#define sqlite3_mutex_enter sqlite3_api->mutex_enter
|
||||
#define sqlite3_mutex_free sqlite3_api->mutex_free
|
||||
#define sqlite3_mutex_leave sqlite3_api->mutex_leave
|
||||
#define sqlite3_mutex_try sqlite3_api->mutex_try
|
||||
#define sqlite3_open_v2 sqlite3_api->open_v2
|
||||
#define sqlite3_release_memory sqlite3_api->release_memory
|
||||
#define sqlite3_result_error_nomem sqlite3_api->result_error_nomem
|
||||
#define sqlite3_result_error_toobig sqlite3_api->result_error_toobig
|
||||
#define sqlite3_sleep sqlite3_api->sleep
|
||||
#define sqlite3_soft_heap_limit sqlite3_api->soft_heap_limit
|
||||
#define sqlite3_vfs_find sqlite3_api->vfs_find
|
||||
#define sqlite3_vfs_register sqlite3_api->vfs_register
|
||||
#define sqlite3_vfs_unregister sqlite3_api->vfs_unregister
|
||||
#define sqlite3_threadsafe sqlite3_api->xthreadsafe
|
||||
#define sqlite3_result_zeroblob sqlite3_api->result_zeroblob
|
||||
#define sqlite3_result_error_code sqlite3_api->result_error_code
|
||||
#define sqlite3_test_control sqlite3_api->test_control
|
||||
#define sqlite3_randomness sqlite3_api->randomness
|
||||
#define sqlite3_context_db_handle sqlite3_api->context_db_handle
|
||||
#define sqlite3_extended_result_codes sqlite3_api->extended_result_codes
|
||||
#define sqlite3_limit sqlite3_api->limit
|
||||
#define sqlite3_next_stmt sqlite3_api->next_stmt
|
||||
#define sqlite3_sql sqlite3_api->sql
|
||||
#define sqlite3_status sqlite3_api->status
|
||||
#define sqlite3_backup_finish sqlite3_api->backup_finish
|
||||
#define sqlite3_backup_init sqlite3_api->backup_init
|
||||
#define sqlite3_backup_pagecount sqlite3_api->backup_pagecount
|
||||
#define sqlite3_backup_remaining sqlite3_api->backup_remaining
|
||||
#define sqlite3_backup_step sqlite3_api->backup_step
|
||||
#define sqlite3_compileoption_get sqlite3_api->compileoption_get
|
||||
#define sqlite3_compileoption_used sqlite3_api->compileoption_used
|
||||
#define sqlite3_create_function_v2 sqlite3_api->create_function_v2
|
||||
#define sqlite3_db_config sqlite3_api->db_config
|
||||
#define sqlite3_db_mutex sqlite3_api->db_mutex
|
||||
#define sqlite3_db_status sqlite3_api->db_status
|
||||
#define sqlite3_extended_errcode sqlite3_api->extended_errcode
|
||||
#define sqlite3_log sqlite3_api->log
|
||||
#define sqlite3_soft_heap_limit64 sqlite3_api->soft_heap_limit64
|
||||
#define sqlite3_sourceid sqlite3_api->sourceid
|
||||
#define sqlite3_stmt_status sqlite3_api->stmt_status
|
||||
#define sqlite3_strnicmp sqlite3_api->strnicmp
|
||||
#define sqlite3_unlock_notify sqlite3_api->unlock_notify
|
||||
#define sqlite3_wal_autocheckpoint sqlite3_api->wal_autocheckpoint
|
||||
#define sqlite3_wal_checkpoint sqlite3_api->wal_checkpoint
|
||||
#define sqlite3_wal_hook sqlite3_api->wal_hook
|
||||
#define sqlite3_blob_reopen sqlite3_api->blob_reopen
|
||||
#define sqlite3_vtab_config sqlite3_api->vtab_config
|
||||
#define sqlite3_vtab_on_conflict sqlite3_api->vtab_on_conflict
|
||||
/* Version 3.7.16 and later */
|
||||
#define sqlite3_close_v2 sqlite3_api->close_v2
|
||||
#define sqlite3_db_filename sqlite3_api->db_filename
|
||||
#define sqlite3_db_readonly sqlite3_api->db_readonly
|
||||
#define sqlite3_db_release_memory sqlite3_api->db_release_memory
|
||||
#define sqlite3_errstr sqlite3_api->errstr
|
||||
#define sqlite3_stmt_busy sqlite3_api->stmt_busy
|
||||
#define sqlite3_stmt_readonly sqlite3_api->stmt_readonly
|
||||
#define sqlite3_stricmp sqlite3_api->stricmp
|
||||
#define sqlite3_uri_boolean sqlite3_api->uri_boolean
|
||||
#define sqlite3_uri_int64 sqlite3_api->uri_int64
|
||||
#define sqlite3_uri_parameter sqlite3_api->uri_parameter
|
||||
#define sqlite3_uri_vsnprintf sqlite3_api->xvsnprintf
|
||||
#define sqlite3_wal_checkpoint_v2 sqlite3_api->wal_checkpoint_v2
|
||||
/* Version 3.8.7 and later */
|
||||
#define sqlite3_auto_extension sqlite3_api->auto_extension
|
||||
#define sqlite3_bind_blob64 sqlite3_api->bind_blob64
|
||||
#define sqlite3_bind_text64 sqlite3_api->bind_text64
|
||||
#define sqlite3_cancel_auto_extension sqlite3_api->cancel_auto_extension
|
||||
#define sqlite3_load_extension sqlite3_api->load_extension
|
||||
#define sqlite3_malloc64 sqlite3_api->malloc64
|
||||
#define sqlite3_msize sqlite3_api->msize
|
||||
#define sqlite3_realloc64 sqlite3_api->realloc64
|
||||
#define sqlite3_reset_auto_extension sqlite3_api->reset_auto_extension
|
||||
#define sqlite3_result_blob64 sqlite3_api->result_blob64
|
||||
#define sqlite3_result_text64 sqlite3_api->result_text64
|
||||
#define sqlite3_strglob sqlite3_api->strglob
|
||||
/* Version 3.8.11 and later */
|
||||
#define sqlite3_value_dup sqlite3_api->value_dup
|
||||
#define sqlite3_value_free sqlite3_api->value_free
|
||||
#define sqlite3_result_zeroblob64 sqlite3_api->result_zeroblob64
|
||||
#define sqlite3_bind_zeroblob64 sqlite3_api->bind_zeroblob64
|
||||
/* Version 3.9.0 and later */
|
||||
#define sqlite3_value_subtype sqlite3_api->value_subtype
|
||||
#define sqlite3_result_subtype sqlite3_api->result_subtype
|
||||
/* Version 3.10.0 and later */
|
||||
#define sqlite3_status64 sqlite3_api->status64
|
||||
#define sqlite3_strlike sqlite3_api->strlike
|
||||
#define sqlite3_db_cacheflush sqlite3_api->db_cacheflush
|
||||
/* Version 3.12.0 and later */
|
||||
#define sqlite3_system_errno sqlite3_api->system_errno
|
||||
/* Version 3.14.0 and later */
|
||||
#define sqlite3_trace_v2 sqlite3_api->trace_v2
|
||||
#define sqlite3_expanded_sql sqlite3_api->expanded_sql
|
||||
/* Version 3.18.0 and later */
|
||||
#define sqlite3_set_last_insert_rowid sqlite3_api->set_last_insert_rowid
|
||||
/* Version 3.20.0 and later */
|
||||
#define sqlite3_prepare_v3 sqlite3_api->prepare_v3
|
||||
#define sqlite3_prepare16_v3 sqlite3_api->prepare16_v3
|
||||
#define sqlite3_bind_pointer sqlite3_api->bind_pointer
|
||||
#define sqlite3_result_pointer sqlite3_api->result_pointer
|
||||
#define sqlite3_value_pointer sqlite3_api->value_pointer
|
||||
/* Version 3.22.0 and later */
|
||||
#define sqlite3_vtab_nochange sqlite3_api->vtab_nochange
|
||||
#define sqlite3_value_nochange sqlite3_api->value_nochange
|
||||
#define sqlite3_vtab_collation sqlite3_api->vtab_collation
|
||||
/* Version 3.24.0 and later */
|
||||
#define sqlite3_keyword_count sqlite3_api->keyword_count
|
||||
#define sqlite3_keyword_name sqlite3_api->keyword_name
|
||||
#define sqlite3_keyword_check sqlite3_api->keyword_check
|
||||
#define sqlite3_str_new sqlite3_api->str_new
|
||||
#define sqlite3_str_finish sqlite3_api->str_finish
|
||||
#define sqlite3_str_appendf sqlite3_api->str_appendf
|
||||
#define sqlite3_str_vappendf sqlite3_api->str_vappendf
|
||||
#define sqlite3_str_append sqlite3_api->str_append
|
||||
#define sqlite3_str_appendall sqlite3_api->str_appendall
|
||||
#define sqlite3_str_appendchar sqlite3_api->str_appendchar
|
||||
#define sqlite3_str_reset sqlite3_api->str_reset
|
||||
#define sqlite3_str_errcode sqlite3_api->str_errcode
|
||||
#define sqlite3_str_length sqlite3_api->str_length
|
||||
#define sqlite3_str_value sqlite3_api->str_value
|
||||
/* Version 3.25.0 and later */
|
||||
#define sqlite3_create_window_function sqlite3_api->create_window_function
|
||||
/* Version 3.26.0 and later */
|
||||
#define sqlite3_normalized_sql sqlite3_api->normalized_sql
|
||||
/* Version 3.28.0 and later */
|
||||
#define sqlite3_stmt_isexplain sqlite3_api->stmt_isexplain
|
||||
#define sqlite3_value_frombind sqlite3_api->value_frombind
|
||||
/* Version 3.30.0 and later */
|
||||
#define sqlite3_drop_modules sqlite3_api->drop_modules
|
||||
/* Version 3.31.0 and later */
|
||||
#define sqlite3_hard_heap_limit64 sqlite3_api->hard_heap_limit64
|
||||
#define sqlite3_uri_key sqlite3_api->uri_key
|
||||
#define sqlite3_filename_database sqlite3_api->filename_database
|
||||
#define sqlite3_filename_journal sqlite3_api->filename_journal
|
||||
#define sqlite3_filename_wal sqlite3_api->filename_wal
|
||||
/* Version 3.32.0 and later */
|
||||
#define sqlite3_create_filename sqlite3_api->create_filename
|
||||
#define sqlite3_free_filename sqlite3_api->free_filename
|
||||
#define sqlite3_database_file_object sqlite3_api->database_file_object
|
||||
/* Version 3.34.0 and later */
|
||||
#define sqlite3_txn_state sqlite3_api->txn_state
|
||||
/* Version 3.36.1 and later */
|
||||
#define sqlite3_changes64 sqlite3_api->changes64
|
||||
#define sqlite3_total_changes64 sqlite3_api->total_changes64
|
||||
/* Version 3.37.0 and later */
|
||||
#define sqlite3_autovacuum_pages sqlite3_api->autovacuum_pages
|
||||
/* Version 3.38.0 and later */
|
||||
#define sqlite3_error_offset sqlite3_api->error_offset
|
||||
#define sqlite3_vtab_rhs_value sqlite3_api->vtab_rhs_value
|
||||
#define sqlite3_vtab_distinct sqlite3_api->vtab_distinct
|
||||
#define sqlite3_vtab_in sqlite3_api->vtab_in
|
||||
#define sqlite3_vtab_in_first sqlite3_api->vtab_in_first
|
||||
#define sqlite3_vtab_in_next sqlite3_api->vtab_in_next
|
||||
/* Version 3.39.0 and later */
|
||||
#ifndef SQLITE_OMIT_DESERIALIZE
|
||||
#define sqlite3_deserialize sqlite3_api->deserialize
|
||||
#define sqlite3_serialize sqlite3_api->serialize
|
||||
#endif
|
||||
#define sqlite3_db_name sqlite3_api->db_name
|
||||
/* Version 3.40.0 and later */
|
||||
#define sqlite3_value_encoding sqlite3_api->value_encoding
|
||||
/* Version 3.41.0 and later */
|
||||
#define sqlite3_is_interrupted sqlite3_api->is_interrupted
|
||||
/* Version 3.43.0 and later */
|
||||
#define sqlite3_stmt_explain sqlite3_api->stmt_explain
|
||||
/* Version 3.44.0 and later */
|
||||
#define sqlite3_get_clientdata sqlite3_api->get_clientdata
|
||||
#define sqlite3_set_clientdata sqlite3_api->set_clientdata
|
||||
#endif /* !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION) */
|
||||
|
||||
#if !defined(SQLITE_CORE) && !defined(SQLITE_OMIT_LOAD_EXTENSION)
|
||||
/* This case when the file really is being compiled as a loadable
|
||||
** extension */
|
||||
# define SQLITE_EXTENSION_INIT1 const sqlite3_api_routines *sqlite3_api=0;
|
||||
# define SQLITE_EXTENSION_INIT2(v) sqlite3_api=v;
|
||||
# define SQLITE_EXTENSION_INIT3 \
|
||||
extern const sqlite3_api_routines *sqlite3_api;
|
||||
#else
|
||||
/* This case when the file is being statically linked into the
|
||||
** application */
|
||||
# define SQLITE_EXTENSION_INIT1 /*no-op*/
|
||||
# define SQLITE_EXTENSION_INIT2(v) (void)v; /* unused parameter */
|
||||
# define SQLITE_EXTENSION_INIT3 /*no-op*/
|
||||
#endif
|
||||
|
||||
#endif /* SQLITE3EXT_H */
|
||||
@@ -0,0 +1,666 @@
|
||||
#include "ANSLLM.h"
|
||||
#include <CkGlobal.h>
|
||||
#include <CkBinData.h>
|
||||
#include <CkByteData.h>
|
||||
#include <CkStringTable.h>
|
||||
#include <CkStringBuilder.h>
|
||||
#include <CkJsonObject.h>
|
||||
|
||||
static bool ansllmLicenceValid = false;
|
||||
static std::once_flag ansllmLicenseOnceFlag;
|
||||
|
||||
namespace ANSCENTER
|
||||
{
|
||||
static void VerifyGlobalANSLMLicense(const std::string& licenseKey) {
|
||||
try {
|
||||
static const std::vector<std::pair<int, std::string>> licenseChecks = {
|
||||
{1000, "ANNHUB-LV"},
|
||||
{1001, "DLHUB-LV"},
|
||||
{1002, "ODHUB-LV"},
|
||||
{1003, "ANSVIS"},
|
||||
{1004, "ANSFR"},
|
||||
{1005, "ANSOCR"},
|
||||
{1006, "ANSALPR"},
|
||||
{1007, "ANSCV"},
|
||||
{1008, "ANSSRT"}
|
||||
};
|
||||
ansllmLicenceValid = false;
|
||||
for (const auto& [productId, productName] : licenseChecks) {
|
||||
if (ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, productId, productName)) {
|
||||
ansllmLicenceValid = true;
|
||||
break; // Stop at the first valid license
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
ansllmLicenceValid = false;
|
||||
}
|
||||
}
|
||||
ANSLLM::ANSLLM() {
|
||||
_unlockCode = "ANSDRC.CB1122026_MEQCIFwO1IFQCG0BhZwsXFO68QUU6mDB5uge4duOsqOJanEyAiAB67ahqnXin4SRy0vIegISgbFlpldmbuS5gbU21GYVqA==";// "ANSDRC.CB1082025_Ax6P3M7F8B3d";//
|
||||
}
|
||||
ANSLLM::~ANSLLM() noexcept = default;
|
||||
|
||||
void ANSLLM::CheckLicense() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
// Check once globally
|
||||
std::call_once(ansllmLicenseOnceFlag, [this]() {
|
||||
VerifyGlobalANSLMLicense(_licenseKey);
|
||||
});
|
||||
// Update this instance's local license flag
|
||||
_isLicenseValid = ansllmLicenceValid;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogFatal("ANSLLM::CheckLicense. Error:", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
void ANSLLM::CheckUnlockCode() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
CkGlobal glob;
|
||||
_unlockCode = "ANSDRC.CB1122026_MEQCIFwO1IFQCG0BhZwsXFO68QUU6mDB5uge4duOsqOJanEyAiAB67ahqnXin4SRy0vIegISgbFlpldmbuS5gbU21GYVqA==";// "ANSDRC.CB1082025_Ax6P3M7F8B3d";
|
||||
_isUnlockCodeValid = glob.UnlockBundle(_unlockCode.c_str());
|
||||
|
||||
if (!_isUnlockCodeValid) {
|
||||
_logger.LogFatal("ANSLLM::CheckUnlockCode", glob.lastErrorText(), __FILE__, __LINE__);
|
||||
return;
|
||||
}
|
||||
|
||||
int status = glob.get_UnlockStatus();
|
||||
if (status != 2) {
|
||||
_logger.LogDebug("ANSLLM::CheckUnlockCode", "Unlocked in trial mode.", __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::CheckUnlockCode", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
|
||||
bool ANSLLM::Initialize(const std::string& licenseKey, bool localLLM) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
_licenseKey = licenseKey;
|
||||
_localLLM = localLLM;
|
||||
CheckLicense();
|
||||
CheckUnlockCode();
|
||||
|
||||
if (_localLLM) {
|
||||
// Auto-configure for local Ollama: use custom provider with ChatCompletions API spec
|
||||
// Must use "custom" — setting "openai" resets BaseUrl to api.openai.com
|
||||
ai.put_Provider("custom");
|
||||
ai.put_ApiSpec("ChatCompletions");
|
||||
ai.put_BaseUrl(_ollamaBaseUrl.c_str());
|
||||
ai.put_ApiKey("ollama"); // Ollama doesn't require a real API key
|
||||
_logger.LogDebug("ANSLLM::Initialize", "Configured for local Ollama at " + _ollamaBaseUrl, __FILE__, __LINE__);
|
||||
}
|
||||
|
||||
return _isLicenseValid && _isUnlockCodeValid;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::Initialize", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool ANSLLM::SetLLMProvider(const std::string& llmProvider) {
|
||||
if (llmProvider.empty()) return false;
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
ai.put_Provider(llmProvider.c_str());
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::SetLLMProvider", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
std::string ANSLLM::GetLLMProvider() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
CkString str;
|
||||
ai.get_Provider(str);
|
||||
const char* cstr = str.getString();
|
||||
return (cstr != nullptr) ? std::string(cstr) : std::string();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::GetLLMProvider", e.what(), __FILE__, __LINE__);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
bool ANSLLM::SetApiKey(const std::string& apiKey) {
|
||||
if (apiKey.empty()) return false;
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
ai.put_ApiKey(apiKey.c_str());
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::SetApiKey", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::vector<std::string> ANSLLM::GetModelList() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
std::vector<std::string> modelList;
|
||||
|
||||
try {
|
||||
CkStringTable st;
|
||||
|
||||
if (!ai.GetModels(st)) {
|
||||
const char* errText = ai.lastErrorText();
|
||||
const std::string errorMsg = (errText != nullptr) ? errText : "Unknown error";
|
||||
_logger.LogError("ANSLLM::GetModelList",
|
||||
"Failed to get models: " + errorMsg,
|
||||
__FILE__, __LINE__);
|
||||
return modelList;
|
||||
}
|
||||
|
||||
const int count = st.get_Count();
|
||||
|
||||
if (count <= 0) {
|
||||
return modelList;
|
||||
}
|
||||
|
||||
modelList.reserve(static_cast<size_t>(count));
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
const char* modelName = st.stringAt(i);
|
||||
if (modelName != nullptr && modelName[0] != '\0') {
|
||||
modelList.emplace_back(modelName);
|
||||
}
|
||||
}
|
||||
|
||||
return modelList;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::GetModelList",
|
||||
std::string("Exception: ") + e.what(),
|
||||
__FILE__, __LINE__);
|
||||
return modelList;
|
||||
}
|
||||
catch (...) {
|
||||
_logger.LogFatal("ANSLLM::GetModelList",
|
||||
"Unknown exception occurred",
|
||||
__FILE__, __LINE__);
|
||||
return modelList;
|
||||
}
|
||||
}
|
||||
|
||||
bool ANSLLM::SetModel(const std::string& modelName) {
|
||||
if (modelName.empty()) return false;
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
ai.put_Model(modelName.c_str());
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::SetModel", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool ANSLLM::SetTranscriptMode(bool transcriptMode) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
ai.put_FullTranscript(transcriptMode);
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::SetTranscriptMode", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ANSLLM::InputAddText(const std::string& inputText) {
|
||||
if (inputText.empty()) return false;
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
ai.InputAddText(inputText.c_str());
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::InputAddText", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::string ANSLLM::Ask(const std::string& prompt) {
|
||||
if (prompt.empty()) return "";
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
// Ask the AI for text output.
|
||||
bool success = ai.Ask(prompt.c_str());
|
||||
if (success == false) {
|
||||
const char* errText = ai.lastErrorText();
|
||||
return (errText != nullptr) ? std::string(errText) : std::string("Unknown error");
|
||||
}
|
||||
|
||||
// Get the text response.
|
||||
CkStringBuilder sbResponse;
|
||||
ai.GetOutputTextSb(sbResponse);
|
||||
const char* cstr = sbResponse.getAsString();
|
||||
return (cstr != nullptr) ? std::string(cstr) : std::string();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::Ask", e.what(), __FILE__, __LINE__);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
bool ANSLLM::CreateConversation(const std::string& systemMessage, const std::string& developerMessage, const std::string& conversationName) {
|
||||
|
||||
if (systemMessage.empty() || developerMessage.empty() || conversationName.empty()) {
|
||||
return false;
|
||||
}
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
return ai.NewConvo(conversationName.c_str(), systemMessage.c_str(), developerMessage.c_str());
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::CreateConversation", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ANSLLM::SetConversation(const std::string& conversationName) {
|
||||
|
||||
if (conversationName.empty()) return false;
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
|
||||
ai.put_SelectedConvo(conversationName.c_str());
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::SetConversation", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ANSLLM::InputAddImageUrl(const std::string& imageURL, const std::string& summary) {
|
||||
if (imageURL.empty()) return false;
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
return ai.InputAddImageUrl(imageURL.c_str(), summary.c_str());
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::InputAddImageUrl", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ANSLLM::InputAddImageFromPath(const std::string& filePath, const char* summary) {
|
||||
|
||||
if (filePath.empty()) return false;
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
CkBinData bd;
|
||||
bool success = bd.LoadFile(filePath.c_str());
|
||||
if (!success) {
|
||||
const char* errText = bd.lastErrorText();
|
||||
_logger.LogError("ANSLLM::InputAddImageFromPath",
|
||||
(errText != nullptr) ? errText : "Failed to load file",
|
||||
__FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
return ai.InputAddImageData(bd, summary);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::InputAddImageFromPath", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool ANSLLM::InputAddImageFromBase64(const std::string& jpegBase64String, const char* summary) {
|
||||
if (jpegBase64String.empty()) return false;
|
||||
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
CkBinData bd;
|
||||
|
||||
// Load base64 encoded JPEG data into CkBinData
|
||||
bool success = bd.AppendEncoded(jpegBase64String.c_str(), "base64");
|
||||
if (!success) {
|
||||
_logger.LogError("ANSLLM::InputAddImage",
|
||||
bd.lastErrorText(),
|
||||
__FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
return ai.InputAddImageData(bd, summary);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::InputAddImage", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ANSLLM::InputAddFileData(const std::string& filePath, const char* summary) {
|
||||
if (filePath.empty()) return false;
|
||||
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
CkBinData bd;
|
||||
bool success = bd.LoadFile(filePath.c_str());
|
||||
if (!success) {
|
||||
const char* errText = bd.lastErrorText();
|
||||
_logger.LogError("ANSLLM::InputAddFileData",
|
||||
(errText != nullptr) ? errText : "Failed to load file",
|
||||
__FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
return ai.InputAddImageData(bd, summary);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::InputAddFileData", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool ANSLLM::InputAddFileURL(const std::string& fileURL, const char* summary) {
|
||||
if (fileURL.empty()) return false;
|
||||
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
|
||||
return ai.InputAddFileUrl(fileURL.c_str(), summary);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::InputAddFileURL", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Extra methods
|
||||
bool ANSLLM::ClearInputs() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
ai.InputClear();
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::ClearInputs", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
std::vector<std::string> ANSLLM::GetConversationList() {
|
||||
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
std::vector<std::string> convList;
|
||||
try {
|
||||
CkStringTable st;
|
||||
|
||||
if (!ai.ListConvos(st)) {
|
||||
const char* errText = ai.lastErrorText();
|
||||
const std::string errorMsg = (errText != nullptr) ? errText : "Unknown error";
|
||||
_logger.LogError("ANSLLM::GetConversationList",
|
||||
"Failed to get conversations: " + errorMsg,
|
||||
__FILE__, __LINE__);
|
||||
return convList;
|
||||
}
|
||||
|
||||
const int count = st.get_Count();
|
||||
|
||||
if (count <= 0) {
|
||||
return convList;
|
||||
}
|
||||
|
||||
convList.reserve(static_cast<size_t>(count));
|
||||
|
||||
for (int i = 0; i < count; ++i) {
|
||||
const char* convName = st.stringAt(i);
|
||||
if (convName != nullptr && convName[0] != '\0') {
|
||||
convList.emplace_back(convName);
|
||||
}
|
||||
}
|
||||
return convList;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::GetConversationList", e.what(), __FILE__, __LINE__);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
bool ANSLLM::DeleteConversation(const std::string& conversationName) {
|
||||
|
||||
if (conversationName.empty()) return false;
|
||||
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
return ai.DeleteConvo(conversationName.c_str());
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::DeleteConversation", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool ANSLLM::SetUTF8(bool b) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
ai.put_Utf8(b);
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::SetUTF8", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ANSLLM::GetUTF8() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
return ai.get_Utf8();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::GetUTF8", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int ANSLLM::PollAi(bool abort) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
return ai.PollAi(abort);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::PollAi", e.what(), __FILE__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
bool ANSLLM::SetLocalLLM(bool localLLM) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
_localLLM = localLLM;
|
||||
if (_localLLM) {
|
||||
ai.put_Provider("custom");
|
||||
ai.put_ApiSpec("ChatCompletions");
|
||||
ai.put_BaseUrl(_ollamaBaseUrl.c_str());
|
||||
ai.put_ApiKey("ollama");
|
||||
_logger.LogDebug("ANSLLM::SetLocalLLM", "Configured for local Ollama at " + _ollamaBaseUrl, __FILE__, __LINE__);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::SetLocalLLM", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool ANSLLM::SetOllamaBaseUrl(const std::string& baseUrl) {
|
||||
if (baseUrl.empty()) return false;
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
_ollamaBaseUrl = baseUrl;
|
||||
if (_localLLM) {
|
||||
ai.put_BaseUrl(_ollamaBaseUrl.c_str());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::SetOllamaBaseUrl", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool ANSLLM::SetBaseURL(const std::string& baseURL) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
ai.put_BaseUrl(baseURL.c_str());
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::SetBaseURL", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
int ANSLLM::GetHeartbeatMs() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
return ai.get_HeartbeatMs();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::GetHeartbeatMs", e.what(), __FILE__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
bool ANSLLM::SetHeartbeatMs(int heartbeatMs) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
ai.put_HeartbeatMs(heartbeatMs);
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::SetHeartbeatMs", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool ANSLLM::GetHostedConversation() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
return ai.get_HostedConvo();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::GetHostedConversation", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
bool ANSLLM::SetHostedConversation(bool hostedConversation) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
ai.put_HostedConvo(hostedConversation);
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::SetHostedConversation", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ANSLLM::GetStreamingMode() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
return ai.get_Streaming();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::GetStreamingMode", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
bool ANSLLM::SetStreamingMode(bool streamingMode) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
ai.put_Streaming(streamingMode);
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::SetStreamingMode", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int ANSLLM::GetIdleTimeoutMs() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
return ai.get_IdleTimeoutMs();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::GetIdleTimeoutMs", e.what(), __FILE__, __LINE__);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
bool ANSLLM::SetIdleTimeoutMs(int idleTimeoutMs) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
ai.put_IdleTimeoutMs(idleTimeoutMs);
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::SetIdleTimeoutMs", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::string ANSLLM::GetLastErrorMessage() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
const char* errorText = ai.lastErrorText();
|
||||
return (errorText != nullptr) ? std::string(errorText) : std::string();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::GetLastErrorMessage", e.what(), __FILE__, __LINE__);
|
||||
return e.what();
|
||||
}
|
||||
}
|
||||
std::string ANSLLM::GetOutputText() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
CkString str;
|
||||
ai.GetOutputText(str);
|
||||
const char* cstr = str.getString();
|
||||
return (cstr != nullptr) ? std::string(cstr) : std::string();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::GetOutputText", e.what(), __FILE__, __LINE__);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
std::string ANSLLM::GetOutputTextSb() {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
CkStringBuilder sb;
|
||||
ai.GetOutputTextSb(sb);
|
||||
const char* cstr = sb.getAsString();
|
||||
return (cstr != nullptr) ? std::string(cstr) : std::string();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::GetOutputTextSb", e.what(), __FILE__, __LINE__);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
bool ANSLLM::SetSleepMs(int sleepMs) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
ai.SleepMs(sleepMs);
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::SetSleepMs", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool ANSLLM::SetAskParams(double temperature) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
CkJsonObject obj;
|
||||
std::string strTemp = std::to_string(temperature);
|
||||
obj.UpdateString("temperature", strTemp.c_str());
|
||||
ai.SetAskParams(obj);
|
||||
return true;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
_logger.LogFatal("ANSLLM::SetAskParams", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,146 @@
|
||||
#ifndef ANSLLM_H
|
||||
#define ANSLLM_H
|
||||
#define ANSLLM_API __declspec(dllexport)
|
||||
#pragma once
|
||||
#include "LabVIEWHeader/extcode.h"
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include "ANSLicense.h"
|
||||
#include <CkAi.h>
|
||||
//#include <CkRest.h>
|
||||
//#include "CkAuthGoogle.h"
|
||||
//#include "CkByteData.h"
|
||||
//#include <CkAuthAws.h>
|
||||
//#include <CkXml.h>
|
||||
//#include <CkCrypt2.h>
|
||||
//#include <CkSocket.h>
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
namespace ANSCENTER {
|
||||
class ANSLLM_API ANSLLM {
|
||||
private:
|
||||
// Protects all shared mutable data
|
||||
mutable std::recursive_mutex _mutex;
|
||||
bool _isUnlockCodeValid{ false };
|
||||
bool _isLicenseValid{ false };
|
||||
bool _localLLM{ false };
|
||||
std::string _licenseKey;
|
||||
std::string _unlockCode;
|
||||
std::string _ollamaBaseUrl{ "http://localhost:11434/v1" };
|
||||
SPDLogger& _logger = SPDLogger::GetInstance("ANSLLM", false);
|
||||
CkAi ai;
|
||||
|
||||
public:
|
||||
ANSLLM();
|
||||
~ANSLLM() noexcept;
|
||||
[[nodiscard]] bool Initialize(const std::string& licenseKey, bool localLLM = false);
|
||||
void CheckLicense();
|
||||
void CheckUnlockCode();
|
||||
|
||||
bool SetLLMProvider(const std::string& llmProvider);
|
||||
[[nodiscard]] std::string GetLLMProvider();
|
||||
bool SetApiKey(const std::string& apiKey);
|
||||
[[nodiscard]] std::vector<std::string> GetModelList();
|
||||
bool SetTranscriptMode(bool transcriptMode);
|
||||
bool SetModel(const std::string& modelName);
|
||||
|
||||
bool InputAddText(const std::string& inputText);
|
||||
bool InputAddImageUrl(const std::string& imageURL, const std::string& summary);
|
||||
bool InputAddImageFromPath(const std::string& filePath, const char* summary);
|
||||
bool InputAddImageFromBase64(const std::string& base64Image, const char* summary);
|
||||
|
||||
bool InputAddFileURL(const std::string& fileURL, const char* summary);
|
||||
bool InputAddFileData(const std::string& filePath, const char* summary);
|
||||
bool ClearInputs();
|
||||
[[nodiscard]] std::vector<std::string> GetConversationList();
|
||||
|
||||
bool CreateConversation(const std::string& systemMessage, const std::string& developerMessage, const std::string& conversationName);
|
||||
bool SetConversation(const std::string& conversationName);
|
||||
bool DeleteConversation(const std::string& conversationName);
|
||||
[[nodiscard]] std::string Ask(const std::string& prompt);
|
||||
|
||||
|
||||
// Other methods and members
|
||||
bool SetUTF8(bool b);
|
||||
[[nodiscard]] bool GetUTF8();
|
||||
|
||||
// PollAi
|
||||
[[nodiscard]] int PollAi(bool abort);
|
||||
|
||||
// Local LLM methods
|
||||
bool SetLocalLLM(bool localLLM);
|
||||
bool SetOllamaBaseUrl(const std::string& baseUrl);
|
||||
bool SetBaseURL(const std::string& baseURL);
|
||||
[[nodiscard]] int GetHeartbeatMs();
|
||||
bool SetHeartbeatMs(int heartbeatMs);
|
||||
|
||||
[[nodiscard]] bool GetHostedConversation();
|
||||
bool SetHostedConversation(bool hostedConversation);
|
||||
|
||||
[[nodiscard]] bool GetStreamingMode();
|
||||
bool SetStreamingMode(bool streamingMode);
|
||||
|
||||
[[nodiscard]] int GetIdleTimeoutMs();
|
||||
bool SetIdleTimeoutMs(int idleTimeoutMs);
|
||||
|
||||
bool SetSleepMs(int sleepMs);
|
||||
[[nodiscard]] std::string GetLastErrorMessage();
|
||||
|
||||
// Parameters methods
|
||||
[[nodiscard]] std::string GetOutputText();
|
||||
[[nodiscard]] std::string GetOutputTextSb();
|
||||
|
||||
bool SetAskParams(double temperature);
|
||||
//bool SetProviderParams(std::string providerParams);
|
||||
};
|
||||
}
|
||||
|
||||
extern "C" ANSLLM_API int CreateANSLLMHandle(ANSCENTER::ANSLLM** Handle, const char* licenseKey, int localLLM = 0);
|
||||
extern "C" ANSLLM_API int ReleaseANSLLMHandle(ANSCENTER::ANSLLM** Handle);
|
||||
|
||||
extern "C" ANSLLM_API int SetANSLLMProvider(ANSCENTER::ANSLLM** Handle, const char* provider);
|
||||
extern "C" ANSLLM_API int GetANSLLMProvider(ANSCENTER::ANSLLM** Handle, LStrHandle provider);
|
||||
|
||||
extern "C" ANSLLM_API int SetApiKey(ANSCENTER::ANSLLM** Handle, const char* apiKey);
|
||||
extern "C" ANSLLM_API int ANSLLMGetModelList(ANSCENTER::ANSLLM** Handle, LStrHandle modelList);
|
||||
extern "C" ANSLLM_API int ANSLLMSetTranscriptMode(ANSCENTER::ANSLLM** Handle, int transcriptMode);
|
||||
extern "C" ANSLLM_API int ANSLLMSetModel(ANSCENTER::ANSLLM** Handle, const char* modelName);
|
||||
|
||||
extern "C" ANSLLM_API int ANSLLMInputAddText(ANSCENTER::ANSLLM** Handle, const char* inputText);
|
||||
extern "C" ANSLLM_API int ANSLLMInputAddImageUrl(ANSCENTER::ANSLLM** Handle, const char* imageURL, const char* summary);
|
||||
extern "C" ANSLLM_API int ANSLLMInputAddImageFromPath(ANSCENTER::ANSLLM** Handle, const char* filePath, const char* summary);
|
||||
extern "C" ANSLLM_API int ANSLLMInputAddImageFromBase64(ANSCENTER::ANSLLM** Handle, const char* base64Image, const char* summary);
|
||||
extern "C" ANSLLM_API int ANSLLMInputAddFileURL(ANSCENTER::ANSLLM** Handle, const char* fileURL, const char* summary);
|
||||
extern "C" ANSLLM_API int ANSLLMInputAddFileData(ANSCENTER::ANSLLM** Handle, const char* filePath, const char* summary);
|
||||
extern "C" ANSLLM_API int ANSLLMCreateConversation(ANSCENTER::ANSLLM** Handle, const char* systemMessage, const char* developerMessage, const char* conversationName);
|
||||
extern "C" ANSLLM_API int ANSLLMSetConversation(ANSCENTER::ANSLLM** Handle, const char* conversationName);
|
||||
extern "C" ANSLLM_API int ANSLLMAsk(ANSCENTER::ANSLLM** Handle, const char* prompt, LStrHandle response);
|
||||
|
||||
extern "C" ANSLLM_API int ANSLLMClearInput(ANSCENTER::ANSLLM** Handle);
|
||||
extern "C" ANSLLM_API int ANSLLMGetConversationList(ANSCENTER::ANSLLM** Handle, LStrHandle conversationList);
|
||||
extern "C" ANSLLM_API int ANSLLMDeleteConversation(ANSCENTER::ANSLLM** Handle, const char* conversationName);
|
||||
extern "C" ANSLLM_API int ANSLLMSetUTF8(ANSCENTER::ANSLLM** Handle, int utf8Mode);
|
||||
extern "C" ANSLLM_API int ANSLLMGetUTF8(ANSCENTER::ANSLLM** Handle);
|
||||
extern "C" ANSLLM_API int ANSLLMPollAi(ANSCENTER::ANSLLM** Handle, int abort);
|
||||
extern "C" ANSLLM_API int ANSLLMSetLocalLLM(ANSCENTER::ANSLLM** Handle, int localLLM);
|
||||
extern "C" ANSLLM_API int ANSLLMSetOllamaBaseUrl(ANSCENTER::ANSLLM** Handle, const char* baseUrl);
|
||||
extern "C" ANSLLM_API int ANSLLMSetBaseURL(ANSCENTER::ANSLLM** Handle, const char* baseURL);
|
||||
|
||||
extern "C" ANSLLM_API int ANSLLMSetHeartbeatMs(ANSCENTER::ANSLLM** Handle, int heartbeatMs);
|
||||
extern "C" ANSLLM_API int ANSLLMGetHeartbeatMs(ANSCENTER::ANSLLM** Handle);
|
||||
extern "C" ANSLLM_API int ANSLLMSetHostedConversation(ANSCENTER::ANSLLM** Handle, int hostedConversationMode);
|
||||
extern "C" ANSLLM_API int ANSLLMGetHostedConversation(ANSCENTER::ANSLLM** Handle);
|
||||
|
||||
extern "C" ANSLLM_API int ANSLLMGetStreamingMode(ANSCENTER::ANSLLM** Handle);
|
||||
extern "C" ANSLLM_API int ANSLLMSetStreamingMode(ANSCENTER::ANSLLM** Handle, int streamingMode);
|
||||
extern "C" ANSLLM_API int ANSLLMGetIdleTimeoutMs(ANSCENTER::ANSLLM** Handle);
|
||||
extern "C" ANSLLM_API int ANSLLMSetIdleTimeoutMs(ANSCENTER::ANSLLM** Handle, int idleTimeoutMs);
|
||||
|
||||
extern "C" ANSLLM_API int ANSLLMSetSleepMs(ANSCENTER::ANSLLM** Handle, int sleepMs);
|
||||
extern "C" ANSLLM_API int ANSLLMGetLastErrorMessage(ANSCENTER::ANSLLM** Handle, LStrHandle LastErrorMessage);
|
||||
extern "C" ANSLLM_API int ANSLLMGetOutputText(ANSCENTER::ANSLLM** Handle, LStrHandle outputText);
|
||||
extern "C" ANSLLM_API int ANSLLMGetOutputTextSb(ANSCENTER::ANSLLM** Handle, LStrHandle outputTextSb);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,30 @@
|
||||
# ANSLLM — LLM Integration DLL
|
||||
add_library(ANSLLM SHARED
|
||||
ANSLLM.cpp
|
||||
ANSLLM.h
|
||||
dllmain.cpp
|
||||
pch.cpp
|
||||
pch.h
|
||||
framework.h
|
||||
)
|
||||
|
||||
target_include_directories(ANSLLM PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
${SHARED_INCLUDE_DIR}
|
||||
)
|
||||
|
||||
target_link_libraries(ANSLLM
|
||||
PRIVATE ANSLicensingSystem
|
||||
PRIVATE anslicensing
|
||||
PRIVATE labview
|
||||
PRIVATE opencv
|
||||
PRIVATE chilkat
|
||||
PRIVATE boost
|
||||
)
|
||||
|
||||
if(WIN32)
|
||||
target_link_libraries(ANSLLM PRIVATE ${WIN_COMMON_LIBS} crypt32)
|
||||
endif()
|
||||
|
||||
target_compile_definitions(ANSLLM PRIVATE UNICODE _UNICODE ANSLLM_EXPORTS _USRDLL)
|
||||
target_precompile_headers(ANSLLM PRIVATE pch.h)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
|
||||
#define NOMINMAX // Prevent windows.h from defining min/max macros
|
||||
// which break std::min / std::max (C2589)
|
||||
// Windows Header Files
|
||||
#include <windows.h>
|
||||
@@ -0,0 +1,5 @@
|
||||
// pch.cpp: source file corresponding to the pre-compiled header
|
||||
|
||||
#include "pch.h"
|
||||
|
||||
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
|
||||
@@ -0,0 +1,18 @@
|
||||
// pch.h: This is a precompiled header file.
|
||||
// Files listed below are compiled only once, improving build performance for future builds.
|
||||
// This also affects IntelliSense performance, including code completion and many code browsing features.
|
||||
// However, files listed here are ALL re-compiled if any one of them is updated between builds.
|
||||
// Do not add files here that you will be updating frequently as this negates the performance advantage.
|
||||
|
||||
#ifndef PCH_H
|
||||
#define PCH_H
|
||||
|
||||
// add headers that you want to pre-compile here
|
||||
#include "framework.h"
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
#endif //PCH_H
|
||||
@@ -0,0 +1,843 @@
|
||||
#include "ANSLPR_OV.h"
|
||||
namespace ANSCENTER {
|
||||
|
||||
void tryPush(const std::weak_ptr<Worker>& worker, std::shared_ptr<Task>&& task) {
|
||||
try {
|
||||
std::shared_ptr<Worker>(worker)->push(task);
|
||||
} catch (const std::bad_weak_ptr&) {}
|
||||
}
|
||||
|
||||
void fillROIColor(cv::Mat& displayImage, cv::Rect roi, cv::Scalar color, double opacity) {
|
||||
if (opacity > 0) {
|
||||
roi = roi & cv::Rect(0, 0, displayImage.cols, displayImage.rows);
|
||||
cv::Mat textROI = displayImage(roi);
|
||||
cv::addWeighted(color, opacity, textROI, 1.0 - opacity , 0.0, textROI);
|
||||
}
|
||||
}
|
||||
|
||||
void putTextOnImage(cv::Mat& displayImage, std::string str, cv::Point p,
|
||||
cv::HersheyFonts font, double fontScale, cv::Scalar color,
|
||||
int thickness = 1, cv::Scalar bgcolor = cv::Scalar(),
|
||||
double opacity = 0) {
|
||||
int baseline = 0;
|
||||
cv::Size textSize = cv::getTextSize(str, font, 0.5, 1, &baseline);
|
||||
fillROIColor(displayImage, cv::Rect(cv::Point(p.x, p.y + baseline),
|
||||
cv::Point(p.x + textSize.width, p.y - textSize.height)),
|
||||
bgcolor, opacity);
|
||||
cv::putText(displayImage, str, p, font, fontScale, color, thickness);
|
||||
}
|
||||
|
||||
Detector::Detector(ov::Core& core,
|
||||
const std::string& deviceName,
|
||||
const std::string& xmlPath,
|
||||
const std::vector<float>& detectionTresholds,
|
||||
const bool autoResize) :
|
||||
m_autoResize(autoResize),
|
||||
m_detectionTresholds{ detectionTresholds }
|
||||
{
|
||||
slog::info << "Reading model: " << xmlPath << slog::endl;
|
||||
std::shared_ptr<ov::Model> model = core.read_model(xmlPath);
|
||||
logBasicModelInfo(model);
|
||||
// Check model inputs and outputs
|
||||
ov::OutputVector inputs = model->inputs();
|
||||
if (inputs.size() != 1) {
|
||||
throw std::logic_error("Detector should have only one input");
|
||||
}
|
||||
|
||||
m_detectorInputName = model->input().get_any_name();
|
||||
|
||||
ov::Layout modelLayout = ov::layout::get_layout(model->input());
|
||||
if (modelLayout.empty())
|
||||
modelLayout = { "NCHW" };
|
||||
|
||||
ov::OutputVector outputs = model->outputs();
|
||||
if (outputs.size() != 1) {
|
||||
throw std::logic_error("Vehicle Detection network should have only one output");
|
||||
}
|
||||
|
||||
ov::Output<ov::Node> output = outputs[0];
|
||||
|
||||
m_detectorOutputName = output.get_any_name();
|
||||
ov::Shape output_shape = output.get_shape();
|
||||
|
||||
if (output_shape.size() != 4) {
|
||||
throw std::logic_error("Incorrect output dimensions for SSD");
|
||||
}
|
||||
|
||||
if (maxProposalCount != output_shape[2]) {
|
||||
throw std::logic_error("unexpected ProposalCount");
|
||||
}
|
||||
if (objectSize != output_shape[3]) {
|
||||
throw std::logic_error("Output should have 7 as a last dimension");
|
||||
}
|
||||
|
||||
ov::preprocess::PrePostProcessor ppp(model);
|
||||
ov::preprocess::InputInfo& inputInfo = ppp.input();
|
||||
ov::preprocess::InputTensorInfo& inputTensorInfo = inputInfo.tensor();
|
||||
// configure desired input type and layout, the
|
||||
// use preprocessor to convert to actual model input type and layout
|
||||
inputTensorInfo.set_element_type(ov::element::u8);
|
||||
inputTensorInfo.set_layout({ "NHWC" });
|
||||
if (autoResize) {
|
||||
inputTensorInfo.set_spatial_dynamic_shape();
|
||||
}
|
||||
|
||||
ov::preprocess::InputModelInfo& inputModelInfo = inputInfo.model();
|
||||
inputModelInfo.set_layout(modelLayout);
|
||||
|
||||
ov::preprocess::PreProcessSteps& preProcessSteps = inputInfo.preprocess();
|
||||
preProcessSteps.convert_layout(modelLayout);
|
||||
preProcessSteps.convert_element_type(ov::element::f32);
|
||||
if (autoResize) {
|
||||
preProcessSteps.resize(ov::preprocess::ResizeAlgorithm::RESIZE_LINEAR);
|
||||
}
|
||||
|
||||
model = ppp.build();
|
||||
|
||||
slog::info << "Preprocessor configuration: " << slog::endl;
|
||||
slog::info << ppp << slog::endl;
|
||||
m_compiled_model = core.compile_model(model, deviceName);
|
||||
logCompiledModelInfo(m_compiled_model, xmlPath, deviceName, "Vehicle And License Plate Detection");
|
||||
}
|
||||
|
||||
ov::InferRequest Detector::createInferRequest() {
|
||||
return m_compiled_model.create_infer_request();
|
||||
}
|
||||
void Detector::setImage(ov::InferRequest& inferRequest, const cv::Mat& img) {
|
||||
ov::Tensor inputTensor = inferRequest.get_tensor(m_detectorInputName);
|
||||
ov::Shape shape = inputTensor.get_shape();
|
||||
if (m_autoResize) {
|
||||
if (!img.isSubmatrix()) {
|
||||
// just wrap Mat object with Tensor without additional memory allocation
|
||||
ov::Tensor frameTensor = wrapMat2Tensor(img);
|
||||
inferRequest.set_tensor(m_detectorInputName, frameTensor);
|
||||
}
|
||||
else {
|
||||
throw std::logic_error("Sparse matrix are not supported");
|
||||
}
|
||||
}
|
||||
else {
|
||||
// resize and copy data from image to tensor using OpenCV
|
||||
resize2tensor(img, inputTensor);
|
||||
}
|
||||
}
|
||||
std::list<Detector::Result> Detector::getResults(ov::InferRequest& inferRequest,
|
||||
cv::Size upscale,
|
||||
std::vector<std::string>& rawResults) {
|
||||
// there is no big difference if InferReq of detector from another device is passed
|
||||
// because the processing is the same for the same topology
|
||||
std::list<Result> results;
|
||||
ov::Tensor output_tensor = inferRequest.get_tensor(m_detectorOutputName);
|
||||
const float* const detections = output_tensor.data<float>();
|
||||
// pretty much regular SSD post-processing
|
||||
for (int i = 0; i < maxProposalCount; i++) {
|
||||
float image_id = detections[i * objectSize + 0]; // in case of batch
|
||||
if (image_id < 0) { // indicates end of detections
|
||||
break;
|
||||
}
|
||||
size_t label = static_cast<decltype(m_detectionTresholds.size())>(detections[i * objectSize + 1]);
|
||||
float confidence = detections[i * objectSize + 2];
|
||||
if (label - 1 < m_detectionTresholds.size() && confidence < m_detectionTresholds[label - 1]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
cv::Rect rect;
|
||||
rect.x = static_cast<int>(detections[i * objectSize + 3] * upscale.width);
|
||||
rect.y = static_cast<int>(detections[i * objectSize + 4] * upscale.height);
|
||||
rect.width = static_cast<int>(detections[i * objectSize + 5] * upscale.width) - rect.x;
|
||||
rect.height = static_cast<int>(detections[i * objectSize + 6] * upscale.height) - rect.y;
|
||||
results.push_back(Result{ label, confidence, rect });
|
||||
std::ostringstream rawResultsStream;
|
||||
rawResultsStream << "[" << i << "," << label << "] element, prob = " << confidence
|
||||
<< " (" << rect.x << "," << rect.y << ")-(" << rect.width << "," << rect.height << ")";
|
||||
rawResults.push_back(rawResultsStream.str());
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
VehicleAttributesClassifier::VehicleAttributesClassifier(ov::Core& core,
|
||||
const std::string& deviceName,
|
||||
const std::string& xmlPath,
|
||||
const bool autoResize) :m_autoResize(autoResize)
|
||||
{
|
||||
slog::info << "Reading model: " << xmlPath << slog::endl;
|
||||
std::shared_ptr<ov::Model> model = core.read_model(xmlPath);
|
||||
logBasicModelInfo(model);
|
||||
ov::OutputVector inputs = model->inputs();
|
||||
if (inputs.size() != 1) {
|
||||
throw std::logic_error("Vehicle Attribs topology should have only one input");
|
||||
}
|
||||
m_attributesInputName = model->input().get_any_name();
|
||||
ov::Layout modelLayout = ov::layout::get_layout(model->input());
|
||||
if (modelLayout.empty())
|
||||
modelLayout = { "NCHW" };
|
||||
|
||||
ov::OutputVector outputs = model->outputs();
|
||||
if (outputs.size() != 2) {
|
||||
throw std::logic_error("Vehicle Attribs Network expects networks having two outputs");
|
||||
}
|
||||
|
||||
// color is the first output
|
||||
m_outputNameForColor = outputs[0].get_any_name();
|
||||
// type is the second output.
|
||||
m_outputNameForType = outputs[1].get_any_name();
|
||||
ov::preprocess::PrePostProcessor ppp(model);
|
||||
ov::preprocess::InputInfo& inputInfo = ppp.input();
|
||||
ov::preprocess::InputTensorInfo& inputTensorInfo = inputInfo.tensor();
|
||||
// configure desired input type and layout, the
|
||||
// use preprocessor to convert to actual model input type and layout
|
||||
inputTensorInfo.set_element_type(ov::element::u8);
|
||||
inputTensorInfo.set_layout({ "NHWC" });
|
||||
if (autoResize) {
|
||||
inputTensorInfo.set_spatial_dynamic_shape();
|
||||
}
|
||||
ov::preprocess::PreProcessSteps& preProcessSteps = inputInfo.preprocess();
|
||||
preProcessSteps.convert_layout(modelLayout);
|
||||
preProcessSteps.convert_element_type(ov::element::f32);
|
||||
if (autoResize) {
|
||||
preProcessSteps.resize(ov::preprocess::ResizeAlgorithm::RESIZE_LINEAR);
|
||||
}
|
||||
|
||||
ov::preprocess::InputModelInfo& inputModelInfo = inputInfo.model();
|
||||
inputModelInfo.set_layout(modelLayout);
|
||||
|
||||
model = ppp.build();
|
||||
|
||||
slog::info << "Preprocessor configuration: " << slog::endl;
|
||||
slog::info << ppp << slog::endl;
|
||||
|
||||
m_compiled_model = core.compile_model(model, deviceName);
|
||||
logCompiledModelInfo(m_compiled_model, xmlPath, deviceName, "Vehicle Attributes Recognition");
|
||||
}
|
||||
ov::InferRequest VehicleAttributesClassifier::createInferRequest() {
|
||||
return m_compiled_model.create_infer_request();
|
||||
}
|
||||
void VehicleAttributesClassifier::setImage(ov::InferRequest& inferRequest,
|
||||
const cv::Mat& img,
|
||||
const cv::Rect vehicleRect)
|
||||
{
|
||||
ov::Tensor inputTensor = inferRequest.get_tensor(m_attributesInputName);
|
||||
ov::Shape shape = inputTensor.get_shape();
|
||||
if (m_autoResize) {
|
||||
ov::Tensor frameTensor = wrapMat2Tensor(img);
|
||||
ov::Coordinate p00({ 0, static_cast<size_t>(vehicleRect.y), static_cast<size_t>(vehicleRect.x), 0 });
|
||||
ov::Coordinate p01({ 1, static_cast<size_t>(vehicleRect.y + vehicleRect.height), static_cast<size_t>(vehicleRect.x) + vehicleRect.width, 3 });
|
||||
ov::Tensor roiTensor(frameTensor, p00, p01);
|
||||
inferRequest.set_tensor(m_attributesInputName, roiTensor);
|
||||
}
|
||||
else {
|
||||
const cv::Mat& vehicleImage = img(vehicleRect);
|
||||
resize2tensor(vehicleImage, inputTensor);
|
||||
}
|
||||
}
|
||||
std::pair<std::string, std::string> VehicleAttributesClassifier::getResults(ov::InferRequest& inferRequest) {
|
||||
static const std::string colors[] = {
|
||||
"white", "gray", "yellow", "red", "green", "blue", "black"
|
||||
};
|
||||
static const std::string types[] = {
|
||||
"car", "van", "truck", "bus"
|
||||
};
|
||||
|
||||
// 7 possible colors for each vehicle and we should select the one with the maximum probability
|
||||
ov::Tensor colorsTensor = inferRequest.get_tensor(m_outputNameForColor);
|
||||
const float* colorsValues = colorsTensor.data<float>();
|
||||
|
||||
// 4 possible types for each vehicle and we should select the one with the maximum probability
|
||||
ov::Tensor typesTensor = inferRequest.get_tensor(m_outputNameForType);
|
||||
const float* typesValues = typesTensor.data<float>();
|
||||
|
||||
const auto color_id = std::max_element(colorsValues, colorsValues + 7) - colorsValues;
|
||||
const auto type_id = std::max_element(typesValues, typesValues + 4) - typesValues;
|
||||
|
||||
return std::pair<std::string, std::string>(colors[color_id], types[type_id]);
|
||||
}
|
||||
|
||||
Lpr::Lpr(ov::Core& core,
|
||||
const std::string& deviceName,
|
||||
const std::string& xmlPath,
|
||||
const bool autoResize) :m_autoResize(autoResize)
|
||||
{
|
||||
slog::info << "Reading model: " << xmlPath << slog::endl;
|
||||
std::shared_ptr<ov::Model> model = core.read_model(xmlPath);
|
||||
logBasicModelInfo(model);
|
||||
// LPR network should have 2 inputs (and second is just a stub) and one output
|
||||
// Check inputs
|
||||
ov::OutputVector inputs = model->inputs();
|
||||
if (inputs.size() != 1 && inputs.size() != 2) {
|
||||
throw std::logic_error("LPR should have 1 or 2 inputs");
|
||||
}
|
||||
|
||||
for (auto input : inputs) {
|
||||
if (input.get_shape().size() == 4) {
|
||||
m_LprInputName = input.get_any_name();
|
||||
m_modelLayout = ov::layout::get_layout(input);
|
||||
if (m_modelLayout.empty())
|
||||
m_modelLayout = { "NCHW" };
|
||||
}
|
||||
// LPR model that converted from Caffe have second a stub input
|
||||
if (input.get_shape().size() == 2)
|
||||
m_LprInputSeqName = input.get_any_name();
|
||||
}
|
||||
|
||||
// Check outputs
|
||||
|
||||
m_maxSequenceSizePerPlate = 1;
|
||||
|
||||
ov::OutputVector outputs = model->outputs();
|
||||
if (outputs.size() != 1) {
|
||||
throw std::logic_error("LPR should have 1 output");
|
||||
}
|
||||
|
||||
m_LprOutputName = outputs[0].get_any_name();
|
||||
|
||||
for (size_t dim : outputs[0].get_shape()) {
|
||||
if (dim == 1) {
|
||||
continue;
|
||||
}
|
||||
if (m_maxSequenceSizePerPlate == 1) {
|
||||
m_maxSequenceSizePerPlate = dim;
|
||||
}
|
||||
else {
|
||||
throw std::logic_error("Every dimension of LPR output except for one must be of size 1");
|
||||
}
|
||||
}
|
||||
|
||||
ov::preprocess::PrePostProcessor ppp(model);
|
||||
ov::preprocess::InputInfo& inputInfo = ppp.input(m_LprInputName);
|
||||
ov::preprocess::InputTensorInfo& inputTensorInfo = inputInfo.tensor();
|
||||
// configure desired input type and layout, the
|
||||
// use preprocessor to convert to actual model input type and layout
|
||||
inputTensorInfo.set_element_type(ov::element::u8);
|
||||
inputTensorInfo.set_layout({ "NHWC" });
|
||||
if (autoResize) {
|
||||
inputTensorInfo.set_spatial_dynamic_shape();
|
||||
}
|
||||
|
||||
ov::preprocess::PreProcessSteps& preProcessSteps = inputInfo.preprocess();
|
||||
preProcessSteps.convert_layout(m_modelLayout);
|
||||
preProcessSteps.convert_element_type(ov::element::f32);
|
||||
if (autoResize) {
|
||||
preProcessSteps.resize(ov::preprocess::ResizeAlgorithm::RESIZE_LINEAR);
|
||||
}
|
||||
|
||||
ov::preprocess::InputModelInfo& inputModelInfo = inputInfo.model();
|
||||
inputModelInfo.set_layout(m_modelLayout);
|
||||
|
||||
model = ppp.build();
|
||||
|
||||
slog::info << "Preprocessor configuration: " << slog::endl;
|
||||
slog::info << ppp << slog::endl;
|
||||
|
||||
m_compiled_model = core.compile_model(model, deviceName);
|
||||
logCompiledModelInfo(m_compiled_model, xmlPath, deviceName, "License Plate Recognition");
|
||||
}
|
||||
ov::InferRequest Lpr::createInferRequest() {
|
||||
return m_compiled_model.create_infer_request();
|
||||
}
|
||||
void Lpr::setImage(ov::InferRequest& inferRequest, const cv::Mat& img, const cv::Rect plateRect) {
|
||||
ov::Tensor inputTensor = inferRequest.get_tensor(m_LprInputName);
|
||||
ov::Shape shape = inputTensor.get_shape();
|
||||
if ((shape.size() == 4) && m_autoResize) {
|
||||
// autoResize is set
|
||||
ov::Tensor frameTensor = wrapMat2Tensor(img);
|
||||
ov::Coordinate p00({ 0, static_cast<size_t>(plateRect.y), static_cast<size_t>(plateRect.x), 0 });
|
||||
ov::Coordinate p01({ 1, static_cast<size_t>(plateRect.y + plateRect.height), static_cast<size_t>(plateRect.x + plateRect.width), 3 });
|
||||
ov::Tensor roiTensor(frameTensor, p00, p01);
|
||||
inferRequest.set_tensor(m_LprInputName, roiTensor);
|
||||
}
|
||||
else {
|
||||
const cv::Mat& vehicleImage = img(plateRect);
|
||||
resize2tensor(vehicleImage, inputTensor);
|
||||
}
|
||||
|
||||
if (m_LprInputSeqName != "") {
|
||||
ov::Tensor inputSeqTensor = inferRequest.get_tensor(m_LprInputSeqName);
|
||||
float* data = inputSeqTensor.data<float>();
|
||||
std::fill(data, data + inputSeqTensor.get_shape()[0], 1.0f);
|
||||
}
|
||||
}
|
||||
std::string Lpr::getResults(ov::InferRequest& inferRequest) {
|
||||
static const char* const items[] = {
|
||||
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
|
||||
"", "", "", "",
|
||||
"", "", "", "",
|
||||
"", "", "", "",
|
||||
"", "", "", "",
|
||||
"", "", "", "",
|
||||
"", "", "", "",
|
||||
"", "", "", "",
|
||||
"", "", "", "",
|
||||
"", "",
|
||||
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
|
||||
"K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
|
||||
"U", "V", "W", "X", "Y", "Z"
|
||||
};
|
||||
std::string result;
|
||||
result.reserve(14u + 6u); // the longest province name + 6 plate signs
|
||||
|
||||
ov::Tensor lprOutputTensor = inferRequest.get_tensor(m_LprOutputName);
|
||||
ov::element::Type precision = lprOutputTensor.get_element_type();
|
||||
|
||||
// up to 88 items per license plate, ended with "-1"
|
||||
switch (precision) {
|
||||
case ov::element::i32:
|
||||
{
|
||||
const auto data = lprOutputTensor.data<int32_t>();
|
||||
for (int i = 0; i < m_maxSequenceSizePerPlate; i++) {
|
||||
int32_t val = data[i];
|
||||
if (val == -1) {
|
||||
break;
|
||||
}
|
||||
result += items[val];
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case ov::element::f32:
|
||||
{
|
||||
const auto data = lprOutputTensor.data<float>();
|
||||
for (int i = 0; i < m_maxSequenceSizePerPlate; i++) {
|
||||
int32_t val = int32_t(data[i]);
|
||||
if (val == -1) {
|
||||
break;
|
||||
}
|
||||
result += items[val];
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
throw std::logic_error("Not expected output blob precision");
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
// Utilities
|
||||
ReborningVideoFrame::~ReborningVideoFrame() {
|
||||
try {
|
||||
const std::shared_ptr<Worker>& worker = std::shared_ptr<Worker>(context.readersContext.readersWorker);
|
||||
context.videoFramesContext.lastFrameIdsMutexes[sourceID].lock();
|
||||
const auto frameId = ++context.videoFramesContext.lastframeIds[sourceID];
|
||||
context.videoFramesContext.lastFrameIdsMutexes[sourceID].unlock();
|
||||
std::shared_ptr<ReborningVideoFrame> reborn = std::make_shared<ReborningVideoFrame>(context, sourceID, frameId, frame);
|
||||
worker->push(std::make_shared<Reader>(reborn));
|
||||
}
|
||||
catch (const std::bad_weak_ptr&) {}
|
||||
}
|
||||
|
||||
void ResAggregator::process() {
|
||||
Context& context = static_cast<ReborningVideoFrame*>(sharedVideoFrame.get())->context;
|
||||
context.freeDetectionInfersCount += context.detectorsInfers.inferRequests.lockedSize();
|
||||
context.frameCounter++;
|
||||
context.boxesAndDescrs = boxesAndDescrs;
|
||||
try {
|
||||
std::shared_ptr<Worker>(context.resAggregatorsWorker)->stop();
|
||||
}
|
||||
catch (const std::bad_weak_ptr&) {}
|
||||
}
|
||||
|
||||
bool DetectionsProcessor::isReady() {
|
||||
Context& context = static_cast<ReborningVideoFrame*>(sharedVideoFrame.get())->context;
|
||||
if (requireGettingNumberOfDetections) {
|
||||
classifiersAggregator = std::make_shared<ClassifiersAggregator>(sharedVideoFrame);
|
||||
std::list<Detector::Result> results;
|
||||
results = context.inferTasksContext.detector.getResults(*inferRequest, sharedVideoFrame->frame.size(), classifiersAggregator->rawDetections);
|
||||
for (Detector::Result result : results) {
|
||||
switch (result.label) {
|
||||
case 1:// Vehicle
|
||||
{
|
||||
vehicleRects.emplace_back(result.location & cv::Rect{ cv::Point(0, 0), sharedVideoFrame->frame.size() });
|
||||
break;
|
||||
}
|
||||
case 2:// License Plate
|
||||
{
|
||||
// expanding a bounding box a bit, better for the license plate recognition
|
||||
result.location.x -= 5;
|
||||
result.location.y -= 5;
|
||||
result.location.width += 10;
|
||||
result.location.height += 10;
|
||||
plateRects.emplace_back(result.location & cv::Rect{ cv::Point(0, 0), sharedVideoFrame->frame.size() });
|
||||
break;
|
||||
}
|
||||
default:
|
||||
throw std::runtime_error("Unexpected detection results"); // must never happen
|
||||
break;
|
||||
}
|
||||
}
|
||||
context.detectorsInfers.inferRequests.lockedPushBack(*inferRequest);
|
||||
requireGettingNumberOfDetections = false;
|
||||
}
|
||||
|
||||
if ((vehicleRects.empty()) && (plateRects.empty())) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
InferRequestsContainer& attributesInfers = context.attributesInfers;
|
||||
attributesInfers.inferRequests.mutex.lock();
|
||||
const std::size_t numberOfAttributesInferRequestsAcquired = std::min(vehicleRects.size(), attributesInfers.inferRequests.container.size());
|
||||
reservedAttributesRequests.assign(attributesInfers.inferRequests.container.end() - numberOfAttributesInferRequestsAcquired,attributesInfers.inferRequests.container.end());
|
||||
attributesInfers.inferRequests.container.erase(attributesInfers.inferRequests.container.end() - numberOfAttributesInferRequestsAcquired,attributesInfers.inferRequests.container.end());
|
||||
attributesInfers.inferRequests.mutex.unlock();
|
||||
|
||||
InferRequestsContainer& platesInfers = context.platesInfers;
|
||||
platesInfers.inferRequests.mutex.lock();
|
||||
const std::size_t numberOfLprInferRequestsAcquired = std::min(plateRects.size(), platesInfers.inferRequests.container.size());
|
||||
reservedLprRequests.assign(platesInfers.inferRequests.container.end() - numberOfLprInferRequestsAcquired, platesInfers.inferRequests.container.end());
|
||||
platesInfers.inferRequests.container.erase(platesInfers.inferRequests.container.end() - numberOfLprInferRequestsAcquired,platesInfers.inferRequests.container.end());
|
||||
platesInfers.inferRequests.mutex.unlock();
|
||||
return numberOfAttributesInferRequestsAcquired || numberOfLprInferRequestsAcquired;
|
||||
}
|
||||
}
|
||||
|
||||
void DetectionsProcessor::process() {
|
||||
Context& context = static_cast<ReborningVideoFrame*>(sharedVideoFrame.get())->context;
|
||||
auto vehicleRectsIt = vehicleRects.begin();
|
||||
for (auto attributesRequestIt = reservedAttributesRequests.begin(); attributesRequestIt != reservedAttributesRequests.end();
|
||||
vehicleRectsIt++, attributesRequestIt++) {
|
||||
const cv::Rect vehicleRect = *vehicleRectsIt;
|
||||
ov::InferRequest& attributesRequest = *attributesRequestIt;
|
||||
context.detectionsProcessorsContext.vehicleAttributesClassifier.setImage(attributesRequest, sharedVideoFrame->frame, vehicleRect);
|
||||
|
||||
attributesRequest.set_callback(
|
||||
std::bind(
|
||||
[](std::shared_ptr<ClassifiersAggregator> classifiersAggregator,
|
||||
ov::InferRequest& attributesRequest,
|
||||
cv::Rect rect,
|
||||
Context& context) {
|
||||
attributesRequest.set_callback([](std::exception_ptr) {}); // destroy the stored bind object
|
||||
const std::pair<std::string, std::string>& attributes =context.detectionsProcessorsContext.vehicleAttributesClassifier.getResults(attributesRequest);
|
||||
if (((classifiersAggregator->sharedVideoFrame->frameId == 0 && !context.isVideo) || context.isVideo)) {
|
||||
classifiersAggregator->rawAttributes.lockedPushBack("Vehicle Attributes results:" + attributes.first + ';' + attributes.second);
|
||||
}
|
||||
classifiersAggregator->push(BboxAndDescr{ BboxAndDescr::ObjectType::VEHICLE, rect, attributes.first + ' ' + attributes.second });
|
||||
context.attributesInfers.inferRequests.lockedPushBack(attributesRequest);
|
||||
},
|
||||
classifiersAggregator,
|
||||
std::ref(attributesRequest),
|
||||
vehicleRect,
|
||||
std::ref(context)));
|
||||
attributesRequest.start_async();
|
||||
}
|
||||
vehicleRects.erase(vehicleRects.begin(), vehicleRectsIt);
|
||||
|
||||
auto plateRectsIt = plateRects.begin();
|
||||
for (auto lprRequestsIt = reservedLprRequests.begin(); lprRequestsIt != reservedLprRequests.end(); plateRectsIt++, lprRequestsIt++) {
|
||||
const cv::Rect plateRect = *plateRectsIt;
|
||||
ov::InferRequest& lprRequest = *lprRequestsIt;
|
||||
context.detectionsProcessorsContext.lpr.setImage(lprRequest, sharedVideoFrame->frame, plateRect);
|
||||
|
||||
lprRequest.set_callback(
|
||||
std::bind(
|
||||
[](std::shared_ptr<ClassifiersAggregator> classifiersAggregator,
|
||||
ov::InferRequest& lprRequest,
|
||||
cv::Rect rect,
|
||||
Context& context) {
|
||||
lprRequest.set_callback([](std::exception_ptr) {}); // destroy the stored bind object
|
||||
|
||||
std::string result = context.detectionsProcessorsContext.lpr.getResults(lprRequest);
|
||||
|
||||
if (((classifiersAggregator->sharedVideoFrame->frameId == 0 && !context.isVideo) || context.isVideo)) {
|
||||
classifiersAggregator->rawDecodedPlates.lockedPushBack("License Plate Recognition results:" + result);
|
||||
}
|
||||
classifiersAggregator->push(BboxAndDescr{ BboxAndDescr::ObjectType::PLATE, rect, std::move(result) });
|
||||
context.platesInfers.inferRequests.lockedPushBack(lprRequest);
|
||||
}, classifiersAggregator,
|
||||
std::ref(lprRequest),
|
||||
plateRect,
|
||||
std::ref(context)));
|
||||
|
||||
lprRequest.start_async();
|
||||
}
|
||||
plateRects.erase(plateRects.begin(), plateRectsIt);
|
||||
|
||||
if (!vehicleRects.empty() || !plateRects.empty()) {
|
||||
tryPush(context.detectionsProcessorsContext.detectionsProcessorsWorker,
|
||||
std::make_shared<DetectionsProcessor>(sharedVideoFrame, std::move(classifiersAggregator), std::move(vehicleRects), std::move(plateRects)));
|
||||
}
|
||||
}
|
||||
|
||||
bool InferTask::isReady() {
|
||||
InferRequestsContainer& detectorsInfers = static_cast<ReborningVideoFrame*>(sharedVideoFrame.get())->context.detectorsInfers;
|
||||
if (detectorsInfers.inferRequests.container.empty()) {
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
detectorsInfers.inferRequests.mutex.lock();
|
||||
if (detectorsInfers.inferRequests.container.empty()) {
|
||||
detectorsInfers.inferRequests.mutex.unlock();
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
return true; // process() will unlock the mutex
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void InferTask::process() {
|
||||
Context& context = static_cast<ReborningVideoFrame*>(sharedVideoFrame.get())->context;
|
||||
InferRequestsContainer& detectorsInfers = context.detectorsInfers;
|
||||
std::reference_wrapper<ov::InferRequest> inferRequest = detectorsInfers.inferRequests.container.back();
|
||||
detectorsInfers.inferRequests.container.pop_back();
|
||||
detectorsInfers.inferRequests.mutex.unlock();
|
||||
context.inferTasksContext.detector.setImage(inferRequest, sharedVideoFrame->frame);
|
||||
inferRequest.get().set_callback(
|
||||
std::bind(
|
||||
[](VideoFrame::Ptr sharedVideoFrame,
|
||||
ov::InferRequest& inferRequest,
|
||||
Context& context) {
|
||||
inferRequest.set_callback([](std::exception_ptr) {}); // destroy the stored bind object
|
||||
tryPush(context.detectionsProcessorsContext.detectionsProcessorsWorker,
|
||||
std::make_shared<DetectionsProcessor>(sharedVideoFrame, &inferRequest));
|
||||
}, sharedVideoFrame,
|
||||
inferRequest,
|
||||
std::ref(context)));
|
||||
|
||||
inferRequest.get().start_async();
|
||||
// do not push as callback does it
|
||||
}
|
||||
|
||||
bool Reader::isReady() {
|
||||
Context& context = static_cast<ReborningVideoFrame*>(sharedVideoFrame.get())->context;
|
||||
context.readersContext.lastCapturedFrameIdsMutexes[sharedVideoFrame->sourceID].lock();
|
||||
if (context.readersContext.lastCapturedFrameIds[sharedVideoFrame->sourceID] + 1 == sharedVideoFrame->frameId) {
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
context.readersContext.lastCapturedFrameIdsMutexes[sharedVideoFrame->sourceID].unlock();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
void Reader::process() {
|
||||
unsigned sourceID = sharedVideoFrame->sourceID;
|
||||
sharedVideoFrame->timestamp = std::chrono::steady_clock::now();
|
||||
Context& context = static_cast<ReborningVideoFrame*>(sharedVideoFrame.get())->context;
|
||||
const std::vector<std::shared_ptr<InputChannel>>& inputChannels = context.readersContext.inputChannels;
|
||||
if (inputChannels[sourceID]->read(sharedVideoFrame->frame)) {
|
||||
context.readersContext.lastCapturedFrameIds[sourceID]++;
|
||||
context.readersContext.lastCapturedFrameIdsMutexes[sourceID].unlock();
|
||||
tryPush(context.inferTasksContext.inferTasksWorker, std::make_shared<InferTask>(sharedVideoFrame));
|
||||
}
|
||||
else {
|
||||
context.readersContext.lastCapturedFrameIds[sourceID]++;
|
||||
context.readersContext.lastCapturedFrameIdsMutexes[sourceID].unlock();
|
||||
try {
|
||||
std::shared_ptr<Worker>(context.resAggregatorsWorker)->stop();
|
||||
}
|
||||
catch (const std::bad_weak_ptr&) {}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Main class
|
||||
/// </summary>
|
||||
ANSALPR_OV::ANSALPR_OV() {};
|
||||
ANSALPR_OV::~ANSALPR_OV() {
|
||||
if (_detector == nullptr) {
|
||||
delete _detector;
|
||||
_detector = nullptr;
|
||||
}
|
||||
if (_vehicleAttributesClassifier == nullptr) {
|
||||
delete _vehicleAttributesClassifier;
|
||||
_vehicleAttributesClassifier = nullptr;
|
||||
}
|
||||
if (_lpr == nullptr) {
|
||||
delete _lpr;
|
||||
_lpr = nullptr;
|
||||
}
|
||||
};
|
||||
bool ANSALPR_OV::Destroy() {
|
||||
if (_detector == nullptr) {
|
||||
delete _detector;
|
||||
_detector = nullptr;
|
||||
}
|
||||
if (_vehicleAttributesClassifier == nullptr) {
|
||||
delete _vehicleAttributesClassifier;
|
||||
_vehicleAttributesClassifier = nullptr;
|
||||
}
|
||||
if (_lpr == nullptr) {
|
||||
delete _lpr;
|
||||
_lpr = nullptr;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
bool ANSALPR_OV::Initialize(const std::string& licenseKey, const std::string& modelZipFilePath, const std::string& modelZipPassword) {
|
||||
try {
|
||||
_licenseKey = licenseKey;
|
||||
_licenseValid = false;
|
||||
CheckLicense();
|
||||
if (!_licenseValid) {
|
||||
this->_logger->LogError("ANSALPR_OV::Initialize.", "License is not valid.", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
// Extract model folder
|
||||
// 0. Check if the modelZipFilePath exist?
|
||||
if (!FileExist(modelZipFilePath)) {
|
||||
this->_logger->LogFatal("ANSALPR_OV::Initialize", "Model zip file is not exist", __FILE__, __LINE__);
|
||||
}
|
||||
// 1. Unzip model zip file to a special location with folder name as model file (and version)
|
||||
std::string outputFolder;
|
||||
std::vector<std::string> passwordArray;
|
||||
if (!modelZipPassword.empty()) passwordArray.push_back(modelZipPassword);
|
||||
passwordArray.push_back("AnsDemoModels20@!");
|
||||
passwordArray.push_back("Sh7O7nUe7vJ/417W0gWX+dSdfcP9hUqtf/fEqJGqxYL3PedvHubJag==");
|
||||
passwordArray.push_back("3LHxGrjQ7kKDJBD9MX86H96mtKLJaZcTYXrYRdQgW8BKGt7enZHYMg==");
|
||||
std::string modelName = GetFileNameWithoutExtension(modelZipFilePath);
|
||||
|
||||
size_t vectorSize = passwordArray.size();
|
||||
for (size_t i = 0; i < vectorSize; i++) {
|
||||
if (ExtractPasswordProtectedZip(modelZipFilePath, passwordArray[i], modelName, _modelFolder, false))
|
||||
break; // Break the loop when the condition is met.
|
||||
}
|
||||
// 2. Check if the outputFolder exist
|
||||
if (!FolderExist(_modelFolder)) {
|
||||
this->_logger->LogError("ANSALPR_OV::Initialize. Output model folder is not exist", _modelFolder, __FILE__, __LINE__);
|
||||
return false; // That means the model file is not exist or the password is not correct
|
||||
}
|
||||
|
||||
_vehicleLPModel = CreateFilePath(_modelFolder, "vehiclelp.xml");
|
||||
_vehicleAtModel = CreateFilePath(_modelFolder, "vehicle.xml");
|
||||
_lprModel = CreateFilePath(_modelFolder, "lpr.xml");
|
||||
|
||||
ov::Core core;
|
||||
int FLAGS_nthreads = 0;
|
||||
|
||||
std::set<std::string> devices;
|
||||
for (const std::string& netDevices : { "GPU", "GPU", "GPU" }) {
|
||||
if (netDevices.empty()) {
|
||||
continue;
|
||||
}
|
||||
for (const std::string& device : parseDevices(netDevices)) {
|
||||
devices.insert(device);
|
||||
}
|
||||
}
|
||||
|
||||
std::map<std::string, int32_t> device_nstreams = parseValuePerDevice(devices, "");
|
||||
|
||||
for (const std::string& device : devices) {
|
||||
if ("CPU" == device) {
|
||||
if (FLAGS_nthreads != 0) {
|
||||
core.set_property("CPU", ov::inference_num_threads(FLAGS_nthreads));
|
||||
}
|
||||
//core.set_property("CPU", ov::affinity(ov::Affinity::NONE));
|
||||
core.set_property("CPU", ov::streams::num((device_nstreams.count("CPU") > 0 ? ov::streams::Num(device_nstreams["CPU"]) : ov::streams::AUTO)));
|
||||
|
||||
device_nstreams["CPU"] = core.get_property("CPU", ov::streams::num);
|
||||
}
|
||||
if ("GPU" == device) {
|
||||
core.set_property("GPU", ov::streams::num(device_nstreams.count("GPU") > 0 ? ov::streams::Num(device_nstreams["GPU"]) : ov::streams::AUTO));
|
||||
|
||||
device_nstreams["GPU"] = core.get_property("GPU", ov::streams::num);
|
||||
if (devices.end() != devices.find("CPU")) {
|
||||
core.set_property("GPU", ov::intel_gpu::hint::queue_throttle(ov::intel_gpu::hint::ThrottleLevel(1)));
|
||||
}
|
||||
}
|
||||
}
|
||||
double FLAGS_t = 0.5;
|
||||
if(FileExist(_vehicleLPModel))_detector = new Detector(core, "GPU", _vehicleLPModel, { static_cast<float>(FLAGS_t), static_cast<float>(FLAGS_t) }, false);
|
||||
else {
|
||||
this->_logger->LogFatal("ANSALPR_OV::Initialize", _vehicleLPModel, __FILE__, __LINE__);
|
||||
|
||||
}
|
||||
if(FileExist(_vehicleAtModel))_vehicleAttributesClassifier = new VehicleAttributesClassifier(core, "GPU", _vehicleAtModel, false);
|
||||
else {
|
||||
this->_logger->LogFatal("ANSALPR_OV::Initialize", _vehicleAtModel, __FILE__, __LINE__);
|
||||
|
||||
}
|
||||
if(FileExist(_lprModel)) _lpr = new Lpr(core, "CPU", _lprModel, false);
|
||||
else {
|
||||
this->_logger->LogFatal("ANSALPR_OV::Initialize", _lprModel, __FILE__, __LINE__);
|
||||
|
||||
}
|
||||
|
||||
if (FileExist(_vehicleLPModel) &&
|
||||
FileExist(_vehicleAtModel) &&
|
||||
FileExist(_lprModel))return true;
|
||||
else return false;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger->LogFatal("ANSALPR_OV::Initialize", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
bool ANSALPR_OV::Inference(const cv::Mat& input, std::string& lprResult) {
|
||||
cv::Mat frame = input.clone();
|
||||
std::shared_ptr<IInputSource> inputSource = std::make_shared<ImageSource>(frame, true);
|
||||
std::vector<std::shared_ptr<InputChannel>> _inputChannels;
|
||||
_inputChannels.push_back(InputChannel::create(inputSource));
|
||||
unsigned nireq = 1;
|
||||
bool isVideo = false;
|
||||
std::size_t nclassifiersireq{ 0 };
|
||||
std::size_t nrecognizersireq{ 0 };
|
||||
nclassifiersireq = nireq * 3;
|
||||
nrecognizersireq = nireq * 3;
|
||||
|
||||
Context context = { _inputChannels,
|
||||
*_detector,
|
||||
*_vehicleAttributesClassifier,
|
||||
*_lpr,
|
||||
2,
|
||||
nireq,
|
||||
isVideo,
|
||||
nclassifiersireq,
|
||||
nrecognizersireq };
|
||||
|
||||
std::shared_ptr<Worker> worker = std::make_shared<Worker>(2);
|
||||
context.readersContext.readersWorker = worker;
|
||||
context.inferTasksContext.inferTasksWorker = worker;
|
||||
context.detectionsProcessorsContext.detectionsProcessorsWorker = worker;
|
||||
context.resAggregatorsWorker = worker;
|
||||
for (unsigned sourceID = 0; sourceID < _inputChannels.size(); sourceID++) {
|
||||
VideoFrame::Ptr sharedVideoFrame = std::make_shared<ReborningVideoFrame>(context, sourceID, 0);
|
||||
worker->push(std::make_shared<Reader>(sharedVideoFrame));
|
||||
}
|
||||
|
||||
// Running
|
||||
worker->runThreads();
|
||||
worker->threadFunc();
|
||||
worker->join();
|
||||
|
||||
std::list<BboxAndDescr> boxesAndDescrs = context.boxesAndDescrs;
|
||||
std::vector<ALPRObject> output;
|
||||
output.clear();
|
||||
for(const BboxAndDescr boxesAndDescr: boxesAndDescrs)
|
||||
{
|
||||
if (boxesAndDescr.objectType == ANSCENTER::BboxAndDescr::ObjectType::PLATE) {
|
||||
ALPRObject result;
|
||||
result.classId = 0;
|
||||
result.className = boxesAndDescr.descr;
|
||||
result.confidence = 1.0;
|
||||
result.box = boxesAndDescr.rect;
|
||||
output.push_back(result);
|
||||
}
|
||||
}
|
||||
lprResult = VectorDetectionToJsonString(output);
|
||||
return true;
|
||||
};
|
||||
bool ANSALPR_OV::Inference(const cv::Mat& input, const std::vector<cv::Rect>& Bbox, std::string& lprResult) {
|
||||
return true;
|
||||
}
|
||||
std::string ANSALPR_OV::VectorDetectionToJsonString(const std::vector<ALPRObject>& dets) {
|
||||
boost::property_tree::ptree root;
|
||||
boost::property_tree::ptree detectedObjects;
|
||||
for (int i = 0; i < dets.size(); i++) {
|
||||
boost::property_tree::ptree detectedNode;
|
||||
detectedNode.put("class_id", dets[i].classId);
|
||||
detectedNode.put("class_name", dets[i].className);
|
||||
detectedNode.put("prob", dets[i].confidence);
|
||||
detectedNode.put("x", dets[i].box.x);
|
||||
detectedNode.put("y", dets[i].box.y);
|
||||
detectedNode.put("width", dets[i].box.width);
|
||||
detectedNode.put("height", dets[i].box.height);
|
||||
detectedNode.put("mask", "");//Todo: convert masks to mask with comma seperated dets[i].mask);
|
||||
detectedNode.put("extra_info", "");
|
||||
|
||||
// we might add masks into this using comma seperated string
|
||||
detectedObjects.push_back(std::make_pair("", detectedNode));
|
||||
}
|
||||
root.add_child("results", detectedObjects);
|
||||
std::ostringstream stream;
|
||||
boost::property_tree::write_json(stream, root, false);
|
||||
std::string trackingResult = stream.str();
|
||||
return trackingResult;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
// ANSGpuFrameRegistry.cpp — Cross-DLL singleton resolver for ANSLPR.dll.
|
||||
//
|
||||
// Finds the canonical ANSGpuFrameRegistry instance exported by ANSCV.dll
|
||||
// via GetProcAddress. No link dependency on ANSCV.lib needed.
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#define NOMINMAX
|
||||
#include <windows.h>
|
||||
#include "ANSGpuFrameRegistry.h"
|
||||
|
||||
ANSGpuFrameRegistry* ANSGpuFrameRegistry::resolveProcessWide() {
|
||||
// ANSCV.dll is always loaded before inference starts (it provides RTSP).
|
||||
HMODULE hMod = GetModuleHandleA("ANSCV.dll");
|
||||
if (hMod) {
|
||||
typedef ANSGpuFrameRegistry* (*GetInstanceFn)();
|
||||
auto fn = reinterpret_cast<GetInstanceFn>(
|
||||
GetProcAddress(hMod, "ANSGpuFrameRegistry_GetInstance"));
|
||||
if (fn) return fn();
|
||||
}
|
||||
// Fallback: local instance (unit tests without ANSCV.dll).
|
||||
static ANSGpuFrameRegistry local;
|
||||
return &local;
|
||||
}
|
||||
@@ -0,0 +1,769 @@
|
||||
#include "ANSLPR.h"
|
||||
#include <json.hpp>
|
||||
#include "ANSLibsLoader.h"
|
||||
|
||||
static bool ansalprLicenceValid = false;
|
||||
// Global once_flag to protect license checking
|
||||
static std::once_flag ansalprLicenseOnceFlag;
|
||||
template <typename T>
|
||||
T GetData(const boost::property_tree::ptree& pt, const std::string& key)
|
||||
{
|
||||
T ret;
|
||||
if (boost::optional<T> data = pt.get_optional<T>(key))
|
||||
{
|
||||
ret = data.get();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
namespace ANSCENTER {
|
||||
// Checker class
|
||||
void ALPRChecker::Init(int framesToStore) {
|
||||
maxFrames = framesToStore;
|
||||
}
|
||||
|
||||
int ALPRChecker::levenshteinDistance(const std::string& s1, const std::string& s2) {
|
||||
try {
|
||||
int len1 = static_cast<int>(s1.size());
|
||||
int len2 = static_cast<int>(s2.size());
|
||||
std::vector<std::vector<int>> dp(len1 + 1, std::vector<int>(len2 + 1));
|
||||
|
||||
for (int i = 0; i <= len1; i++) dp[i][0] = i;
|
||||
for (int j = 0; j <= len2; j++) dp[0][j] = j;
|
||||
|
||||
for (int i = 1; i <= len1; i++) {
|
||||
for (int j = 1; j <= len2; j++) {
|
||||
if (s1[i - 1] == s2[j - 1]) {
|
||||
dp[i][j] = dp[i - 1][j - 1];
|
||||
}
|
||||
else {
|
||||
dp[i][j] = std::min({ dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + 1 });
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[len1][len2];
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
float ALPRChecker::computeIoU(const cv::Rect& a, const cv::Rect& b) {
|
||||
int x1 = std::max(a.x, b.x);
|
||||
int y1 = std::max(a.y, b.y);
|
||||
int x2 = std::min(a.x + a.width, b.x + b.width);
|
||||
int y2 = std::min(a.y + a.height, b.y + b.height);
|
||||
int intersection = std::max(0, x2 - x1) * std::max(0, y2 - y1);
|
||||
int unionArea = a.area() + b.area() - intersection;
|
||||
return (unionArea > 0) ? static_cast<float>(intersection) / unionArea : 0.f;
|
||||
}
|
||||
|
||||
std::string ALPRChecker::majorityVote(const std::deque<std::string>& history) {
|
||||
if (history.empty()) return "";
|
||||
|
||||
// Count frequency of each plate text, weighted by recency
|
||||
// (more recent entries get higher weight)
|
||||
std::map<std::string, float> weightedFreq;
|
||||
int n = static_cast<int>(history.size());
|
||||
for (int i = 0; i < n; i++) {
|
||||
// Recency weight: older=1.0, newest=2.0
|
||||
float weight = 1.0f + static_cast<float>(i) / std::max(1, n - 1);
|
||||
weightedFreq[history[i]] += weight;
|
||||
}
|
||||
|
||||
// Also group similar plates (Levenshtein <= 1) under the most frequent variant
|
||||
std::string bestText;
|
||||
float bestScore = 0.f;
|
||||
for (const auto& kv : weightedFreq) {
|
||||
float totalScore = kv.second;
|
||||
// Add scores from similar plates
|
||||
for (const auto& other : weightedFreq) {
|
||||
if (other.first == kv.first) continue;
|
||||
// Use quick length check to skip obvious non-matches
|
||||
int lenDiff = std::abs(static_cast<int>(kv.first.size()) - static_cast<int>(other.first.size()));
|
||||
if (lenDiff <= 1) {
|
||||
// Only compute Levenshtein for candidates within 1 char length difference
|
||||
int dist = 0;
|
||||
// Inline fast Levenshtein for short strings
|
||||
const std::string& s1 = kv.first;
|
||||
const std::string& s2 = other.first;
|
||||
int len1 = static_cast<int>(s1.size()), len2 = static_cast<int>(s2.size());
|
||||
if (len1 <= 15 && len2 <= 15) {
|
||||
std::vector<int> prev(len2 + 1), curr(len2 + 1);
|
||||
for (int j = 0; j <= len2; j++) prev[j] = j;
|
||||
for (int i = 1; i <= len1; i++) {
|
||||
curr[0] = i;
|
||||
for (int j = 1; j <= len2; j++) {
|
||||
if (s1[i - 1] == s2[j - 1]) curr[j] = prev[j - 1];
|
||||
else curr[j] = 1 + std::min({ prev[j], curr[j - 1], prev[j - 1] });
|
||||
}
|
||||
std::swap(prev, curr);
|
||||
}
|
||||
dist = prev[len2];
|
||||
}
|
||||
if (dist == 1) {
|
||||
totalScore += other.second * 0.5f; // partial credit for 1-edit variants
|
||||
}
|
||||
}
|
||||
}
|
||||
if (totalScore > bestScore) {
|
||||
bestScore = totalScore;
|
||||
bestText = kv.first;
|
||||
}
|
||||
}
|
||||
return bestText;
|
||||
}
|
||||
|
||||
// Original API — backward compatible, no spatial tracking
|
||||
std::string ALPRChecker::checkPlate(const std::string& cameraId, const std::string& detectedPlate) {
|
||||
// Delegate to spatial version with empty box (disables IoU matching)
|
||||
return checkPlate(cameraId, detectedPlate, cv::Rect());
|
||||
}
|
||||
|
||||
// Enhanced API with bounding box for spatial plate tracking
|
||||
std::string ALPRChecker::checkPlate(const std::string& cameraId, const std::string& detectedPlate, const cv::Rect& plateBox) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
try {
|
||||
if (detectedPlate.empty()) return detectedPlate;
|
||||
|
||||
auto& plates = trackedPlates[cameraId];
|
||||
bool hasSpatial = (plateBox.width > 0 && plateBox.height > 0);
|
||||
|
||||
// Increment framesSinceLastSeen for all plates (will be reset for matched plate)
|
||||
for (auto& p : plates) {
|
||||
p.framesSinceLastSeen++;
|
||||
}
|
||||
|
||||
// Periodic pruning: remove plates not seen for a long time
|
||||
_pruneCounter++;
|
||||
if (_pruneCounter >= 30 && plates.size() > 10) {
|
||||
_pruneCounter = 0;
|
||||
int staleThreshold = maxFrames * 3; // 180 frames = ~6 seconds at 30fps
|
||||
plates.erase(
|
||||
std::remove_if(plates.begin(), plates.end(),
|
||||
[staleThreshold](const TrackedPlate& p) {
|
||||
return p.framesSinceLastSeen > staleThreshold;
|
||||
}),
|
||||
plates.end());
|
||||
}
|
||||
|
||||
// Step 1: Find matching tracked plate
|
||||
// Strategy: try IoU first, then text similarity as fallback
|
||||
int bestIdx = -1;
|
||||
float bestIoU = 0.f;
|
||||
|
||||
// 1a. Try spatial IoU matching
|
||||
if (hasSpatial) {
|
||||
for (int i = 0; i < static_cast<int>(plates.size()); i++) {
|
||||
float iou = computeIoU(plateBox, plates[i].lastBox);
|
||||
if (iou > iouMatchThreshold && iou > bestIoU) {
|
||||
bestIoU = iou;
|
||||
bestIdx = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 1b. If IoU failed, try text similarity against locked texts
|
||||
if (bestIdx < 0) {
|
||||
for (int i = 0; i < static_cast<int>(plates.size()); i++) {
|
||||
if (!plates[i].lockedText.empty()) {
|
||||
int dist = levenshteinDistance(detectedPlate, plates[i].lockedText);
|
||||
if (dist <= 1) {
|
||||
bestIdx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Also check against recent history even if not locked
|
||||
if (bestIdx < 0 && !plates[i].textHistory.empty()) {
|
||||
int checkCount = std::min(static_cast<int>(plates[i].textHistory.size()), 3);
|
||||
for (int j = static_cast<int>(plates[i].textHistory.size()) - 1;
|
||||
j >= static_cast<int>(plates[i].textHistory.size()) - checkCount; j--) {
|
||||
int dist = levenshteinDistance(detectedPlate, plates[i].textHistory[j]);
|
||||
if (dist <= 1) {
|
||||
bestIdx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2: Create new tracked plate if no match found
|
||||
if (bestIdx < 0) {
|
||||
// Before creating a new plate, check if this detection conflicts with
|
||||
// a nearby locked plate — if so, it's likely a bad OCR read and we should
|
||||
// return the locked text instead of creating a garbage entry
|
||||
if (hasSpatial) {
|
||||
float bestProximity = 0.f;
|
||||
int proximityIdx = -1;
|
||||
for (int i = 0; i < static_cast<int>(plates.size()); i++) {
|
||||
if (plates[i].lockedText.empty()) continue;
|
||||
if (plates[i].lastBox.width <= 0 || plates[i].lastBox.height <= 0) continue;
|
||||
float cx1 = plateBox.x + plateBox.width * 0.5f;
|
||||
float cy1 = plateBox.y + plateBox.height * 0.5f;
|
||||
float cx2 = plates[i].lastBox.x + plates[i].lastBox.width * 0.5f;
|
||||
float cy2 = plates[i].lastBox.y + plates[i].lastBox.height * 0.5f;
|
||||
float dx = std::abs(cx1 - cx2);
|
||||
float dy = std::abs(cy1 - cy2);
|
||||
float maxDim = std::max(static_cast<float>(plateBox.width), static_cast<float>(plates[i].lastBox.width));
|
||||
if (dx < maxDim * 2.0f && dy < maxDim * 1.5f) {
|
||||
float proximity = 1.0f / (1.0f + dx + dy);
|
||||
if (proximity > bestProximity) {
|
||||
bestProximity = proximity;
|
||||
proximityIdx = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (proximityIdx >= 0) {
|
||||
auto& tp = plates[proximityIdx];
|
||||
tp.lastBox = plateBox;
|
||||
tp.framesSinceLastSeen = 0;
|
||||
tp.textHistory.push_back(detectedPlate);
|
||||
return tp.lockedText;
|
||||
}
|
||||
}
|
||||
|
||||
TrackedPlate newPlate;
|
||||
newPlate.lastBox = plateBox;
|
||||
newPlate.textHistory.push_back(detectedPlate);
|
||||
plates.push_back(newPlate);
|
||||
return detectedPlate;
|
||||
}
|
||||
|
||||
// Step 3: Update matched tracked plate
|
||||
auto& tp = plates[bestIdx];
|
||||
if (hasSpatial) tp.lastBox = plateBox;
|
||||
tp.framesSinceLastSeen = 0; // Reset — this plate was just seen
|
||||
|
||||
// Store RAW detection (not corrected — avoids feedback loop)
|
||||
tp.textHistory.push_back(detectedPlate);
|
||||
if (static_cast<int>(tp.textHistory.size()) > maxFrames) {
|
||||
tp.textHistory.pop_front();
|
||||
}
|
||||
|
||||
// Step 4: Majority vote to find stable text
|
||||
std::string voted = majorityVote(tp.textHistory);
|
||||
|
||||
// Step 5: Lock logic — once we have enough consistent reads, lock the text
|
||||
if (tp.lockedText.empty()) {
|
||||
int matchCount = 0;
|
||||
int lookback = std::min(static_cast<int>(tp.textHistory.size()), maxFrames);
|
||||
for (int i = static_cast<int>(tp.textHistory.size()) - 1;
|
||||
i >= static_cast<int>(tp.textHistory.size()) - lookback; i--) {
|
||||
if (tp.textHistory[i] == voted) matchCount++;
|
||||
}
|
||||
if (matchCount >= minVotesToStabilize) {
|
||||
tp.lockedText = voted;
|
||||
tp.lockCount = 1;
|
||||
}
|
||||
return voted;
|
||||
}
|
||||
else {
|
||||
int dist = levenshteinDistance(detectedPlate, tp.lockedText);
|
||||
if (dist == 0) {
|
||||
tp.lockCount++;
|
||||
return tp.lockedText;
|
||||
}
|
||||
else {
|
||||
int newDist = levenshteinDistance(voted, tp.lockedText);
|
||||
if (newDist > 1) {
|
||||
int newMatchCount = 0;
|
||||
int recent = std::min(static_cast<int>(tp.textHistory.size()), minVotesToStabilize * 2);
|
||||
for (int i = static_cast<int>(tp.textHistory.size()) - 1;
|
||||
i >= static_cast<int>(tp.textHistory.size()) - recent; i--) {
|
||||
if (tp.textHistory[i] == voted) newMatchCount++;
|
||||
}
|
||||
if (newMatchCount >= minVotesToStabilize) {
|
||||
tp.lockedText = voted;
|
||||
tp.lockCount = 1;
|
||||
return tp.lockedText;
|
||||
}
|
||||
}
|
||||
return tp.lockedText;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
return detectedPlate;
|
||||
}
|
||||
}
|
||||
//
|
||||
static void VerifyGlobalANSALPRLicense(const std::string& licenseKey) {
|
||||
try {
|
||||
ansalprLicenceValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, 1006, "ANSALPR");//Default productId=1006
|
||||
if (!ansalprLicenceValid) { // we also support ANSTS license
|
||||
ansalprLicenceValid = ANSCENTER::ANSLicenseHelper::LicenseVerification(licenseKey, 1003, "ANSVIS");//Default productId=1003 (ANSVIS)
|
||||
}
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
ansalprLicenceValid = false;
|
||||
}
|
||||
}
|
||||
void ANSALPR::CheckLicense() {
|
||||
try {
|
||||
// Check once globally
|
||||
std::call_once(ansalprLicenseOnceFlag, [this]() {
|
||||
VerifyGlobalANSALPRLicense(_licenseKey);
|
||||
});
|
||||
|
||||
// Update this instance's local license flag
|
||||
_licenseValid = ansalprLicenceValid;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogFatal("ANSODBase::CheckLicense. Error:", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
bool ANSALPR::LoadEngine() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ANSALPR::Initialize(const std::string& licenseKey, const std::string& modelZipFilePath, const std::string& modelZipPassword, double detectorThreshold, double ocrThreshold, double colorThreshold) {
|
||||
try {
|
||||
ANSCENTER::ANSLibsLoader::Initialize();
|
||||
_licenseKey = licenseKey;
|
||||
_licenseValid = false;
|
||||
_detectorThreshold = detectorThreshold;
|
||||
_ocrThreshold = ocrThreshold;
|
||||
_colorThreshold = colorThreshold;
|
||||
ANSALPR::CheckLicense();
|
||||
if (!_licenseValid) {
|
||||
this->_logger.LogError("ANSALPR::Initialize.", "License is not valid.", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
// Extract model folder
|
||||
// 0. Check if the modelZipFilePath exist?
|
||||
if (!FileExist(modelZipFilePath)) {
|
||||
this->_logger.LogFatal("ANSALPR::Initialize", "Model zip file is not exist", __FILE__, __LINE__);
|
||||
}
|
||||
// 1. Unzip model zip file to a special location with folder name as model file (and version)
|
||||
std::string outputFolder;
|
||||
std::vector<std::string> passwordArray;
|
||||
if (!modelZipPassword.empty()) passwordArray.push_back(modelZipPassword);
|
||||
passwordArray.push_back("AnsDemoModels20@!");
|
||||
passwordArray.push_back("Sh7O7nUe7vJ/417W0gWX+dSdfcP9hUqtf/fEqJGqxYL3PedvHubJag==");
|
||||
passwordArray.push_back("3LHxGrjQ7kKDJBD9MX86H96mtKLJaZcTYXrYRdQgW8BKGt7enZHYMg==");
|
||||
std::string modelName = GetFileNameWithoutExtension(modelZipFilePath);
|
||||
//this->_logger.LogDebug("ANSFDBase::Initialize. Model name", modelName);
|
||||
|
||||
size_t vectorSize = passwordArray.size();
|
||||
for (size_t i = 0; i < vectorSize; i++) {
|
||||
if (ExtractPasswordProtectedZip(modelZipFilePath, passwordArray[i], modelName, _modelFolder, false))
|
||||
break; // Break the loop when the condition is met.
|
||||
}
|
||||
// 2. Check if the outputFolder exist
|
||||
if (!FolderExist(_modelFolder)) {
|
||||
this->_logger.LogError("ANSFDBase::Initialize. Output model folder is not exist", _modelFolder, __FILE__, __LINE__);
|
||||
return false; // That means the model file is not exist or the password is not correct
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger.LogFatal("ANSALPR::Initialize", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
ANSALPR::~ANSALPR(){};
|
||||
bool ANSALPR::Destroy() { return true; };
|
||||
|
||||
std::vector<cv::Rect> ANSCENTER::ANSALPR::GetBoundingBoxes(const std::string& strBBoxes) {
|
||||
std::vector<cv::Rect> bBoxes;
|
||||
bBoxes.clear();
|
||||
std::stringstream ss;
|
||||
ss << strBBoxes;
|
||||
boost::property_tree::ptree pt;
|
||||
boost::property_tree::read_json(ss, pt);
|
||||
BOOST_FOREACH(const boost::property_tree::ptree::value_type & child, pt.get_child("results"))
|
||||
{
|
||||
const boost::property_tree::ptree& result = child.second;
|
||||
const auto x = GetData<float>(result, "x");
|
||||
const auto y = GetData<float>(result, "y");
|
||||
const auto width = GetData<float>(result, "width");
|
||||
const auto height = GetData<float>(result, "height");
|
||||
cv::Rect rectTemp;
|
||||
rectTemp.x = x;
|
||||
rectTemp.y = y;
|
||||
rectTemp.width = width;
|
||||
rectTemp.height = height;
|
||||
bBoxes.push_back(rectTemp);
|
||||
}
|
||||
return bBoxes;
|
||||
}
|
||||
std::string ANSCENTER::ANSALPR::PolygonToString(const std::vector<cv::Point2f>& polygon) {
|
||||
if (polygon.empty()) {
|
||||
return "";
|
||||
}
|
||||
std::string result;
|
||||
result.reserve(polygon.size() * 20);
|
||||
|
||||
char buffer[64];
|
||||
for (size_t i = 0; i < polygon.size(); ++i) {
|
||||
if (i > 0) {
|
||||
snprintf(buffer, sizeof(buffer), ";%.3f;%.3f", polygon[i].x, polygon[i].y);
|
||||
}
|
||||
else {
|
||||
snprintf(buffer, sizeof(buffer), "%.3f;%.3f", polygon[i].x, polygon[i].y);
|
||||
}
|
||||
result += buffer;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string ANSALPR::VectorDetectionToJsonString(const std::vector<Object>& dets) {
|
||||
if (dets.empty()) {
|
||||
return R"({"results":[]})";
|
||||
}
|
||||
try {
|
||||
nlohmann::json root;
|
||||
auto& results = root["results"] = nlohmann::json::array();
|
||||
|
||||
for (const auto& det : dets) {
|
||||
results.push_back({
|
||||
{"class_id", std::to_string(det.classId)},
|
||||
{"track_id", std::to_string(det.trackId)},
|
||||
{"class_name", det.className},
|
||||
{"prob", std::to_string(det.confidence)},
|
||||
{"x", std::to_string(det.box.x)},
|
||||
{"y", std::to_string(det.box.y)},
|
||||
{"width", std::to_string(det.box.width)},
|
||||
{"height", std::to_string(det.box.height)},
|
||||
{"mask", ""}, // TODO: convert masks to comma separated string
|
||||
{"extra_info", det.extraInfo},
|
||||
{"camera_id", det.cameraId},
|
||||
{"polygon", PolygonToString(det.polygon)},
|
||||
{"kps", KeypointsToString(det.kps)}
|
||||
});
|
||||
}
|
||||
return root.dump();
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogFatal("ANSALPR::VectorDetectionToJsonString", e.what(), __FILE__, __LINE__);
|
||||
return R"({"results":[],"error":"Serialization failed"})";
|
||||
}
|
||||
}
|
||||
|
||||
void ANSALPR::SetPlateFormats(const std::vector<std::string>& formats) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_plateFormats.clear();
|
||||
_plateFormats = formats;
|
||||
}
|
||||
void ANSALPR::SetPlateFormat(const std::string& format) {
|
||||
std::lock_guard<std::recursive_mutex> lock(_mutex);
|
||||
_plateFormats.clear();
|
||||
_plateFormats.push_back(format);
|
||||
}
|
||||
|
||||
std::vector<std::string> ANSALPR::GetPlateFormats() const {
|
||||
return _plateFormats;
|
||||
}
|
||||
|
||||
std::string ANSCENTER::ANSALPR::KeypointsToString(const std::vector<float>& kps) {
|
||||
if (kps.empty()) {
|
||||
return "";
|
||||
}
|
||||
std::string result;
|
||||
result.reserve(kps.size() * 10);
|
||||
|
||||
char buffer[32];
|
||||
for (size_t i = 0; i < kps.size(); ++i) {
|
||||
if (i > 0) result += ';';
|
||||
snprintf(buffer, sizeof(buffer), "%.3f", kps[i]);
|
||||
result += buffer;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
std::vector<cv::Point2f> ANSCENTER::ANSALPR::RectToNormalizedPolygon(const cv::Rect& rect, float imageWidth, float imageHeight) {
|
||||
// Ensure imageWidth and imageHeight are non-zero to avoid division by zero
|
||||
if (imageWidth <= 0 || imageHeight <= 0) {
|
||||
std::vector<cv::Point2f> emptyPolygon;
|
||||
return emptyPolygon;
|
||||
}
|
||||
|
||||
// Calculate normalized points for each corner of the rectangle
|
||||
std::vector<cv::Point2f> polygon = {
|
||||
{ rect.x / imageWidth, rect.y / imageHeight }, // Top-left
|
||||
{ (rect.x + rect.width) / imageWidth, rect.y / imageHeight }, // Top-right
|
||||
{ (rect.x + rect.width) / imageWidth, (rect.y + rect.height) / imageHeight }, // Bottom-right
|
||||
{ rect.x / imageWidth, (rect.y + rect.height) / imageHeight } // Bottom-left
|
||||
};
|
||||
|
||||
return polygon;
|
||||
}
|
||||
|
||||
// Functions for screen size division
|
||||
double ANSALPR::calculateDistanceToCenter(const cv::Point& center, const cv::Rect& rect) {
|
||||
cv::Point rectCenter(rect.x + rect.width / 2, rect.y + rect.height / 2);
|
||||
return std::sqrt(std::pow(rectCenter.x - center.x, 2) + std::pow(rectCenter.y - center.y, 2));
|
||||
}
|
||||
std::vector<ANSALPR::ImageSection> ANSALPR::divideImage(const cv::Mat& image) {
|
||||
if (image.empty()) {
|
||||
std::cerr << "Error: Empty image!" << std::endl;
|
||||
return cachedSections;
|
||||
}
|
||||
cv::Size currentSize(image.cols, image.rows);
|
||||
|
||||
// Check if the image size has changed
|
||||
if (currentSize == previousImageSize) {
|
||||
return cachedSections; // Return cached sections if size is the same
|
||||
}
|
||||
|
||||
// Update previous size
|
||||
previousImageSize = currentSize;
|
||||
cachedSections.clear();
|
||||
|
||||
int width = image.cols;
|
||||
int height = image.rows;
|
||||
int maxDimension = std::max(width, height);
|
||||
int numSections = 10;// std::max(1, numSections); // Ensure at least 1 section
|
||||
if (maxDimension <= 2560)numSections = 8;
|
||||
if (maxDimension <= 1280)numSections = 6;
|
||||
if (maxDimension <= 960)numSections = 4;
|
||||
if (maxDimension <= 640)numSections = 2;
|
||||
if (maxDimension <= 320)numSections = 1;
|
||||
|
||||
int gridRows = std::sqrt(numSections);
|
||||
int gridCols = (numSections + gridRows - 1) / gridRows; // Ensure all sections are covered
|
||||
|
||||
int sectionWidth = width / gridCols;
|
||||
int sectionHeight = height / gridRows;
|
||||
|
||||
cv::Point imageCenter(width / 2, height / 2);
|
||||
std::vector<std::pair<double, ImageSection>> distancePriorityList;
|
||||
|
||||
// Create sections and store their distance from the center
|
||||
for (int r = 0; r < gridRows; ++r) {
|
||||
for (int c = 0; c < gridCols; ++c) {
|
||||
int x = c * sectionWidth;
|
||||
int y = r * sectionHeight;
|
||||
int w = (c == gridCols - 1) ? width - x : sectionWidth;
|
||||
int h = (r == gridRows - 1) ? height - y : sectionHeight;
|
||||
|
||||
ImageSection section(cv::Rect(x, y, w, h));
|
||||
double distance = calculateDistanceToCenter(imageCenter, section.region);
|
||||
distancePriorityList.emplace_back(distance, section);
|
||||
}
|
||||
}
|
||||
|
||||
// Sort sections based on distance from center, then top-to-bottom, then left-to-right
|
||||
std::sort(distancePriorityList.begin(), distancePriorityList.end(),
|
||||
[](const std::pair<double, ImageSection>& a, const std::pair<double, ImageSection>& b) {
|
||||
if (std::abs(a.first - b.first) > 1e-5) {
|
||||
return a.first < b.first; // Sort by closest distance to center
|
||||
}
|
||||
// If distance is the same, prioritize top to bottom, then left to right
|
||||
return a.second.region.y == b.second.region.y
|
||||
? a.second.region.x < b.second.region.x
|
||||
: a.second.region.y < b.second.region.y;
|
||||
});
|
||||
|
||||
// Assign priority
|
||||
int priority = 1;
|
||||
for (auto& entry : distancePriorityList) {
|
||||
entry.second.priority = priority++;
|
||||
cachedSections.push_back(entry.second);
|
||||
}
|
||||
|
||||
return cachedSections;
|
||||
}
|
||||
|
||||
|
||||
std::vector<ANSALPR::ImageSection> ANSALPR::createSlideScreens(const cv::Mat& image) {
|
||||
if (image.empty()) {
|
||||
std::cerr << "Error: Empty image!" << std::endl;
|
||||
return cachedSections;
|
||||
}
|
||||
|
||||
cv::Size currentSize(image.cols, image.rows);
|
||||
if (currentSize == previousImageSize) {
|
||||
return cachedSections;
|
||||
}
|
||||
previousImageSize = currentSize;
|
||||
cachedSections.clear();
|
||||
|
||||
int maxSize = std::max(image.cols, image.rows);
|
||||
const int minCellSize = 320;
|
||||
int maxSections = 10;
|
||||
const float minAspectRatio = 0.8f;
|
||||
const float maxAspectRatio = 1.2f;
|
||||
|
||||
if (maxSize <= 640) maxSections = 1;
|
||||
else if (maxSize <= 960) maxSections = 2;
|
||||
else if (maxSize <= 1280) maxSections = 4;
|
||||
else if (maxSize <= 2560) maxSections = 6;
|
||||
else if (maxSize <= 3840) maxSections = 8;
|
||||
else maxSections = 10;
|
||||
|
||||
int width = image.cols;
|
||||
int height = image.rows;
|
||||
int bestRows = 1, bestCols = 1;
|
||||
int bestTileSize = std::numeric_limits<int>::max();
|
||||
|
||||
for (int rows = 1; rows <= maxSections; ++rows) {
|
||||
for (int cols = 1; cols <= maxSections; ++cols) {
|
||||
if (rows * cols > maxSections) continue;
|
||||
|
||||
int tileWidth = (width + cols - 1) / cols;
|
||||
int tileHeight = (height + rows - 1) / rows;
|
||||
|
||||
if (tileWidth < minCellSize || tileHeight < minCellSize)
|
||||
continue;
|
||||
|
||||
float aspectRatio = static_cast<float>(tileWidth) / static_cast<float>(tileHeight);
|
||||
if (aspectRatio < minAspectRatio || aspectRatio > maxAspectRatio)
|
||||
continue;
|
||||
|
||||
int maxTileDim = std::max(tileWidth, tileHeight);
|
||||
if (maxTileDim < bestTileSize) {
|
||||
bestTileSize = maxTileDim;
|
||||
bestRows = rows;
|
||||
bestCols = cols;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Generate tiles using bestRows, bestCols
|
||||
int tileWidth = (width + bestCols - 1) / bestCols;
|
||||
int tileHeight = (height + bestRows - 1) / bestRows;
|
||||
|
||||
int priority = 1;
|
||||
for (int r = bestRows - 1; r >= 0; --r) {
|
||||
for (int c = 0; c < bestCols; ++c) {
|
||||
int x = c * tileWidth;
|
||||
int y = r * tileHeight;
|
||||
|
||||
int w = std::min(tileWidth, width - x);
|
||||
int h = std::min(tileHeight, height - y);
|
||||
|
||||
if (w <= 0 || h <= 0) continue;
|
||||
|
||||
cv::Rect region(x, y, w, h);
|
||||
ImageSection section(region);
|
||||
section.priority = priority++;
|
||||
cachedSections.push_back(section);
|
||||
}
|
||||
}
|
||||
|
||||
return cachedSections;
|
||||
}
|
||||
int ANSALPR::getHighestPriorityRegion() {
|
||||
if (!cachedSections.empty()) {
|
||||
return cachedSections.front().priority; // First element has the highest priority
|
||||
}
|
||||
return 0; // Return empty rect if no sections exist
|
||||
}
|
||||
int ANSALPR::getLowestPriorityRegion() {
|
||||
if (!cachedSections.empty()) {
|
||||
return cachedSections.back().priority; // Last element has the lowest priority
|
||||
}
|
||||
return 0; // Return empty rect if no sections exist
|
||||
}
|
||||
cv::Rect ANSALPR::getRegionByPriority(int priority) {
|
||||
for (const auto& section : cachedSections) {
|
||||
if (section.priority == priority) {
|
||||
return section.region;
|
||||
}
|
||||
}
|
||||
return cv::Rect(); // Return empty rect if priority not found
|
||||
}
|
||||
|
||||
std::vector<Object> ANSALPR::AdjustLicensePlateBoundingBoxes(
|
||||
const std::vector<Object>& detectionsInROI,
|
||||
const cv::Rect& roi,
|
||||
const cv::Size& fullImageSize,
|
||||
float aspectRatio,
|
||||
int padding
|
||||
) {
|
||||
std::vector<Object> adjustedDetections;
|
||||
|
||||
try {
|
||||
// Basic input validation
|
||||
if (detectionsInROI.empty()) {
|
||||
return adjustedDetections;
|
||||
}
|
||||
|
||||
if (roi.width <= 0 || roi.height <= 0 ||
|
||||
fullImageSize.width <= 0 || fullImageSize.height <= 0) {
|
||||
return adjustedDetections;
|
||||
}
|
||||
|
||||
for (const auto& detInROI : detectionsInROI) {
|
||||
try {
|
||||
if (detInROI.box.width <= 0 || detInROI.box.height <= 0)
|
||||
continue; // Skip invalid box
|
||||
|
||||
// Convert ROI-relative box to full-image coordinates
|
||||
cv::Rect detInFullImg;
|
||||
try {
|
||||
detInFullImg = detInROI.box + roi.tl();
|
||||
detInFullImg &= cv::Rect(0, 0, fullImageSize.width, fullImageSize.height);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "[AdjustBBox] Failed to calculate full image box: " << e.what() << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if it touches ROI border
|
||||
bool touchesLeft = detInROI.box.x <= 0;
|
||||
bool touchesRight = detInROI.box.x + detInROI.box.width >= roi.width - 1;
|
||||
bool touchesTop = detInROI.box.y <= 0;
|
||||
bool touchesBottom = detInROI.box.y + detInROI.box.height >= roi.height - 1;
|
||||
bool touchesBorder = touchesLeft || touchesRight || touchesTop || touchesBottom;
|
||||
|
||||
// Compute target width based on aspect ratio
|
||||
int targetWidth = 0, expandWidth = 0;
|
||||
try {
|
||||
targetWidth = std::max(detInFullImg.width, static_cast<int>(detInFullImg.height * aspectRatio));
|
||||
expandWidth = std::max(0, targetWidth - detInFullImg.width);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "[AdjustBBox] Aspect ratio adjustment failed: " << e.what() << std::endl;
|
||||
continue;
|
||||
}
|
||||
|
||||
int expandLeft = expandWidth / 2;
|
||||
int expandRight = expandWidth - expandLeft;
|
||||
int padX = touchesBorder ? padding : 0;
|
||||
int padY = touchesBorder ? (padding / 2) : 0;
|
||||
|
||||
// Apply padded and expanded box
|
||||
int newX = std::max(0, detInFullImg.x - expandLeft - padX);
|
||||
int newY = std::max(0, detInFullImg.y - padY);
|
||||
int newWidth = detInFullImg.width + expandWidth + 2 * padX;
|
||||
int newHeight = detInFullImg.height + 2 * padY;
|
||||
|
||||
// Clamp to image boundaries
|
||||
if (newX + newWidth > fullImageSize.width) {
|
||||
newWidth = fullImageSize.width - newX;
|
||||
}
|
||||
if (newY + newHeight > fullImageSize.height) {
|
||||
newHeight = fullImageSize.height - newY;
|
||||
}
|
||||
|
||||
if (newWidth <= 0 || newHeight <= 0)
|
||||
continue;
|
||||
|
||||
// Construct adjusted object
|
||||
Object adjustedDet = detInROI;
|
||||
adjustedDet.box = cv::Rect(newX, newY, newWidth, newHeight);
|
||||
adjustedDetections.push_back(adjustedDet);
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "[AdjustBBox] Exception per detection: " << e.what() << std::endl;
|
||||
continue;
|
||||
}
|
||||
catch (...) {
|
||||
std::cerr << "[AdjustBBox] Unknown exception per detection." << std::endl;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
std::cerr << "[AdjustBBox] Fatal error: " << e.what() << std::endl;
|
||||
}
|
||||
catch (...) {
|
||||
std::cerr << "[AdjustBBox] Unknown fatal error occurred." << std::endl;
|
||||
}
|
||||
return adjustedDetections;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
#ifndef ANSLPR_H
|
||||
#define ANSLPR_H
|
||||
#define ANSLPR_API __declspec(dllexport)
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include "Utility.h"
|
||||
#include "ANSLicense.h"
|
||||
#include "LabVIEWHeader/extcode.h"
|
||||
#include "ANSEngineCommon.h"
|
||||
#define MAX_ALPR_FRAME 60
|
||||
namespace ANSCENTER
|
||||
{
|
||||
enum Country {
|
||||
VIETNAM = 0,
|
||||
CHINA = 1,
|
||||
AUSTRALIA = 2,
|
||||
USA = 3,
|
||||
INDONESIA = 4
|
||||
};
|
||||
class ALPRChecker {
|
||||
private:
|
||||
int maxFrames;
|
||||
int minVotesToStabilize = 3; // need at least 3 identical reads to lock
|
||||
float iouMatchThreshold = 0.3f; // IoU to match same physical plate across frames
|
||||
std::recursive_mutex _mutex;
|
||||
|
||||
// Per-plate spatial tracking: each tracked plate has a location + text history
|
||||
struct TrackedPlate {
|
||||
cv::Rect lastBox;
|
||||
std::deque<std::string> textHistory; // rolling history of OCR reads
|
||||
std::string lockedText; // majority-vote winner (empty = not locked yet)
|
||||
int lockCount = 0; // how many consecutive frames the lock held
|
||||
int framesSinceLastSeen = 0; // how many checkPlate calls since this plate was last matched
|
||||
};
|
||||
int _pruneCounter = 0; // counts checkPlate calls for periodic pruning
|
||||
// cameraId -> list of tracked plates
|
||||
std::unordered_map<std::string, std::vector<TrackedPlate>> trackedPlates;
|
||||
|
||||
[[nodiscard]] int levenshteinDistance(const std::string& s1, const std::string& s2);
|
||||
[[nodiscard]] float computeIoU(const cv::Rect& a, const cv::Rect& b);
|
||||
[[nodiscard]] std::string majorityVote(const std::deque<std::string>& history);
|
||||
public:
|
||||
void Init(int framesToStore = MAX_ALPR_FRAME);
|
||||
ALPRChecker(int framesToStore = MAX_ALPR_FRAME) : maxFrames(framesToStore) {}
|
||||
// Original API (backward compatible) — no spatial tracking
|
||||
[[nodiscard]] std::string checkPlate(const std::string& cameraId, const std::string& detectedPlate);
|
||||
// Enhanced API with bounding box for spatial plate tracking
|
||||
[[nodiscard]] std::string checkPlate(const std::string& cameraId, const std::string& detectedPlate, const cv::Rect& plateBox);
|
||||
};
|
||||
|
||||
class ANSLPR_API ANSALPR {
|
||||
|
||||
protected:
|
||||
SPDLogger& _logger = SPDLogger::GetInstance("ALPR", false);
|
||||
bool _debugFlag{ false };
|
||||
bool _licenseValid{ false };
|
||||
bool _isInitialized{ false};
|
||||
double _detectorThreshold{ 0.5 };
|
||||
double _ocrThreshold{ 0.5 };
|
||||
double _colorThreshold{ 0.0 };// If we use the colour information, this threshold should be greater than 0.0
|
||||
std::vector<std::string> _plateFormats;
|
||||
std::string _licenseKey{};
|
||||
std::string _modelFolder;
|
||||
struct ImageSection {
|
||||
cv::Rect region;
|
||||
int priority;
|
||||
ImageSection(const cv::Rect& r) : region(r), priority(0) {}
|
||||
};
|
||||
cv::Size previousImageSize = cv::Size(0, 0);
|
||||
std::vector<ImageSection> cachedSections;
|
||||
int _currentPriority{ 0 }; // None
|
||||
cv::Rect _detectedArea;// Area where license plate are detected
|
||||
Country _country;
|
||||
std::recursive_mutex _mutex;
|
||||
public:
|
||||
[[nodiscard]] virtual bool Initialize(const std::string& licenseKey, const std::string& modelZipFilePath, const std::string& modelZipPassword, double detectorThreshold, double ocrThreshold, double colourTheshold=0);
|
||||
[[nodiscard]] virtual bool LoadEngine();
|
||||
[[nodiscard]] virtual bool Inference(const cv::Mat& input, std::string& lprResult) = 0;
|
||||
[[nodiscard]] virtual bool Inference(const cv::Mat& input, std::string& lprResult,const std::string &cameraId) = 0;
|
||||
[[nodiscard]] virtual bool Inference(const cv::Mat& input, const std::vector<cv::Rect> & Bbox, std::string& lprResult) = 0;
|
||||
[[nodiscard]] virtual bool Inference(const cv::Mat& input, const std::vector<cv::Rect> & Bbox, std::string& lprResult,const std::string & cameraId) = 0;
|
||||
[[nodiscard]] virtual std::vector<Object> RunInference(const cv::Mat& input, const std::string &cameraId) = 0;
|
||||
[[nodiscard]] std::string VectorDetectionToJsonString(const std::vector<Object>& dets);
|
||||
void SetPlateFormats(const std::vector<std::string>& formats);
|
||||
void SetPlateFormat(const std::string& format);
|
||||
[[nodiscard]] std::vector<std::string> GetPlateFormats() const;
|
||||
virtual ~ANSALPR();
|
||||
void CheckLicense();
|
||||
|
||||
/// Enable/disable deep pipeline benchmarking.
|
||||
/// When enabled, logs per-stage timing for the full ALPR pipeline
|
||||
/// (LP detection, OCR, color classification, validation, serialization).
|
||||
/// Also propagates the flag to sub-detectors (_lpDetector, _ocrDetector, etc.).
|
||||
virtual void ActivateDebugger(bool debugFlag) { _debugFlag = debugFlag; }
|
||||
[[nodiscard]] virtual bool Destroy() = 0;
|
||||
[[nodiscard]] static std::vector<cv::Rect> GetBoundingBoxes(const std::string& strBBoxes);
|
||||
[[nodiscard]] static std::string PolygonToString(const std::vector<cv::Point2f>& polygon);
|
||||
[[nodiscard]] static std::string KeypointsToString(const std::vector<float>& kps);
|
||||
[[nodiscard]] static std::vector<cv::Point2f> RectToNormalizedPolygon(const cv::Rect& rect, float imageWidth, float imageHeight);
|
||||
protected:
|
||||
// Utilities for scanning license plate
|
||||
// Function to seperate screen size
|
||||
[[nodiscard]] double calculateDistanceToCenter(const cv::Point& center, const cv::Rect& rect);
|
||||
[[nodiscard]] std::vector<ImageSection> divideImage(const cv::Mat& image);
|
||||
[[nodiscard]] std::vector<ANSALPR::ImageSection> createSlideScreens(const cv::Mat& image);
|
||||
[[nodiscard]] int getHighestPriorityRegion();
|
||||
[[nodiscard]] int getLowestPriorityRegion();
|
||||
[[nodiscard]] cv::Rect getRegionByPriority(int priority);
|
||||
[[nodiscard]] std::vector<Object> AdjustLicensePlateBoundingBoxes(const std::vector<Object>& detectionsInROI,
|
||||
const cv::Rect& roi, const cv::Size& fullImageSize,
|
||||
float aspectRatio = 2.0f, // width at least 2x height
|
||||
int padding = 10 // base padding
|
||||
);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
extern "C" ANSLPR_API int CreateANSALPRHandle(ANSCENTER::ANSALPR **Handle, const char* licenseKey, const char* modelZipFilePath, const char* modelZipPassword, int engineType, double detectorThreshold, double ocrThreshold, double colourThreshold);
|
||||
extern "C" ANSLPR_API int LoadANSALPREngineHandle(ANSCENTER::ANSALPR** Handle);
|
||||
|
||||
extern "C" ANSLPR_API std::string ANSALPR_RunInference(ANSCENTER::ANSALPR **Handle, unsigned char* jpeg_string, unsigned int bufferLength);
|
||||
extern "C" ANSLPR_API std::string ANSALPR_RunInferenceWithCamID(ANSCENTER::ANSALPR** Handle, unsigned char* jpeg_string, unsigned int bufferLength, const char* cameraId);
|
||||
extern "C" ANSLPR_API std::string ANSALPR_RunInferenceBinary(ANSCENTER::ANSALPR * *Handle, unsigned char* jpeg_bytes, unsigned int width, unsigned int height);
|
||||
extern "C" ANSLPR_API std::string ANSALPR_RunInferenceBinaryInCroppedImages(ANSCENTER::ANSALPR * *Handle, unsigned char* jpeg_bytes, unsigned int width, unsigned int height, const char* strBboxes);
|
||||
extern "C" ANSLPR_API std::string ANSALPR_RunInferenceInCroppedImages(ANSCENTER::ANSALPR** Handle, unsigned char* jpeg_string, unsigned int bufferLength, const char* strBboxes);
|
||||
extern "C" ANSLPR_API std::string ANSALPR_RunInferenceInCroppedImagesWithCamID(ANSCENTER::ANSALPR** Handle, unsigned char* jpeg_string, unsigned int bufferLength, const char* strBboxes, const char* cameraId);
|
||||
|
||||
extern "C" ANSLPR_API int ReleaseANSALPRHandle(ANSCENTER::ANSALPR **Handle);
|
||||
//// For LabVIEW API
|
||||
extern "C" ANSLPR_API int ANSALPR_RunInference_LV(ANSCENTER::ANSALPR **Handle, unsigned char* jpeg_string, unsigned int bufferLength, LStrHandle detectionResult);
|
||||
extern "C" ANSLPR_API int ANSALPR_RunInferenceWithCamID_LV(ANSCENTER::ANSALPR** Handle, unsigned char* jpeg_string, unsigned int bufferLength, const char* cameraId, LStrHandle detectionResult);
|
||||
extern "C" ANSLPR_API int ANSALPR_RunInferenceBinary_LV(ANSCENTER::ANSALPR * *Handle, unsigned char* jpeg_bytes, unsigned int width, unsigned int height, LStrHandle detectionResult);
|
||||
extern "C" ANSLPR_API int ANSALPR_RunInferenceInCroppedImages_LV(ANSCENTER::ANSALPR * *Handle, unsigned char* jpeg_string, unsigned int bufferLength, const char* strBboxes, LStrHandle detectionResult);
|
||||
extern "C" ANSLPR_API int ANSALPR_RunInferenceInCroppedImagesWithCamID_LV(ANSCENTER::ANSALPR** Handle, unsigned char* jpeg_string, unsigned int bufferLength, const char* strBboxes, const char* cameraId, LStrHandle detectionResult);
|
||||
extern "C" ANSLPR_API int ANSALPR_RunInferenceComplete_LV(ANSCENTER::ANSALPR** Handle, cv::Mat** cvImage, const char* cameraId, int getJpegString, int jpegImageSize, LStrHandle detectionResult, LStrHandle imageStr);
|
||||
extern "C" ANSLPR_API int ANSALPR_RunInferenceComplete_CPP(ANSCENTER::ANSALPR** Handle, cv::Mat** cvImage, const char* cameraId, int getJpegString, int jpegImageSize,std::string& detectionResult, std::string& imageStr);
|
||||
extern "C" ANSLPR_API int ANSALPR_RunInferencesComplete_LV(ANSCENTER::ANSALPR** Handle, cv::Mat** cvImage, const char* cameraId, int maxImageSize,const char* strBboxes, LStrHandle detectionResult);
|
||||
|
||||
// Get/Set format
|
||||
extern "C" ANSLPR_API int ANSALPR_SetFormat(ANSCENTER::ANSALPR** Handle, const char* format);
|
||||
extern "C" ANSLPR_API int ANSALPR_SetFormats(ANSCENTER::ANSALPR** Handle, const char* formats);// comma separated formats
|
||||
extern "C" ANSLPR_API int ANSALPR_GetFormats(ANSCENTER::ANSALPR** Handle, LStrHandle formats);// comma separated formats
|
||||
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,188 @@
|
||||
#ifndef ANSLPRCPU_H
|
||||
#define ANSLPRCPU_H
|
||||
#pragma once
|
||||
#include "ANSLPR.h"
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <include/paddleocr.h>
|
||||
#include "openvino/openvino.hpp"
|
||||
#include "openvino/runtime/intel_gpu/properties.hpp"
|
||||
|
||||
namespace ANSCENTER
|
||||
{
|
||||
|
||||
class ANSOVDetector { // OpenVINO Yolov8 detector
|
||||
public:
|
||||
[[nodiscard]] bool Initialize(const std::string& modelFilePath, const std::string& classFilePath, std::vector <std::string>& _classes);
|
||||
[[nodiscard]] std::vector<Object> RunInference(const cv::Mat& input);
|
||||
private:
|
||||
std::string _modelFilePath;
|
||||
std::string _classFilePath;
|
||||
float _detectionScoreThreshold{ 0.5 };
|
||||
float _modelConfThreshold{ 0.48 };
|
||||
float _modelMNSThreshold{ 0.5 };
|
||||
bool _isInitialized{ false };
|
||||
private:
|
||||
void PreprocessImage(cv::Mat& frame);
|
||||
[[nodiscard]] cv::Rect GetBoundingBox(const cv::Rect& src);
|
||||
[[nodiscard]] std::vector<Object>PostProcessing();
|
||||
[[nodiscard]] std::vector <std::string> LoadClassesFromFile();
|
||||
cv::Mat resized_frame_;
|
||||
cv::Point2f factor_;
|
||||
cv::Size2f model_input_shape_;
|
||||
cv::Size model_output_shape_;
|
||||
ov::Tensor input_tensor_;
|
||||
ov::InferRequest inference_request_;
|
||||
ov::CompiledModel compiled_model_;
|
||||
ov::Core _core;
|
||||
[[nodiscard]] std::string GetOpenVINODevice();
|
||||
std::recursive_mutex _mutex;
|
||||
};
|
||||
class ANSLPR_API ANSALPR_CPU :public ANSALPR {
|
||||
private:
|
||||
ANSCENTER::EngineType engineType;
|
||||
|
||||
ANSCENTER::ModelConfig _lpdmodelConfig;
|
||||
ANSCENTER::ModelConfig _ocrModelConfig;
|
||||
ANSCENTER::ModelConfig _lpColourModelConfig;
|
||||
|
||||
std::string _lpdLabels;
|
||||
std::string _ocrLabels;
|
||||
std::string _lpColourLabels;
|
||||
cv::Mat _frameBuffer; // Reusable buffer for color conversion
|
||||
std::string runOCROnPlate(const cv::Mat& frame, const cv::Rect& plateRect);
|
||||
std::unique_ptr<ANSCENTER::ANSODBase> _lprDetector = nullptr;
|
||||
//std::unique_ptr<ANSCENTER::ANSODBase> _lpColourDetector = nullptr; // License plate colour classifier
|
||||
|
||||
std::vector <std::string> _lprModelClass; //model that recognise license plate
|
||||
Country _country;
|
||||
std::recursive_mutex _mutex;
|
||||
std::vector<std::string> ValidVNMotobikeList = { "29BA", "29BB", "29BC", "29BD", "29BE",
|
||||
"29BF", "29BG", "29BH", "29BK", "29BL",
|
||||
"29BM", "29BN", "29BP", "29AA", "29AD",
|
||||
"29AE", "29AF", "29AG", "29AH", "29AK",
|
||||
"29AL", "29AM", "29AN", "29AP", "29AS",
|
||||
"29AT", "29AU", "29AV", "29AX", "29AY","29AZ","29AB", "29AC"//HN
|
||||
"59AA", "59TA", "59FA", "59CA", "59HA",
|
||||
"59KA", "59CB", "59LA", "59UA", "59MA",
|
||||
"59GA", "59DB", "59PA", "59NA", "59EA",
|
||||
"59SA", "59VA", "59YA", "59YB", "59ZA",
|
||||
"59ZB", "59NB", "59XB", "59BA", "59XA",
|
||||
"59XB", "59FA", //HCM
|
||||
"43AA", "43AC", "43AD", "43AE", "43AF", "43AG", "43AB",// Da Nang
|
||||
"67AA", "67AD", "67AE", "67AH", "67AB",
|
||||
"67AF", "67AK", "67AL", "67AM", "67AN",// An Giang
|
||||
"94AB", "94AE", "94AF", "94AH", "94AK","94FD","94FE",// Bac Lieu
|
||||
"98AA", "98AH", "98AB", "98AC", "98AD",
|
||||
"98AE", "98AF", "98AG", "98AK", "98AL",// Bac Giang
|
||||
"71AA", "71AF", "71AG", "71AH", "71AK",
|
||||
"71AB", "71AC", "71AD", "71AE", // Ben Tre
|
||||
"77AA", "77AE", "77AB", "77AH",
|
||||
"77AK", "77AC", "77AD", "77AF",
|
||||
"77AG", "77AN", "77AM", // Binh Dinh
|
||||
"69AA", "69AB", "69AC", "69AD", "69AE",
|
||||
"69AF", "69KA", "69AK", "69AL", "69AM", // Ca Mau
|
||||
"65MA", "65AA", "65CA", "65DB", "65EA",
|
||||
"65FA", "65GA", "65HA", "65KA", // Can Tho
|
||||
"76AA", "76AU", "76AV", "76AH", "76AB",
|
||||
"76AC", "76AD", "76AE", "76AF", "76AK",
|
||||
"76AL", "76AM", "76AN", "76AP", "76AS","76AT",// Quang Ngai
|
||||
"84HA", "66PA", "84AE", "88AB", "68MA",
|
||||
"63AB", "84AF", "66HA", "60AD", "47AG", "74AE",
|
||||
"84AH", "76AA", "21BA", "68HA", "68AA", "28FN",
|
||||
"49AL", "12HA", "66MA", "15AA", "15AC", "83VK",
|
||||
"95EA", "64AA", "12FA", "21AA", "83YM", "38AG",
|
||||
"68TA", "63AE", "79NA", "12ZA", "20AC", "64EA",
|
||||
"66VA", "83CA", "21FA", "68KB", "74AB", "63AK",
|
||||
"23AB", "47AK", "60AM", "23AE", "74AK", "84CA",
|
||||
"47AV", "26AA", "28FC", "21LA", "76AS", "68FA",
|
||||
"21GA", "47AH", "47AU", "23AC", "63AN", "21KA",
|
||||
"49AD", "74AH", "20AK", "26AC", "74AF", "76AC",
|
||||
"68NA", "60AE", "22LA", "12XA", "15AH", "26AD",
|
||||
"23AA", "83EA", "47AA", "38AA", "26AB", "60AB",
|
||||
"60AF", "38AE", "28FD", "66BA", "68CA", "20AD",
|
||||
"21EA", "38AS", "60AA", "88AC", "64BA", "22AB",
|
||||
"49AG", "38AK", "47AS", "64AC", "47AD", "68SA",
|
||||
"12BA", "15AE", "28FM", "26AM", "79ZA", "66FA",
|
||||
"63AH", "64DA", "47AM", "68UA", "26AG", "84AC",
|
||||
"66SA", "76AD", "49AN", "26AF", "79XA", "28FS",
|
||||
"49AA", "63AS", "84BA", "12SA", "83DB", "60AL",
|
||||
"21HA", "38AF", "22AA", "63AF", "49AE", "47AT",
|
||||
"23AD", "84AD", "23AH", "79CA", "60AH", "66GA",
|
||||
"26AK", "21CA", "38AB", "28FE", "26AP", "47AP",
|
||||
"47AE", "76AV", "49AC", "38AN", "12PA", "76AE",
|
||||
"64FA", "12VA", "22FA", "79DA", "95NA", "76AP",
|
||||
"66NA", "47AB", "74AG", "60AC", "68GA", "63AC",
|
||||
"47AN", "22YA", "68PA", "47AC", "83GE", "26AL",
|
||||
"64DB", "47AL", "83ZY", "60AN", "64GA", "84AL",
|
||||
"49AK", "28FB", "20AE", "20AG", "76AU", "66KA",
|
||||
"64CA", "76AK", "23AV", "38AX", "79HA", "47AX",
|
||||
"49AM", "83PT", "15AF", "95AA", "38AM", "76AT",
|
||||
"47AF", "74AC", "20AB", "64HA", "12UA", "22SA",
|
||||
"68BA", "49AH", "23AK", "20AF", "63AD", "60AK",
|
||||
"28FZ", "28FL", "76AB", "49AF", "26AH", "63AL",
|
||||
"74AA", "79DB", "22NA", "23AU", "23AY", "83TG",
|
||||
"63AM", "64KA", "23AM", "74AD", "79VA", "95XA",
|
||||
"28FF", "68KA", "49AP", "83XL", "83MF", "64HB",
|
||||
"15AN", "15AK", "76AF", "22TA", "12LA", "38AL",
|
||||
"95HA", "68EA", "84AA", "66CA", "26AE", "66LA",
|
||||
"76AN", "23AX", "95RA", "68B", "68LA", "20AH",
|
||||
"20AA", "76AH", "38AC", "12TA", "76AM",
|
||||
"59C3", "29S7", "29V7", "29L2", "72K1", "72D1", "98AF",
|
||||
"59D2", "59S1", "59K2", "67K1", "59C4", "98AG", "59E2",
|
||||
"67H2", "94L1", "29S1", "59E1", "59X4", "94B2", "67L1",
|
||||
"29E1", "72C1", "29V3", "67M1", "29X7", "29X1", "98AD",
|
||||
"59N3", "59C1", "59D3", "59P1", "67F1", "59M2", "59K1",
|
||||
"98AB", "59T2", "72E1", "59V1", "29N2", "29E2", "67D2",
|
||||
"72E2", "29T2", "29V5", "59L3", "29S2", "59L2", "72G1",
|
||||
"59G2", "30Z1", "67N1", "98B1", "59U1", "59D1", "72C2",
|
||||
"98B2", "98M1", "29P1", "67M2", "98H1", "29V1", "29K2",
|
||||
"59V2", "59F2", "59N1", "29Z1", "98D1", "59C2", "94K1",
|
||||
"67G1", "98AL", "29Y5", "59B1", "41B1", "94G1", "59G1",
|
||||
"94M1", "59X2", "67C1", "67D1", "59X3", "67B1", "98AC",
|
||||
"67C2", "98B3", "59Z1", "59Z2", "98AH", "59S3", "94F1",
|
||||
"98L1", "59H2", "98D2", "29D1", "59M1", "29B2", "29D2",
|
||||
"29U1", "59U2", "29H2", "29K1", "94B1", "98C1", "59F1",
|
||||
"67H1", "29F1", "29Y1", "59P2", "67L2", "59X1", "98G1",
|
||||
"29C1", "67B2", "59T1", "29G2", "29L5", "29S6", "29Y7",
|
||||
"59Y2", "67E1", "98AK", "94C1", "29X2", "98K1", "98AE",
|
||||
"98AA", "29H1", "29T1", "67K2", "94D2", "94E1", "29M1",
|
||||
"29N1", "59Y1", "72H1", "29X3", "98F1", "29C2", "94K2",
|
||||
"72F1", "29M2", "29Y3", "59P3", "29E3", "29G1", "59V3",
|
||||
"59Y3", "29P2", "59N2", "29L1", "98E1", "59G3", "29X5",
|
||||
"59S2", "94D1", "59H1", "29B1", "59L1", "50X1", "72L1",
|
||||
"43B1", "68L1", "70G1", "36M1", "81N1", "90K1", "17B1", "64E1", "99D1", "60B2", "74L1", "60C1", "68M1", "63B7", "34B1", "69M1", "24B1", "15M1", "83Y1", "48C1", "95H1", "79X1", "17B6", "36E1", "38K1", "25N1", "25U1", "61B1", "36C1", "36B3", "38F1", "99G1", "69N1", "97D1", "92T1", "92B1", "88B1", "97G1", "14U1", "63A1", "26N1", "19D1", "93C1", "73B1", "84B1", "81K1", "18L1", "64D1", "35M1", "61N1", "83P1", "15S1", "82B1", "92U1", "43D1", "22L1", "63B5", "64G1", "27N1", "14X1", "62C1", "81D1", "38G1", "19F1", "34K1", "49P1", "89H1", "14T1", "19M1", "78D1", "76A1", "66K1", "66C1", "71C1", "37K1", "19G1", "15F1", "85C1", "49B1", "21B1", "89F1", "23M1", "66L1", "90B5", "93M1", "14P1", "77N1", "36B8", "86B1", "12U1", "63B3", "21L1", "36G5", "65G1", "82E1", "61H1", "65H1", "84A1", "23F1", "95C1", "99K1", "49G1", "92D1", "36K3", "92N1", "82X1", "83M1", "11N1", "14K1", "19H1", "93H1", "60A1", "79A1", "20D1", "90D1", "81C1", "66P1", "36K1", "92V1", "18B1", "37P1", "22Y1", "23H1", "26D1", "66G1", "78F1", "49C1", "26H1", "38P1", "47T1", "74H1", "63P1", "47D1", "15D1", "23D1", "68E1", "20B1", "49F1", "43K1", "65K1", "27Z1", "92S1", "79H1", "21E1", "35Y1", "14S1", "75E1", "24Y1", "12T1", "27P1", "77B1", "88H1", "60B3", "23P1", "61F1", "99H1", "23K1", "59A3", "26C1", "81B1", "74E1", "66B1", "22S1", "92P1", "93B1", "69B1", "81P1", "12H1", "62K1", "35A1", "77C1", "27V1", "68N1", "12D1", "64K1", "41A1", "12Z1", "76C1", "38B1", "78G1", "74K1", "69H1", "94A1", "61K1", "86B7", "82G1", "14N1", "82M1", "76E1", "18E1", "61C1", "15N1", "90A1", "77F1", "34D1", "47B1", "62S1", "43E1", "81M1", "92X1", "75B1", "34F1", "70H1", "62B1", "26B1", "60B4", "61A1", "12B1", "90T1", "92E1", "34C1", "47G1", "97B1", "25S1", "70E1", "93Y1", "47S1", "37F1", "28N1", "11K1", "38E1", "78M1", "74C1", "12S1", "75S1", "37A1", "28D1", "65L1", "22B1", "99B1", "74G1", "79K1", "76K1", "76H1", "23B1", "15R1", "36B1", "74D1", "62L1", "37E1", "78E1", "89K1", "26M1", "25F1", "48H1", "79D1", "43H1", "76F1", "36L1", "43L1", "21K1", "88L1", "27S1", "92K1", "77D1", "19N1", "66H1", "36H5", "62N1", "18G1", "75D1", "37L1", "68K1", "28C1", "26E1", "35N1", "85H1", "62D1", "27U1", "19E1", "99E1", "14Y1", "49L1", "66M1", "73F1", "70K1", "36F5", "97H1", "93E1", "68P1", "43F1", "48G1", "75K1", "62U1", "86B9", "65F1", "27L1", "70L1", "63B8", "78L1", "11Z1", "68C1", "18D1", "15L1", "99C1", "49E1", "84E1", "69E1", "38A1", "48D1", "68S1", "81E1", "84K1", "63B6", "24T1", "95A1", "86B4", "34M1", "84L1", "24V1", "14M1", "36H1", "15B1", "69F1", "47E1", "38H1", "88D1", "28E1", "60C2", "63B9", "75Y1", "21D1", "35H1", "68F1", "86B5", "15H1", "36B5", "83X1", "17B7", "12V1", "86B8", "95E1", "63B2", "74F1", "86C1", "48K1", "89M1", "85D1", "71C4", "34E1", "97C1", "88E1", "81F1", "60B5", "84M1", "92H1", "28L1", "34H1", "38X1", "82L1", "61E1", "82F1", "62P1", "93F1", "65B1", "93L1", "95B1", "15P1", "77G1", "28M1", "35B1", "68G1", "36C2", "68D1", "69K1", "14L1", "36M3", "24X1", "24Z1", "86A1", "88C1", "15E1", "77E1", "83E1", "47L1", "25T1", "89C1", "71C3", "49D1", "36L6", "48F1", "36B6", "34P1", "84D1", "15C1", "38M1", "85F1", "77K1", "86B3", "74B1", "78H1", "89G1", "64A2", "15K1", "85B1", "49K1", "21H1", "73C1", "47U1", "65E1", "18C1", "69D1", "63B1", "95G1", "19L1", "20G1", "76D1", "29A1", "68T1", "75L1", "12L1", "89L1", "37C1", "27B1", "19C1", "11H1", "81X1", "70B1", "11V1", "43G1", "22A1", "83C1", "75C1", "79C1", "22F1", "92F1", "81G1", "81T1", "28H1", "66N1", "71B1", "18H1", "76P1", "26F1", "81U1", "34N1", "64F1", "76N1", "24S1", "26P1", "63B4", "35T1", "36N1", "47F1", "81L1", "61G1", "77M1", "34G1", "26G1", "97F1", "62H1", "28F1", "62T1", "93G1", "73D1", "65A1", "47P1", "74P1", "82N1", "20E1", "36D1", "60B1", "49M1", "37H1", "37M1", "38D1", "84F1", "88F1", "36B2", "65C1", "92M1", "86B6", "75H1", "38L1", "20C1", "97E1", "85E1", "38N1", "26K1", "89B1", "99F1", "28B1", "34L1", "86B2", "66F1", "77L1", "27Y1", "68H1", "37D1", "92L1", "82K1", "99A1", "69L1", "76M1", "90B4", "48B1", "95D1", "20H1", "64H1", "79Z1", "92G1", "23G1", "21G1", "37G1", "35K1", "81H1", "83Z1", "76T1", "36F1", "36B4", "14B9", "47K1", "20K1", "62M1", "84H1", "62F1", "74A1", "18A1", "73H1", "37N1", "79N1", "61D1", "11P1", "15G1", "47N1", "19K1", "71C2", "81S1", "11M1", "60B7", "60B8", "62G1", "71A1", "24P1", "69A1", "38C1", "49N1", "21C1", "84G1", "37B1", "72A1", "88K1", "88G1", "83V1", "78C1", "73K1", "78K1", "73E189D1", "67A1", "27X1", "62A1", "18K1", "70F1", "36K5", "19B1", "49H1", "66S1", "12P1"};
|
||||
ALPRChecker alprChecker;
|
||||
std::vector<std::string> ValidVNCarList = { "94H", "49F", "93A", "20F", "81H", "95R", "38R", "29F", "81F", "28G", "19A", "85B", "2", "43H", "51L", "28C", "21A", "51D", "50F", "24H", "93R", "92H", "71G", "75H", "86G", "30L", "79A", "82B", "79H", "78C", "61E", "70A", "90C", "72G", "34B", "17E", "18E", "78A", "37F", "51E", "71A", "28F", "47E", "83D", "81B", "84C", "71H", "76G", "92E", "36A", "69R", "30M", "27R", "71D", "19B", "34E", "38K", "88G", "68G", "30E", "68E", "25F", "74D", "98K", "89H", "36R", "84D", "61F", "49G", "25H", "17F", "14R", "36H", "47G", "90A", "68A", "83C", "26B", "15B", "61C", "15K", "47H", "78E", "75D", "15C", "63E", "34C", "36F", "38G", "15E", "93F", "22G", "60B", "94D", "62R", "24D", "11R", "12A", "76A", "94C", "97R", "24E", "26A", "15F", "72A", "49H", "62D", "98C", "71B", "61A", "12C", "27A", "78R", "51M", "69E", "76D", "78F", "49R", "81A", "64F", "29D", "18A", "19F", "21E", "92A", "65G", "86E", "62G", "61K", "47A", "23R", "14F", "95D", "36B", "74R", "11H", "24C", "11G", "66D", "63A", "43R", "70F", "86B", "61G", "47M", "67C", "37D", "43G", "14H", "90F", "51G", "86A", "11E", "29K", "85C", "83F", "24B", "98R", "19E", "61B", "90D", "82G", "14K", "74G", "72D", "85A", "19C", "37G", "98E", "74F", "28H", "90E", "89D", "35R", "97H", "83H", "95A", "20C", "65E", "15R", "73C", "37A", "38E", "77G", "94B", "17A", "75R", "98F", "65R", "76R", "20B", "24G", "25B", "73G", "62F", "29G", "77C", "22H", "14D", "23F", "93C", "19R", "15D", "47R", "79D", "60G", "77A", "82C", "63G", "21H", "81E", "25D", "12D", "37R", "36K", "84F", "98G", "28B", "51N", "18F", "50R", "74C", "35C", "30G", "64A", "95F", "18C", "99G", "99B", "37C", "76H", "60K", "67R", "75A", "83R", "28E", "65F", "17D", "92G", "23C", "60R", "90R", "38A", "43D", "50H", "43C", "77H", "47B", "89F", "82F", "65H", "89E", "62C", "24R", "26G", "84E", "17C", "65B", "34A", "12B", "64R", "29H", "71C", "88D", "79F", "76C", "98A", "69H", "22B", "29A", "72R", "67H", "48C", "22D", "60C", "35H", "38H", "63P", "70D", "49D", "18H", "89A", "72E", "92D", "26H", "73R", "85G", "20E", "98H", "69C", "18B", "73B", "22E", "34G", "30K", "20D", "50A", "34D", "15H", "34H", "71E", "62E", "64C", "51R", "82D", "99E", "70R", "18D", "92F", "94R", "24A", "85H", "11C", "73E", "95E", "86C", "94F", "86R", "37K", "23B", "20H", "73D", "95H", "35A", "89B", "82H", "67F", "70H", "97F", "29E", "97A", "51K", "68D", "37B", "82E", "18R", "86H", "35B", "43E", "35F", "95B", "70E", "21D", "27F", "36E", "63D", "68C", "50E", "36G", "75F", "21G", "29B", "93B", "22A", "18G", "43F", "93G", "62A", "83B", "28D", "75C", "22C", "21R", "25E", "23G", "97C", "75E", "79E", "19H", "47K", "65C", "35E", "20R", "68B", "89R", "67A", "75G", "81R", "78B", "77D", "78G", "20K", "36D", "66C", "38F", "27G", "19D", "67B", "84G", "22F", "61D", "20G", "48A", "76F", "48H", "92B", "85R", "26C", "65A", "70B", "38D", "14C", "66A", "73A", "49C", "74E", "68R", "66B", "74A", "49E", "17B", "69D", "51C", "85F", "21F", "99C", "17G", "72H", "94E", "51F", "92R", "60H", "21B", "93D", "19G", "86F", "51A", "66R", "72B", "26D", "64E", "93H", "12H", "97E", "60E", "82A", "60A", "83E", "27D", "64B", "11B", "11D", "76B", "95G", "14A", "61R", "21C", "30F", "23H", "89C", "97G", "62B", "63R", "88B", "98B", "90B", "67G", "69F", "73H", "20A", "72C", "65D", "68H", "51H", "79G", "70C", "90G", "66G", "83A", "77F", "63B", "64G", "25A", "88E", "68F", "99D", "26E", "94A", "48F", "34R", "61H", "90H", "74B", "14G", "12F", "15A", "27E", "69A", "35D", "12E", "85E", "25C", "29M", "89G", "17R", "78D", "84R", "95C", "15G", "28R", "99A", "69G", "48D", "97D", "27C", "78H", "14E", "79R", "73F", "88A", "48E", "48B", "64H", "99R", "14B", "77R", "75B", "88F", "84B", "11A", "67E", "12R", "50M", "11F", "79C", "49A", "43A", "88R", "77E", "48G", "51B", "81D", "74H", "93E", "37H", "88C", "71F", "94G", "38C", "29C", "43B", "30H", "81G", "28A", "26R", "66H", "66E", "17H", "79B", "49B", "63C", "98D", "81C", "69B", "63H", "85D", "26F", "22R", "83G", "37E", "12G", "77B", "35G", "62H", "60D", "60F", "99H", "70G", "76E", "84A", "72F", "25R", "27B", "30A", "47F", "34F", "97B", "23E", "36C", "66F", "48R", "92C", "71R", "23A", "50G", "47C", "82R", "63F", "84H", "38B", "47D", "67D", "25G", "86D", "88H", "64D", "24F", "23D", "99F" };
|
||||
std::unique_ptr<PaddleOCR::PPOCR> ppocr = std::make_unique<PaddleOCR::PPOCR>();
|
||||
[[nodiscard]] std::string AnalyseLicensePlateText(const std::string& ocrText);
|
||||
[[nodiscard]] char convertDigitToLetter(char c);
|
||||
[[nodiscard]] char convertLetterToDigit(char c);
|
||||
[[nodiscard]] char fixLPDigit(char c);
|
||||
[[nodiscard]] int findSubstringIndex(const std::string& str);
|
||||
[[nodiscard]] std::string convertStringToDigits(const std::string& input);
|
||||
[[nodiscard]] std::string convertStringToLetters(const std::string& input);
|
||||
[[nodiscard]] int searchDiplomacyLP(const std::string& input);
|
||||
[[nodiscard]] bool ValidateVNMotobikeLP(const std::string& input);
|
||||
[[nodiscard]] bool ValidateVNCarLP(const std::string& input);
|
||||
bool valid;
|
||||
cv::Mat alignPlateForOCR(const cv::Mat& fullImage, const cv::Rect& bbox);
|
||||
cv::Mat enhanceForOCR(const cv::Mat& plateROI);
|
||||
public:
|
||||
ANSALPR_CPU();
|
||||
~ANSALPR_CPU();
|
||||
[[nodiscard]] bool Initialize(const std::string& licenseKey, const std::string& modelZipFilePath, const std::string& modelZipPassword, double detectorThreshold, double ocrThreshold, double colourThreshold) override;
|
||||
[[nodiscard]] bool LoadEngine() override;
|
||||
[[nodiscard]] bool Inference(const cv::Mat& input, std::string& lprResult) override;
|
||||
[[nodiscard]] bool Inference(const cv::Mat& input, std::string& lprResult, const std::string & cameraId) override;
|
||||
[[nodiscard]] bool Inference(const cv::Mat& input, const std::vector<cv::Rect> & Bbox, std::string& lprResult) override;
|
||||
[[nodiscard]] bool Inference(const cv::Mat& input, const std::vector<cv::Rect> & Bbox, std::string& lprResult, const std::string & cameraId) override;
|
||||
[[nodiscard]] std::vector<Object> RunInference(const cv::Mat& input, const std::string & cameraId) override;
|
||||
//std::string VectorDetectionToJsonString(const std::vector<Object>& dets);
|
||||
[[nodiscard]] bool Destroy() override;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,191 @@
|
||||
#ifndef ANSLPROD_H
|
||||
#define ANSLPROD_H
|
||||
#pragma once
|
||||
#include "ANSLPR.h"
|
||||
#include "NV12PreprocessHelper.h"
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
|
||||
namespace ANSCENTER
|
||||
{
|
||||
class ANSLPR_API ANSALPR_OD :public ANSALPR {
|
||||
private:
|
||||
ANSCENTER::EngineType engineType;
|
||||
std::unique_ptr<ANSCENTER::ANSODBase>_lpDetector = nullptr; // License plate detector
|
||||
std::unique_ptr<ANSCENTER::ANSODBase>_ocrDetector = nullptr; // OCR detector
|
||||
std::unique_ptr<ANSCENTER::ANSODBase>_lpColourDetector = nullptr; // License plate colour classifier
|
||||
|
||||
ANSCENTER::ModelConfig _lpdmodelConfig;
|
||||
ANSCENTER::ModelConfig _ocrModelConfig;
|
||||
ANSCENTER::ModelConfig _lpColourModelConfig;
|
||||
cv::Ptr<cv::CLAHE> _clahe; // Reusable CLAHE instance
|
||||
ANSCENTER::NV12PreprocessHelper _nv12Helper; // NV12 crop for high-res plate OCR
|
||||
|
||||
std::string _lpdLabels;
|
||||
std::string _ocrLabels;
|
||||
std::string _lpColourLabels;
|
||||
cv::Mat _frameBuffer; // Reusable buffer for color conversion
|
||||
cv::Mat enhanceAndDebug(const cv::Mat& roi);
|
||||
std::vector <std::string> _lprModelClass; //model that recognise license plate
|
||||
#ifdef FNS_DEBUG
|
||||
void showDebugComparison(const cv::Mat& roi, const cv::Mat& processed,
|
||||
const std::vector<std::vector<cv::Point>>& contours, int bestIdx,
|
||||
const cv::RotatedRect& bestRect);
|
||||
#endif
|
||||
[[nodiscard]] bool MatchesPlateFormat(const std::string& plate) const;
|
||||
std::vector<std::string> ValidVNMotobikeList = { "29BA", "29BB", "29BC", "29BD", "29BE",
|
||||
"29BF", "29BG", "29BH", "29BK", "29BL",
|
||||
"29BM", "29BN", "29BP", "29AA", "29AD",
|
||||
"29AE", "29AF", "29AG", "29AH", "29AK",
|
||||
"29AL", "29AM", "29AN", "29AP", "29AS",
|
||||
"29AT", "29AU", "29AV", "29AX", "29AY","29AZ","29AB", "29AC"//HN
|
||||
"59AA", "59TA", "59FA", "59CA", "59HA",
|
||||
"59KA", "59CB", "59LA", "59UA", "59MA",
|
||||
"59GA", "59DB", "59PA", "59NA", "59EA",
|
||||
"59SA", "59VA", "59YA", "59YB", "59ZA",
|
||||
"59ZB", "59NB", "59XB", "59BA", "59XA",
|
||||
"59XB", "59FA", //HCM
|
||||
"43AA", "43AC", "43AD", "43AE", "43AF", "43AG", "43AB",// Da Nang
|
||||
"67AA", "67AD", "67AE", "67AH", "67AB",
|
||||
"67AF", "67AK", "67AL", "67AM", "67AN",// An Giang
|
||||
"94AB", "94AE", "94AF", "94AH", "94AK","94FD","94FE",// Bac Lieu
|
||||
"98AA", "98AH", "98AB", "98AC", "98AD",
|
||||
"98AE", "98AF", "98AG", "98AK", "98AL",// Bac Giang
|
||||
"71AA", "71AF", "71AG", "71AH", "71AK",
|
||||
"71AB", "71AC", "71AD", "71AE", // Ben Tre
|
||||
"77AA", "77AE", "77AB", "77AH",
|
||||
"77AK", "77AC", "77AD", "77AF",
|
||||
"77AG", "77AN", "77AM", // Binh Dinh
|
||||
"69AA", "69AB", "69AC", "69AD", "69AE",
|
||||
"69AF", "69KA", "69AK", "69AL", "69AM", // Ca Mau
|
||||
"65MA", "65AA", "65CA", "65DB", "65EA",
|
||||
"65FA", "65GA", "65HA", "65KA", // Can Tho
|
||||
"76AA", "76AU", "76AV", "76AH", "76AB",
|
||||
"76AC", "76AD", "76AE", "76AF", "76AK",
|
||||
"76AL", "76AM", "76AN", "76AP", "76AS","76AT",// Quang Ngai
|
||||
"84HA", "66PA", "84AE", "88AB", "68MA",
|
||||
"63AB", "84AF", "66HA", "60AD", "47AG", "74AE",
|
||||
"84AH", "76AA", "21BA", "68HA", "68AA", "28FN",
|
||||
"49AL", "12HA", "66MA", "15AA", "15AC", "83VK",
|
||||
"95EA", "64AA", "12FA", "21AA", "83YM", "38AG",
|
||||
"68TA", "63AE", "79NA", "12ZA", "20AC", "64EA",
|
||||
"66VA", "83CA", "21FA", "68KB", "74AB", "63AK",
|
||||
"23AB", "47AK", "60AM", "23AE", "74AK", "84CA",
|
||||
"47AV", "26AA", "28FC", "21LA", "76AS", "68FA",
|
||||
"21GA", "47AH", "47AU", "23AC", "63AN", "21KA",
|
||||
"49AD", "74AH", "20AK", "26AC", "74AF", "76AC",
|
||||
"68NA", "60AE", "22LA", "12XA", "15AH", "26AD",
|
||||
"23AA", "83EA", "47AA", "38AA", "26AB", "60AB",
|
||||
"60AF", "38AE", "28FD", "66BA", "68CA", "20AD",
|
||||
"21EA", "38AS", "60AA", "88AC", "64BA", "22AB",
|
||||
"49AG", "38AK", "47AS", "64AC", "47AD", "68SA",
|
||||
"12BA", "15AE", "28FM", "26AM", "79ZA", "66FA",
|
||||
"63AH", "64DA", "47AM", "68UA", "26AG", "84AC",
|
||||
"66SA", "76AD", "49AN", "26AF", "79XA", "28FS",
|
||||
"49AA", "63AS", "84BA", "12SA", "83DB", "60AL",
|
||||
"21HA", "38AF", "22AA", "63AF", "49AE", "47AT",
|
||||
"23AD", "84AD", "23AH", "79CA", "60AH", "66GA",
|
||||
"26AK", "21CA", "38AB", "28FE", "26AP", "47AP",
|
||||
"47AE", "76AV", "49AC", "38AN", "12PA", "76AE",
|
||||
"64FA", "12VA", "22FA", "79DA", "95NA", "76AP",
|
||||
"66NA", "47AB", "74AG", "60AC", "68GA", "63AC",
|
||||
"47AN", "22YA", "68PA", "47AC", "83GE", "26AL",
|
||||
"64DB", "47AL", "83ZY", "60AN", "64GA", "84AL",
|
||||
"49AK", "28FB", "20AE", "20AG", "76AU", "66KA",
|
||||
"64CA", "76AK", "23AV", "38AX", "79HA", "47AX",
|
||||
"49AM", "83PT", "15AF", "95AA", "38AM", "76AT",
|
||||
"47AF", "74AC", "20AB", "64HA", "12UA", "22SA",
|
||||
"68BA", "49AH", "23AK", "20AF", "63AD", "60AK",
|
||||
"28FZ", "28FL", "76AB", "49AF", "26AH", "63AL",
|
||||
"74AA", "79DB", "22NA", "23AU", "23AY", "83TG",
|
||||
"63AM", "64KA", "23AM", "74AD", "79VA", "95XA",
|
||||
"28FF", "68KA", "49AP", "83XL", "83MF", "64HB",
|
||||
"15AN", "15AK", "76AF", "22TA", "12LA", "38AL",
|
||||
"95HA", "68EA", "84AA", "66CA", "26AE", "66LA",
|
||||
"76AN", "23AX", "95RA", "68B", "68LA", "20AH",
|
||||
"20AA", "76AH", "38AC", "12TA", "76AM",
|
||||
"59C3", "29S7", "29V7", "29L2", "72K1", "72D1", "98AF",
|
||||
"59D2", "59S1", "59K2", "67K1", "59C4", "98AG", "59E2",
|
||||
"67H2", "94L1", "29S1", "59E1", "59X4", "94B2", "67L1",
|
||||
"29E1", "72C1", "29V3", "67M1", "29X7", "29X1", "98AD",
|
||||
"59N3", "59C1", "59D3", "59P1", "67F1", "59M2", "59K1",
|
||||
"98AB", "59T2", "72E1", "59V1", "29N2", "29E2", "67D2",
|
||||
"72E2", "29T2", "29V5", "59L3", "29S2", "59L2", "72G1",
|
||||
"59G2", "30Z1", "67N1", "98B1", "59U1", "59D1", "72C2",
|
||||
"98B2", "98M1", "29P1", "67M2", "98H1", "29V1", "29K2",
|
||||
"59V2", "59F2", "59N1", "29Z1", "98D1", "59C2", "94K1",
|
||||
"67G1", "98AL", "29Y5", "59B1", "41B1", "94G1", "59G1",
|
||||
"94M1", "59X2", "67C1", "67D1", "59X3", "67B1", "98AC",
|
||||
"67C2", "98B3", "59Z1", "59Z2", "98AH", "59S3", "94F1",
|
||||
"98L1", "59H2", "98D2", "29D1", "59M1", "29B2", "29D2",
|
||||
"29U1", "59U2", "29H2", "29K1", "94B1", "98C1", "59F1",
|
||||
"67H1", "29F1", "29Y1", "59P2", "67L2", "59X1", "98G1",
|
||||
"29C1", "67B2", "59T1", "29G2", "29L5", "29S6", "29Y7",
|
||||
"59Y2", "67E1", "98AK", "94C1", "29X2", "98K1", "98AE",
|
||||
"98AA", "29H1", "29T1", "67K2", "94D2", "94E1", "29M1",
|
||||
"29N1", "59Y1", "72H1", "29X3", "98F1", "29C2", "94K2",
|
||||
"72F1", "29M2", "29Y3", "59P3", "29E3", "29G1", "59V3",
|
||||
"59Y3", "29P2", "59N2", "29L1", "98E1", "59G3", "29X5",
|
||||
"59S2", "94D1", "59H1", "29B1", "59L1", "50X1", "72L1",
|
||||
"43B1", "68L1", "70G1", "36M1", "81N1", "90K1", "17B1", "64E1", "99D1", "60B2", "74L1", "60C1", "68M1", "63B7", "34B1", "69M1", "24B1", "15M1", "83Y1", "48C1", "95H1", "79X1", "17B6", "36E1", "38K1", "25N1", "25U1", "61B1", "36C1", "36B3", "38F1", "99G1", "69N1", "97D1", "92T1", "92B1", "88B1", "97G1", "14U1", "63A1", "26N1", "19D1", "93C1", "73B1", "84B1", "81K1", "18L1", "64D1", "35M1", "61N1", "83P1", "15S1", "82B1", "92U1", "43D1", "22L1", "63B5", "64G1", "27N1", "14X1", "62C1", "81D1", "38G1", "19F1", "34K1", "49P1", "89H1", "14T1", "19M1", "78D1", "76A1", "66K1", "66C1", "71C1", "37K1", "19G1", "15F1", "85C1", "49B1", "21B1", "89F1", "23M1", "66L1", "90B5", "93M1", "14P1", "77N1", "36B8", "86B1", "12U1", "63B3", "21L1", "36G5", "65G1", "82E1", "61H1", "65H1", "84A1", "23F1", "95C1", "99K1", "49G1", "92D1", "36K3", "92N1", "82X1", "83M1", "11N1", "14K1", "19H1", "93H1", "60A1", "79A1", "20D1", "90D1", "81C1", "66P1", "36K1", "92V1", "18B1", "37P1", "22Y1", "23H1", "26D1", "66G1", "78F1", "49C1", "26H1", "38P1", "47T1", "74H1", "63P1", "47D1", "15D1", "23D1", "68E1", "20B1", "49F1", "43K1", "65K1", "27Z1", "92S1", "79H1", "21E1", "35Y1", "14S1", "75E1", "24Y1", "12T1", "27P1", "77B1", "88H1", "60B3", "23P1", "61F1", "99H1", "23K1", "59A3", "26C1", "81B1", "74E1", "66B1", "22S1", "92P1", "93B1", "69B1", "81P1", "12H1", "62K1", "35A1", "77C1", "27V1", "68N1", "12D1", "64K1", "41A1", "12Z1", "76C1", "38B1", "78G1", "74K1", "69H1", "94A1", "61K1", "86B7", "82G1", "14N1", "82M1", "76E1", "18E1", "61C1", "15N1", "90A1", "77F1", "34D1", "47B1", "62S1", "43E1", "81M1", "92X1", "75B1", "34F1", "70H1", "62B1", "26B1", "60B4", "61A1", "12B1", "90T1", "92E1", "34C1", "47G1", "97B1", "25S1", "70E1", "93Y1", "47S1", "37F1", "28N1", "11K1", "38E1", "78M1", "74C1", "12S1", "75S1", "37A1", "28D1", "65L1", "22B1", "99B1", "74G1", "79K1", "76K1", "76H1", "23B1", "15R1", "36B1", "74D1", "62L1", "37E1", "78E1", "89K1", "26M1", "25F1", "48H1", "79D1", "43H1", "76F1", "36L1", "43L1", "21K1", "88L1", "27S1", "92K1", "77D1", "19N1", "66H1", "36H5", "62N1", "18G1", "75D1", "37L1", "68K1", "28C1", "26E1", "35N1", "85H1", "62D1", "27U1", "19E1", "99E1", "14Y1", "49L1", "66M1", "73F1", "70K1", "36F5", "97H1", "93E1", "68P1", "43F1", "48G1", "75K1", "62U1", "86B9", "65F1", "27L1", "70L1", "63B8", "78L1", "11Z1", "68C1", "18D1", "15L1", "99C1", "49E1", "84E1", "69E1", "38A1", "48D1", "68S1", "81E1", "84K1", "63B6", "24T1", "95A1", "86B4", "34M1", "84L1", "24V1", "14M1", "36H1", "15B1", "69F1", "47E1", "38H1", "88D1", "28E1", "60C2", "63B9", "75Y1", "21D1", "35H1", "68F1", "86B5", "15H1", "36B5", "83X1", "17B7", "12V1", "86B8", "95E1", "63B2", "74F1", "86C1", "48K1", "89M1", "85D1", "71C4", "34E1", "97C1", "88E1", "81F1", "60B5", "84M1", "92H1", "28L1", "34H1", "38X1", "82L1", "61E1", "82F1", "62P1", "93F1", "65B1", "93L1", "95B1", "15P1", "77G1", "28M1", "35B1", "68G1", "36C2", "68D1", "69K1", "14L1", "36M3", "24X1", "24Z1", "86A1", "88C1", "15E1", "77E1", "83E1", "47L1", "25T1", "89C1", "71C3", "49D1", "36L6", "48F1", "36B6", "34P1", "84D1", "15C1", "38M1", "85F1", "77K1", "86B3", "74B1", "78H1", "89G1", "64A2", "15K1", "85B1", "49K1", "21H1", "73C1", "47U1", "65E1", "18C1", "69D1", "63B1", "95G1", "19L1", "20G1", "76D1", "29A1", "68T1", "75L1", "12L1", "89L1", "37C1", "27B1", "19C1", "11H1", "81X1", "70B1", "11V1", "43G1", "22A1", "83C1", "75C1", "79C1", "22F1", "92F1", "81G1", "81T1", "28H1", "66N1", "71B1", "18H1", "76P1", "26F1", "81U1", "34N1", "64F1", "76N1", "24S1", "26P1", "63B4", "35T1", "36N1", "47F1", "81L1", "61G1", "77M1", "34G1", "26G1", "97F1", "62H1", "28F1", "62T1", "93G1", "73D1", "65A1", "47P1", "74P1", "82N1", "20E1", "36D1", "60B1", "49M1", "37H1", "37M1", "38D1", "84F1", "88F1", "36B2", "65C1", "92M1", "86B6", "75H1", "38L1", "20C1", "97E1", "85E1", "38N1", "26K1", "89B1", "99F1", "28B1", "34L1", "86B2", "66F1", "77L1", "27Y1", "68H1", "37D1", "92L1", "82K1", "99A1", "69L1", "76M1", "90B4", "48B1", "95D1", "20H1", "64H1", "79Z1", "92G1", "23G1", "21G1", "37G1", "35K1", "81H1", "83Z1", "76T1", "36F1", "36B4", "14B9", "47K1", "20K1", "62M1", "84H1", "62F1", "74A1", "18A1", "73H1", "37N1", "79N1", "61D1", "11P1", "15G1", "47N1", "19K1", "71C2", "81S1", "11M1", "60B7", "60B8", "62G1", "71A1", "24P1", "69A1", "38C1", "49N1", "21C1", "84G1", "37B1", "72A1", "88K1", "88G1", "83V1", "78C1", "73K1", "78K1", "73E189D1", "67A1", "27X1", "62A1", "18K1", "70F1", "36K5", "19B1", "49H1", "66S1", "12P1" };
|
||||
ALPRChecker alprChecker;
|
||||
std::vector<std::string> ValidVNCarList = { "94H", "49F", "93A", "20F", "81H", "95R", "38R", "29F", "81F", "28G", "19A", "85B", "2", "43H", "51L", "28C", "21A", "51D", "50F", "24H", "93R", "92H", "71G", "75H", "86G", "30L", "79A", "82B", "79H", "78C", "61E", "70A", "90C", "72G", "34B", "17E", "18E", "78A", "37F", "51E", "71A", "28F", "47E", "83D", "81B", "84C", "71H", "76G", "92E", "36A", "69R", "30M", "27R", "71D", "19B", "34E", "38K", "88G", "68G", "30E", "68E", "25F", "74D", "98K", "89H", "36R", "84D", "61F", "49G", "25H", "17F", "14R", "36H", "47G", "90A", "68A", "83C", "26B", "15B", "61C", "15K", "47H", "78E", "75D", "15C", "63E", "34C", "36F", "38G", "15E", "93F", "22G", "60B", "94D", "62R", "24D", "11R", "12A", "76A", "94C", "97R", "24E", "26A", "15F", "72A", "49H", "62D", "98C", "71B", "61A", "12C", "27A", "78R", "51M", "69E", "76D", "78F", "49R", "81A", "64F", "29D", "18A", "19F", "21E", "92A", "65G", "86E", "62G", "61K", "47A", "23R", "14F", "95D", "36B", "74R", "11H", "24C", "11G", "66D", "63A", "43R", "70F", "86B", "61G", "47M", "67C", "37D", "43G", "14H", "90F", "51G", "86A", "11E", "29K", "85C", "83F", "24B", "98R", "19E", "61B", "90D", "82G", "14K", "74G", "72D", "85A", "19C", "37G", "98E", "74F", "28H", "90E", "89D", "35R", "97H", "83H", "95A", "20C", "65E", "15R", "73C", "37A", "38E", "77G", "94B", "17A", "75R", "98F", "65R", "76R", "20B", "24G", "25B", "73G", "62F", "29G", "77C", "22H", "14D", "23F", "93C", "19R", "15D", "47R", "79D", "60G", "77A", "82C", "63G", "21H", "81E", "25D", "12D", "37R", "36K", "84F", "98G", "28B", "51N", "18F", "50R", "74C", "35C", "30G", "64A", "95F", "18C", "99G", "99B", "37C", "76H", "60K", "67R", "75A", "83R", "28E", "65F", "17D", "92G", "23C", "60R", "90R", "38A", "43D", "50H", "43C", "77H", "47B", "89F", "82F", "65H", "89E", "62C", "24R", "26G", "84E", "17C", "65B", "34A", "12B", "64R", "29H", "71C", "88D", "79F", "76C", "98A", "69H", "22B", "29A", "72R", "67H", "48C", "22D", "60C", "35H", "38H", "63P", "70D", "49D", "18H", "89A", "72E", "92D", "26H", "73R", "85G", "20E", "98H", "69C", "18B", "73B", "22E", "34G", "30K", "20D", "50A", "34D", "15H", "34H", "71E", "62E", "64C", "51R", "82D", "99E", "70R", "18D", "92F", "94R", "24A", "85H", "11C", "73E", "95E", "86C", "94F", "86R", "37K", "23B", "20H", "73D", "95H", "35A", "89B", "82H", "67F", "70H", "97F", "29E", "97A", "51K", "68D", "37B", "82E", "18R", "86H", "35B", "43E", "35F", "95B", "70E", "21D", "27F", "36E", "63D", "68C", "50E", "36G", "75F", "21G", "29B", "93B", "22A", "18G", "43F", "93G", "62A", "83B", "28D", "75C", "22C", "21R", "25E", "23G", "97C", "75E", "79E", "19H", "47K", "65C", "35E", "20R", "68B", "89R", "67A", "75G", "81R", "78B", "77D", "78G", "20K", "36D", "66C", "38F", "27G", "19D", "67B", "84G", "22F", "61D", "20G", "48A", "76F", "48H", "92B", "85R", "26C", "65A", "70B", "38D", "14C", "66A", "73A", "49C", "74E", "68R", "66B", "74A", "49E", "17B", "69D", "51C", "85F", "21F", "99C", "17G", "72H", "94E", "51F", "92R", "60H", "21B", "93D", "19G", "86F", "51A", "66R", "72B", "26D", "64E", "93H", "12H", "97E", "60E", "82A", "60A", "83E", "27D", "64B", "11B", "11D", "76B", "95G", "14A", "61R", "21C", "30F", "23H", "89C", "97G", "62B", "63R", "88B", "98B", "90B", "67G", "69F", "73H", "20A", "72C", "65D", "68H", "51H", "79G", "70C", "90G", "66G", "83A", "77F", "63B", "64G", "25A", "88E", "68F", "99D", "26E", "94A", "48F", "34R", "61H", "90H", "74B", "14G", "12F", "15A", "27E", "69A", "35D", "12E", "85E", "25C", "29M", "89G", "17R", "78D", "84R", "95C", "15G", "28R", "99A", "69G", "48D", "97D", "27C", "78H", "14E", "79R", "73F", "88A", "48E", "48B", "64H", "99R", "14B", "77R", "75B", "88F", "84B", "11A", "67E", "12R", "50M", "11F", "79C", "49A", "43A", "88R", "77E", "48G", "51B", "81D", "74H", "93E", "37H", "88C", "71F", "94G", "38C", "29C", "43B", "30H", "81G", "28A", "26R", "66H", "66E", "17H", "79B", "49B", "63C", "98D", "81C", "69B", "63H", "85D", "26F", "22R", "83G", "37E", "12G", "77B", "35G", "62H", "60D", "60F", "99H", "70G", "76E", "84A", "72F", "25R", "27B", "30A", "47F", "34F", "97B", "23E", "36C", "66F", "48R", "92C", "71R", "23A", "50G", "47C", "82R", "63F", "84H", "38B", "47D", "67D", "25G", "86D", "88H", "64D", "24F", "23D", "99F" };
|
||||
[[nodiscard]] std::string AnalyseLicensePlateText(const std::string& ocrText);
|
||||
[[nodiscard]] char convertDigitToLetter(char c);
|
||||
[[nodiscard]] char convertLetterToDigit(char c);
|
||||
[[nodiscard]] char fixLPDigit(char c);
|
||||
[[nodiscard]] int findSubstringIndex(const std::string& str);
|
||||
[[nodiscard]] std::string convertStringToDigits(const std::string& input);
|
||||
[[nodiscard]] std::string convertStringToLetters(const std::string& input);
|
||||
[[nodiscard]] int searchDiplomacyLP(const std::string& input);
|
||||
[[nodiscard]] bool ValidateVNMotobikeLP(const std::string& input);
|
||||
[[nodiscard]] bool ValidateVNCarLP(const std::string& input);
|
||||
bool valid;
|
||||
cv::Mat alignPlateForOCR(const cv::Mat& fullImage, const cv::Rect& bbox);
|
||||
cv::Mat enhanceForOCR(const cv::Mat& plateROI);
|
||||
[[nodiscard]] std::string DetectLicensePlateString(const cv::Mat& lprROI, const std::string& cameraId);
|
||||
[[nodiscard]] std::string DetectLPColourDetector(const cv::Mat& lprROI, const std::string& cameraId);
|
||||
[[nodiscard]] std::string DetectLPColourCached(const cv::Mat& lprROI, const std::string& cameraId, const std::string& plateText);
|
||||
|
||||
// LPC colour cache: keyed by plate text (e.g. "29BA-12345"),
|
||||
// stores { colour, hitCount }. Plate colour doesn't change, so
|
||||
// we only run the classifier once per unique plate and reuse the
|
||||
// result for subsequent frames.
|
||||
struct ColourCacheEntry {
|
||||
std::string colour;
|
||||
int hitCount = 0;
|
||||
};
|
||||
std::unordered_map<std::string, ColourCacheEntry> _colourCache;
|
||||
static constexpr size_t COLOUR_CACHE_MAX_SIZE = 200;
|
||||
|
||||
public:
|
||||
ANSALPR_OD();
|
||||
~ANSALPR_OD();
|
||||
[[nodiscard]] bool Initialize(const std::string& licenseKey, const std::string& modelZipFilePath, const std::string& modelZipPassword, double detectorThreshold, double ocrThreshold, double colourThreshold) override;
|
||||
[[nodiscard]] bool LoadEngine() override;
|
||||
[[nodiscard]] bool Inference(const cv::Mat& input, std::string& lprResult) override;
|
||||
[[nodiscard]] bool Inference(const cv::Mat& input, std::string& lprResult, const std::string &cameraId) override;
|
||||
[[nodiscard]] bool Inference(const cv::Mat& input, const std::vector<cv::Rect> & Bbox, std::string& lprResult) override;
|
||||
[[nodiscard]] bool Inference(const cv::Mat& input, const std::vector<cv::Rect> & Bbox, std::string& lprResult, const std::string & cameraId) override;
|
||||
[[nodiscard]] std::vector<Object> RunInferenceSingleFrame(const cv::Mat& input, const std::string& cameraId);
|
||||
[[nodiscard]] std::vector<Object> RunInference(const cv::Mat& input, const std::string& cameraId) override;
|
||||
[[nodiscard]] std::vector<std::string> DetectLicensePlateStringBatch(const std::vector<cv::Mat>& lprROIs, const std::string& cameraId);
|
||||
[[nodiscard]] std::vector<std::string> DetectLPColourDetectorBatch(const std::vector<cv::Mat>& lprROIs, const std::string& cameraId);
|
||||
[[nodiscard]] std::string ProcessSingleOCRResult(const std::vector<Object>& ocrOutput);
|
||||
[[nodiscard]] bool Destroy() override;
|
||||
|
||||
/// Propagate debug flag to all sub-detectors (LPD, OCR, colour)
|
||||
void ActivateDebugger(bool debugFlag) override {
|
||||
_debugFlag = debugFlag;
|
||||
if (_lpDetector) _lpDetector->ActivateDebugger(debugFlag);
|
||||
if (_ocrDetector) _ocrDetector->ActivateDebugger(debugFlag);
|
||||
if (_lpColourDetector) _lpColourDetector->ActivateDebugger(debugFlag);
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,328 @@
|
||||
#ifndef ANSLPROV_H
|
||||
#define ANSLPROV_H
|
||||
#pragma once
|
||||
#include "ANSLPR.h"
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "openvino/openvino.hpp"
|
||||
#include "openvino/runtime/intel_gpu/properties.hpp"
|
||||
#include "utils/args_helper.hpp"
|
||||
#include "utils/input_wrappers.hpp"
|
||||
#include "utils/common.hpp"
|
||||
#include "utils/ocv_common.hpp"
|
||||
#include "utils/threads_common.hpp"
|
||||
#include "utils/grid_mat.hpp"
|
||||
#include "monitors/presenter.h"
|
||||
|
||||
namespace ANSCENTER
|
||||
{
|
||||
void tryPush(const std::weak_ptr<Worker>& worker, std::shared_ptr<Task>&& task);
|
||||
|
||||
struct BboxAndDescr {
|
||||
enum class ObjectType {
|
||||
NONE,
|
||||
VEHICLE,
|
||||
PLATE,
|
||||
} objectType;
|
||||
cv::Rect rect;
|
||||
std::string descr;
|
||||
};
|
||||
|
||||
class Detector {
|
||||
public:
|
||||
struct Result {
|
||||
std::size_t label;
|
||||
float confidence;
|
||||
cv::Rect location;
|
||||
};
|
||||
static constexpr int maxProposalCount = 200;
|
||||
static constexpr int objectSize = 7; // Output should have 7 as a last dimension"
|
||||
Detector() = default;
|
||||
Detector(ov::Core& core,
|
||||
const std::string& deviceName,
|
||||
const std::string& xmlPath,
|
||||
const std::vector<float>& detectionTresholds,
|
||||
const bool autoResize);
|
||||
ov::InferRequest createInferRequest();
|
||||
void setImage(ov::InferRequest& inferRequest, const cv::Mat& img);
|
||||
std::list<Result> getResults(ov::InferRequest& inferRequest, cv::Size upscale, std::vector<std::string>& rawResults);
|
||||
|
||||
private:
|
||||
bool m_autoResize;
|
||||
std::vector<float> m_detectionTresholds;
|
||||
std::string m_detectorInputName;
|
||||
std::string m_detectorOutputName;
|
||||
ov::CompiledModel m_compiled_model;
|
||||
};
|
||||
|
||||
class VehicleAttributesClassifier {
|
||||
public:
|
||||
VehicleAttributesClassifier() = default;
|
||||
VehicleAttributesClassifier(ov::Core& core,
|
||||
const std::string& deviceName,
|
||||
const std::string& xmlPath,
|
||||
const bool autoResize);
|
||||
ov::InferRequest createInferRequest();
|
||||
void setImage(ov::InferRequest& inferRequest, const cv::Mat& img, const cv::Rect vehicleRect);
|
||||
std::pair<std::string, std::string> getResults(ov::InferRequest& inferRequest);
|
||||
private:
|
||||
bool m_autoResize;
|
||||
std::string m_attributesInputName;
|
||||
std::string m_outputNameForColor;
|
||||
std::string m_outputNameForType;
|
||||
ov::CompiledModel m_compiled_model;
|
||||
};
|
||||
|
||||
class Lpr {
|
||||
public:
|
||||
Lpr() = default;
|
||||
Lpr(ov::Core& core,
|
||||
const std::string& deviceName,
|
||||
const std::string& xmlPath,
|
||||
const bool autoResize);
|
||||
ov::InferRequest createInferRequest();
|
||||
void setImage(ov::InferRequest& inferRequest, const cv::Mat& img, const cv::Rect plateRect);
|
||||
std::string getResults(ov::InferRequest& inferRequest);
|
||||
|
||||
private:
|
||||
bool m_autoResize;
|
||||
int m_maxSequenceSizePerPlate = 0;
|
||||
std::string m_LprInputName;
|
||||
std::string m_LprInputSeqName;
|
||||
std::string m_LprOutputName;
|
||||
ov::Layout m_modelLayout;
|
||||
ov::CompiledModel m_compiled_model;
|
||||
};
|
||||
|
||||
//Utilities
|
||||
struct InferRequestsContainer {
|
||||
InferRequestsContainer() = default;
|
||||
InferRequestsContainer(const InferRequestsContainer&) = delete;
|
||||
InferRequestsContainer& operator=(const InferRequestsContainer&) = delete;
|
||||
|
||||
void assign(const std::vector<ov::InferRequest>& inferRequests) {
|
||||
actualInferRequests = inferRequests;
|
||||
this->inferRequests.container.clear();
|
||||
for (auto& ir : this->actualInferRequests) {
|
||||
this->inferRequests.container.push_back(ir);
|
||||
}
|
||||
}
|
||||
ConcurrentContainer<std::vector<std::reference_wrapper<ov::InferRequest>>> inferRequests;
|
||||
std::vector<ov::InferRequest> actualInferRequests;
|
||||
};
|
||||
|
||||
|
||||
// stores all global data for tasks
|
||||
struct Context {
|
||||
Context(const std::vector<std::shared_ptr<InputChannel>>& inputChannels,
|
||||
const Detector& detector,
|
||||
const VehicleAttributesClassifier& vehicleAttributesClassifier,
|
||||
const Lpr& lpr,
|
||||
uint64_t lastFrameId,
|
||||
uint64_t nireq,
|
||||
bool isVideo,
|
||||
std::size_t nclassifiersireq,
|
||||
std::size_t nrecognizersireq) :
|
||||
readersContext{ inputChannels, std::vector<int64_t>(inputChannels.size(), -1), std::vector<std::mutex>(inputChannels.size()) },
|
||||
inferTasksContext{ detector },
|
||||
detectionsProcessorsContext{ vehicleAttributesClassifier, lpr },
|
||||
videoFramesContext{ std::vector<uint64_t>(inputChannels.size(), lastFrameId), std::vector<std::mutex>(inputChannels.size()) },
|
||||
nireq{ nireq },
|
||||
isVideo{ isVideo },
|
||||
freeDetectionInfersCount{ 0 },
|
||||
frameCounter{ 0 }
|
||||
{
|
||||
std::vector<ov::InferRequest> detectorInferRequests;
|
||||
std::vector<ov::InferRequest> attributesInferRequests;
|
||||
std::vector<ov::InferRequest> lprInferRequests;
|
||||
detectorInferRequests.reserve(nireq);
|
||||
attributesInferRequests.reserve(nclassifiersireq);
|
||||
lprInferRequests.reserve(nrecognizersireq);
|
||||
std::generate_n(std::back_inserter(detectorInferRequests), nireq, [&] {return inferTasksContext.detector.createInferRequest(); });
|
||||
std::generate_n(std::back_inserter(attributesInferRequests), nclassifiersireq, [&] {return detectionsProcessorsContext.vehicleAttributesClassifier.createInferRequest(); });
|
||||
std::generate_n(std::back_inserter(lprInferRequests), nrecognizersireq, [&] {return detectionsProcessorsContext.lpr.createInferRequest(); });
|
||||
detectorsInfers.assign(detectorInferRequests);
|
||||
attributesInfers.assign(attributesInferRequests);
|
||||
platesInfers.assign(lprInferRequests);
|
||||
}
|
||||
struct {
|
||||
std::vector<std::shared_ptr<InputChannel>> inputChannels;
|
||||
std::vector<int64_t> lastCapturedFrameIds;
|
||||
std::vector<std::mutex> lastCapturedFrameIdsMutexes;
|
||||
std::weak_ptr<Worker> readersWorker;
|
||||
} readersContext;
|
||||
|
||||
struct {
|
||||
Detector detector;
|
||||
std::weak_ptr<Worker> inferTasksWorker;
|
||||
} inferTasksContext;
|
||||
|
||||
struct {
|
||||
VehicleAttributesClassifier vehicleAttributesClassifier;
|
||||
Lpr lpr;
|
||||
std::weak_ptr<Worker> detectionsProcessorsWorker;
|
||||
} detectionsProcessorsContext;
|
||||
struct {
|
||||
std::vector<uint64_t> lastframeIds;
|
||||
std::vector<std::mutex> lastFrameIdsMutexes;
|
||||
} videoFramesContext;
|
||||
|
||||
std::weak_ptr<Worker> resAggregatorsWorker;
|
||||
std::mutex classifiersAggregatorPrintMutex;
|
||||
uint64_t nireq;
|
||||
bool isVideo;
|
||||
std::atomic<std::vector<ov::InferRequest>::size_type> freeDetectionInfersCount;
|
||||
std::atomic<uint32_t> frameCounter;
|
||||
InferRequestsContainer detectorsInfers, attributesInfers, platesInfers;
|
||||
PerformanceMetrics metrics;
|
||||
std::list<BboxAndDescr> boxesAndDescrs;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
// End of Context
|
||||
class ReborningVideoFrame : public VideoFrame {
|
||||
public:
|
||||
ReborningVideoFrame(Context& context,
|
||||
const unsigned sourceID,
|
||||
const int64_t frameId,
|
||||
const cv::Mat& frame = cv::Mat()) :
|
||||
VideoFrame{ sourceID, frameId, frame },
|
||||
context(context) {}
|
||||
virtual ~ReborningVideoFrame();
|
||||
Context& context;
|
||||
};
|
||||
|
||||
// draws results on the frame
|
||||
class ResAggregator : public Task {
|
||||
public:
|
||||
ResAggregator(const VideoFrame::Ptr& sharedVideoFrame,
|
||||
std::list<BboxAndDescr>&& boxesAndDescrs) :
|
||||
Task{ sharedVideoFrame, 4.0 },
|
||||
boxesAndDescrs{ std::move(boxesAndDescrs) } {}
|
||||
|
||||
bool isReady() override {
|
||||
return true;
|
||||
}
|
||||
void process() override;
|
||||
private:
|
||||
std::list<BboxAndDescr> boxesAndDescrs;
|
||||
};
|
||||
|
||||
// waits for all classifiers and recognisers accumulating results
|
||||
class ClassifiersAggregator {
|
||||
public:
|
||||
std::vector<std::string> rawDetections;
|
||||
ConcurrentContainer<std::list<std::string>> rawAttributes;
|
||||
ConcurrentContainer<std::list<std::string>> rawDecodedPlates;
|
||||
|
||||
explicit ClassifiersAggregator(const VideoFrame::Ptr& sharedVideoFrame) :
|
||||
sharedVideoFrame{ sharedVideoFrame } {}
|
||||
~ClassifiersAggregator() {
|
||||
std::mutex& printMutex = static_cast<ReborningVideoFrame*>(sharedVideoFrame.get())->context.classifiersAggregatorPrintMutex;
|
||||
printMutex.lock();
|
||||
if (!rawDetections.empty()) {
|
||||
slog::debug << "Frame #: " << sharedVideoFrame->frameId << slog::endl;
|
||||
slog::debug << rawDetections;
|
||||
// destructor assures that none uses the container
|
||||
for (const std::string& rawAttribute : rawAttributes.container) {
|
||||
slog::debug << rawAttribute << slog::endl;
|
||||
}
|
||||
for (const std::string& rawDecodedPlate : rawDecodedPlates.container) {
|
||||
slog::debug << rawDecodedPlate << slog::endl;
|
||||
}
|
||||
}
|
||||
printMutex.unlock();
|
||||
tryPush(static_cast<ReborningVideoFrame*>(sharedVideoFrame.get())->context.resAggregatorsWorker,
|
||||
std::make_shared<ResAggregator>(sharedVideoFrame, std::move(boxesAndDescrs)));
|
||||
}
|
||||
|
||||
void push(BboxAndDescr&& bboxAndDescr) {
|
||||
boxesAndDescrs.lockedPushBack(std::move(bboxAndDescr));
|
||||
}
|
||||
const VideoFrame::Ptr sharedVideoFrame;
|
||||
|
||||
private:
|
||||
ConcurrentContainer<std::list<BboxAndDescr>> boxesAndDescrs;
|
||||
};
|
||||
|
||||
// extracts detections from blob InferRequests and runs classifiers and recognisers
|
||||
class DetectionsProcessor : public Task {
|
||||
public:
|
||||
DetectionsProcessor(VideoFrame::Ptr sharedVideoFrame, ov::InferRequest* inferRequest) :
|
||||
Task{ sharedVideoFrame, 1.0 },
|
||||
inferRequest{ inferRequest },
|
||||
requireGettingNumberOfDetections{ true }
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
DetectionsProcessor(VideoFrame::Ptr sharedVideoFrame,
|
||||
std::shared_ptr<ClassifiersAggregator>&& classifiersAggregator,
|
||||
std::list<cv::Rect>&& vehicleRects,
|
||||
std::list<cv::Rect>&& plateRects) :
|
||||
Task{ sharedVideoFrame, 1.0 },
|
||||
classifiersAggregator{ std::move(classifiersAggregator) },
|
||||
inferRequest{ nullptr },
|
||||
vehicleRects{ std::move(vehicleRects) },
|
||||
plateRects{ std::move(plateRects) },
|
||||
requireGettingNumberOfDetections{ false }
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool isReady() override;
|
||||
void process() override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<ClassifiersAggregator> classifiersAggregator; // when no one stores this object we will draw
|
||||
ov::InferRequest* inferRequest;
|
||||
std::list<cv::Rect> vehicleRects;
|
||||
std::list<cv::Rect> plateRects;
|
||||
std::vector<std::reference_wrapper<ov::InferRequest>> reservedAttributesRequests;
|
||||
std::vector<std::reference_wrapper<ov::InferRequest>> reservedLprRequests;
|
||||
bool requireGettingNumberOfDetections;
|
||||
|
||||
};
|
||||
// runs detection
|
||||
class InferTask : public Task {
|
||||
public:
|
||||
explicit InferTask(VideoFrame::Ptr sharedVideoFrame) :
|
||||
Task{ sharedVideoFrame, 5.0 } {}
|
||||
bool isReady() override;
|
||||
void process() override;
|
||||
};
|
||||
|
||||
class Reader : public Task {
|
||||
public:
|
||||
explicit Reader(VideoFrame::Ptr sharedVideoFrame) :
|
||||
Task{ sharedVideoFrame, 2.0 } {}
|
||||
bool isReady() override;
|
||||
void process() override;
|
||||
};
|
||||
|
||||
|
||||
class ANSLPR_API ANSALPR_OV :public ANSALPR {
|
||||
private:
|
||||
std::string _vehicleLPModel; //model that detects both vehicle and license plate
|
||||
std::string _vehicleAtModel; //model that detects vehicle attributes
|
||||
std::string _lprModel; //model that recognise license plate
|
||||
Detector* _detector = nullptr;
|
||||
VehicleAttributesClassifier* _vehicleAttributesClassifier = nullptr;
|
||||
Lpr* _lpr = nullptr;
|
||||
[[nodiscard]] std::string VectorDetectionToJsonString(const std::vector<ALPRObject>& dets);
|
||||
public:
|
||||
ANSALPR_OV();
|
||||
~ANSALPR_OV();
|
||||
[[nodiscard]] bool Initialize(const std::string& licenseKey, const std::string& modelZipFilePath, const std::string& modelZipPassword) override;
|
||||
[[nodiscard]] bool Inference(const cv::Mat& input, std::string& lprResult);
|
||||
[[nodiscard]] bool Inference(const cv::Mat& input, const std::vector<cv::Rect>& Bbox, std::string& lprResult);
|
||||
[[nodiscard]] bool Destroy() override;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,776 @@
|
||||
#include "ANSLPR_RT.h"
|
||||
|
||||
namespace ANSCENTER {
|
||||
ANSALPR_RT::ANSALPR_RT() {
|
||||
_licenseValid = false;
|
||||
_globalViewId = 0;
|
||||
_focusedOnLPRId = 0;
|
||||
_platesTypesClassifierId = 0;
|
||||
}
|
||||
ANSALPR_RT::~ANSALPR_RT() {
|
||||
CloseReferences();
|
||||
}
|
||||
bool ANSALPR_RT::Destroy() {
|
||||
CloseReferences();
|
||||
return true;
|
||||
}
|
||||
bool ANSALPR_RT::CloseReferences() {
|
||||
try {
|
||||
bool session_closed = CloseDetector(_globalViewId);
|
||||
if (!session_closed) {
|
||||
this->_logger->LogFatal("ANSALPR_RT::CloseReferences", "Cannot close global view model reference", __FILE__, __LINE__);
|
||||
}
|
||||
else {
|
||||
_globalViewId = 0;
|
||||
}
|
||||
session_closed = CloseDetector(_focusedOnLPRId);
|
||||
if (!session_closed) {
|
||||
this->_logger->LogFatal("ANSALPR_RT::CloseReferences", "Cannot close focused on LRP model reference", __FILE__, __LINE__);
|
||||
}
|
||||
else {
|
||||
_focusedOnLPRId = 0;
|
||||
}
|
||||
|
||||
session_closed = ClosePlatesTypesClassifier(_platesTypesClassifierId);
|
||||
if (!session_closed) {
|
||||
this->_logger->LogFatal("ANSALPR_RT::CloseReferences", "Cannot close plate type classifer model reference", __FILE__, __LINE__);
|
||||
}
|
||||
else {
|
||||
_platesTypesClassifierId = 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger->LogFatal("ANSALPR_RT::CloseReferences", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool ANSALPR_RT::Initialize(const std::string& licenseKey, const std::string& modelZipFilePath, const std::string& modelZipPassword) {
|
||||
try {
|
||||
_licenseKey = licenseKey;
|
||||
_licenseValid = false;
|
||||
CheckLicense();
|
||||
if (!_licenseValid) {
|
||||
this->_logger->LogError("ANSALPR::Initialize.", "License is not valid.", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
// Extract model folder
|
||||
// 0. Check if the modelZipFilePath exist?
|
||||
if (!FileExist(modelZipFilePath)) {
|
||||
this->_logger->LogFatal("ANSALPR::Initialize", "Model zip file is not exist", __FILE__, __LINE__);
|
||||
}
|
||||
// 1. Unzip model zip file to a special location with folder name as model file (and version)
|
||||
std::string outputFolder;
|
||||
std::vector<std::string> passwordArray;
|
||||
if (!modelZipPassword.empty()) passwordArray.push_back(modelZipPassword);
|
||||
passwordArray.push_back("AnsDemoModels20@!");
|
||||
passwordArray.push_back("Sh7O7nUe7vJ/417W0gWX+dSdfcP9hUqtf/fEqJGqxYL3PedvHubJag==");
|
||||
passwordArray.push_back("3LHxGrjQ7kKDJBD9MX86H96mtKLJaZcTYXrYRdQgW8BKGt7enZHYMg==");
|
||||
std::string modelName = GetFileNameWithoutExtension(modelZipFilePath);
|
||||
//this->_logger->LogInfo("ANSFDBase::Initialize. Model name", modelName);
|
||||
|
||||
size_t vectorSize = passwordArray.size();
|
||||
for (size_t i = 0; i < vectorSize; i++) {
|
||||
if (ExtractPasswordProtectedZip(modelZipFilePath, passwordArray[i], modelName, _modelFolder, false))
|
||||
break; // Break the loop when the condition is met.
|
||||
}
|
||||
// 2. Check if the outputFolder exist
|
||||
if (!FolderExist(_modelFolder)) {
|
||||
this->_logger->LogError("ANSFDBase::Initialize. Output model folder is not exist", _modelFolder, __FILE__, __LINE__);
|
||||
return false; // That means the model file is not exist or the password is not correct
|
||||
}
|
||||
|
||||
|
||||
//3. Get License plate models
|
||||
_globalModelFileName = CreateFilePath(_modelFolder, "anslpr_alpr_focused_on_lp.onnx");
|
||||
if (!FileExist(_globalModelFileName)) {
|
||||
this->_logger->LogError("ANSALPR::Initialize. Global view model does not exist", _globalModelFileName, __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
_focusedOnLPRModelFileName = CreateFilePath(_modelFolder, "anslpr_alpr_global_view.onnx");
|
||||
if (!FileExist(_focusedOnLPRModelFileName)) {
|
||||
this->_logger->LogError("ANSALPR::Initialize. Focused On LRP model does not exist", _focusedOnLPRModelFileName, __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
_platesTypesClassifierFileName = CreateFilePath(_modelFolder, "plates_types_7.onnx");
|
||||
if (!FileExist(_platesTypesClassifierFileName)) {
|
||||
this->_logger->LogError("ANSALPR::Initialize.LRP classifier model does not exist", _platesTypesClassifierFileName, __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
_platesTypesLabelsFileName = CreateFilePath(_modelFolder, "plates_types_7.txt");
|
||||
if (!FileExist(_platesTypesLabelsFileName)) {
|
||||
this->_logger->LogError("ANSALPR::Initialize.LRP classifier label model does not exist", _platesTypesLabelsFileName, __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Load models
|
||||
size_t len = _globalModelFileName.size();
|
||||
|
||||
//step 1 : Initializes a new detector by loading its model file. In return, you get a unique id. The repo comes with two models namely anslpr_alpr_focused_on_lpand anslpr_alpr_global_view.
|
||||
//So you have to call this function twice to initialize both models.
|
||||
|
||||
_globalViewId = InitYoloDetector(len, _globalModelFileName.c_str());
|
||||
if (_globalViewId <= 0) {
|
||||
this->_logger->LogError("ANSALPR::Initialize.", "Global view model cannot be loaded", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
len = _focusedOnLPRModelFileName.size();
|
||||
_focusedOnLPRId = InitYoloDetector(len, _focusedOnLPRModelFileName.c_str());
|
||||
|
||||
if (_focusedOnLPRId <= 0) {
|
||||
this->_logger->LogError("ANSALPR::Initialize.", "Focused on LPR model cannot be loaded", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
|
||||
len = _platesTypesClassifierFileName.size();
|
||||
_platesTypesClassifierId = InitPlatesClassifer(len, _platesTypesClassifierFileName.c_str(), _platesTypesLabelsFileName.size(), _platesTypesLabelsFileName.c_str());
|
||||
|
||||
if (_platesTypesClassifierId <= 0) {
|
||||
this->_logger->LogError("ANSALPR::Initialize.", "Plate type classifier model cannot be loaded", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger->LogFatal("ANSALPR::Initialize", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
bool ANSALPR_RT::Inference(const cv::Mat& input, std::string& lprResult)
|
||||
{
|
||||
if (!_licenseValid) {
|
||||
lprResult = "";
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
const size_t lpn_len = 15;
|
||||
char* lpn = new char[lpn_len + 1];
|
||||
std::vector<ALPRObject> output;
|
||||
output.clear();
|
||||
cv::Rect bbox;
|
||||
bool detected = TwoStageLPRPlatesTypeDetection(
|
||||
input.cols,//width of image
|
||||
input.rows,//height of image i.e. the specified dimensions of the image
|
||||
input.channels(),// pixel type : 1 (8 bpp greyscale image) 3 (RGB 24 bpp image) or 4 (RGBA 32 bpp image)
|
||||
input.data,
|
||||
input.step,// source image bytes buffer
|
||||
_globalViewId,
|
||||
_focusedOnLPRId,//id : unique interger to identify the detector to be used
|
||||
_platesTypesClassifierId,//unique id to identify the platestype classifier
|
||||
lpn_len,
|
||||
lpn, bbox);
|
||||
lprResult = lpn;
|
||||
if (detected) {
|
||||
ALPRObject result;
|
||||
result.classId = 0;
|
||||
result.className = lpn;
|
||||
result.confidence = 1.0;
|
||||
result.box = bbox;
|
||||
output.push_back(result);
|
||||
}
|
||||
lprResult = VectorDetectionToJsonString(output);
|
||||
return detected;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger->LogFatal("ANSALPR::CheckStatus", e.what(), __FILE__, __LINE__);
|
||||
lprResult = "";
|
||||
return false;
|
||||
};
|
||||
}
|
||||
bool ANSALPR_RT::Inference(const cv::Mat& input, const std::vector<cv::Rect>& Bbox, std::string& lprResult) {
|
||||
if (!_licenseValid) {
|
||||
lprResult = "";
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
const size_t lpn_len = 15;
|
||||
char* lpn = new char[lpn_len + 1];
|
||||
std::vector<ALPRObject> output;
|
||||
output.clear();
|
||||
bool detected = false;
|
||||
if (Bbox.size() > 0) {
|
||||
cv::Mat frame = input.clone();
|
||||
for (std::vector<cv::Rect>::iterator it = Bbox.begin(); it != Bbox.end(); it++) {
|
||||
int x1, y1, x2, y2;
|
||||
x1 = (*it).x;
|
||||
y1 = (*it).y;
|
||||
x2 = (*it).x + (*it).width;
|
||||
y2 = (*it).y + (*it).height;
|
||||
|
||||
// Get cropped objects
|
||||
cv::Rect objectPos(cv::Point(x1, y1), cv::Point(x2, y2));
|
||||
cv::Mat croppedObject = frame(objectPos);
|
||||
|
||||
cv::Rect bbox;
|
||||
detected = TwoStageLPRPlatesTypeDetection(
|
||||
croppedObject.cols,//width of image
|
||||
croppedObject.rows,//height of image i.e. the specified dimensions of the image
|
||||
croppedObject.channels(),// pixel type : 1 (8 bpp greyscale image) 3 (RGB 24 bpp image) or 4 (RGBA 32 bpp image)
|
||||
croppedObject.data,
|
||||
croppedObject.step,// source image bytes buffer
|
||||
_globalViewId,
|
||||
_focusedOnLPRId,//id : unique interger to identify the detector to be used
|
||||
_platesTypesClassifierId,//unique id to identify the platestype classifier
|
||||
lpn_len,
|
||||
lpn, bbox);
|
||||
lprResult = lpn;
|
||||
if (detected) {
|
||||
ALPRObject result;
|
||||
result.classId = 0;
|
||||
result.className = lpn;
|
||||
result.confidence = 1.0;
|
||||
result.box.x = bbox.x + x1;
|
||||
result.box.y = bbox.y + y1;
|
||||
result.box.width = bbox.width;
|
||||
result.box.height = bbox.height;
|
||||
output.push_back(result);
|
||||
}
|
||||
}
|
||||
lprResult = VectorDetectionToJsonString(output);
|
||||
}
|
||||
else {
|
||||
cv::Rect bbox;
|
||||
detected = TwoStageLPRPlatesTypeDetection(
|
||||
input.cols,//width of image
|
||||
input.rows,//height of image i.e. the specified dimensions of the image
|
||||
input.channels(),// pixel type : 1 (8 bpp greyscale image) 3 (RGB 24 bpp image) or 4 (RGBA 32 bpp image)
|
||||
input.data,
|
||||
input.step,// source image bytes buffer
|
||||
_globalViewId,
|
||||
_focusedOnLPRId,//id : unique interger to identify the detector to be used
|
||||
_platesTypesClassifierId,//unique id to identify the platestype classifier
|
||||
lpn_len,
|
||||
lpn, bbox);
|
||||
lprResult = lpn;
|
||||
if (detected) {
|
||||
ALPRObject result;
|
||||
result.classId = 0;
|
||||
result.className = lpn;
|
||||
result.confidence = 1.0;
|
||||
result.box = bbox;
|
||||
output.push_back(result);
|
||||
}
|
||||
lprResult = VectorDetectionToJsonString(output);
|
||||
}
|
||||
return detected;
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger->LogFatal("ANSALPR::CheckStatus", e.what(), __FILE__, __LINE__);
|
||||
lprResult = "";
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
// Private:
|
||||
std::string ANSALPR_RT::VectorDetectionToJsonString(const std::vector<ALPRObject>& dets) {
|
||||
boost::property_tree::ptree root;
|
||||
boost::property_tree::ptree detectedObjects;
|
||||
for (int i = 0; i < dets.size(); i++) {
|
||||
boost::property_tree::ptree detectedNode;
|
||||
detectedNode.put("class_id", dets[i].classId);
|
||||
detectedNode.put("class_name", dets[i].className);
|
||||
detectedNode.put("prob", dets[i].confidence);
|
||||
detectedNode.put("x", dets[i].box.x);
|
||||
detectedNode.put("y", dets[i].box.y);
|
||||
detectedNode.put("width", dets[i].box.width);
|
||||
detectedNode.put("height", dets[i].box.height);
|
||||
detectedNode.put("mask", "");//Todo: convert masks to mask with comma seperated dets[i].mask);
|
||||
detectedNode.put("extra_info", "");
|
||||
|
||||
// we might add masks into this using comma seperated string
|
||||
detectedObjects.push_back(std::make_pair("", detectedNode));
|
||||
}
|
||||
root.add_child("results", detectedObjects);
|
||||
std::ostringstream stream;
|
||||
boost::property_tree::write_json(stream, root, false);
|
||||
std::string trackingResult = stream.str();
|
||||
return trackingResult;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ANSALPR_RT::CheckStatus(OrtStatus* status) {
|
||||
try {
|
||||
if (status != nullptr) {
|
||||
const char* msg = g_ort->GetErrorMessage(status);
|
||||
this->_logger->LogError("ANSALPR::CheckStatus", msg, __FILE__, __LINE__);
|
||||
g_ort->ReleaseStatus(status);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
catch (std::exception& e) {
|
||||
this->_logger->LogFatal("ANSALPR::CheckStatus", e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
|
||||
std::list<Yolov5_alpr_onxx_detector*>::const_iterator ANSALPR_RT::GetDetector(unsigned int id, const std::list<Yolov5_alpr_onxx_detector*>& detectors,
|
||||
const std::list<unsigned int>& detectors_ids)
|
||||
{
|
||||
assert(detectors_ids.size() == detectors.size());
|
||||
std::list<Yolov5_alpr_onxx_detector*>::const_iterator it(detectors.begin());
|
||||
std::list<unsigned int>::const_iterator it_id(detectors_ids.begin());
|
||||
while (it != detectors.end() && it_id != detectors_ids.end()) {
|
||||
if (*it_id == id) {
|
||||
return it;
|
||||
}
|
||||
else {
|
||||
it_id++;
|
||||
it++;
|
||||
}
|
||||
}
|
||||
return detectors.end();
|
||||
}
|
||||
|
||||
std::list<Plates_types_classifier*>::const_iterator ANSALPR_RT::GetPlatesTypesClassifier(unsigned int id, const std::list<Plates_types_classifier*>& plates_types_classifiers,
|
||||
const std::list<unsigned int>& plates_types_classifiers_ids) {
|
||||
assert(plates_types_classifiers_ids.size() == plates_types_classifiers.size());
|
||||
std::list<Plates_types_classifier*>::const_iterator it(plates_types_classifiers.begin());
|
||||
std::list<unsigned int>::const_iterator it_id(plates_types_classifiers_ids.begin());
|
||||
while (it != plates_types_classifiers.end() && it_id != plates_types_classifiers_ids.end()) {
|
||||
if (*it_id == id) {
|
||||
return it;
|
||||
}
|
||||
else {
|
||||
it_id++;
|
||||
it++;
|
||||
}
|
||||
}
|
||||
return plates_types_classifiers.end();
|
||||
}
|
||||
|
||||
unsigned int ANSALPR_RT::GetNewId(const std::list<unsigned int>& detectors_ids) {
|
||||
if (detectors_ids.size()) {
|
||||
auto result = std::minmax_element(detectors_ids.begin(), detectors_ids.end());
|
||||
return *result.second + 1;
|
||||
}
|
||||
else return 1;
|
||||
}
|
||||
bool ANSALPR_RT::CloseDetector(unsigned int id, std::list<Ort::Env*>& _envs, std::list<Ort::SessionOptions*>& _lsessionOptions, std::list<Yolov5_alpr_onxx_detector*>& _detectors,
|
||||
std::list<unsigned int>& _detectors_ids) {
|
||||
assert(_detectors_ids.size() == _detectors.size()
|
||||
&& _detectors_ids.size() == _envs.size()
|
||||
&& _detectors_ids.size() == _lsessionOptions.size());
|
||||
std::list<Yolov5_alpr_onxx_detector*>::iterator it(_detectors.begin());
|
||||
std::list<unsigned int>::iterator it_id(_detectors_ids.begin());
|
||||
std::list<Ort::SessionOptions*>::iterator it_sessionOptions(_lsessionOptions.begin());
|
||||
std::list<Ort::Env*>::iterator it_envs(_envs.begin());
|
||||
while (it != _detectors.end() && it_id != _detectors_ids.end()
|
||||
&& it_envs != _envs.end() && it_sessionOptions != _lsessionOptions.end()
|
||||
) {
|
||||
if (*it_id == id) {
|
||||
if (*it != nullptr) delete* it;
|
||||
if (*it_sessionOptions != nullptr) delete* it_sessionOptions;
|
||||
if (*it_envs != nullptr) delete* it_envs;
|
||||
it_envs = _envs.erase(it_envs);
|
||||
it_sessionOptions = _lsessionOptions.erase(it_sessionOptions);
|
||||
it = _detectors.erase(it);
|
||||
it_id = _detectors_ids.erase(it_id);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
it_sessionOptions++;
|
||||
it_envs++;
|
||||
it_id++;
|
||||
it++;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool ANSALPR_RT::CloseDetector(unsigned int id, std::list<Ort::Env*>& _envs, std::list<Ort::SessionOptions*>& _lsessionOptions, std::list<Plates_types_classifier*>& _detectors,
|
||||
std::list<unsigned int>& _detectors_ids) {
|
||||
assert(_detectors_ids.size() == _detectors.size()
|
||||
&& _detectors_ids.size() == _envs.size()
|
||||
&& _detectors_ids.size() == _lsessionOptions.size());
|
||||
std::list<Plates_types_classifier*>::iterator it(_detectors.begin());
|
||||
std::list<unsigned int>::iterator it_id(_detectors_ids.begin());
|
||||
std::list<Ort::SessionOptions*>::iterator it_sessionOptions(_lsessionOptions.begin());
|
||||
std::list<Ort::Env*>::iterator it_envs(_envs.begin());
|
||||
while (it != _detectors.end() && it_id != _detectors_ids.end()
|
||||
&& it_envs != _envs.end() && it_sessionOptions != _lsessionOptions.end()
|
||||
) {
|
||||
if (*it_id == id) {
|
||||
if (*it != nullptr) delete* it;
|
||||
if (*it_sessionOptions != nullptr) delete* it_sessionOptions;
|
||||
if (*it_envs != nullptr) delete* it_envs;
|
||||
it_envs = _envs.erase(it_envs);
|
||||
it_sessionOptions = _lsessionOptions.erase(it_sessionOptions);
|
||||
it = _detectors.erase(it);
|
||||
it_id = _detectors_ids.erase(it_id);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
it_sessionOptions++;
|
||||
it_envs++;
|
||||
it_id++;
|
||||
it++;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Private interface
|
||||
unsigned int ANSALPR_RT::InitYoloDetector(unsigned int len, const char* model_file)
|
||||
{
|
||||
assert(detectors_ids.size() == detectors.size());
|
||||
const std::string model_filename(model_file, len);
|
||||
if (!model_filename.size() || !std::filesystem::exists(model_filename)
|
||||
|| !std::filesystem::is_regular_file(model_filename)
|
||||
)
|
||||
{
|
||||
this->_logger->LogError("ANSALPR::InitYoloDetector. Model file is not regular file.", model_filename, __FILE__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
//step 2 declare an onnx runtime environment
|
||||
std::string instanceName{ "image-classification-inference" };
|
||||
// https://github.com/microsoft/onnxruntime/blob/rel-1.6.0/include/onnxruntime/core/session/onnxruntime_c_api.h#L123
|
||||
Ort::Env* penv = new Ort::Env(OrtLoggingLevel::ORT_LOGGING_LEVEL_WARNING, instanceName.c_str());
|
||||
if (penv != nullptr) {
|
||||
//step 3 declare options for the runtime environment
|
||||
Ort::SessionOptions* psessionOptions = new Ort::SessionOptions();
|
||||
if (psessionOptions != nullptr) {
|
||||
psessionOptions->SetIntraOpNumThreads(1);
|
||||
// Sets graph optimization level
|
||||
// Available levels are
|
||||
// ORT_DISABLE_ALL -> To disable all optimizations
|
||||
// ORT_ENABLE_BASIC -> To enable basic optimizations (Such as redundant node
|
||||
// removals) ORT_ENABLE_EXTENDED -> To enable extended optimizations
|
||||
// (Includes level 1 + more complex optimizations like node fusions)
|
||||
// ORT_ENABLE_ALL -> To Enable All possible optimizations
|
||||
psessionOptions->SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);
|
||||
#ifdef ANSLPR_USE_CUDA
|
||||
// Optionally add more execution providers via session_options
|
||||
// E.g. for CUDA include cuda_provider_factory.h and uncomment the following line:
|
||||
// nullptr for Status* indicates success
|
||||
OrtStatus* status = OrtSessionOptionsAppendExecutionProvider_CUDA(*psessionOptions, 0);
|
||||
//or status =nullptr; //if you don t have CUDA
|
||||
if (status == nullptr) {
|
||||
#endif //ANSLPR_USE_CUDA
|
||||
Yolov5_alpr_onxx_detector* onnx_net = nullptr;
|
||||
#ifdef _WIN32
|
||||
//step 4 declare an onnx session (ie model), by giving references to the runtime environment, session options and file path to the model
|
||||
std::wstring widestr = std::wstring(model_filename.begin(), model_filename.end());
|
||||
onnx_net = new Yolov5_alpr_onxx_detector(*penv, widestr.c_str(), *psessionOptions);
|
||||
#else
|
||||
onnx_net = new Yolov5_alpr_onxx_detector(*penv, model_filename.c_str(), *psessionOptions);
|
||||
#endif
|
||||
if (onnx_net != nullptr && penv != nullptr && psessionOptions != nullptr) {
|
||||
std::unique_lock<std::mutex> lck(mtxMutex, std::defer_lock);
|
||||
lck.lock();
|
||||
detectors_envs.push_back(penv);
|
||||
l_detectors_sessionOptions.push_back(psessionOptions);
|
||||
detectors.push_back(onnx_net);
|
||||
unsigned int id = GetNewId(detectors_ids);
|
||||
detectors_ids.push_back(id);
|
||||
lck.unlock();
|
||||
return id;
|
||||
}
|
||||
else {
|
||||
this->_logger->LogError("ANSALPR::InitYoloDetector. Error while creating onnxruntime session with file", model_filename.c_str(), __FILE__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
#ifdef ANSLPR_USE_CUDA
|
||||
}
|
||||
else {
|
||||
CheckStatus(status);
|
||||
this->_logger->LogError("ANSALPR::InitYoloDetector.", "Cuda error", __FILE__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
#endif //ANSLPR_USE_CUDA
|
||||
}
|
||||
else {
|
||||
this->_logger->LogError("ANSALPR::InitYoloDetector.", "Error while creating SessionOptions", __FILE__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
this->_logger->LogError("ANSALPR::InitYoloDetector.", "Error while creating while creating session environment (Ort::Env)", __FILE__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
unsigned int ANSALPR_RT::InitPlatesClassifer(unsigned int len_models_filename, const char* model_file, unsigned int len_labels_filename, const char* labels_file)
|
||||
{
|
||||
assert(plates_types_classifier_ids.size() == plates_types_classifiers.size());
|
||||
const std::string model_filename(model_file, len_models_filename);
|
||||
const std::string labels_filename(labels_file, len_labels_filename);
|
||||
if (!model_filename.size() || !std::filesystem::exists(model_filename)
|
||||
|| !std::filesystem::is_regular_file(model_filename)
|
||||
|| !labels_filename.size() || !std::filesystem::exists(labels_filename)
|
||||
|| !std::filesystem::is_regular_file(labels_filename)
|
||||
)
|
||||
{
|
||||
this->_logger->LogDebug("ANSALPR::InitPlatesClassifer. Model file is not regular file.", model_filename, __FILE__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
//step 2 declare an onnx runtime environment
|
||||
std::string instanceName{ "image-classification-inference" };
|
||||
// https://github.com/microsoft/onnxruntime/blob/rel-1.6.0/include/onnxruntime/core/session/onnxruntime_c_api.h#L123
|
||||
Ort::Env* penv = new Ort::Env(OrtLoggingLevel::ORT_LOGGING_LEVEL_WARNING, instanceName.c_str());
|
||||
if (penv != nullptr) {
|
||||
//step 3 declare options for the runtime environment
|
||||
Ort::SessionOptions* psessionOptions = new Ort::SessionOptions();
|
||||
if (psessionOptions != nullptr) {
|
||||
psessionOptions->SetIntraOpNumThreads(1);
|
||||
// Sets graph optimization level
|
||||
// Available levels are
|
||||
// ORT_DISABLE_ALL -> To disable all optimizations
|
||||
// ORT_ENABLE_BASIC -> To enable basic optimizations (Such as redundant node
|
||||
// removals) ORT_ENABLE_EXTENDED -> To enable extended optimizations
|
||||
// (Includes level 1 + more complex optimizations like node fusions)
|
||||
// ORT_ENABLE_ALL -> To Enable All possible optimizations
|
||||
psessionOptions->SetGraphOptimizationLevel(GraphOptimizationLevel::ORT_ENABLE_EXTENDED);
|
||||
#ifdef ANSLPR_USE_CUDA
|
||||
// Optionally add more execution providers via session_options
|
||||
// E.g. for CUDA include cuda_provider_factory.h and uncomment the following line:
|
||||
// nullptr for Status* indicates success
|
||||
OrtStatus* status = OrtSessionOptionsAppendExecutionProvider_CUDA(*psessionOptions, 0);
|
||||
//or status =nullptr; //if you don t have CUDA
|
||||
if (status == nullptr) {
|
||||
#endif //ANSLPR_USE_CUDA
|
||||
Plates_types_classifier* onnx_net = nullptr;
|
||||
#ifdef _WIN32
|
||||
//step 4 declare an onnx session (ie model), by giving references to the runtime environment, session options and file path to the model
|
||||
std::wstring widestr = std::wstring(model_filename.begin(), model_filename.end());
|
||||
onnx_net = new Plates_types_classifier(*penv, widestr.c_str(), *psessionOptions, labels_filename);
|
||||
#else
|
||||
onnx_net = new Plates_types_classifier(*penv, model_filename.c_str(), *psessionOptions, labels_filename);
|
||||
#endif
|
||||
if (onnx_net != nullptr && penv != nullptr && psessionOptions != nullptr) {
|
||||
std::unique_lock<std::mutex> lck(mtxMutex, std::defer_lock);
|
||||
lck.lock();
|
||||
plates_types_envs.push_back(penv);
|
||||
l_plates_types_classifier_sessionOptions.push_back(psessionOptions);
|
||||
plates_types_classifiers.push_back(onnx_net);
|
||||
unsigned int id = GetNewId(plates_types_classifier_ids);
|
||||
plates_types_classifier_ids.push_back(id);
|
||||
lck.unlock();
|
||||
return id;
|
||||
}
|
||||
else {
|
||||
this->_logger->LogError("ANSALPR::InitPlatesClassifer. Error while creating onnxruntime session with file.", model_filename, __FILE__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
#ifdef ANSLPR_USE_CUDA
|
||||
}
|
||||
else {
|
||||
CheckStatus(status);
|
||||
this->_logger->LogError("ANSALPR::InitPlatesClassifer.", "Cuda error", __FILE__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
#endif //ANSLPR_USE_CUDA
|
||||
}
|
||||
else {
|
||||
this->_logger->LogError("ANSALPR::InitPlatesClassifer.", "Error while creating SessionOptions", __FILE__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
this->_logger->LogError("ANSALPR::InitPlatesClassifer.", "Error while creating session environment (Ort::Env)", __FILE__, __LINE__);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
bool ANSALPR_RT::TwoStageALPR(const int width,//width of image
|
||||
const int height,//height of image i.e. the specified dimensions of the image
|
||||
const int pixOpt,// pixel type : 1 (8 bpp greyscale image) 3 (RGB 24 bpp image) or 4 (RGBA 32 bpp image)
|
||||
void* pbData, unsigned int step,// source image bytes buffer
|
||||
unsigned int id_global_view,
|
||||
unsigned int id_focused_on_lp,
|
||||
unsigned int lpn_len,
|
||||
char* lpn, cv::Rect& bbox)
|
||||
{
|
||||
if ((pixOpt != 1) && (pixOpt != 3) && (pixOpt != 4) || height <= 0 || width <= 0 || pbData == nullptr) {
|
||||
this->_logger->LogError("ANSALPR::TwoStageLPR.", "Condition on image (pixOpt != 1) && (pixOpt != 3) && (pixOpt != 4) || height <= 0 || width <= 0 || pbData == nullptr not met", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
cv::Mat destMat;
|
||||
if (pixOpt == 1)
|
||||
{
|
||||
destMat = cv::Mat(height, width, CV_8UC1, pbData, step);
|
||||
}
|
||||
if (pixOpt == 3)
|
||||
{
|
||||
destMat = cv::Mat(height, width, CV_8UC3, pbData, step);
|
||||
}
|
||||
if (pixOpt == 4)
|
||||
{
|
||||
destMat = cv::Mat(height, width, CV_8UC4, pbData, step);
|
||||
}
|
||||
std::list<Yolov5_alpr_onxx_detector*>::const_iterator it_global_view = GetDetector(id_global_view, detectors, detectors_ids);
|
||||
if (it_global_view != detectors.end()) {
|
||||
std::list<Yolov5_alpr_onxx_detector*>::const_iterator it_focused_on_lp = GetDetector(id_focused_on_lp, detectors, detectors_ids);
|
||||
std::string lpn_str;
|
||||
std::list<cv::Rect> ROIs;
|
||||
|
||||
std::unique_lock<std::mutex> lck(mtxMutex, std::defer_lock);
|
||||
if (it_focused_on_lp != detectors.end()) {
|
||||
lck.lock();
|
||||
//for normal plates
|
||||
ROIs = (*it_global_view)->TwoStage_LPR(*(*it_focused_on_lp), destMat, lpn_str);
|
||||
|
||||
//for small plates
|
||||
lck.unlock();
|
||||
}
|
||||
else {
|
||||
this->_logger->LogError("ANSALPR::TwoStageLPR.", "id_focused_on_lp does not point to a valid detector", __FILE__, __LINE__);
|
||||
lck.lock();
|
||||
//for normal plates
|
||||
ROIs = (*it_global_view)->TwoStage_LPR(*(*it_global_view), destMat, lpn_str);
|
||||
//for small plates
|
||||
lck.unlock();
|
||||
}
|
||||
std::string::const_iterator it_lpn(lpn_str.begin());
|
||||
int i = 0;
|
||||
while (it_lpn != lpn_str.end() && i < lpn_len - 1) {
|
||||
lpn[i] = *it_lpn;
|
||||
i++; it_lpn++;
|
||||
}
|
||||
while (i < lpn_len) {
|
||||
lpn[i] = '\0';
|
||||
i++;
|
||||
}
|
||||
bbox = GetGlobalROI(ROIs);
|
||||
return (lpn_str.length() > 0);
|
||||
}
|
||||
else {
|
||||
this->_logger->LogError("ANSALPR::TwoStageLPR.", "id_global_view does not point to a valid detector", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cv::Rect ANSALPR_RT::GetGlobalROI(std::list<cv::Rect> ROIs) {
|
||||
cv::Rect result;
|
||||
if (ROIs.size() > 0) result = ROIs.front();
|
||||
return result;
|
||||
|
||||
/*std::list<cv::Rect>::iterator it;
|
||||
cv::Rect v = ROIs.front();
|
||||
int x_min, y_min, w_min, h_min;
|
||||
int x_max, y_max, w_max, h_max;
|
||||
x_min = v.x;
|
||||
x_max = v.x;
|
||||
y_min = v.y;
|
||||
y_max = v.y;
|
||||
w_min = v.width;
|
||||
w_max = v.width;
|
||||
h_min = v.height;
|
||||
h_max = v.height;
|
||||
for (it = ROIs.begin(); it != ROIs.end(); ++it) {
|
||||
if (x_min > it->x) { x_min = it->x; }
|
||||
if (x_max < it->x) { x_max = it->x; }
|
||||
if (y_min > it->y) { y_min = it->y; }
|
||||
if (y_max < it->y) { y_max = it->y; }
|
||||
if (w_min > it->width) { w_min = it->width; }
|
||||
if (w_max < it->width) { w_max = it->width; }
|
||||
if (h_min > it->height) { w_min = it->height; }
|
||||
if (h_max < it->height) { w_max = it->height; }
|
||||
}
|
||||
v.x = x_min - 2;
|
||||
v.y = y_min - 2;
|
||||
v.width = x_max + w_max - x_min + 2;
|
||||
v.height = h_max + 2;
|
||||
return v;*/
|
||||
}
|
||||
|
||||
bool ANSALPR_RT::TwoStageLPRPlatesTypeDetection(const int width,//width of image
|
||||
const int height,//height of image i.e. the specified dimensions of the image
|
||||
const int pixOpt,// pixel type : 1 (8 bpp greyscale image) 3 (RGB 24 bpp image) or 4 (RGBA 32 bpp image)
|
||||
void* pbData, unsigned int step,// source image bytes buffer
|
||||
unsigned int id_global_view,
|
||||
unsigned int id_focused_on_lp,
|
||||
unsigned int id_plates_types_classifier,
|
||||
unsigned int lpn_len, char* lpn, cv::Rect& bbox)
|
||||
{
|
||||
if ((pixOpt != 1) && (pixOpt != 3) && (pixOpt != 4) || height <= 0 || width <= 0 || pbData == nullptr) {
|
||||
this->_logger->LogError("ANSALPR::TwoStageLPRPlatesTypeDetection.", "Condition on image (pixOpt != 1) && (pixOpt != 3) && (pixOpt != 4) || height <= 0 || width <= 0 || pbData == nullptr not met", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
else {
|
||||
cv::Mat destMat;
|
||||
if (pixOpt == 1)
|
||||
{
|
||||
destMat = cv::Mat(height, width, CV_8UC1, pbData, step);
|
||||
}
|
||||
if (pixOpt == 3)
|
||||
{
|
||||
destMat = cv::Mat(height, width, CV_8UC3, pbData, step);
|
||||
}
|
||||
if (pixOpt == 4)
|
||||
{
|
||||
destMat = cv::Mat(height, width, CV_8UC4, pbData, step);
|
||||
}
|
||||
std::list<Plates_types_classifier*>::const_iterator it_plates_types_classifier = GetPlatesTypesClassifier(id_plates_types_classifier,
|
||||
plates_types_classifiers, plates_types_classifier_ids);
|
||||
if (it_plates_types_classifier != plates_types_classifiers.end()) {
|
||||
std::list<Yolov5_alpr_onxx_detector*>::const_iterator it_global_view = GetDetector(id_global_view, detectors, detectors_ids);
|
||||
if (it_global_view != detectors.end()) {
|
||||
std::list<Yolov5_alpr_onxx_detector*>::const_iterator it_focused_on_lp = GetDetector(id_focused_on_lp, detectors, detectors_ids);
|
||||
std::string lpn_str;
|
||||
std::list<cv::Rect> ROIs;
|
||||
std::unique_lock<std::mutex> lck(mtxMutex, std::defer_lock);
|
||||
if (it_focused_on_lp != detectors.end()) {
|
||||
lck.lock();
|
||||
//for normal plates
|
||||
ROIs = (*it_global_view)->TwoStageLPR(*(*it_focused_on_lp), *(*it_plates_types_classifier), destMat, lpn_str);
|
||||
//for small plates
|
||||
lck.unlock();
|
||||
}
|
||||
else {
|
||||
this->_logger->LogError("ANSALPR::TwoStageLPRPlatesTypeDetection.", "id_focused_on_lp does not point to a valid detector", __FILE__, __LINE__);
|
||||
lck.lock();
|
||||
//for normal plates
|
||||
ROIs = (*it_global_view)->TwoStageLPR(*(*it_global_view), *(*it_plates_types_classifier), destMat, lpn_str);
|
||||
//for small plates
|
||||
lck.unlock();
|
||||
}
|
||||
std::string::const_iterator it_lpn(lpn_str.begin());
|
||||
int i = 0;
|
||||
while (it_lpn != lpn_str.end() && i < lpn_len - 1) {
|
||||
lpn[i] = *it_lpn;
|
||||
i++; it_lpn++;
|
||||
}
|
||||
while (i < lpn_len) {
|
||||
lpn[i] = '\0';
|
||||
i++;
|
||||
}
|
||||
|
||||
bbox = GetGlobalROI(ROIs);
|
||||
return (lpn_str.length() > 0);
|
||||
}
|
||||
else {
|
||||
this->_logger->LogError("ANSALPR::TwoStageLPRPlatesTypeDetection.", "id_global_view does not point to a valid detector", __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
this->_logger->LogError("ANSALPR::TwoStageLPRPlatesTypeDetection.", "id_plates_types_classifier does not point to a valid detector", __FILE__, __LINE__);
|
||||
return TwoStageALPR(width,//width of image
|
||||
height,//height of image i.e. the specified dimensions of the image
|
||||
pixOpt,// pixel type : 1 (8 bpp greyscale image) 3 (RGB 24 bpp image) or 4 (RGBA 32 bpp image)
|
||||
pbData, step,// source image bytes buffer
|
||||
id_global_view, id_focused_on_lp, lpn_len, lpn, bbox);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool ANSALPR_RT::CloseDetector(unsigned int id)
|
||||
{
|
||||
assert(detectors_ids.size() == detectors.size());
|
||||
std::unique_lock<std::mutex> lck(mtxMutex, std::defer_lock);
|
||||
lck.lock();
|
||||
bool session_closed = CloseDetector(id, detectors_envs, l_detectors_sessionOptions, detectors, detectors_ids);
|
||||
lck.unlock();
|
||||
return session_closed;
|
||||
}
|
||||
bool ANSALPR_RT::ClosePlatesTypesClassifier(unsigned int id)
|
||||
{
|
||||
assert(plates_types_classifier_ids.size() == plates_types_classifiers.size());
|
||||
std::unique_lock<std::mutex> lck(mtxMutex, std::defer_lock);
|
||||
lck.lock();
|
||||
bool session_closed = CloseDetector(id, plates_types_envs, l_plates_types_classifier_sessionOptions, plates_types_classifiers, plates_types_classifier_ids);
|
||||
lck.unlock();
|
||||
return session_closed;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
#ifndef ANSLPRRT_H
|
||||
#define ANSLPRRT_H
|
||||
#pragma once
|
||||
#include "ANSLPR.h"
|
||||
#include <onnxruntime_c_api.h>
|
||||
#include <onnxruntime_cxx_api.h>
|
||||
#include "yolov5_alpr_onnx_detector.h"
|
||||
#include "ONNX_detector.h"
|
||||
#include <thread> // std::thread
|
||||
#include <mutex> // std::mutex, std::unique_lock, std::defer_lock
|
||||
|
||||
namespace ANSCENTER
|
||||
{
|
||||
class ANSLPR_API ANSALPR_RT :public ANSALPR {
|
||||
private:
|
||||
std::mutex mtxMutex; // mutex for critical section
|
||||
std::string _globalModelFileName{};
|
||||
std::string _focusedOnLPRModelFileName{};
|
||||
std::string _platesTypesClassifierFileName{};
|
||||
std::string _platesTypesLabelsFileName{};
|
||||
|
||||
size_t _globalViewId;
|
||||
size_t _focusedOnLPRId;
|
||||
size_t _platesTypesClassifierId;
|
||||
[[nodiscard]] bool CloseReferences();
|
||||
|
||||
public:
|
||||
ANSALPR_RT();
|
||||
~ANSALPR_RT();
|
||||
[[nodiscard]] bool Initialize(const std::string& licenseKey, const std::string& modelZipFilePath, const std::string& modelZipPassword) override;
|
||||
[[nodiscard]] bool Inference(const cv::Mat& input, std::string& lprResult);
|
||||
[[nodiscard]] bool Inference(const cv::Mat& input, const std::vector<cv::Rect>& Bbox, std::string& lprResult);
|
||||
[[nodiscard]] bool Destroy() override;
|
||||
|
||||
private:
|
||||
[[nodiscard]] std::string VectorDetectionToJsonString(const std::vector<Object>& dets);
|
||||
void CheckStatus(OrtStatus* status);
|
||||
cv::Rect GetGlobalROI(std::list<cv::Rect> ROIs);
|
||||
std::list<Yolov5_alpr_onxx_detector*>::const_iterator GetDetector(unsigned int id, const std::list<Yolov5_alpr_onxx_detector*>& detectors,
|
||||
const std::list<unsigned int>& detectors_ids);
|
||||
std::list<Plates_types_classifier*>::const_iterator GetPlatesTypesClassifier(unsigned int id, const std::list<Plates_types_classifier*>& plates_types_classifiers,
|
||||
const std::list<unsigned int>& plates_types_classifiers_ids);
|
||||
unsigned int GetNewId(const std::list<unsigned int>& detectors_ids);
|
||||
bool CloseDetector(unsigned int id, std::list<Ort::Env*>& _envs, std::list<Ort::SessionOptions*>& _lsessionOptions, std::list<Yolov5_alpr_onxx_detector*>& _detectors,
|
||||
std::list<unsigned int>& _detectors_ids);
|
||||
bool CloseDetector(unsigned int id, std::list<Ort::Env*>& _envs, std::list<Ort::SessionOptions*>& _lsessionOptions, std::list<Plates_types_classifier*>& _detectors,
|
||||
std::list<unsigned int>& _detectors_ids);
|
||||
|
||||
unsigned int InitYoloDetector(unsigned int len, const char* model_file);
|
||||
unsigned int InitPlatesClassifer(unsigned int len_models_filename, const char* model_file, unsigned int len_labels_filename, const char* labels_file);
|
||||
bool TwoStageALPR(const int width,//width of image
|
||||
const int height,//height of image i.e. the specified dimensions of the image
|
||||
const int pixOpt,// pixel type : 1 (8 bpp greyscale image) 3 (RGB 24 bpp image) or 4 (RGBA 32 bpp image)
|
||||
void* pbData, unsigned int step,// source image bytes buffer
|
||||
unsigned int id_global_view,
|
||||
unsigned int id_focused_on_lp,
|
||||
unsigned int lpn_len, char* lpn, cv::Rect& bbox);
|
||||
bool TwoStageLPRPlatesTypeDetection(const int width,//width of image
|
||||
const int height,//height of image i.e. the specified dimensions of the image
|
||||
const int pixOpt,// pixel type : 1 (8 bpp greyscale image) 3 (RGB 24 bpp image) or 4 (RGBA 32 bpp image)
|
||||
void* pbData,
|
||||
unsigned int step,// source image bytes buffer
|
||||
unsigned int id_global_view,
|
||||
unsigned int id_focused_on_lp,
|
||||
unsigned int id_plates_types_classifier,
|
||||
unsigned int lpn_len,
|
||||
char* lpn,
|
||||
cv::Rect& bbox);
|
||||
bool CloseDetector(unsigned int id);
|
||||
bool ClosePlatesTypesClassifier(unsigned int id);
|
||||
|
||||
private:
|
||||
// Version-negotiated OrtApi pointer: caps at the DLL's max supported API level so
|
||||
// that a newer SDK header (ORT_API_VERSION=22) paired with an older runtime DLL
|
||||
// (e.g. ORT 1.17.1, API ≤17) does not emit "[ORT ERROR] API version not available".
|
||||
// Mirrors the negotiation pattern used in EPLoader.cpp.
|
||||
const OrtApi* g_ort = []() -> const OrtApi* {
|
||||
const OrtApiBase* base = OrtGetApiBase();
|
||||
int dllMaxApi = ORT_API_VERSION;
|
||||
const char* verStr = base->GetVersionString();
|
||||
int major = 0, minor = 0;
|
||||
if (verStr && sscanf(verStr, "%d.%d", &major, &minor) == 2)
|
||||
dllMaxApi = minor;
|
||||
int targetApi = (ORT_API_VERSION < dllMaxApi) ? ORT_API_VERSION : dllMaxApi;
|
||||
return base->GetApi(targetApi);
|
||||
}();
|
||||
std::list<Ort::Env*> detectors_envs;
|
||||
std::list<Ort::SessionOptions*> l_detectors_sessionOptions;
|
||||
std::list<Yolov5_alpr_onxx_detector*> detectors;
|
||||
std::list<unsigned int> detectors_ids;
|
||||
std::list<Ort::Env*> plates_types_envs;
|
||||
std::list<Ort::SessionOptions*> l_plates_types_classifier_sessionOptions;
|
||||
std::list<Plates_types_classifier*> plates_types_classifiers;
|
||||
std::list<unsigned int> plates_types_classifier_ids;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,73 @@
|
||||
# ANSLPR — License Plate Recognition DLL
|
||||
# Explicit source list from original vcxproj
|
||||
set(ANSLPR_SOURCES
|
||||
ANSLPR.cpp
|
||||
ANSLPR_CPU.cpp
|
||||
ANSLPR_OD.cpp
|
||||
ANSGpuFrameRegistry.cpp
|
||||
dllmain.cpp
|
||||
pch.cpp
|
||||
)
|
||||
file(GLOB ANSLPR_HEADERS "*.h")
|
||||
file(GLOB ANSLPR_INCLUDE_HEADERS "include/*.h")
|
||||
|
||||
# PaddleOCR sources compiled into ANSLPR (from original vcxproj)
|
||||
set(PADDLE_SOURCES
|
||||
${CMAKE_SOURCE_DIR}/modules/ANSOCR/ANSPaddleOCR/src/clipper.cpp
|
||||
${CMAKE_SOURCE_DIR}/modules/ANSOCR/ANSPaddleOCR/src/ocr_cls.cpp
|
||||
${CMAKE_SOURCE_DIR}/modules/ANSOCR/ANSPaddleOCR/src/ocr_det.cpp
|
||||
${CMAKE_SOURCE_DIR}/modules/ANSOCR/ANSPaddleOCR/src/ocr_rec.cpp
|
||||
${CMAKE_SOURCE_DIR}/modules/ANSOCR/ANSPaddleOCR/src/paddleocr.cpp
|
||||
${CMAKE_SOURCE_DIR}/modules/ANSOCR/ANSPaddleOCR/src/paddleocr_utility.cpp
|
||||
${CMAKE_SOURCE_DIR}/modules/ANSOCR/ANSPaddleOCR/src/paddlestructure.cpp
|
||||
${CMAKE_SOURCE_DIR}/modules/ANSOCR/ANSPaddleOCR/src/postprocess_op.cpp
|
||||
${CMAKE_SOURCE_DIR}/modules/ANSOCR/ANSPaddleOCR/src/preprocess_op.cpp
|
||||
${CMAKE_SOURCE_DIR}/modules/ANSOCR/ANSPaddleOCR/src/structure_layout.cpp
|
||||
${CMAKE_SOURCE_DIR}/modules/ANSOCR/ANSPaddleOCR/src/structure_table.cpp
|
||||
)
|
||||
|
||||
add_library(ANSLPR SHARED
|
||||
${ANSLPR_HEADERS}
|
||||
${ANSLPR_SOURCES}
|
||||
${ANSLPR_INCLUDE_HEADERS}
|
||||
${PADDLE_SOURCES}
|
||||
)
|
||||
|
||||
target_include_directories(ANSLPR PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
|
||||
target_include_directories(ANSLPR PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||
${CMAKE_SOURCE_DIR}/engines/ONNXEngine
|
||||
${CMAKE_SOURCE_DIR}/engines/OpenVINOEngine/include
|
||||
${CMAKE_SOURCE_DIR}/engines/TensorRTAPI/include
|
||||
${CMAKE_SOURCE_DIR}/modules/ANSODEngine
|
||||
${CMAKE_SOURCE_DIR}/modules/ANSOCR
|
||||
${CMAKE_SOURCE_DIR}/modules/ANSOCR/ANSPaddleOCR
|
||||
${CMAKE_SOURCE_DIR}/modules/ANSOCR/ANSPaddleOCR/include
|
||||
${SHARED_INCLUDE_DIR}
|
||||
)
|
||||
|
||||
target_link_libraries(ANSLPR
|
||||
PUBLIC ANSLibsLoader
|
||||
PRIVATE ANSODEngine
|
||||
PRIVATE ANSLicensingSystem
|
||||
PRIVATE ANSMOT
|
||||
PRIVATE labview
|
||||
PRIVATE spdlog_dep
|
||||
PRIVATE opencv
|
||||
PRIVATE onnxruntime
|
||||
PRIVATE tensorrt
|
||||
PRIVATE openvino
|
||||
PRIVATE CUDA::cudart
|
||||
)
|
||||
|
||||
target_compile_definitions(ANSLPR PRIVATE UNICODE _UNICODE
|
||||
ANSLPR_EXPORTS
|
||||
_USRDLL
|
||||
ANSLPR_USE_CUDA
|
||||
LPR_EDITOR_USE_CUDA
|
||||
)
|
||||
|
||||
target_precompile_headers(ANSLPR PRIVATE pch.h)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
|
||||
#define NOMINMAX // Prevent windows.h from defining min/max macros
|
||||
// which break std::min / std::max (C2589)
|
||||
// Windows Header Files
|
||||
#include <windows.h>
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
// Line.h: interface for the C_Line class.
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
*/
|
||||
// Levenshtein.h: interface for the Levenshtein class.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
#if !defined(LEVENSHTEIN_H)
|
||||
#define LEVENSHTEIN_H
|
||||
#if _MSC_VER > 1000
|
||||
#pragma once
|
||||
#endif // _MSC_VER > 1000
|
||||
#include <cstddef>
|
||||
|
||||
/// \brief une classe qui sert � calculer la distance entre deux chaines de caracters
|
||||
/*! \class C_OCROutput
|
||||
* \brief une classe qui sert � calculer la distance entre deux chaines de caracters
|
||||
**
|
||||
*/
|
||||
class Levenshtein
|
||||
{
|
||||
public:
|
||||
//****************************
|
||||
// Get minimum edit (levenstein) between two strings
|
||||
//****************************
|
||||
[[nodiscard]] int Get (const char* a, const char* b);
|
||||
[[nodiscard]] int Get (const char* a, size_t aLen, const char* b, size_t bLen);
|
||||
[[nodiscard]] int Get2 (char const *s, char const *t);
|
||||
[[nodiscard]] int Get2 (char const *s, size_t n, char const *t, size_t dst);
|
||||
//**************************
|
||||
// construct/destruct
|
||||
//**************************
|
||||
Levenshtein();
|
||||
virtual ~Levenshtein();
|
||||
private:
|
||||
//****************************
|
||||
// Get minimum of three values
|
||||
//****************************
|
||||
int Minimum (int a, int b, int c)
|
||||
{
|
||||
int mi = a;
|
||||
if (b < mi) mi = b;
|
||||
if (c < mi) mi = c;
|
||||
return mi;
|
||||
}
|
||||
//**************************************************
|
||||
// Get a pointer to the specified cell of the matrix
|
||||
//**************************************************
|
||||
int *GetCellPointer (int *pOrigin, size_t col, size_t row, size_t nCols)
|
||||
{ return pOrigin + col + (row * (nCols + 1)); }
|
||||
//*****************************************************
|
||||
// Get the contents of the specified cell in the matrix
|
||||
//*****************************************************
|
||||
int GetAt (int *pOrigin, size_t col, size_t row, size_t nCols)
|
||||
{
|
||||
int *pCell = GetCellPointer (pOrigin, col, row, nCols);
|
||||
return *pCell;
|
||||
}
|
||||
//********************************************************
|
||||
// Fill the specified cell in the matrix with the value x
|
||||
//********************************************************
|
||||
void PutAt (int *pOrigin, size_t col, size_t row, size_t nCols, int x)
|
||||
{
|
||||
int *pCell = GetCellPointer (pOrigin, col, row, nCols);
|
||||
*pCell = x;
|
||||
}
|
||||
};
|
||||
#endif // !defined(LEVENSHTEIN_H)
|
||||
@@ -0,0 +1,192 @@
|
||||
/*
|
||||
// Line.h: interface for the C_Line class.
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
*/
|
||||
#if !defined(AFX_LINE_H__E3A0AA20_F151_11D5_B96A_CCFB30288840__INCLUDED_)
|
||||
#define AFX_LINE_H__E3A0AA20_F151_11D5_B96A_CCFB30288840__INCLUDED_
|
||||
#include <list>
|
||||
#include <opencv2/opencv.hpp>
|
||||
#ifdef _DEBUG
|
||||
#include <cassert>
|
||||
#endif //_DEBUG
|
||||
//**************************
|
||||
// conversion between number types
|
||||
//**************************
|
||||
inline float doubleToFloat(const double& value)
|
||||
{
|
||||
#ifdef LPR_PRINTF
|
||||
if ((value > FLT_MAX) || (value < -FLT_MAX))
|
||||
{
|
||||
printf("Probleme de depassement lors d'un cast de DOUBLE en FLOAT\n");
|
||||
}
|
||||
#endif
|
||||
return static_cast<float>(value);
|
||||
}
|
||||
inline float intToFloat(int value)
|
||||
{
|
||||
return static_cast<float>(value);
|
||||
}
|
||||
inline float int64ToFloat(const int64_t& value)
|
||||
{
|
||||
return static_cast<float>(value);
|
||||
}
|
||||
inline int floatToInt(float value)
|
||||
{
|
||||
#ifdef LPR_PRINTF
|
||||
if ((value > INT_MAX) || (value < -INT_MAX))
|
||||
{
|
||||
printf("Probleme de depassement lors d'un cast de float en int\n");
|
||||
}
|
||||
#endif //LPR_PRINTF
|
||||
return static_cast<int>(value);
|
||||
}
|
||||
/// \brief une classe qui modelise une droite dans un repere, donn�e par son ordonn�e � l'origine et sa pente
|
||||
/*! \class C_OCROutput
|
||||
* \brief a class that models a line in a frame, given by its intercept and its slope
|
||||
**
|
||||
*/
|
||||
class C_Line
|
||||
{
|
||||
public:
|
||||
//true if the line doesnot have too large slope and not large ordonnee a l'origine
|
||||
inline bool is_valid() const
|
||||
{
|
||||
return (fabsf(a)<FLT_MAX && fabsf(b)<FLT_MAX);
|
||||
};
|
||||
//true if the line is (nearly) a vertical one
|
||||
inline bool is_vertical() const
|
||||
{
|
||||
return fabsf(a)>FLT_MAX*0.5f;
|
||||
};
|
||||
void reset();
|
||||
//determine si les deux droites sont paralleles
|
||||
inline bool is_parallel(const C_Line & line_) const
|
||||
{
|
||||
const float MAX_FLT_ERROR=0.000001f;
|
||||
return (fabsf(a-line_.a)<MAX_FLT_ERROR);
|
||||
};
|
||||
//retourne l'image de abscisse par la fonction affi,e definie par la droite
|
||||
int get_image_entiere(const int abscisse) const;
|
||||
//retourne l'image de abscisse par la fonction affi,e definie par la droite
|
||||
float get_image(const int abscisse) const;
|
||||
//retourne l'image de abscisse par la fonction affi,e definie par la droite
|
||||
float get_image(const float& abscisse) const;
|
||||
//retourne le coefficient directeur
|
||||
inline float get_skew() const{
|
||||
return a;
|
||||
};
|
||||
//retour l'angle de la droite par rapport a l'horizontale
|
||||
inline float get_skew_angle() const{
|
||||
return static_cast<float>(atanf(a));
|
||||
};
|
||||
//retour l'angle de la droite par rapport a l'horizontale
|
||||
inline float get_skew_angle(const C_Line & l) const{
|
||||
return (l.get_skew_angle()- atanf(a));
|
||||
};
|
||||
/**
|
||||
@brief cette fonction donne l'angle de l'inclinaison d'un caractere
|
||||
cette fonction sert effectueer une correction de l'inclinaison des characteres
|
||||
cette fonction est utilise apres rotation de l'image de la plaque. Elle doit d'abord dterminer
|
||||
si les characteres ainsi reforms prsentent un angle de slant.
|
||||
cette fonction effectuee une correction de l'inclinaison des characteres
|
||||
this function uses the vanishing point detection algorithm, based
|
||||
on the concept of clustering line intersections based on the density of line intersections in a local region.
|
||||
@return the slant angle of the line of characters
|
||||
@see
|
||||
*/
|
||||
inline float get_slant_angle() const{
|
||||
const float pi_sur_2=1.570796327f;
|
||||
float slant_angle(atanf(a));
|
||||
#ifdef _DEBUG
|
||||
assert(slant_angle > -pi_sur_2 - .1 && slant_angle < pi_sur_2 + .1);
|
||||
#endif //_DEBUG
|
||||
if(slant_angle>0) {
|
||||
// * *******
|
||||
// * * *
|
||||
// * * *
|
||||
// * *******
|
||||
// * *
|
||||
// * *
|
||||
// * *
|
||||
// * *
|
||||
//In that case the slant is positive
|
||||
//correction
|
||||
// Y'=y
|
||||
// X'= x-y*cos(teta)
|
||||
// X'= x-y*sin (ret)
|
||||
slant_angle-=pi_sur_2;
|
||||
#ifdef _DEBUG
|
||||
assert(-slant_angle>-FLT_EPSILON);
|
||||
#endif //_DEBUG
|
||||
}
|
||||
else {
|
||||
// * *******
|
||||
// * * *
|
||||
// * * *
|
||||
// * *******
|
||||
// * *
|
||||
// * *
|
||||
// * *
|
||||
// **
|
||||
//In that case the slant is negative
|
||||
//il faut que ret <0
|
||||
slant_angle+=pi_sur_2;
|
||||
#ifdef _DEBUG
|
||||
assert(-slant_angle<FLT_EPSILON);
|
||||
#endif //_DEBUG
|
||||
}
|
||||
#ifdef _DEBUG
|
||||
assert(slant_angle > -pi_sur_2 - .1 && slant_angle < pi_sur_2 + .1);
|
||||
#endif //_DEBUG
|
||||
return -slant_angle;
|
||||
};
|
||||
inline bool is_x_axis() const{
|
||||
if(fabsf(a)>FLT_EPSILON) return false;
|
||||
else return(fabsf(b)<FLT_EPSILON);
|
||||
};
|
||||
//l'ordonnee a l'origine
|
||||
float b;
|
||||
//le coefficient directeur
|
||||
float a;
|
||||
//**************************
|
||||
// construct/destruct
|
||||
//**************************
|
||||
C_Line();
|
||||
C_Line(const C_Line & line_);
|
||||
C_Line(const float & a_,const float & b_);
|
||||
C_Line(const cv::Point2f& A, const cv::Point2f& B);
|
||||
virtual ~C_Line();
|
||||
//retourne le pt d'inter de C_Line avec la dte horiz y=ordonnee
|
||||
float get_abs(const int ordonnee) const;
|
||||
float get_abs(const float ordonnee) const;
|
||||
bool is_horizontale() const
|
||||
{
|
||||
return(fabsf(a)<FLT_EPSILON);
|
||||
};
|
||||
bool is_nearly_the_same(const C_Line & right_op) const;
|
||||
//returns the distance (positive or negative) of the point from its vertical projection on the line
|
||||
float difference_y(const cv::Point2f& pt) const;
|
||||
//retourne la distance du point a sa projection verticale sur la droite
|
||||
float dist_y(const cv::Point& pt) const;
|
||||
//returns the distance of the point from its vertical projection on the line
|
||||
float dist_y(const cv::Point2f& pt) const;
|
||||
//returns sum of distances of a list of points from their vertical projections on the line
|
||||
float dist_y(const std::list<cv::Point2f>& points) const;
|
||||
//returns the distance of the center point of a box from its vertical projection on the line
|
||||
float dist_y_from_center(const cv::Rect& box) const;
|
||||
//returns sum of distances of the center points of a list of boxes,from their vertical projections on the line
|
||||
float dist_y_from_center(const std::list<cv::Rect>& boxes) const;
|
||||
//returns sum of distances (which can be positive or negative) of the center points of a list of boxes,from their vertical projections on the line
|
||||
float difference_y_from_center(const std::list<cv::Rect>& boxes) const;
|
||||
//returns the distance (which can be positive or negative) of the center point of a box from its vertical projection on the line
|
||||
float difference_y_from_center(const cv::Rect& box) const;
|
||||
float dist_y(const std::list<cv::Point>& points) const;
|
||||
//returns average distance of the center points of a list of boxes,from their vertical projections on the line
|
||||
float error(const std::list<cv::Rect>& boxes) const;
|
||||
//returns average distance of a list of points from their vertical projections on the line
|
||||
float error(const std::list<cv::Point2f>& points) const;
|
||||
protected:
|
||||
//true if the line is nearly horizontal
|
||||
bool is_nearly_horiz() const;
|
||||
};
|
||||
#endif // !defined(AFX_LINE_H__E3A0AA20_F151_11D5_B96A_CCFB30288840__INCLUDED_)
|
||||
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
// Line.h: interface for the C_Line class.
|
||||
*/
|
||||
#if !defined(ONNX_RUNTIME_DETECTOR_H)
|
||||
#define ONNX_RUNTIME_DETECTOR_H
|
||||
# pragma once
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <numeric>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <onnxruntime_c_api.h>
|
||||
#include <onnxruntime_cxx_api.h>
|
||||
#ifdef ANSLPR_USE_CUDA
|
||||
#include <cpu_provider_factory.h>
|
||||
#endif //ANSLPR_USE_CUDA
|
||||
#include "utils_alpr_detect.h"
|
||||
class OnnxDetector {
|
||||
public:
|
||||
/***
|
||||
* @brief constructor
|
||||
* @param model_path - path of the TorchScript weight file
|
||||
*/
|
||||
OnnxDetector(Ort::Env& env, const void* model_data, size_t model_data_length, const Ort::SessionOptions& options);
|
||||
OnnxDetector(Ort::Env& env, const ORTCHAR_T* model_path, const Ort::SessionOptions& options);
|
||||
void dump() const;
|
||||
/***
|
||||
* @brief inference module
|
||||
* @param img - input image
|
||||
* @param conf_threshold - confidence threshold
|
||||
* @param iou_threshold - IoU threshold for nms
|
||||
* @return detection result - bounding box, score, class index
|
||||
*/
|
||||
std::vector<std::vector<Detection>>
|
||||
Run(const cv::Mat& img, float conf_threshold, float iou_threshold, bool preserve_aspect_ratio);
|
||||
/***
|
||||
* @brief inference module
|
||||
* @param img - input image
|
||||
* @param conf_threshold - confidence threshold
|
||||
* @param iou_threshold - IoU threshold for nms
|
||||
* @return detection result - bounding box, score, class index
|
||||
*/
|
||||
std::list<std::vector<std::vector<Detection>>>
|
||||
Run(const cv::Mat& img, float iou_threshold);
|
||||
/***
|
||||
* @brief
|
||||
* @return the maximum size of input image (ie width or height of dnn input layer)
|
||||
*/
|
||||
int64_t max_image_size() const;
|
||||
bool is_valid() const {
|
||||
return (session.GetInputCount() > 0 && session.GetOutputCount() > 0);
|
||||
}
|
||||
protected:
|
||||
//session options are created outside the class. The classifier access to its options through a constant reference
|
||||
const Ort::SessionOptions & sessionOptions;
|
||||
Ort::Session session;
|
||||
//ONNX environment are created outside the class. The classifier access to its envirponment through a constant reference
|
||||
const Ort::Env& env;
|
||||
};
|
||||
//non max suppession algorithm to select boxes
|
||||
void nms(const std::vector<cv::Rect>& srcRects, std::vector<cv::Rect>& resRects, std::vector<int>& resIndexs, float thresh);
|
||||
//standard scalar product
|
||||
template <typename T>
|
||||
T vectorProduct(const std::vector<T>& v)
|
||||
{
|
||||
return std::accumulate(v.begin(), v.end(), 1, std::multiplies<T>());
|
||||
}
|
||||
#endif // !defined(ONNX_RUNTIME_DETECTOR_H)
|
||||
@@ -0,0 +1,106 @@
|
||||
|
||||
// StatSommesX_Y_H_dbl.h: interface for the C_SumsRegLineXYHDbl class.
|
||||
//
|
||||
#if !defined(AFX_STATSOMMESX_Y_H_DBL_H__EA007980_0205_11D6_B96A_DB3892D34B43__INCLUDED_)
|
||||
#define AFX_STATSOMMESX_Y_H_DBL_H__EA007980_0205_11D6_B96A_DB3892D34B43__INCLUDED_
|
||||
#ifndef GITHUB
|
||||
#if _MSC_VER > 1000
|
||||
#pragma once
|
||||
#endif // _MSC_VER > 1000
|
||||
#include "Line.h"
|
||||
/// class used in segmentation algo filtering method
|
||||
/**cette classe sert a calculer des sommes moyennes et
|
||||
variances necessaires a l'tabissement d'une droite de regresssion*/
|
||||
/**cette classe sert a calculer des sommes moyennes et
|
||||
variances necessaires a l'tabissement d'une droite de regresssion*/
|
||||
class C_SumsRegLineXYHDbl
|
||||
{
|
||||
public:
|
||||
//! to reset the structure.
|
||||
/*!
|
||||
this function reassign to the member their default value.
|
||||
*/
|
||||
void clear();
|
||||
//gets the barycenter of the points cloud
|
||||
cv::Point2f barycenter(const int nb_elements);
|
||||
C_Line regression_line(const int nb_elements);
|
||||
float pente(const int nb_elements) const;
|
||||
#ifdef _DEBUG
|
||||
bool debug(const float & somme_x_,
|
||||
const float & somme_y_,
|
||||
const float & produit_xy_,
|
||||
const float & somme_carre_x_) const;
|
||||
bool debug(const float & somme_x_,
|
||||
const float & somme_y_,
|
||||
const float & produit_xy_,
|
||||
const float & somme_carre_x_,
|
||||
const int heigth_) const;
|
||||
#endif //_DEBUG
|
||||
//**************************
|
||||
// construct/destruct
|
||||
//**************************
|
||||
C_SumsRegLineXYHDbl();
|
||||
C_SumsRegLineXYHDbl(const int somme_hauteurs_);
|
||||
virtual ~C_SumsRegLineXYHDbl();
|
||||
//sum of x coordinates of points cloud
|
||||
float somme_x;
|
||||
//sum of y coordinates of points cloud
|
||||
float somme_y;
|
||||
//sum of x by y coordinates product of points cloud
|
||||
float produit_xy;
|
||||
//sum of squared x coordinates of points cloud
|
||||
float somme_carre_x;
|
||||
//sum of heights of bouding boxes (not used in that project)
|
||||
int somme_hauteurs;
|
||||
inline void add(const float& x, const float& y, const int hauteur) {
|
||||
somme_x += x;
|
||||
somme_y += y;
|
||||
produit_xy += x*y;
|
||||
somme_carre_x += x*x;
|
||||
somme_hauteurs += hauteur;
|
||||
}
|
||||
inline void operator+=(const cv::Point2f & Center) {
|
||||
somme_x += Center.x;
|
||||
somme_y += Center.y;
|
||||
produit_xy += Center.x*Center.y;
|
||||
somme_carre_x += Center.x*Center.x;
|
||||
}
|
||||
inline void operator-=(const cv::Point2f & Center) {
|
||||
somme_x -= Center.x;
|
||||
somme_y -= Center.y;
|
||||
produit_xy -= Center.x*Center.y;
|
||||
somme_carre_x -= Center.x*Center.x;
|
||||
#ifdef _DEBUG
|
||||
assert(somme_x >=0 && somme_y >=0 &&
|
||||
produit_xy >=0 && somme_carre_x>=0);
|
||||
#endif //_DEBUG
|
||||
}
|
||||
inline void substract(const float & x, const float & y, const int hauteur) {
|
||||
somme_x -= x;
|
||||
somme_y -= y;
|
||||
produit_xy -= x*y;
|
||||
somme_carre_x -=x*x;
|
||||
#ifdef _DEBUG
|
||||
assert(somme_x >=0 && somme_y >=0 &&
|
||||
produit_xy >=0 && somme_carre_x>=0);
|
||||
#endif //_DEBUG
|
||||
somme_hauteurs -= hauteur;
|
||||
}
|
||||
inline void operator+=(const C_SumsRegLineXYHDbl & stat) {
|
||||
somme_x += stat.somme_x;
|
||||
somme_y += stat.somme_y;
|
||||
produit_xy += stat.produit_xy;
|
||||
somme_carre_x += stat.somme_carre_x;
|
||||
somme_hauteurs+=stat.somme_hauteurs;
|
||||
}
|
||||
inline void subtract(const cv::Point2f & Center, const int hauteur) {
|
||||
somme_x -= Center.x;
|
||||
somme_y -= Center.y;
|
||||
produit_xy -= Center.x*Center.y;
|
||||
somme_carre_x-=Center.x*Center.x;
|
||||
somme_hauteurs-=hauteur;
|
||||
}
|
||||
bool operator ==(const C_SumsRegLineXYHDbl & right_op) const;
|
||||
};
|
||||
#endif //GITHUB
|
||||
#endif // !defined(AFX_STATSOMMESX_Y_H_DBL_H__EA007980_0205_11D6_B96A_DB3892D34B43__INCLUDED_)
|
||||
@@ -0,0 +1,810 @@
|
||||
/*
|
||||
// Line.h: interface for the C_Line class.
|
||||
*/
|
||||
#if !defined(UTILS_ALPR_ONNX_H)
|
||||
#define UTILS_ALPR_ONNX_H
|
||||
#pragma once
|
||||
#include <opencv2/core.hpp>
|
||||
#include "Levenshtein.h"
|
||||
#include "Line.h"
|
||||
enum Det {
|
||||
tl_x = 0,
|
||||
tl_y = 1,
|
||||
br_x = 2,
|
||||
br_y = 3,
|
||||
score = 4,
|
||||
class_idx = 5
|
||||
};
|
||||
struct Detection {
|
||||
cv::Rect bbox;
|
||||
float score;
|
||||
int class_idx;
|
||||
};
|
||||
struct LPN_signature {
|
||||
int nb_caracs_on_first_line;
|
||||
int nb_caracs_on_second_line;
|
||||
int nb_caracs_on_third_line;
|
||||
};
|
||||
//std::string getTrueLPN(const std::string& filename, const bool& vrai_lpn_after_underscore);
|
||||
/**
|
||||
@brief
|
||||
//rearrange detected bounding boxes from left to right
|
||||
@param[out] boxes : set of detected boxes
|
||||
@param[out] confidences : confidences detected boxes
|
||||
@param[out] classIds : std::list of indeces that indicate the classes of each of the above detected boxes
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
void sort_from_left_to_right(std::list<cv::Rect>& boxes, std::list<float>& confidences, std::list<int>& classIds);
|
||||
/**
|
||||
@brief
|
||||
@param r1 : first rectangle
|
||||
@param r2 : second rectangle
|
||||
@return the intersection rect of the rectangles
|
||||
@see
|
||||
*/
|
||||
cv::Rect get_inter(const cv::Rect& r1, const cv::Rect& r2);
|
||||
/**
|
||||
@brief
|
||||
//return true if the intersection of the first argument box and the second has an interect area that is at least 90% of the box (which means box is nearly entirely in the second argument)
|
||||
@param box : box a bounding box
|
||||
@param rect_im : ROi or second bounding box
|
||||
@return true if intersection is at least 90% of the box (which means box is nearly entirely in the second argument)
|
||||
@see
|
||||
*/
|
||||
bool is_in_rect_if(const cv::Rect& box, const cv::Rect& rect_im);
|
||||
/**
|
||||
@brief
|
||||
//return true if the intersection of the first argument box and the second has an interect area that is at least 90% of the box (which means box is nearly entirely in the second argument)
|
||||
@param box : box a bounding box
|
||||
@param rect_im : ROi or second bounding box
|
||||
@return true if intersection is at least 90% of the box (which means box is nearly entirely in the second argument)
|
||||
@see
|
||||
*/
|
||||
bool is_in_rect_if(const cv::Rect& box, const cv::Rect& rect_im, const float min_area_ratio);
|
||||
/**
|
||||
@brief
|
||||
///from all heights of the boxes, get the median value
|
||||
@param[in] boxes : set of detected boxes
|
||||
@return the median height of the boxes
|
||||
@see
|
||||
*/
|
||||
int get_median_height(const std::list<cv::Rect>& boxes);
|
||||
/**
|
||||
@brief
|
||||
//returns the iou (intersection over union) of two boxes
|
||||
@param r1 : first rectangle
|
||||
@param r2 : second rectangle
|
||||
@return the iou (a float value between 0 and 1)
|
||||
@see
|
||||
*/
|
||||
float iou(const cv::Rect& r1, const cv::Rect& r2);
|
||||
/**
|
||||
@brief
|
||||
//if two boxes have an iou (intersection over union) that is too large, then they cannot represent two adjacent characters of the license plate
|
||||
//so we discard the one with the lowest confidence rate
|
||||
@param[out] boxes : set of detected boxes
|
||||
@param[out] confidences : confidences detected boxes
|
||||
@param[out] classIds : std::list of indeces that indicate the classes of each of the above detected boxes
|
||||
@param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
void filter_iou2(
|
||||
std::list<cv::Rect>& boxes,
|
||||
std::list<float>& confidences,
|
||||
std::list<int>& classIds, float nmsThreshold = 0.5f);
|
||||
/** @brief get barycenters of a list of bounding boxes.
|
||||
* @param[in] boxes : bounding boxes.
|
||||
* @param[out] les_points : the barycenters of the above boxes.
|
||||
* @return void
|
||||
* @see
|
||||
*/
|
||||
void get_barycenters(const std::list<cv::Rect>& boxes, std::list<cv::Point2f>& les_points);
|
||||
/**
|
||||
@brief
|
||||
//checks if the bounding boxes are ordered from left to right
|
||||
@param[in] boxes : set of detected boxes
|
||||
@return true if the bounding boxes are ordered from left to right
|
||||
@see
|
||||
*/
|
||||
#ifdef _DEBUG
|
||||
//cette fonction verifie que la liste est triee de gauche a droite
|
||||
bool debug_left(const std::list<cv::Rect>& boxes);
|
||||
#endif //_DEBUG
|
||||
/**
|
||||
@brief
|
||||
//examines how boxes are disposed and filter out boxes with a position that are incompatible with the positions of other boxes
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[out] angles_with_horizontal_line : angles determined by segment joining centroids of two consecutives boxes and the horizontal line
|
||||
@param[out] mean_angles_par_rapport_horiz : mean of the above angles - roughly the tilt of the license plate
|
||||
@param[out] standard_deviation_consecutives_angles : standard deviation of the above angles (normally a small value, since boxes should be aligned)
|
||||
@param[out] interdistances : distances between centroids of two consecutives boxes
|
||||
@param[out] mean_interdistances : mean of the above distances
|
||||
@param[out] standard_deviation_interdistances : standard deviation of the above distances
|
||||
@param[out] mean_produit_interdistance_avec_angle : parameter produced by the algorithm
|
||||
@param[out] standard_deviation_produit_interdistance_avec_angle : parameter produced by the algorithm
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
void filtre_grubbs_sides(const std::list<cv::Rect>& boxes, std::list<float>& angles_with_horizontal_line,
|
||||
float& mean_angles_par_rapport_horiz,
|
||||
float& standard_deviation_consecutives_angles,
|
||||
std::list<int>& interdistances,
|
||||
float& mean_interdistances,
|
||||
float& standard_deviation_interdistances,
|
||||
float& mean_produit_interdistance_avec_angle, float& standard_deviation_produit_interdistance_avec_angle);
|
||||
/**
|
||||
@brief
|
||||
//characters on a license plate can be disposed on two lines (bi level) or on just one unique line (single level).
|
||||
//anycase the ascii characters and there bouding boxes must nbe ordered in the inthe same way of the registration ascii chain.
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] l_confidences : confidences of detected boxes
|
||||
@param[in] l_classIds : classes indeces of detected boxes.
|
||||
@param[out] l_reordered : std list of the detected boxes when they are rearranged in the order of the registration string
|
||||
@param[out] l_reordered_confidences : confidences of the above boxes
|
||||
@param[out] l_reordered_classIds : classes indeces of the above boxes.
|
||||
@param[out] levels : levels are int that indicates on which line of the license plate the box lies : -1 = upper line, +1 = lower line, 0 if the license plate have just one line
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
void is_bi_level_plate(const std::list<cv::Rect>& boxes, const std::list<float>& l_confidences, const std::list<int>& l_classIds,
|
||||
std::list<cv::Rect>& l_reordered, std::list<float>& l_reordered_confidences, std::list<int>& l_reordered_classIds, std::list<int>& levels);
|
||||
/**
|
||||
@brief
|
||||
//characters on a license plate can be disposed on two lines (bi level) or on just one unique line (single level).
|
||||
//anycase the ascii characters and there bouding boxes must nbe ordered in the inthe same way of the registration ascii chain.
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] l_classIds : classes indeces of detected boxes.
|
||||
@param[out] l_reordered : std list of the detected boxes when they are rearranged in the order of the registration string
|
||||
@param[out] l_reordered_classIds : classes indeces of the above boxes.
|
||||
@param[out] levels : levels are int that indicates on which line of the license plate the box lies : -1 = upper line, +1 = lower line, 0 if the license plate have just one line
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
void is_bi_level_plate(const std::list<cv::Rect>& boxes, const std::list<int>& l_classIds,
|
||||
std::list<cv::Rect>& l_reordered, std::list<int>& l_reordered_classIds, std::list<int>& levels);
|
||||
//returns true if we can merge two list of characters bounding boxes on a single row
|
||||
bool merge_is_acceptable(const std::list<cv::Rect>& boxes_on_one_line, const std::list<cv::Rect>& boxes_on_another_line);
|
||||
/**
|
||||
@brief
|
||||
//checks if the character of the lpn is a digit or a letter or something else (by ex examples misread carac or license plate bounding box, ...)
|
||||
@param[in] input : a character (of the license plate).
|
||||
@return 1 if the carac is a digit (0...9), 0 if the carac is a letter (A,B,...,Z), -1 else
|
||||
@see
|
||||
*/
|
||||
int is_digit(const char input);
|
||||
//removes from lists every box which id doenot correspond to a lp character
|
||||
void filter_out_everything_but_characters(std::list<cv::Rect>& boxes,
|
||||
std::list<int>& l_classIds);
|
||||
//removes from lists every box which id doenot correspond to a lp character
|
||||
void filter_out_everything_but_characters(std::list<cv::Rect>& boxes,
|
||||
std::list<float>& l_confidences, std::list<int>& l_classIds);
|
||||
/**
|
||||
@brief
|
||||
//the dnn has detected boxes that represent characters of the license plate, this function now etracts from these boxes the license plate number.
|
||||
//it can deal with license pates that have two lines of charcaters
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] confidences : confidences of each bb detected by the dnn
|
||||
@param[in] classIds : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[out] tri_left_vect_of_detected_boxes :set of detected boxes when they rearranged from left to right
|
||||
@param[out] tri_left_confidences : confidences corresponding detected boxes
|
||||
@param[out] tri_left_classIds : set of indeces of the above detected boxes
|
||||
@param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
@return the lpn extracted out of detections
|
||||
@see
|
||||
*/
|
||||
std::string get_single_lpn(
|
||||
const std::vector<cv::Rect>& boxes,
|
||||
const std::vector<float>& confidences, const std::vector<int>& classIds,
|
||||
std::vector<cv::Rect>& tri_left_vect_of_detected_boxes,
|
||||
std::vector<float>& tri_left_confidences, std::vector<int>& tri_left_classIds, float nmsThreshold = 0.5f);
|
||||
/**
|
||||
@brief
|
||||
//the dnn has detected boxes that represent characters of the license plate, this function now etracts from these boxes the license plate number.
|
||||
//it can deal with license pates that have two lines of charcaters
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] confidences : confidences of each bb detected by the dnn
|
||||
@param[in] classIds : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[out] tri_left_vect_of_detected_boxes :set of detected boxes when they rearranged from left to right
|
||||
@param[out] tri_left_confidences : confidences corresponding detected boxes
|
||||
@param[out] tri_left_classIds : set of indeces of the above detected boxes
|
||||
@param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
@return the lpn extracted out of detections
|
||||
@see
|
||||
*/
|
||||
std::string get_single_lpn(
|
||||
const std::vector<cv::Rect>& boxes,
|
||||
const std::vector<int>& classIds,
|
||||
std::vector<cv::Rect>& tri_left_vect_of_detected_boxes,
|
||||
std::vector<int>& tri_left_classIds, float nmsThreshold = 0.5f);
|
||||
//
|
||||
/**
|
||||
@brief : given the index of a bounding box, we can predict if this box is a single character or if it represents the license plate area or if it is the roi of an entire vehicle
|
||||
@param[in] classId: current box class index
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
@return single character--> returns 1 or license plate--> returns 2 or vehicle--> returns 3 or negative index--> returns 0 must be an error
|
||||
@see
|
||||
*/
|
||||
int is_this_box_a_character_a_license_plate_or_a_vehicle(const int classId, const int classId_last_country);
|
||||
//
|
||||
//classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
/***
|
||||
* @brief
|
||||
@param[in] classId: current box class index
|
||||
* @param[in] number_of_characters_latin_numberplate : number of characters in a latin alphabet (usually 36 = 26 letters + 10 digits)
|
||||
* @return //single character--> returns 1
|
||||
//license plate--> returns 2
|
||||
//negative index--> returns 0 must be an error
|
||||
*/
|
||||
int is_this_box_a_character(const int classId, const int number_of_characters_latin_numberplate);
|
||||
/**
|
||||
@brief
|
||||
//groups detected boxes that correspond to the same vehicle. The separation is based on raw detections of license plates from the dnn
|
||||
//it can deal with license pates that have two lines of charcaters
|
||||
//output lists look like : first box = license plate (either a detected box either the global rect englobing characters boxes, second element = vehicle (either a detected vehicle either (0,0,0,0)
|
||||
//and remaining elements are characters
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] confidences : confidences of each bb detected by the dnn
|
||||
@param[in] classIds : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[out] l_vect_of_boxes_in_a_license_plate : double list of detected boxes when they are grouped. A single list contains all the boxes of a single vehicle present in image.
|
||||
@param[out] l_vect_of_confidences_in_a_license_plate : double list of corresponding confidences.
|
||||
@param[out] l_vect_of_classIds_in_a_license_plate : double list of corresponding indeces.
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
void group_characters_in_the_same_license_plate(
|
||||
//raw detections
|
||||
const std::list<cv::Rect>& boxes,
|
||||
const std::list<float>& confidences, const std::list<int>& classIds,
|
||||
//detections of same lp are regrouped in a vector
|
||||
std::list < std::list<cv::Rect>>& l_vect_of_boxes_in_a_license_plate,
|
||||
std::list < std::list<float>>& l_vect_of_confidences_in_a_license_plate, std::list < std::list<int>>& l_vect_of_classIds_in_a_license_plate
|
||||
, const int classId_last_country
|
||||
);
|
||||
/**
|
||||
@brief
|
||||
//groups detected boxes that correspond to the same vehicle. The separation is based on raw detections of license plates from the dnn
|
||||
//it can deal with license pates that have two lines of charcaters
|
||||
//output lists look like : first box = license plate (either a detected box either the global rect englobing characters boxes, second element = vehicle (either a detected vehicle either (0,0,0,0)
|
||||
//and remaining elements are characters
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] confidences : confidences of each bb detected by the dnn
|
||||
@param[in] classIds : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[out] l_vect_of_boxes_in_a_license_plate : double list of detected boxes when they are grouped. A single list contains all the boxes of a single vehicle present in image.
|
||||
@param[out] l_vect_of_confidences_in_a_license_plate : double list of corresponding confidences.
|
||||
@param[out] l_vect_of_classIds_in_a_license_plate : double list of corresponding indeces.
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
void group_characters_in_the_same_license_plate(
|
||||
//raw detections
|
||||
const std::vector<cv::Rect>& boxes,
|
||||
const std::vector<float>& confidences, const std::vector<int>& classIds,
|
||||
//detections of same lp are regrouped in a vector
|
||||
std::list < std::vector<cv::Rect>>& l_vect_of_boxes_in_a_license_plate,
|
||||
std::list < std::vector<float>>& l_vect_of_confidences_in_a_license_plate, std::list < std::vector<int>>& l_vect_of_classIds_in_a_license_plate
|
||||
, const int classId_last_country
|
||||
);
|
||||
/**
|
||||
@brief
|
||||
//groups detected boxes that correspond to the same vehicle. The separation is based on raw detections of license plates from the dnn
|
||||
//it can deal with license pates that have two lines of charcaters
|
||||
//output lists look like : first box = license plate (either a detected box either the global rect englobing characters boxes, second element = vehicle (either a detected vehicle either (0,0,0,0)
|
||||
//and remaining elements are characters
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] confidences : confidences of each bb detected by the dnn
|
||||
@param[in] classIds : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[out] l_vect_of_boxes_in_a_license_plate : double list of detected boxes when they are grouped. A single list contains all the boxes of a single vehicle present in image.
|
||||
@param[out] l_vect_of_confidences_in_a_license_plate : double list of corresponding confidences.
|
||||
@param[out] l_vect_of_classIds_in_a_license_plate : double list of corresponding indeces.
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
void group_characters_in_the_same_license_plate(
|
||||
//raw detections
|
||||
const std::vector<cv::Rect>& boxes,
|
||||
const std::vector<float>& confidences, const std::vector<int>& classIds,
|
||||
//detections of same lp are regrouped in a vector
|
||||
std::list < std::list<cv::Rect>>& l_vect_of_boxes_in_a_license_plate,
|
||||
std::list < std::list<float>>& l_vect_of_confidences_in_a_license_plate, std::list < std::list<int>>& l_vect_of_classIds_in_a_license_plate
|
||||
, const int classId_last_country
|
||||
);
|
||||
/**
|
||||
@brief
|
||||
//the dnn has detected boxes that represent characters of the license plate, this function now etracts from these boxes the license plate number.
|
||||
//it can deal with license pates that have two lines of charcaters
|
||||
//output lists look like : first box = license plate (either a detected box either the global rect englobing characters boxes, second element = vehicle (either a detected vehicle either (0,0,0,0)
|
||||
//and remaining elements are characters.
|
||||
//Produces double linked lists : inside list is for characters and outside list is for plates.
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] confidences : confidences of each bb detected by the dnn
|
||||
@param[in] classIds : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[out] l_vect_of_boxes_in_a_license_plate : double list of detected boxes when they are grouped. A single list contains all the boxes of a single vehicle present in image.
|
||||
@param[out] l_vect_of_confidences_in_a_license_plate : double list of corresponding confidences.
|
||||
@param[out] l_vect_of_classIds_in_a_license_plate : double list of corresponding indeces.
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
void group_characters_in_the_same_license_plate(
|
||||
//raw detections
|
||||
const std::list<cv::Rect>& boxes, const std::list<float>& confidences, const std::list<int>& classIds,
|
||||
//detections of same lp are regrouped in a vector
|
||||
std::list < std::vector<cv::Rect>>& l_vect_of_boxes_in_a_license_plate,
|
||||
std::list < std::vector<float>>& l_vect_of_confidences_in_a_license_plate, std::list < std::vector<int>>& l_vect_of_classIds_in_a_license_plate, const int classId_last_country
|
||||
//classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
);
|
||||
//1;->ok
|
||||
//2;->size too small
|
||||
//4;->second detection is not a vehicle
|
||||
//6;->detection after first two ones, is not a character
|
||||
int is_detections_of_a_unique_license_plate(const std::vector<int>& classIds);
|
||||
//1;->ok
|
||||
//2;->size too small
|
||||
//4;->second detection is not a vehicle
|
||||
//6;->detection after first two ones, is not a character
|
||||
int is_detections_of_a_unique_license_plate(const std::list<int>& classIds);
|
||||
//From confidences of detections of all boxes of a plate, we get the average confidence.
|
||||
float get_average_confidence_of_license_plate(const std::vector<int>& classIds,
|
||||
const std::vector<float>& confidences);
|
||||
//From confidences of detections of all boxes of a plate, we get the average confidence.
|
||||
float get_average_confidence_of_license_plate(const std::list<int>& classIds,
|
||||
const std::list<float>& confidences);
|
||||
/**
|
||||
@brief
|
||||
For each plate in the image, the detections have been separated. From these, we select the detections of the plates that have have the best detection score.
|
||||
//Uses double linked lists : inside list is for characters and outside list is for plates.
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] confidences : confidences of each bb detected by the dnn
|
||||
@param[in] classIds : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[out] one_lp :set of detected boxes when they rearranged from left to right
|
||||
@param[out] confidence_one_lp : confidences corresponding detected boxes
|
||||
@param[out] classIds_one_lp : set of indeces of the above detected boxes
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
void get_best_plate(
|
||||
//detections when they are separated license plates by license plates
|
||||
const std::list < std::vector<int>>& classIds, const std::list < std::vector<float>>& confidences, const std::list < std::vector<cv::Rect>>& boxes
|
||||
//output the list of the best (most probable/readable) lp
|
||||
, std::list<float>& confidence_one_lp, std::list < cv::Rect>& one_lp, std::list<int>& classIds_one_lp);
|
||||
/**
|
||||
@brief
|
||||
For each plate in the image, the detections have been separated. From these, we select the detections of the plates that have have the best detection score.
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] confidences : confidences of each bb detected by the dnn
|
||||
@param[in] classIds : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[out] one_lp :set of detected boxes when they rearranged from left to right
|
||||
@param[out] confidence_one_lp : confidences corresponding detected boxes
|
||||
@param[out] classIds_one_lp : set of indeces of the above detected boxes
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
void get_best_plate(
|
||||
//detections when they are separated license plates by license plates
|
||||
const std::list < std::list<int>>& classIds, const std::list < std::list<float>>& confidences, const std::list < std::list<cv::Rect>>& boxes
|
||||
//output the list of the best (most probable/readable) lp
|
||||
, std::list<float>& confidence_one_lp, std::list < cv::Rect>& one_lp, std::list<int>& classIds_one_lp);//For each plate in the image, the detections have been separated. From these, we select the detections of the plates that have have the best detection score.
|
||||
/**
|
||||
@brief
|
||||
given the boxes+confidences+classIds and given the actual lpn string in the image (ExactLPN), outputs the lpn that is closest to ExactLPN
|
||||
For each plate in the image, the detections have been separated. From these, we select the detections of the plates that have have the best detection score.
|
||||
@param[in] ExactLPN : the actual license plate number in the image
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] confidences : confidences of each bb detected by the dnn
|
||||
@param[in] classIds : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[out] one_lp :set of detected boxes when they rearranged from left to right
|
||||
@param[out] confidence_one_lp : confidences corresponding detected boxes
|
||||
@param[out] classIds_one_lp : set of indeces of the above detected boxes
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
void get_best_plate(const std::string& ExactLPN,
|
||||
//detections when they are separated license plates by license plates
|
||||
const std::list < std::list<int>>& classIds, const std::list < std::list<float>>& confidences, const std::list < std::list<cv::Rect>>& boxes
|
||||
//output the list of the best (most probable/readable) lp
|
||||
, std::list<float>& confidence_one_lp, std::list < cv::Rect>& one_lp, std::list<int>& classIds_one_lp);
|
||||
/**
|
||||
@brief
|
||||
given the boxes+confidences+classIds and given the actual lpn string in the image (ExactLPN), outputs the lpn that is closest to ExactLPN
|
||||
For each plate in the image, the detections have been separated. From these, we select the detections of the plates that have have the best detection score.
|
||||
@param[in] ExactLPN : the actual license plate number in the image
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] confidences : confidences of each bb detected by the dnn
|
||||
@param[in] classIds : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[out] one_lp :set of detected boxes when they rearranged from left to right
|
||||
@param[out] confidence_one_lp : confidences corresponding detected boxes
|
||||
@param[out] classIds_one_lp : set of indeces of the above detected boxes
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
@return void
|
||||
*/
|
||||
void get_best_plate(const std::string& ExactLPN,
|
||||
//detections when they are separated license plates by license plates
|
||||
const std::list < std::list<int>>& classIds, const std::list < std::list<float>>& confidences, const std::list < std::list<cv::Rect>>& boxes
|
||||
//output the list of the best (most probable/readable) lp
|
||||
, std::vector<float>& confidence_one_lp, std::vector < cv::Rect>& one_lp, std::vector<int>& classIds_one_lp);
|
||||
//this function adds if they dont already exist, a roi for the licene plate (equal to the global rect englobing the boxes) and a blank rect for the vehicle box
|
||||
void add_lp_and_vehicle(const std::list<cv::Rect>& boxes, const std::list<float>& confidences, const std::list<int>& classIds,
|
||||
std::vector<cv::Rect>& tri_left_vect_of_detected_boxes, std::vector<float>& tri_left_confidences, std::vector<int>& tri_left_classIds
|
||||
, const int classId_last_country
|
||||
);
|
||||
//
|
||||
/** @brief
|
||||
this function adds if they dont already exist, a roi for the licene plate (equal to the global rect englobing the boxes) and a blank rect for the vehicle box
|
||||
@param[out] tri_left_vect_of_detected_boxes :set of detected boxes when they rearranged from left to right
|
||||
@param[out] tri_left_confidences : confidences corresponding detected boxes
|
||||
@param[out] tri_left_classIds : set of indeces of the above detected boxes
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
*/
|
||||
void add_lp_and_vehicle(const std::list<cv::Rect>& boxes, const std::list<float>& confidences, const std::list<int>& classIds,
|
||||
std::list<cv::Rect>& tri_left_vect_of_detected_boxes, std::list<float>& tri_left_confidences, std::list<int>& tri_left_classIds
|
||||
, const int classId_last_country
|
||||
);
|
||||
//
|
||||
/** @brief
|
||||
this function adds if they dont already exist, a roi for the licene plate (equal to the global rect englobing the boxes) and a blank rect for the vehicle box
|
||||
@param[out] tri_left_vect_of_detected_boxes :set of detected boxes when they rearranged from left to right
|
||||
@param[out] tri_left_confidences : confidences corresponding detected boxes
|
||||
@param[out] tri_left_classIds : set of indeces of the above detected boxes
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
*/
|
||||
void add_lp_and_vehicle(const std::vector<cv::Rect>& boxes, const std::vector<float>& confidences, const std::vector<int>& classIds,
|
||||
std::vector<cv::Rect>& tri_left_vect_of_detected_boxes, std::vector<float>& tri_left_confidences, std::vector<int>& tri_left_classIds
|
||||
, const int classId_last_country
|
||||
);
|
||||
//
|
||||
/** @brief
|
||||
this function adds if they dont already exist, a roi for the licene plate (equal to the global rect englobing the boxes) and a blank rect for the vehicle box
|
||||
@param[out] tri_left_vect_of_detected_boxes :set of detected boxes when they rearranged from left to right
|
||||
@param[out] tri_left_confidences : confidences corresponding detected boxes
|
||||
@param[out] tri_left_classIds : set of indeces of the above detected boxes
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
*/
|
||||
void add_lp_and_vehicle(const std::vector<cv::Rect>& boxes, const std::vector<float>& confidences, const std::vector<int>& classIds,
|
||||
std::list<cv::Rect>& tri_left_vect_of_detected_boxes, std::list<float>& tri_left_confidences, std::list<int>& tri_left_classIds
|
||||
, const int classId_last_country
|
||||
);
|
||||
/**
|
||||
@brief
|
||||
//the dnn has detected boxes that represent characters of the license plate, this function now etracts from these boxes the license plate number.
|
||||
//it can deal with license pates that have two lines of charcaters
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] confidences : confidences of each bb detected by the dnn
|
||||
@param[in] classIds : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[out] tri_left_vect_of_detected_boxes :set of detected boxes when they rearranged from left to right
|
||||
@param[out] tri_left_confidences : confidences corresponding detected boxes
|
||||
@param[out] tri_left_classIds : set of indeces of the above detected boxes
|
||||
@param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
@return the lpn extracted out of detections
|
||||
@see
|
||||
*/
|
||||
std::string get_best_lpn(
|
||||
//raw detections
|
||||
const std::vector<cv::Rect>& boxes, const std::vector<float>& confidences, const std::vector<int>& classIds,
|
||||
//characters inside the best lpn that have been chosen from the above double linked list
|
||||
std::vector<cv::Rect>& tri_left_vect_of_detected_boxes,
|
||||
std::vector<float>& tri_left_confidences, std::vector<int>& tri_left_classIds, const float nmsThreshold = 0.5f, const int classId_last_country = 96
|
||||
);
|
||||
/**
|
||||
@brief
|
||||
//the dnn has detected boxes that represent characters of the license plate, this function now etracts from these boxes the license plate number.
|
||||
//it can deal with license pates that have two lines of charcaters
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] confidences : confidences of each bb detected by the dnn
|
||||
@param[in] classIds : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[out] tri_left_vect_of_detected_boxes :set of detected boxes when they rearranged from left to right
|
||||
@param[out] tri_left_confidences : confidences of corresponding detected boxes
|
||||
@param[out] tri_left_classIds : set of indeces of the above detected boxes
|
||||
@param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
@return the lpn extracted out of detections
|
||||
@see
|
||||
*/
|
||||
std::string get_best_lpn(
|
||||
//raw detections
|
||||
const std::list<cv::Rect>& boxes, const std::list<float>& confidences, const std::list<int>& classIds,
|
||||
//characters inside the best lpn that have been chosen from the above double linked list
|
||||
std::vector<cv::Rect>& tri_left_vect_of_detected_boxes,
|
||||
std::vector<float>& tri_left_confidences, std::vector<int>& tri_left_classIds, const float nmsThreshold = 0.5f, const int classId_last_country =//95
|
||||
96
|
||||
);
|
||||
/**
|
||||
@brief
|
||||
//the dnn has detected boxes that represent characters of the license plate, this function now etracts from these boxes the license plate number.
|
||||
//it can deal with license pates that have two lines of charcaters
|
||||
@param[in] one_lp : set of detected boxes
|
||||
@param[in] confidence_one_lp : confidences of each bb detected by the dnn
|
||||
@param[in] classIds_one_lp : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[out] tri_left_vect_of_detected_boxes :set of detected boxes when they rearranged from left to right
|
||||
@param[out] tri_left_confidences : confidences corresponding detected boxes
|
||||
@param[out] tri_left_classIds : set of indeces of the above detected boxes
|
||||
@param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
@return the lpn extracted out of detections
|
||||
@see
|
||||
*/
|
||||
void get_best_lpn(const std::list<cv::Rect>& one_lp, const std::list<float>& confidence_one_lp, const std::list<int>& classIds_one_lp,
|
||||
//all lps in the image given by lpn (as string), lp country ppronenace (as class index) and lp area in the image (cv::Rect)
|
||||
std::list <std::string>& lpns, std::list <int>& lp_country_class, std::list < cv::Rect>& lp_rois,
|
||||
//detection inside the chosen lp
|
||||
std::list<int>& chosen_lp_classIds, std::list<float>& chosen_lp_confidences, std::list<cv::Rect>& chosen_lp_boxes);
|
||||
//we know the true license plate number that come from a training image and we want to find the detections boxes to aautomatically annotate the image.
|
||||
//We also have run the nn that produces detections, the goal of this func is to find the detections that are closest to the true lpn
|
||||
/**
|
||||
@brief
|
||||
given the boxes+confidences+classIds and given the actual lpn string in the image (ExactLPN), outputs the lpn that is closest to ExactLPN
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] confidences : confidences of each bb detected by the dnn
|
||||
@param[in] classes : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[in] ExactLPN : the actual license plate number in the image
|
||||
@param[out] best_boxes :set of detected boxes that compose the lp that has the best confidence
|
||||
@param[out] best_confidences : confidences corresponding detected boxes
|
||||
@param[out] best_classes : set of indeces of the above detected boxes
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
@return edit distance to the actual lpn
|
||||
*/
|
||||
int find_nearest_plate_substitutions_allowed(const std::string& ExactLPN,
|
||||
//all lps in the image given by lpn (as string), lp country ppronenace (as class index) and lp area in the image (cv::Rect)
|
||||
std::list <std::string>& lpns, const std::list <int>& lp_country_class, const std::list < cv::Rect>& lp_rois, const
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<int>>& classes, const std::list < std::list<cv::Rect>>& boxes,
|
||||
//output = nearest lpn + its class + its bounding box
|
||||
std::string& best_lpn, int& best_country_class, cv::Rect& best_lpn_roi,
|
||||
//output = characters in nearest lpn
|
||||
std::list<float>& best_confidences, std::list<int>& best_classes, std::list<cv::Rect>& best_boxes);
|
||||
/**
|
||||
@brief
|
||||
//the dnn has detected boxes that represent characters of the license plate, this function now etracts from these boxes the license plate number.
|
||||
//it can deal with license pates that have two lines of charcaters
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] l_confidences : confidences of detected boxes
|
||||
@param[in] l_classIds : classes indeces of detected boxes.
|
||||
@param[out] tri_left_vect_of_detected_boxes :set of detected boxes when they rearranged from left to right
|
||||
@param[out] tri_left_confidences : confidences corresponding detected boxes
|
||||
@param[out] tri_left_classIds : set of indeces of the above detected boxes
|
||||
@param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
@return the lpn extracted out of detections
|
||||
@see
|
||||
*/
|
||||
std::string get_lpn(
|
||||
const std::list<cv::Rect>& l_of_detected_boxes,
|
||||
const std::list<float>& l_confidences, const std::list<int>& l_classIds,
|
||||
std::vector<cv::Rect>& tri_left_vect_of_detected_boxes,
|
||||
std::vector<float>& tri_left_confidences, std::vector<int>& tri_left_classIds,
|
||||
float nmsThreshold = 0.5f
|
||||
);
|
||||
/**
|
||||
@brief
|
||||
//the dnn has detected boxes that represent characters of the license plate, this function now etracts from these boxes the license plate number.
|
||||
//it can deal with license pates that have two lines of charcaters
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] l_confidences : confidences of detected boxes
|
||||
@param[in] l_classIds : classes indeces of detected boxes.
|
||||
@param[out] tri_left_vect_of_detected_boxes :set of detected boxes when they rearranged from left to right
|
||||
@param[out] tri_left_confidences : confidences corresponding detected boxes
|
||||
@param[out] tri_left_classIds : set of indeces of the above detected boxes
|
||||
@param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
@return the lpn extracted out of detections
|
||||
@see
|
||||
*/
|
||||
std::string get_lpn(
|
||||
const std::list<cv::Rect>& l_of_detected_boxes,
|
||||
const std::list<int>& l_classIds,
|
||||
std::vector<cv::Rect>& tri_left_vect_of_detected_boxes,
|
||||
std::vector<int>& tri_left_classIds,
|
||||
float nmsThreshold = 0.5f
|
||||
);
|
||||
/**
|
||||
@brief
|
||||
//the dnn has detected boxes that represent characters of the license plate, this function now etracts from these boxes the license plate number.
|
||||
//it can deal with license pates that have two lines of charcaters
|
||||
@param[in] l_classIds : classes indeces of detected boxes.
|
||||
@return the lpn extracted out of detections
|
||||
@see
|
||||
*/
|
||||
std::string get_lpn(const std::list<int>& l_classIds);
|
||||
/**
|
||||
@brief
|
||||
//the dnn has detected boxes that represent characters of the license plate, this function now etracts from these boxes the license plate number.
|
||||
//it can deal with license pates that have two lines of charcaters
|
||||
@param[in] l_classIds : classes indeces of detected boxes.
|
||||
@return the lpn extracted out of detections
|
||||
@see
|
||||
*/
|
||||
std::string get_lpn(const std::vector<int>& l_classIds);
|
||||
/**
|
||||
@brief
|
||||
//the dnn has detected boxes that represent characters of the license plate, this function now etracts from these boxes the license plate number.
|
||||
//it can deal with license pates that have two lines of charcaters
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] confidences : confidences of each bb detected by the dnn
|
||||
@param[in] classIds : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[out] l_vect_of_boxes_in_a_license_plate : double list of detected boxes when they are grouped. A single list contains all the boxes of a single vehicle present in image.
|
||||
@param[out] l_vect_of_confidences_in_a_license_plate : double list of corresponding confidences.
|
||||
@param[out] l_vect_of_classIds_in_a_license_plate : double list of corresponding indeces.
|
||||
@param[out] l_vect_of_boxes_in_a_license_plate_tri_left : double list of detected boxes when they rearranged in license plates from left to right
|
||||
@param[out] l_vect_of_confidences_in_a_license_plate_tri_left : double list of corresponding confidences.
|
||||
@param[out] l_vect_of_classIds_in_a_license_plate_tri_left : double list of corresponding indeces.
|
||||
@param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
void separate_license_plates_if_necessary_add_blank_vehicles(
|
||||
//raw detections
|
||||
const std::vector<cv::Rect>& boxes, const std::vector<float>& confidences, const std::vector<int>& classIds,
|
||||
//detections when they are separated license plates by license plates
|
||||
std::list<std::string>& lpns, std::list < std::list<cv::Rect>>& l_vect_of_boxes_in_a_license_plate,
|
||||
std::list < std::list<float>>& l_vect_of_confidences_in_a_license_plate, std::list <std::list<int>>& l_vect_of_classIds_in_a_license_plate,
|
||||
//double lists (one element list for each lp detected) of detected characters inside a lp
|
||||
std::list < std::vector<cv::Rect>>& l_vect_of_boxes_in_a_license_plate_tri_left,
|
||||
std::list < std::vector<float>>& l_vect_of_confidences_in_a_license_plate_tri_left, std::list <std::vector<int>>& l_vect_of_classIds_in_a_license_plate_tri_left,
|
||||
const int classId_last_country, //classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
const float nmsThreshold = 0.5f
|
||||
);
|
||||
/**
|
||||
@brief
|
||||
//the dnn has detected boxes that represent characters of the license plate, this function now groups characters in the same license plate and then rearranged from left to right.
|
||||
//it can deal with license pates that have two lines of charcaters.
|
||||
//Produces double linked lists : inside list is for characters and outside list is for plates.
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] confidences : confidences of each bb detected by the dnn
|
||||
@param[in] classIds : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[out] l_vect_of_boxes_in_a_license_plate : double list of detected boxes when they are grouped. A single list contains all the boxes of a single vehicle present in image.
|
||||
@param[out] l_vect_of_confidences_in_a_license_plate : double list of corresponding confidences.
|
||||
@param[out] l_vect_of_classIds_in_a_license_plate : double list of corresponding indeces.
|
||||
@param[out] l_vect_of_boxes_in_a_license_plate_tri_left : double list of detected boxes when they rearranged in license plates from left to right
|
||||
@param[out] l_vect_of_confidences_in_a_license_plate_tri_left : double list of corresponding confidences.
|
||||
@param[out] l_vect_of_classIds_in_a_license_plate_tri_left : double list of corresponding indeces.
|
||||
@param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
//groups detected boxes that correspond to the same vehicle. The separation is based on detected license plates by the dnn
|
||||
void separate_license_plates_if_necessary_add_blank_vehicles(
|
||||
//raw detections
|
||||
const std::list<cv::Rect>& boxes, const std::list<float>& confidences, const std::list<int>& classIds,
|
||||
//detections when they are separated license plates by license plates
|
||||
std::list<std::string>& lpns, std::list < std::list<cv::Rect>>& l_vect_of_boxes_in_a_license_plate,
|
||||
std::list < std::list<float>>& l_vect_of_confidences_in_a_license_plate, std::list <std::list<int>>& l_vect_of_classIds_in_a_license_plate,
|
||||
//double lists (one element list for each lp detected) of detected characters inside a lp
|
||||
std::list < std::vector<cv::Rect>>& l_vect_of_boxes_in_a_license_plate_tri_left,
|
||||
std::list < std::vector<float>>& l_vect_of_confidences_in_a_license_plate_tri_left, std::list <std::vector<int>>& l_vect_of_classIds_in_a_license_plate_tri_left,
|
||||
const int classId_last_country, const float nmsThreshold
|
||||
);
|
||||
/**
|
||||
@brief
|
||||
//the dnn has detected boxes that represent characters of the license plate, this function now groups characters in the same license plate and then rearranged from left to right.
|
||||
//it can deal with license pates that have two lines of charcaters.
|
||||
//Produces double linked lists : inside list is for characters and outside list is for plates.
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] confidences : confidences of each bb detected by the dnn
|
||||
@param[in] classIds : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[out] l_vect_of_boxes_in_a_license_plate : double list of detected boxes when they are grouped. A single list contains all the boxes of a single vehicle present in image.
|
||||
@param[out] l_vect_of_confidences_in_a_license_plate : double list of corresponding confidences.
|
||||
@param[out] l_vect_of_classIds_in_a_license_plate : double list of corresponding indeces.
|
||||
@param[out] l_vect_of_boxes_in_a_license_plate_tri_left : double list of detected boxes when they rearranged in license plates from left to right
|
||||
@param[out] l_vect_of_confidences_in_a_license_plate_tri_left : double list of corresponding confidences.
|
||||
@param[out] l_vect_of_classIds_in_a_license_plate_tri_left : double list of corresponding indeces.
|
||||
@param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
void separate_license_plates_if_necessary_add_blank_vehicles(
|
||||
//raw detections
|
||||
const std::vector<cv::Rect>& boxes, const std::vector<float>& confidences, const std::vector<int>& classIds,
|
||||
//detections when they are separated license plates by license plates
|
||||
std::list<std::string>& lpns, std::list < std::vector<cv::Rect>>& l_vect_of_boxes_in_a_license_plate,
|
||||
std::list < std::vector<float>>& l_vect_of_confidences_in_a_license_plate, std::list <std::vector<int>>& l_vect_of_classIds_in_a_license_plate,
|
||||
//double lists (one element list for each lp detected) of detected characters inside a lp
|
||||
std::list < std::vector<cv::Rect>>& l_vect_of_boxes_in_a_license_plate_tri_left,
|
||||
std::list < std::vector<float>>& l_vect_of_confidences_in_a_license_plate_tri_left, std::list <std::vector<int>>& l_vect_of_classIds_in_a_license_plate_tri_left,
|
||||
const int classId_last_country, const float nmsThreshold = 0.5f
|
||||
);
|
||||
/**
|
||||
@brief
|
||||
//the dnn has detected boxes that represent characters of the license plate, this function now groups characters in the same license plate and then rearranged from left to right.
|
||||
//it can deal with license pates that have two lines of charcaters.
|
||||
//Produces double linked lists : inside list is for characters and outside list is for plates.
|
||||
@param[in] boxes : set of detected boxes
|
||||
@param[in] confidences : confidences of each bb detected by the dnn
|
||||
@param[in] classIds : set of indeces that indicate the classes of each of these detected boxes
|
||||
@param[out] l_vect_of_boxes_in_a_license_plate : double list of detected boxes when they are grouped. A single list contains all the boxes of a single vehicle present in image.
|
||||
@param[out] l_vect_of_confidences_in_a_license_plate : double list of corresponding confidences.
|
||||
@param[out] l_vect_of_classIds_in_a_license_plate : double list of corresponding indeces.
|
||||
@param[out] l_vect_of_boxes_in_a_license_plate_tri_left : double list of detected boxes when they rearranged in license plates from left to right
|
||||
@param[out] l_vect_of_confidences_in_a_license_plate_tri_left : double list of corresponding confidences.
|
||||
@param[out] l_vect_of_classIds_in_a_license_plate_tri_left : double list of corresponding indeces.
|
||||
@param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
void separate_license_plates_if_necessary_add_blank_vehicles(
|
||||
//raw detections
|
||||
const std::list<cv::Rect>& boxes, const std::list<float>& confidences, const std::list<int>& classIds,
|
||||
//detections when they are separated license plates by license plates
|
||||
std::list<std::string>& lpns, std::list < std::vector<cv::Rect>>& l_vect_of_boxes_in_a_license_plate,
|
||||
std::list < std::vector<float>>& l_vect_of_confidences_in_a_license_plate, std::list <std::vector<int>>& l_vect_of_classIds_in_a_license_plate,
|
||||
//double lists (one element list for each lp detected) of detected characters inside a lp
|
||||
std::list < std::vector<cv::Rect>>& l_vect_of_boxes_in_a_license_plate_tri_left,
|
||||
std::list < std::vector<float>>& l_vect_of_confidences_in_a_license_plate_tri_left, std::list <std::vector<int>>& l_vect_of_classIds_in_a_license_plate_tri_left,
|
||||
const int classId_last_country, const float nmsThreshold = 0.5f
|
||||
);
|
||||
//extracts, from a test directory, all images files
|
||||
//void load_images_filenames(const std::string& dir, std::list<std::string>& image_filenames);
|
||||
/***
|
||||
* @brief Padded resize
|
||||
* @param src - input image
|
||||
* @param dst - output image
|
||||
* @param out_size - desired output size
|
||||
* @return padding information - pad width, pad height and zoom scale
|
||||
*/
|
||||
std::vector<float> LetterboxImage(const cv::Mat& src, cv::Mat& dst, const cv::Size& out_size = cv::Size(640, 640));
|
||||
/***
|
||||
* @brief Rescale coordinates to original input image
|
||||
* @param data - detection result after inference and nms
|
||||
* @param pad_w - width padding
|
||||
* @param pad_h - height padding
|
||||
* @param scale - zoom scale
|
||||
* @param img_shape - original input image shape
|
||||
*/
|
||||
void ScaleCoordinates(std::vector<Detection>& data, float pad_w, float pad_h,
|
||||
float scale, const cv::Size& img_shape);
|
||||
/***
|
||||
* @brief Performs Non-Maximum Suppression (NMS) on inference results
|
||||
* @note For 640x640 image, 640 / 32(max stride) = 20, sum up boxes from each yolo layer with stride (8, 16, 32) and
|
||||
* 3 scales at each layer, we can get total number of boxes - (20x20 + 40x40 + 80x80) x 3 = 25200
|
||||
* @param detections - inference results from the network, example [1, 25200, 85], 85 = 4(xywh) + 1(obj conf) + 80(class score)
|
||||
* @param modelWidth - width of model input 640
|
||||
* @param modelHeight - height of model input 640
|
||||
* @param conf_threshold - object confidence(objectness) threshold
|
||||
* @param iou_threshold - IoU threshold for NMS algorithm
|
||||
* @return detections with shape: nx7 (batch_index, x1, y1, x2, y2, score, classification)
|
||||
*/
|
||||
std::vector<std::vector<Detection>> PostProcessing(
|
||||
float* output, // output of onnx runtime ->>> 1,25200,85
|
||||
size_t dimensionsCount,
|
||||
size_t size, // 1x25200x85=2142000
|
||||
int dimensions,
|
||||
float modelWidth, float modelHeight, const cv::Size& img_shape,
|
||||
float conf_threshold, float iou_threshold);
|
||||
/***
|
||||
* @brief Performs Non-Maximum Suppression (NMS) on inference results
|
||||
* @note For 640x640 image, 640 / 32(max stride) = 20, sum up boxes from each yolo layer with stride (8, 16, 32) and
|
||||
* 3 scales at each layer, we can get total number of boxes - (20x20 + 40x40 + 80x80) x 3 = 25200
|
||||
* @param detections - inference results from the network, example [1, 25200, 85], 85 = 4(xywh) + 1(obj conf) + 80(class score)
|
||||
* @param pad_w - width padding
|
||||
* @param pad_h - height padding
|
||||
* @param scale - zoom scale
|
||||
* @param conf_threshold - object confidence(objectness) threshold
|
||||
* @param iou_threshold - IoU threshold for NMS algorithm
|
||||
* @return detections with shape: nx7 (batch_index, x1, y1, x2, y2, score, classification)
|
||||
*/
|
||||
std::vector<std::vector<Detection>> PostProcessing(
|
||||
float* output, // output of onnx runtime ->>> 1,25200,85
|
||||
size_t dimensionsCount,
|
||||
size_t size, // 1x25200x85=2142000
|
||||
int dimensions,
|
||||
//pad_w is the left (and also right) border width in the square image feeded to the model
|
||||
float pad_w, float pad_h, float scale, const cv::Size& img_shape,
|
||||
float conf_threshold, float iou_threshold);
|
||||
//if two boxes have an iou (intersection over union) that is too large, then they cannot represent two adjacent characters of the license plate
|
||||
//so we discard the one with the lowest confidence rate
|
||||
void filter_iou(std::vector<int>& classIds,
|
||||
std::vector<float>& confidences,
|
||||
std::vector<cv::Rect>& vect_of_detected_boxes, const float& nmsThreshold = 0.5f);
|
||||
bool plates_types_differ_with_one_character(const std::string& type1, std::string& lpn
|
||||
, const std::string& type2);
|
||||
//this func extract all info from the filenames of the platesmania dataset :
|
||||
std::string get_plate_sub_type(const std::string& lpn);
|
||||
//this func extract all info from the filenames of the platesmania dataset :
|
||||
std::string get_plate_sub_type(const std::list<char>& lpn);
|
||||
std::vector<std::string> get_plates_types_labels(const std::string& filename);
|
||||
#endif // !defined(UTILS_ANLR_ONNX_H)
|
||||
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
// Line.h: interface for the C_Line class.
|
||||
*/
|
||||
#if !defined(UTILS_IMAGE_FILE_H)
|
||||
#define UTILS_IMAGE_FILE_H
|
||||
#pragma once
|
||||
#include <list>
|
||||
#include "Levenshtein.h"
|
||||
#include <opencv2/core.hpp>
|
||||
/**
|
||||
@brief
|
||||
//return the ascii character that corresponds to index class output by the dnn
|
||||
@param classe : integer index = class identifier, output by the object detection dnn
|
||||
@return an ascii character
|
||||
@see
|
||||
*/
|
||||
char get_char(const int classe);
|
||||
/**
|
||||
@brief
|
||||
//checks if the characters contained in lpn are compatible with the alphabet
|
||||
@param lpn: the registration of the vehicle as a string
|
||||
@return
|
||||
@see
|
||||
*/
|
||||
bool could_be_lpn(const std::string& lpn);
|
||||
/**
|
||||
@brief
|
||||
returns the true license plate number out of a filename
|
||||
you must place the true license plate number in the image filename this way : number+underscore+license plate number,
|
||||
for instance filename 0000000001_3065WWA34.jpg will be interpreted as an image with the license plate 3065WWA34 in it.
|
||||
@param filename: the image filename that contains in it the true registration number
|
||||
@return the lpn contained in the image filename
|
||||
@see
|
||||
*/
|
||||
//retourne l'index du caractere LPChar
|
||||
int get_index(const char LPChar);
|
||||
std::string getTrueLPN(const std::string& filename, const bool& vrai_lpn_after_underscore);
|
||||
//extracts from a test directory all images files
|
||||
void load_images_filenames(const std::string& dir, std::list<std::string>& image_filenames);
|
||||
#endif // !defined(UTILS_IMAGE_FILE_H)
|
||||
@@ -0,0 +1,373 @@
|
||||
#if !defined(UTILS_OPEN_CV)
|
||||
#define UTILS_OPEN_CV
|
||||
#include "Line.h"
|
||||
#include <opencv2/opencv.hpp>
|
||||
//#include <opencv2/text.hpp>
|
||||
#define cvGetSeqElemLPR( param1, param2, param3) cvGetSeqElem( param1, param2)
|
||||
/**
|
||||
@brief
|
||||
//for each box in the container, check that it is nearly entirely contained in the second argument
|
||||
@param box : box a bounding box
|
||||
@param rect_im : ROi or second bounding box
|
||||
@return true if intersection is at least 90% of the box (which means box is nearly entirely in the second argument)
|
||||
@see
|
||||
*/
|
||||
//for each box in the container, check that it is nearly entirely contained in the second argument
|
||||
bool is_in_rect_if(const std::list<cv::Rect>& boxes, const cv::Rect& rect_im);
|
||||
/**
|
||||
@brief
|
||||
//for each box in the container, check that it is nearly entirely contained in the second argument
|
||||
@param box : box a bounding box
|
||||
@param rect_im : ROi or second bounding box
|
||||
@return true if intersection is at least 90% of the box (which means box is nearly entirely in the second argument)
|
||||
@see
|
||||
*/
|
||||
//for each box in the container, check that it is nearly entirely contained in the second argument
|
||||
bool is_in_rect_if(const std::vector<cv::Rect>& boxes, const cv::Rect& rect_im);
|
||||
/**
|
||||
@brief
|
||||
//returns the iou (intersection over union) of two boxes
|
||||
@param r1 : first rectangle
|
||||
@param r2 : second rectangle
|
||||
@return the iou (a float value between 0 and 1)
|
||||
@see
|
||||
*/
|
||||
//float iou(const cv::Rect& r1, const cv::Rect& r2);
|
||||
std::list<cv::Rect> fiter_out(
|
||||
const std::vector<cv::Rect>& true_boxes, // the true boxes extracted from pascal voc xml file, as a list
|
||||
const std::list<cv::Rect>& detected_boxes, // the boxes detected by nn detector, stored in a list of rectagles objects
|
||||
const std::list<float>& ious,
|
||||
std::list<float>& out_ious);
|
||||
std::list<cv::Rect> fiter_out(
|
||||
const std::list<cv::Rect>& true_boxes, // the true boxes extracted from pascal voc xml file, as a list
|
||||
const std::list<cv::Rect>& detected_boxes, // the boxes detected by nn detector, stored in a list of rectagles objects
|
||||
const std::list<float>& ious,
|
||||
std::list<float>& out_ious);
|
||||
float iou(
|
||||
const std::list<cv::Rect>& true_boxes, // the true boxes extracted from pascal voc xml file, as a list
|
||||
const std::list<cv::Rect>& detected_boxes // the boxes detected by nn detector, stored in a list of rectagles objects
|
||||
);
|
||||
float iou(
|
||||
const std::list<cv::Rect>& boxes, // the true boxes extracted from pascal voc xml file, as a list
|
||||
const cv::Rect& box // the boxes detected by nn detector, stored in a list of rectagles objects
|
||||
);
|
||||
/**
|
||||
@brief
|
||||
//rearrange bounding boxes from left to right
|
||||
cette fonction trie la liste de gauche a droite
|
||||
@return reordered set of boxes
|
||||
@see
|
||||
*/
|
||||
std::list<cv::Rect> sort_from_left_to_right(const std::list<cv::Rect>& boxes);
|
||||
/**
|
||||
@brief
|
||||
//rearrange bounding boxes from left to right
|
||||
cette fonction trie la liste de gauche a droite
|
||||
*@param[in] number_of_characters_latin_numberplate : number of characters in a latin alphabet(usually 36 = 26 letters + 10 digits)
|
||||
@return reordered set of boxes
|
||||
@see
|
||||
*/
|
||||
std::list<cv::Rect> sort_from_left_to_right(const std::list<cv::Rect>& boxes, const std::list<int>& classes, std::list<int>& sorted_classes,
|
||||
const int number_of_characters_latin_numberplate
|
||||
//, const int index_first_mark_model
|
||||
);
|
||||
/**
|
||||
@brief
|
||||
//rearrange bounding boxes from left to right
|
||||
cette fonction trie la liste de gauche a droite
|
||||
@return reordered set of boxes
|
||||
@see
|
||||
*/
|
||||
void sort_from_left_to_right(
|
||||
std::list<cv::Rect>& boxes, std::list<float>& confidences,
|
||||
std::list<int>& classIds_);
|
||||
/**
|
||||
@brief
|
||||
cette fonction trie la liste de gauche a droite
|
||||
//rearrange detected bounding boxes from left to right
|
||||
@param[out] :std list of detected boxes
|
||||
@param[out] confidences : confidences of detected boxes
|
||||
@param[out] classIds : std::list of indeces that indicate the classes of each of the above detected boxes
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
void sort_from_left_to_right(
|
||||
std::list<cv::Rect>& boxes,
|
||||
std::list<int>& classIds_);
|
||||
bool is_on_the_left(const cv::Rect& box1, const cv::Rect& box2);
|
||||
//
|
||||
/**
|
||||
@brief cette fonction trie la liste de gauche a droite
|
||||
//rearrange detected bounding boxes from left to right
|
||||
@param[out] :std list of detected boxes
|
||||
@param[out] confidences : confidences of detected boxes
|
||||
@param[out] classIds : std::list of indeces that indicate the classes of each of the above detected boxes
|
||||
@return void
|
||||
@see
|
||||
*/
|
||||
void sort_from_left_to_right(
|
||||
std::list<cv::Rect>& boxes, std::list<float>& confidences);
|
||||
bool is_in_rect(const std::list<cv::Rect>& boxes, const cv::Rect& rect_im);
|
||||
bool is_in_rect(const cv::Rect& box, const cv::Rect& rect_im);
|
||||
bool is_in_rect(const cv::Point& pt, const cv::Rect& rect_im);
|
||||
//type_of_roi_for_iou==1 large
|
||||
//type_of_roi_for_iou ==2 right_side
|
||||
//type_of_roi_for_iou == 3 left side
|
||||
//type_of_roi_for_iou ==0 no expansion
|
||||
//float detect(const cv::Mat & frame, const std::list<cv::Rect>& boxes, const cv::Rect& box, const int w, const int type_of_roi_for_iou);
|
||||
void get_mean_std(const cv::Mat & frame, const cv::Rect& box, float& mean, float& standard_deviation);
|
||||
bool get_upperand_lower_lines
|
||||
(const std::list<cv::Rect>& boxes, C_Line& line_sup, C_Line& line_inf);
|
||||
struct MSERParams
|
||||
{
|
||||
MSERParams(int _delta = 5, int _min_area = 60, int _max_area = 14400,
|
||||
double _max_variation = 0.25, double _min_diversity = .2,
|
||||
int _max_evolution = 200, double _area_threshold = 1.01,
|
||||
double _min_margin = 0.003, int _edge_blur_size = 5)
|
||||
{
|
||||
delta = _delta;
|
||||
minArea = _min_area;
|
||||
maxArea = _max_area;
|
||||
maxVariation = _max_variation;
|
||||
minDiversity = _min_diversity;
|
||||
maxEvolution = _max_evolution;
|
||||
areaThreshold = _area_threshold;
|
||||
minMargin = _min_margin;
|
||||
edgeBlurSize = _edge_blur_size;
|
||||
pass2Only = false;
|
||||
}
|
||||
int delta;
|
||||
int minArea;
|
||||
int maxArea;
|
||||
double maxVariation;
|
||||
double minDiversity;
|
||||
bool pass2Only;
|
||||
int maxEvolution;
|
||||
double areaThreshold;
|
||||
double minMargin;
|
||||
int edgeBlurSize;
|
||||
};
|
||||
/*
|
||||
bool trouve_la_plaque(const cv::Mat& frame
|
||||
, cv::Point& p0, cv::Point& p1, cv::Point& p2, cv::Point& p3,
|
||||
cv::Point& top_left_,
|
||||
cv::Point& top_right_,
|
||||
cv::Point& bottom_right_,
|
||||
cv::Point& bottom_left_,
|
||||
cv::Rect& rect_OpenLP);*/
|
||||
bool trouve_la_plaque(const cv::Mat& frame, const cv::Rect& global_rect
|
||||
, cv::Point& p0, cv::Point& p1, cv::Point& p2, cv::Point& p3,
|
||||
cv::Point& top_left_,
|
||||
cv::Point& top_right_,
|
||||
cv::Point& bottom_right_,
|
||||
cv::Point& bottom_left_,
|
||||
cv::Rect& rect_OpenLP);
|
||||
cv::Rect get_global_rect(const cv::Point& bottom_right,
|
||||
const cv::Point& bottom_left, const cv::Point& top_right
|
||||
, const cv::Point& top_left);
|
||||
//cette fonction retourne le rect englobant la collection
|
||||
//gets the reunion of all the boxes
|
||||
cv::Rect get_global_rect(const std::list<cv::Rect>& l);
|
||||
//cette fonction retourne le rect englobant la collection
|
||||
//gets the reunion of all the boxes
|
||||
cv::Rect get_global_rect(const std::vector<cv::Rect>& l);
|
||||
void vector_to_list(
|
||||
const std::vector<cv::Rect>& boxes,
|
||||
std::list<cv::Rect>& lboxes);
|
||||
bool trouve_la_plaque(const cv::Mat& frame, const cv::Rect& global_rect
|
||||
, cv::Point& p0, cv::Point& p1, cv::Point& p2, cv::Point& p3,
|
||||
cv::Point& top_left_,
|
||||
cv::Point& top_right_,
|
||||
cv::Point& bottom_right_,
|
||||
cv::Point& bottom_left_,
|
||||
cv::Rect& rect_OpenLP);
|
||||
bool trouve_la_plaque(const cv::Mat& frame,
|
||||
const std::list<int>& classes, const std::list<cv::Rect>& boxes
|
||||
,
|
||||
cv::Point& top_left_,
|
||||
cv::Point& top_right_,
|
||||
cv::Point& bottom_right_,
|
||||
cv::Point& bottom_left_, cv::Rect& rect_OpenLP);
|
||||
bool in_quadrilatere(const cv::Point& pt,
|
||||
const cv::Point& top_left, const cv::Point& top_right, const cv::Point& bottom_right, const cv::Point& bottom_left, const float& signed_distance);
|
||||
bool in_quadrilatere(const cv::Point& pt, const std::vector<cv::Point >& contours, const float& signed_distance);
|
||||
bool in_quadrilatere(const cv::Rect& box, const std::vector<cv::Point >& contours, const float& signed_distance);
|
||||
bool in_quadrilatere(const std::list<cv::Rect>& boxes, const std::vector<cv::Point >& contours, const float& signed_distance);
|
||||
#include "opencv2/imgproc/imgproc_c.h"
|
||||
float cosine(const CvPoint& pt1, const CvPoint& pt2, const CvPoint& pt0);
|
||||
float cosine(const CvPoint* pt1, const CvPoint* pt2, const CvPoint* summit);
|
||||
bool is_2D(const CvSeq* polygone);
|
||||
int zappeUnPoint(const CvSeq* polygone, CvMemStorage* storage);
|
||||
float dist(const CvPoint& pt1, const CvPoint& pt2);
|
||||
CvPoint moyennepond(const CvPoint& pt1, const CvPoint& pt2, const int a1, const int a2);
|
||||
CvPoint moyenne(const CvPoint& pt1, const CvPoint& pt2);
|
||||
CvPoint moyenne4(const CvPoint& pt1, const CvPoint& pt2, const CvPoint& pt3, const CvPoint& pt4);
|
||||
CvPoint trouveleplusproche(const CvPoint& ref, const CvPoint& pt1,
|
||||
const CvPoint& pt2, const CvPoint& pt3, const CvPoint& pt4);
|
||||
CvSeq* sort(const CvSeq* note, CvMemStorage* storage);
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// renvoie une seuence de 4 points encadrant la plaque
|
||||
// trouveesur l'image img
|
||||
// IplImage* img
|
||||
// CvMemStorage* storage
|
||||
//peut traiter les images sources couleur ou en niveaux de gris
|
||||
CvSeq* findSquares4(IplImage* img, CvMemStorage* storage, const int nTresh, //
|
||||
//le nb de composantes couleurs ex nb_composantes_color=3 pour une image RGB
|
||||
const int nb_composantes_color, const int dist_min_bord,
|
||||
const int hauteur_plaque_min, const float& rapport_largeur_sur_hauteur_min);
|
||||
CvSeq* findSquares4
|
||||
(IplImage* im_src, const cv::Rect& global_rect, CvMemStorage* storage,
|
||||
const int nTresh, const int mean_carac
|
||||
, const int mean_fond, //
|
||||
const int dist_min_bord,
|
||||
const int hauteur_plaque_min,
|
||||
const float& rapport_largeur_sur_hauteur_min);
|
||||
CvSeq* findSquares4
|
||||
(IplImage* im_src, CvMemStorage* storage,
|
||||
const int nTresh, //
|
||||
//le nb de composantes couleurs ex nb_composantes_color=3 pour une image RGB
|
||||
const int nb_composantes_color,
|
||||
const int dist_min_bord,
|
||||
const int hauteur_plaque_min,
|
||||
const float& rapport_largeur_sur_hauteur_min);
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// renvoie une seuence de 4 points encadrant la plaque
|
||||
// trouveesur l'image im_src
|
||||
// IplImage* im_src
|
||||
// CvMemStorage* storage
|
||||
//peut traiter les images sources couleur ou en niveaux de gris
|
||||
CvSeq* findSquares4
|
||||
(IplImage* imgR, IplImage* imgG, IplImage* imgB, CvMemStorage* storage,
|
||||
const int nTresh, //
|
||||
//le nb de composantes couleurs ex nb_composantes_color=3 pour une image RGB
|
||||
const int dist_min_bord,
|
||||
const int hauteur_plaque_min,
|
||||
const float& rapport_largeur_sur_hauteur_min);
|
||||
CvSeq* findSquares4_multiresolution
|
||||
(IplImage* im_src, CvMemStorage* storage,
|
||||
const int nTresh, //
|
||||
const int dist_min_bord,
|
||||
const int hauteur_plaque_min,
|
||||
const float& rapport_largeur_sur_hauteur_min);
|
||||
CvSeq* findSquares4_multiresolution(
|
||||
IplImage* im_src, CvMemStorage* storage,
|
||||
const int nTresh, const int mean_carac
|
||||
, const int mean_fond, //
|
||||
const int dist_min_bord,
|
||||
const int hauteur_plaque_min,
|
||||
const float& rapport_largeur_sur_hauteur_min);
|
||||
CvSeq* findSquares4_multiresolution
|
||||
(
|
||||
IplImage* im_src, const cv::Rect& global_rect, CvMemStorage* storage,
|
||||
const int nTresh, const int mean_carac
|
||||
, const int mean_fond, //
|
||||
const int dist_min_bord,
|
||||
const int hauteur_plaque_min,
|
||||
const float& rapport_largeur_sur_hauteur_min
|
||||
);
|
||||
CvSeq* findSquares4_multiresolution
|
||||
(
|
||||
IplImage* im_src, CvMemStorage* storage,
|
||||
const int nTresh, //
|
||||
//le nb de composantes couleurs ex nb_composantes_color=3 pour une image RGB
|
||||
const int nb_composantes_color,
|
||||
const int dist_min_bord,
|
||||
const int hauteur_plaque_min,
|
||||
const float& rapport_largeur_sur_hauteur_min
|
||||
);
|
||||
CvSeq* findSquares4_multiresolution
|
||||
(IplImage* imgR, IplImage* imgG, IplImage* imgB, CvMemStorage* storage,
|
||||
const int nTresh, //
|
||||
//le nb de composantes couleurs ex nb_composantes_color=3 pour une image RGB
|
||||
const int dist_min_bord,
|
||||
const int hauteur_plaque_min,
|
||||
const float& rapport_largeur_sur_hauteur_min);
|
||||
/*
|
||||
CvSeq* findSquares4
|
||||
(IplImage* imgR, IplImage* imgG, IplImage* imgB, CvMemStorage* storage,
|
||||
const int nTresh, //
|
||||
//le nb de composantes couleurs ex nb_composantes_color=3 pour une image RGB
|
||||
const int dist_min_bord,
|
||||
const int hauteur_plaque_min,
|
||||
const float& rapport_largeur_sur_hauteur_min);*/
|
||||
//peut traiter les images sources couleur ou en niveaux de gris
|
||||
/*
|
||||
bool trouve_la_plaque(IplImage* etude, const int nTresh, const int nb_composantes_color,
|
||||
int resultat[4][2], const int dist_min_bord,
|
||||
const int hauteur_plaque_min, const float& rapport_largeur_sur_hauteur_min);*/
|
||||
///////////////////////////////////////////////////////////////////////////////////////////
|
||||
// retourne le nb de points en commun entre deux images
|
||||
float percentage_match(IplImage* img1, IplImage* img2);
|
||||
bool quadrilatere_is_convex(const cv::Point& pt0, const cv::Point& pt1, const cv::Point& pt2, const cv::Point& pt3);
|
||||
//must be convex quadrilatere
|
||||
bool width_is_larger(const cv::Point& pt0, const cv::Point& pt1, const cv::Point& pt2, const cv::Point& pt3);
|
||||
//must be convex quadrilatere
|
||||
bool get_corners(const cv::Point& pt0, const cv::Point& pt1, const cv::Point& pt2, const cv::Point& pt3,
|
||||
cv::Point& top_left, cv::Point& top_right, cv::Point& bottom_right, cv::Point& bottom_left);
|
||||
//must be convex quadrilatere
|
||||
bool get_corners(const IplImage* im, const CvPoint& pt0, const CvPoint& pt1, const CvPoint& pt2, const CvPoint& pt3,
|
||||
cv::Point& top_left, cv::Point& top_right, cv::Point& bottom_right, cv::Point& bottom_left);
|
||||
/*
|
||||
bool trouve_la_plaque(IplImage* etude, int nTresh,
|
||||
cv::Point& p0, cv::Point& p1, cv::Point& p2, cv::Point& p3,
|
||||
cv::Point& top_left_,
|
||||
cv::Point& top_right_,
|
||||
cv::Point& bottom_right_,
|
||||
cv::Point& bottom_left_, cv::Rect& rect_OpenLP,
|
||||
const int dist_min_bord
|
||||
,
|
||||
const int hauteur_plaque_min,
|
||||
const float& rapport_largeur_sur_hauteur_min);
|
||||
//peut traiter les images sources couleur ou en niveaux de gris
|
||||
bool trouve_la_plaque(IplImage* etude, int nTresh
|
||||
, const int mean_carac
|
||||
, const int mean_fond,
|
||||
cv::Point& p0, cv::Point& p1, cv::Point& p2, cv::Point& p3,
|
||||
cv::Point& top_left_,
|
||||
cv::Point& top_right_,
|
||||
cv::Point& bottom_right_,
|
||||
cv::Point& bottom_left_, cv::Rect& rect_OpenLP,
|
||||
const int dist_min_bord
|
||||
,
|
||||
const int hauteur_plaque_min,
|
||||
const float& rapport_largeur_sur_hauteur_min);
|
||||
*/
|
||||
//peut traiter les images sources couleur ou en niveaux de gris
|
||||
bool trouve_la_plaque(IplImage* etude, const cv::Rect& global_rect, int nTresh
|
||||
, const int mean_carac
|
||||
, const int mean_fond,
|
||||
cv::Point& p0, cv::Point& p1, cv::Point& p2, cv::Point& p3,
|
||||
cv::Point& top_left_,
|
||||
cv::Point& top_right_,
|
||||
cv::Point& bottom_right_,
|
||||
cv::Point& bottom_left_, cv::Rect& rect_OpenLP,
|
||||
const int dist_min_bord
|
||||
,
|
||||
const int hauteur_plaque_min,
|
||||
const float& rapport_largeur_sur_hauteur_min);
|
||||
bool trouve_la_plaque(const cv::Mat& frame, const cv::Rect& global_rect
|
||||
, cv::Point& p0, cv::Point& p1, cv::Point& p2, cv::Point& p3,
|
||||
cv::Point& top_left_,
|
||||
cv::Point& top_right_,
|
||||
cv::Point& bottom_right_,
|
||||
cv::Point& bottom_left_,
|
||||
cv::Rect& rect_OpenLP);
|
||||
bool trouve_la_plaque(const cv::Mat& frame,
|
||||
const std::list<int>& classes, const std::list<cv::Rect>& boxes
|
||||
,
|
||||
cv::Point& top_left_,
|
||||
cv::Point& top_right_,
|
||||
cv::Point& bottom_right_,
|
||||
cv::Point& bottom_left_, cv::Rect& rect_OpenLP);
|
||||
std::string get_plate_type(
|
||||
const std::list<cv::Rect>& vect_of_detected_boxes,
|
||||
const std::list<int>& classIds, const int number_of_characters_latin_numberplate
|
||||
);
|
||||
std::string get_plate_type(
|
||||
const std::vector<cv::Rect>& vect_of_detected_boxes,
|
||||
const std::vector<int>& classIds, const int number_of_characters_latin_numberplate
|
||||
);
|
||||
#endif // !defined UTILS_OPEN_CV
|
||||
#pragma once
|
||||
@@ -0,0 +1,532 @@
|
||||
/*
|
||||
// Line.h: interface for the C_Line class.
|
||||
*/
|
||||
#if !defined(YOLOV5_ALPR_ONNX_DETECTOR)
|
||||
#define YOLOV5_ALPR_ONNX_DETECTOR
|
||||
#include "ONNX_detector.h"
|
||||
class Plates_types_classifier : public OnnxDetector
|
||||
{
|
||||
public:
|
||||
//**************************
|
||||
// construct/destruct
|
||||
//**************************
|
||||
Plates_types_classifier(Ort::Env& env, const void* model_data, size_t model_data_length, const Ort::SessionOptions& options
|
||||
, const std::vector<std::string>& labels_);
|
||||
Plates_types_classifier(Ort::Env& env, const ORTCHAR_T* model_path, const Ort::SessionOptions& options
|
||||
, const std::vector<std::string>& labels_);
|
||||
Plates_types_classifier(Ort::Env& env, const ORTCHAR_T* model_path, const Ort::SessionOptions& options
|
||||
, const std::string& labels_filename);
|
||||
Plates_types_classifier(Ort::Env& env, const void* model_data, size_t model_data_length, const Ort::SessionOptions& options
|
||||
, const std::string& labels_filename);
|
||||
OnnxDetector& get_ref() {
|
||||
return *this;
|
||||
};
|
||||
const Plates_types_classifier& get_const_ref() {
|
||||
return *this;
|
||||
};
|
||||
virtual ~Plates_types_classifier();
|
||||
std::string GetPlatesType(const cv::Mat& img, float& uncalibrated_confidence);
|
||||
void get_best_plate(const cv::Mat& frame,
|
||||
//detections when they are separated license plates by license plates
|
||||
const std::list < std::list<int>>& classIds, const std::list < std::list<float>>& confidences, const std::list < std::list<cv::Rect>>& boxes
|
||||
//output the list of the best (most probable/readable) lp
|
||||
, std::list<float>& confidence_one_lp, std::list < cv::Rect>& one_lp, std::list<int>& classIds_one_lp);
|
||||
//nov 21 update this func with plates_types_classifier classifier
|
||||
//uses double linked lists : inside list is for characters and outside list is for plates.
|
||||
//For each plate in the image, the detections have been separated. From these, we select the detections of the plates that have have the best detection score.
|
||||
void get_best_plate(const cv::Mat& frame,
|
||||
//detections when they are separated license plates by license plates
|
||||
const std::list < std::vector<int>>& classIds, const std::list < std::vector<float>>& confidences, const std::list < std::vector<cv::Rect>>& boxes
|
||||
//output the list of the best (most probable/readable) lp
|
||||
, std::list<float>& confidence_one_lp, std::list < cv::Rect>& one_lp, std::list<int>& classIds_one_lp);
|
||||
private:
|
||||
int GetPlatesClasse(const cv::Mat& img, float& uncalibrated_confidence);
|
||||
protected:
|
||||
std::vector<std::string> labels;
|
||||
};
|
||||
class Yolov5_alpr_onxx_detector : public OnnxDetector
|
||||
{
|
||||
public:
|
||||
//**************************
|
||||
// construct/destruct
|
||||
//**************************
|
||||
Yolov5_alpr_onxx_detector(Ort::Env& env, const void* model_data, size_t model_data_length, const Ort::SessionOptions& options);
|
||||
Yolov5_alpr_onxx_detector(Ort::Env& env, const ORTCHAR_T* model_path, const Ort::SessionOptions& options);
|
||||
OnnxDetector& get_ref() {
|
||||
return *this;
|
||||
};
|
||||
const OnnxDetector& get_const_ref() {
|
||||
return *this;
|
||||
};
|
||||
virtual ~Yolov5_alpr_onxx_detector();
|
||||
/** @brief Given the @p input frame, create input blob, run net then, from result detections, assembies license plates present in the input image.
|
||||
* uses double linked lists : inside list is for characters and outside list is for plates.
|
||||
* @param[in] frame : input image.
|
||||
* @param[out] classIds : classes indeces in resulting detected bounding boxes.
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : set of detected bounding boxes.
|
||||
* @param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
*/
|
||||
void detect_with_different_confidences_then_separate_plates(const cv::Mat& frame, std::list<std::vector<int>>& classIds,
|
||||
std::list < std::vector<float>>& confidences, std::list < std::vector<cv::Rect>>& boxes,
|
||||
float nmsThreshold);
|
||||
/** @brief Given the @p input frame, create input blob, run net and return result detections.
|
||||
* @param[in] frame : input image.
|
||||
* @param[out] classIds : classes indeces in resulting detected bounding boxes.
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : set of detected bounding boxes.
|
||||
* @param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
*/
|
||||
void raw_detections_with_different_confidences(const cv::Mat& frame, std::list<std::list<int>>& classIds,
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<cv::Rect>>& boxes,
|
||||
float nmsThreshold);
|
||||
/** @brief Given the @p input frame, create input blob, run net then, from result detections, assembies license plates present in the input image.
|
||||
* uses double linked lists : inside list is for characters and outside list is for plates.
|
||||
* @param[in] frame : input image.
|
||||
* @param[out] classIds : classes indeces in resulting detected bounding boxes.
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : set of detected bounding boxes.
|
||||
* @param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
* @param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
*/
|
||||
void detect_with_different_confidences_then_separate_plates(const cv::Mat& frame, std::list<std::vector<int>>& classIds,
|
||||
std::list < std::vector<float>>& confidences, std::list < std::vector<cv::Rect>>& boxes, std::list <std::list<std::string>>& lpns,
|
||||
float nmsThreshold, const int classId_last_country//classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
);
|
||||
/** @brief Given the @p input frame, create input blob, run net then, from result detections, assembies license plates present in the input image.
|
||||
* @param[in] frame : input image.
|
||||
* @param[out] classIds : classes indeces in resulting detected bounding boxes.
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : set of detected bounding boxes.
|
||||
* @param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
* @param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
*/
|
||||
void detect_with_different_confidences_then_separate_plates(const cv::Mat& frame, std::list<std::list<int>>& classIds,
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<cv::Rect>>& boxes,
|
||||
std::list <std::list<std::string>>& lpns,
|
||||
float nmsThreshold, const int classId_last_country//classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
);
|
||||
/** @brief Given the @p input frame, create input blob, run net and return result detections.
|
||||
* @param[in] frame : input image.
|
||||
* @param[out] classIds : classes indeces in resulting detected bounding boxes.
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : set of detected bounding boxes.
|
||||
* @param[in] confThreshold A threshold used to filter boxes by confidences.
|
||||
* @param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
*/
|
||||
void detect(const cv::Mat& frame, std::vector<int>& classIds, std::vector<float>& confidences, std::vector<cv::Rect>& boxes,
|
||||
const float confThreshold = 0.7f, float nmsThreshold = 0.5f);
|
||||
// Given the @p input frame, create input blob, run net and return result detections.
|
||||
//this func can manage list of boxes of characters that dont have an englobing lp box (gloabal rect)
|
||||
//output lists look like : first box = license plate (either a detected box either the global rect englobing characters boxes, second element = vehicle (either a detected vehicle either (0,0,0,0)
|
||||
//Produces double linked lists : inside list is for characters and outside list is for plates.
|
||||
/** @brief
|
||||
* @param[in] frame : input image.
|
||||
* @param[out] classIds : classes indeces in resulting detected bounding boxes.
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : set of detected bounding boxes.
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
* @param[in] confThreshold A threshold used to filter boxes by confidences.
|
||||
* @param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
*/
|
||||
//and remaining elements are characters
|
||||
void detect_and_add_lp_and_vehicle_if_necessary(const cv::Mat& frame, std::list<std::vector<int>>& classIds,
|
||||
std::list < std::vector<float>>& confidences, std::list < std::vector<cv::Rect>>& boxes,
|
||||
std::list<std::string>& lpns,
|
||||
const int classId_last_country,
|
||||
const float confThreshold = 0.7f, float nmsThreshold = 0.5f);
|
||||
// Given the @p input frame, create input blob, run net and return result detections.
|
||||
//this func can manage list of boxes of characters that dont have an englobing lp box (gloabal rect)
|
||||
//output lists look like : first box = license plate (either a detected box either the global rect englobing characters boxes, second element = vehicle (either a detected vehicle either (0,0,0,0)
|
||||
//and remaining elements are characters
|
||||
/** @brief
|
||||
* @param[in] frame : input image.
|
||||
* @param[out] classIds : classes indeces in resulting detected bounding boxes.
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : set of detected bounding boxes.
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
* @param[in] confThreshold A threshold used to filter boxes by confidences.
|
||||
* @param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
*/
|
||||
void detect_and_add_lp_and_vehicle_if_necessary(const cv::Mat& frame, std::list<std::list<int>>& classIds,
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<cv::Rect>>& boxes,
|
||||
std::list<std::string>& lpns,
|
||||
const int classId_last_country,
|
||||
const float confThreshold = 0.7f, float nmsThreshold = 0.5f);
|
||||
std::list<cv::Rect> TwoStage_LPR(Yolov5_alpr_onxx_detector& parking_detector, const cv::Mat& frame, std::string& lpn);
|
||||
std::list<cv::Rect> TwoStageLPR(Yolov5_alpr_onxx_detector& parking_detector, Plates_types_classifier& plates_types_classifier, const cv::Mat& frame, std::string& lpn);
|
||||
/** @brief Given the @p input frame, create input blob, run net and return result detections.
|
||||
* @param[in] frame : input image.
|
||||
* @param[out] classIds : classes indeces in resulting detected bounding boxes.
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : set of detected bounding boxes.
|
||||
* @param[in] confThreshold A threshold used to filter boxes by confidences.
|
||||
* @param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
*/
|
||||
void detect(const cv::Mat& frame, std::list<int>& classIds, std::list<float>& confidences, std::list<cv::Rect>& boxes,
|
||||
const float confThreshold = 0.7f, float nmsThreshold = 0.5f);
|
||||
/** @brief process an image file.
|
||||
* @param[in] image_filename : filename of the he input image.
|
||||
* @param[out] lpn : the license plate number found in the image by the dnn detector.
|
||||
* @param[in] show_image : boolean if true the image will be displayed in a window with license plate in image banner
|
||||
* @param[in] time_delay : time delay in ms after which the image is destroyed
|
||||
@param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
* @return void
|
||||
* @see
|
||||
*/
|
||||
void detect(const std::string& image_filename, std::list<std::string>& lpns, const int classId_last_country
|
||||
//, const bool show_image=false, const int time_delay=0
|
||||
);
|
||||
/** @brief from image dir, extract license plate numbers.
|
||||
this performs a two stage lpn detection : first a global nn detects lpn of a free flow vehicle, then a second nn focuses and reads the lpn of the previously detected lpn.
|
||||
Difference between this func and detect func is that
|
||||
two_stage_lpr first focus on license plate detection and secondly on its characters. This is supposed to get best results.
|
||||
* @param[in] dir : directory path that contains all images files that will be proceeded.
|
||||
* @return void
|
||||
* @see
|
||||
*/
|
||||
float two_stage_lpr(const std::string& dir);
|
||||
/** @brief from image dir, extract license plate numbers.
|
||||
this performs a two stage lpn detection : first a global nn detects lpn of a free flow vehicle, then a second nn focuses and reads the lpn of the previously detected lpn.
|
||||
Difference between this func and detect func is that
|
||||
two_stage_lpr first focus on license plate detection and secondly on its characters. This is supposed to get best results.
|
||||
* @param[in] dir : directory path that contains all images files that will be proceeded.
|
||||
* @return void
|
||||
* @see
|
||||
*/
|
||||
float two_stage_lpr(Yolov5_alpr_onxx_detector& parking_detector, const std::string& dir);
|
||||
/** @brief from image dir, extract license plate numbers.
|
||||
this performs a two stage lpn detection : first a global nn detects lpn of a free flow vehicle, then a second nn focuses and reads the lpn of the previously detected lpn.
|
||||
Difference between this func and detect func is that
|
||||
two_stage_lpr first focus on license plate detection and secondly on its characters. This is supposed to get best results.
|
||||
* @param[in] frame : input image.
|
||||
* @param[out] lpn : the license plate number found in the image by the dnn detector.
|
||||
@param[in] classes : set of indeces that indicate the classes of each of these detected boxes
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : A set of bounding boxes.
|
||||
* @param[out] chosen_lp_classIds : set of indeces that indicate the classes of each box of the license plate that has been chosen by engine (ie the one with the highest confidence)
|
||||
* @param[out] chosen_lp_confidences : detection confidences of the corresponding boxes
|
||||
* @param[out] chosen_lp_boxes : A set of bounding boxes of the license plate that has been chosen by engine (ie the one with the highest confidence)
|
||||
*/
|
||||
void two_stage_lpr(Yolov5_alpr_onxx_detector& parking_detector, const cv::Mat& frame,
|
||||
//double linked lists to separate lps
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<int>>& classes, std::list < std::list<cv::Rect>>& boxes,
|
||||
//all lps in the image given by lpn (as string), lp country ppronenace (as class index) and lp area in the image (cv::Rect)
|
||||
std::list <std::string>& lpns, std::list <int>& lp_country_class, std::list < cv::Rect>& lp_rois,
|
||||
//detection inside the chosen lp
|
||||
std::list<int>& chosen_lp_classIds, std::list<float>& chosen_lp_confidences, std::list<cv::Rect>& chosen_lp_boxes
|
||||
);
|
||||
/** @brief from image dir, extract license plate numbers.
|
||||
this performs a two stage lpn detection : first a global nn detects lpn of a free flow vehicle, then a second nn focuses and reads the lpn of the previously detected lpn.
|
||||
Difference between this func and detect func is that
|
||||
two_stage_lpr first focus on license plate detection and secondly on its characters. This is supposed to get best results.
|
||||
* @param[in] frame : input image.
|
||||
* @param[out] lpn : the license plate number found in the image by the dnn detector.
|
||||
@param[in] classes : set of indeces that indicate the classes of each of these detected boxes
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : A set of bounding boxes.
|
||||
* @param[out] chosen_lp_classIds : set of indeces that indicate the classes of each box of the license plate that has been chosen by engine (ie the one with the highest confidence)
|
||||
* @param[out] chosen_lp_confidences : detection confidences of the corresponding boxes
|
||||
* @param[out] chosen_lp_boxes : A set of bounding boxes of the license plate that has been chosen by engine (ie the one with the highest confidence)
|
||||
*/
|
||||
void two_stage_lpr(Yolov5_alpr_onxx_detector& parking_detector, Plates_types_classifier& plates_types_classifier, const cv::Mat& frame,
|
||||
//double linked lists to separate lps
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<int>>& classes, std::list < std::list<cv::Rect>>& boxes,
|
||||
//all lps in the image given by lpn (as string), lp country ppronenace (as class index) and lp area in the image (cv::Rect)
|
||||
std::list <std::string>& lpns, std::list <int>& lp_country_class, std::list < cv::Rect>& lp_rois,
|
||||
//detection inside the chosen lp
|
||||
std::list<int>& chosen_lp_classIds, std::list<float>& chosen_lp_confidences, std::list<cv::Rect>& chosen_lp_boxes
|
||||
);
|
||||
/** @brief from image frame, extract license plate number.
|
||||
this performs a two stage lpn detection : first a global nn detects lpn of a free flow vehicle, then a second nn focuses and reads the lpn of the previously detected lpn.
|
||||
Difference between this func and detect func is that
|
||||
two_stage_lpr first focus on license plate detection and secondly on its characters. This is supposed to get best results.
|
||||
* @param[in] image_filename : filename of the he input image.
|
||||
* @param[out] lpn : the license plate number found in the image by the dnn detector.
|
||||
@param[in] classes : set of indeces that indicate the classes of each of these detected boxes
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : A set of bounding boxes.
|
||||
* @param[out] chosen_lp_classIds : set of indeces that indicate the classes of each box of the license plate that has been chosen by engine (ie the one with the highest confidence)
|
||||
* @param[out] chosen_lp_confidences : detection confidences of the corresponding boxes
|
||||
* @param[out] chosen_lp_boxes : A set of bounding boxes of the license plate that has been chosen by engine (ie the one with the highest confidence)
|
||||
*/
|
||||
void two_stage_lpr(Yolov5_alpr_onxx_detector& parking_detector, const std::string& image_filename,
|
||||
//double linked lists to separate lps
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<int>>& classes, std::list < std::list<cv::Rect>>& boxes,
|
||||
//all lps in the image given by lpn (as string), lp country ppronenace (as class index) and lp area in the image (cv::Rect)
|
||||
std::list <std::string>& lpns, std::list <int>& lp_country_class, std::list < cv::Rect>& lp_rois,
|
||||
//detection inside the chosen lp
|
||||
std::list<int>& chosen_lp_classIds, std::list<float>& chosen_lp_confidences, std::list<cv::Rect>& chosen_lp_boxes
|
||||
);
|
||||
/** @brief from image filename, extract license plate number.
|
||||
this performs a two stage lpn detection : first a global nn detects lpn of a free flow vehicle, then a second nn focuses and reads the lpn of the previously detected lpn.
|
||||
Difference between this func and detect func is that
|
||||
two_stage_lpr first focus on license plate detection and secondly on its characters. This is supposed to get best results.
|
||||
* @param[in] image_filename : filename of the he input image.
|
||||
* @param[out] lpn : the license plate number found in the image by the dnn detector.
|
||||
* @param[in] classes : set of indeces that indicate the classes of each of these detected boxes
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : A set of bounding boxes.
|
||||
* @param[out] chosen_lp_classIds : set of indeces that indicate the classes of each box of the license plate that has been chosen by engine (ie the one with the highest confidence)
|
||||
* @param[out] chosen_lp_confidences : detection confidences of the corresponding boxes
|
||||
* @param[out] chosen_lp_boxes : A set of bounding boxes of the license plate that has been chosen by engine (ie the one with the highest confidence)
|
||||
*/
|
||||
void two_stage_lpr(const std::string& image_filename,
|
||||
//double linked lists to separate lps
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<int>>& classes, std::list < std::list<cv::Rect>>& boxes,
|
||||
//all lps in the image given by lpn (as string), lp country ppronenace (as class index) and lp area in the image (cv::Rect)
|
||||
std::list <std::string>& lpns, std::list <int>& lp_country_class, std::list < cv::Rect>& lp_rois,
|
||||
//detection inside the chosen lp
|
||||
std::list<int>& chosen_lp_classIds, std::list<float>& chosen_lp_confidences, std::list<cv::Rect>& chosen_lp_boxes
|
||||
);
|
||||
/** @brief Given the @p input frame, create input blob, run net and return result detections.
|
||||
//output lists look like : first box = license plate (either a detected box either the global rect englobing characters boxes, second element = vehicle (either a detected vehicle either (0,0,0,0)
|
||||
//and remaining elements are characters.
|
||||
//Produces double linked lists : inside list is for characters and outside list is for plates.
|
||||
* @param[in] frame : The input image.
|
||||
* @param[out] classIds Class indexes in result detection.
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : set of detected bounding boxes.
|
||||
* @param[out] one_lp :set of detected boxes when they rearranged from left to right
|
||||
* @param[out] confidence_one_lp : confidences corresponding detected boxes
|
||||
* @param[out] classIds_one_lp : set of indeces of the above detected boxes
|
||||
* @param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
* @param[in] confThreshold A threshold used to filter boxes by confidences.
|
||||
* @param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
*/
|
||||
void detect_lpn_and_add_lp_and_vehicle_if_necessary(const cv::Mat& frame, std::list < std::vector<int>>& classIds,
|
||||
std::list < std::vector<float>>& confidences, std::list < std::vector<cv::Rect>>& boxes
|
||||
, std::list<float>& confidence_one_lp, std::list < cv::Rect>& one_lp, std::list<int>& classIds_one_lp,
|
||||
const int classId_last_country,
|
||||
//const C_OCROutputs& availableAlpha,
|
||||
const float confThreshold = 0.7f, float nmsThreshold = 0.5f);
|
||||
/** @brief Given the @p input frame, create input blob, run net then, from result detections, assembies license plates present in the input image.
|
||||
//it selects just one lpn although all lps have been detected and stored in double linked lists, then from these lists, selects the one that is the best
|
||||
//(with best confidences of its characters and with greateast size)
|
||||
//output lists look like : first box = license plate (either a detected box either the global rect englobing characters boxes, second element = vehicle (either a detected vehicle either (0,0,0,0)
|
||||
//and remaining elements are characters
|
||||
* @param[in] frame : The input image.
|
||||
* @param[out] classIds Class indexes in result detection.
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : set of detected bounding boxes.
|
||||
* @param[out] one_lp :set of detected boxes when they rearranged from left to right
|
||||
* @param[out] confidence_one_lp : confidences corresponding detected boxes
|
||||
* @param[out] classIds_one_lp : set of indeces of the above detected boxes
|
||||
* @param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
* @param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
*/
|
||||
void detect_with_different_confidences_then_separate_plates(const cv::Mat& frame, std::list < std::list<int>>& classIds,
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<cv::Rect>>& boxes
|
||||
, std::list<float>& confidence_one_lp, std::list < cv::Rect>& one_lp, std::list<int>& classIds_one_lp,
|
||||
const int classId_last_country,
|
||||
//const C_OCROutputs& availableAlpha,
|
||||
float nmsThreshold);
|
||||
//
|
||||
//Given the @p input frame, create input blob, run net and return result detections.
|
||||
//it selects just one lpn although all lps are detected in double linked lists
|
||||
////this func can manage list of boxes of characters that dont have an englobing lp box (gloabal rect)
|
||||
////output lists look like : first box = license plate (either a detected box either the global rect englobing characters boxes, second element = vehicle (either a detected vehicle either (0,0,0,0)
|
||||
////and remaining elements are characters
|
||||
//Produces double linked lists : inside list is for characters and outside list is for plates.
|
||||
//
|
||||
void detect_lpn_and_add_lp_and_vehicle_if_necessary(const cv::Mat& frame, Plates_types_classifier& plates_types_classifier, std::list < std::vector<int>>& classIds,
|
||||
std::list < std::vector<float>>& confidences, std::list < std::vector<cv::Rect>>& boxes
|
||||
, std::list<float>& confidence_one_lp, std::list < cv::Rect>& one_lp, std::list<int>& classIds_one_lp,
|
||||
const int classId_last_country,//classId_last_country : is the class index of the last country in the list of detected classes.We remember that ascii(latin) characters come fist(36 classes) then come the license plates countries(another 60 classses) then come a long list of vehicles classes
|
||||
//const C_OCROutputs & availableAlpha,
|
||||
const float confThreshold, float nmsThreshold);
|
||||
//Given the @p input frame, create input blob, run net then, from result detections, assembly license plates present in the input image.
|
||||
void detect_with_different_confidences_then_separate_plates(const cv::Mat& frame, Plates_types_classifier& plates_types_classifier, std::list < std::list<int>>& classIds,
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<cv::Rect>>& boxes
|
||||
, std::list<float>& confidence_one_lp, std::list < cv::Rect>& one_lp, std::list<int>& classIds_one_lp,
|
||||
const int classId_last_country,//classId_last_country : is the class index of the last country in the list of detected classes.We remember that ascii(latin) characters come fist(36 classes) then come the license plates countries(another 60 classses) then come a long list of vehicles classes
|
||||
//const C_OCROutputs & availableAlpha,
|
||||
float nmsThreshold);
|
||||
/** @brief Given the @p input frame, create input blob, run net then, from result detections, assembies license plates present in the input image.
|
||||
//it selects just one lpn although all lps have been detected and stored in double linked lists, then from these lists, selects the one that is the best
|
||||
//(with best confidences of its characters and with greateast size)
|
||||
//output lists look like : first box = license plate (either a detected box either the global rect englobing characters boxes, second element = vehicle (either a detected vehicle either (0,0,0,0)
|
||||
//and remaining elements are characters
|
||||
* @param[in] frame : The input image.
|
||||
@param[in] ExactLPN : the actual license plate number in the image
|
||||
* @param[out] classIds Class indexes in result detection.
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : set of detected bounding boxes.
|
||||
* @param[out] one_lp :set of detected boxes when they rearranged from left to right
|
||||
* @param[out] confidence_one_lp : confidences corresponding detected boxes
|
||||
* @param[out] classIds_one_lp : set of indeces of the above detected boxes
|
||||
* @param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
* @param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
*/
|
||||
void detect_with_different_confidences_then_separate_plates(const cv::Mat& frame, const std::string& ExactLPN, std::list < std::list<int>>& classIds,
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<cv::Rect>>& boxes
|
||||
, std::list<float>& confidence_one_lp, std::list < cv::Rect>& one_lp, std::list<int>& classIds_one_lp,
|
||||
const int classId_last_country,
|
||||
//const C_OCROutputs& availableAlpha,
|
||||
float nmsThreshold);
|
||||
/** @brief Given the @p input frame, create input blob, run net then, from result detections, assembies license plates present in the input image.
|
||||
//it selects just one lpn although all lps have been detected and stored in double linked lists, then from these lists, selects the one that is the best
|
||||
//(with best confidences of its characters and with greateast size)
|
||||
//output lists look like : first box = license plate (either a detected box either the global rect englobing characters boxes, second element = vehicle (either a detected vehicle either (0,0,0,0)
|
||||
//and remaining elements are characters
|
||||
* @param[in] frame : The input image.
|
||||
* @param[in] ExactLPN : the actual license plate number in the image
|
||||
* @param[out] classIds Class indexes in result detection.
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : set of detected bounding boxes.
|
||||
* @param[out] one_lp :set of detected boxes when they rearranged from left to right
|
||||
* @param[out] confidence_one_lp : confidences corresponding detected boxes
|
||||
* @param[out] classIds_one_lp : set of indeces of the above detected boxes
|
||||
* @param[in] nmsThreshold A threshold used in non maximum suppression.
|
||||
* @param[in] classId_last_country : is the class index of the last country in the list of detected classes.
|
||||
*/
|
||||
void detect_with_different_confidences_then_separate_plates(const cv::Mat& frame, const std::string& ExactLPN, std::list < std::list<int>>& classIds,
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<cv::Rect>>& boxes
|
||||
, std::vector<float>& confidence_one_lp, std::vector < cv::Rect>& one_lp, std::vector<int>& classIds_one_lp,
|
||||
const int classId_last_country,
|
||||
//const C_OCROutputs& availableAlpha,
|
||||
float nmsThreshold);
|
||||
};
|
||||
/** @brief from image filename, extract license plate number.
|
||||
this performs a two stage lpn detection : first a global nn detects lpn of a free flow vehicle, then a second nn focuses and reads the lpn of the previously detected lpn.
|
||||
Difference between this func and detect func is that
|
||||
two_stage_lpr first focus on license plate detection and secondly on its characters. This is supposed to get best results.
|
||||
* @param[in] freeflow_detectors : yolo detectors that detect license plates in images that are not focused and that can contain multiple license plates.
|
||||
* @param[in] parking_detectors : yolo detectors that read characters in a localized and focused license plate in image
|
||||
* @param[in] image_filename : filename of the he input image.
|
||||
* @param[out] lpn : the license plate number found in the image by the dnn detector.
|
||||
* @param[in] classes : set of indeces that indicate the classes of each of these detected boxes
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : A set of bounding boxes.
|
||||
* @param[out] chosen_lp_classIds : set of indeces that indicate the classes of each box of the license plate that has been chosen by engine (ie the one with the highest confidence)
|
||||
* @param[out] chosen_lp_confidences : detection confidences of the corresponding boxes
|
||||
* @param[out] chosen_lp_boxes : A set of bounding boxes of the license plate that has been chosen by engine (ie the one with the highest confidence)
|
||||
*/
|
||||
void two_stage_lpr(const std::list<Yolov5_alpr_onxx_detector*>& freeflow_detectors, const std::list<Yolov5_alpr_onxx_detector*>& parking_detectors,
|
||||
const std::string& image_filename,
|
||||
//double linked lists to separate lps
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<int>>& classes, std::list < std::list<cv::Rect>>& boxes,
|
||||
//all lps in the image given by lpn (as string), lp country ppronenace (as class index) and lp area in the image (cv::Rect)
|
||||
std::list <std::string>& lpns, std::list <int>& lp_country_class, std::list < cv::Rect>& lp_rois,
|
||||
//detection inside the chosen lp
|
||||
std::list<int>& chosen_lp_classIds, std::list<float>& chosen_lp_confidences, std::list<cv::Rect>& chosen_lp_boxes
|
||||
);
|
||||
//two stage lpn detection : first a global nn detects lpn of a free flow vehicle, then a second nn focuses and reads the lpn of the previously detected lpn.
|
||||
void two_stage_lpr(const std::list<Yolov5_alpr_onxx_detector*>& freeflow_detectors, const std::list<Yolov5_alpr_onxx_detector*>& parking_detectors
|
||||
, Plates_types_classifier& plates_types_classifier,
|
||||
const std::string& image_filename,
|
||||
//double linked lists to separate lps
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<int>>& classes, std::list < std::list<cv::Rect>>& boxes,
|
||||
//all lps in the image given by lpn (as string), lp country ppronenace (as class index) and lp area in the image (cv::Rect)
|
||||
std::list <std::string>& lpns, std::list <int>& lp_country_class, std::list < cv::Rect>& lp_rois,
|
||||
//detection inside the chosen lp
|
||||
std::list<int>& chosen_lp_classIds, std::list<float>& chosen_lp_confidences, std::list<cv::Rect>& chosen_lp_boxes
|
||||
);
|
||||
//two stage lpn detection : first a global nn detects lpn of a free flow vehicle, then a second nn focuses and reads the lpn of the previously detected lpn.
|
||||
void two_stage_lpr(const std::list<Yolov5_alpr_onxx_detector*>& freeflow_detectors, const std::list<Yolov5_alpr_onxx_detector*>& parking_detectors
|
||||
, Plates_types_classifier& plates_types_classifier,
|
||||
const cv::Mat& frame,
|
||||
//double linked lists to separate lps
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<int>>& classes, std::list < std::list<cv::Rect>>& boxes,
|
||||
//all lps in the image given by lpn (as string), lp country ppronenace (as class index) and lp area in the image (cv::Rect)
|
||||
std::list <std::string>& lpns, std::list <int>& lp_country_class, std::list < cv::Rect>& lp_rois,
|
||||
//detection inside the chosen lp
|
||||
std::list<int>& chosen_lp_classIds, std::list<float>& chosen_lp_confidences, std::list<cv::Rect>& chosen_lp_boxes
|
||||
);
|
||||
//make a bounding box greater (just to be sure we don t miss something in detections)
|
||||
void get_larger_roi(cv::Rect& lpn_roi, const int width, const int height
|
||||
);
|
||||
//make a bounding box greater (just to be sure we don t miss something in detections)
|
||||
void get_larger_roi(cv::Rect& lpn_roi, const int width, const int height, const float& scale_x, const float& scale_y
|
||||
);
|
||||
Yolov5_alpr_onxx_detector* get_detector_with_smallest_size_bigger_than_image(const std::list<Yolov5_alpr_onxx_detector*>& detectors, const int max_size);
|
||||
Yolov5_alpr_onxx_detector* get_detector_with_smallest_size_bigger_than_image(const std::list<Yolov5_alpr_onxx_detector*>& detectors, const int width, const int height);
|
||||
/** @brief from image frame, extract license plate number.
|
||||
this performs a two stage lpn detection : first a global nn detects lpn of a free flow vehicle, then a second nn focuses and reads the lpn of the previously detected lpn.
|
||||
Difference between this func and detect func is that
|
||||
two_stage_lpr first focus on license plate detection and secondly on its characters. This is supposed to get best results.
|
||||
* @param[in] freeflow_detectors : yolo detectors that detect license plates in images that are not focused and that can contain multiple license plates.
|
||||
* @param[in] parking_detectors : yolo detectors that read characters in a localized and focused license plate in image
|
||||
* @param[in] frame : input image.
|
||||
* @param[out] lpn : the license plate number found in the image by the dnn detector.
|
||||
* @param[in] classes : set of indeces that indicate the classes of each of these detected boxes
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : A set of bounding boxes.
|
||||
* @param[out] chosen_lp_classIds : set of indeces that indicate the classes of each box of the license plate that has been chosen by engine (ie the one with the highest confidence)
|
||||
* @param[out] chosen_lp_confidences : detection confidences of the corresponding boxes
|
||||
* @param[out] chosen_lp_boxes : A set of bounding boxes of the license plate that has been chosen by engine (ie the one with the highest confidence)
|
||||
*/
|
||||
void two_stage_lpr(const std::list<Yolov5_alpr_onxx_detector*>& freeflow_detectors, const std::list<Yolov5_alpr_onxx_detector*>& parking_detectors,
|
||||
const cv::Mat& frame,
|
||||
//double linked lists to separate lps
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<int>>& classes, std::list < std::list<cv::Rect>>& boxes,
|
||||
//all lps in the image given by lpn (as string), lp country ppronenace (as class index) and lp area in the image (cv::Rect)
|
||||
std::list <std::string>& lpns, std::list <int>& lp_country_class, std::list < cv::Rect>& lp_rois,
|
||||
//detection inside the chosen lp
|
||||
std::list<int>& chosen_lp_classIds, std::list<float>& chosen_lp_confidences, std::list<cv::Rect>& chosen_lp_boxes
|
||||
);
|
||||
/** @brief from image filename, extract license plate number.
|
||||
this performs a two stage lpn detection : first a global nn detects lpn of a free flow vehicle, then a second nn focuses and reads the lpn of the previously detected lpn.
|
||||
Difference between this func and detect func is that
|
||||
two_stage_lpr first focus on license plate detection and secondly on its characters. This is supposed to get best results.
|
||||
* @param[in] image_filename : filename of the he input image.
|
||||
* @param[out] lpn : the license plate number found in the image by the dnn detector.
|
||||
* @param[in] classes : set of indeces that indicate the classes of each of these detected boxes
|
||||
* @param[out] confidences : detection confidences of detected bounding boxes
|
||||
* @param[out] boxes : A set of bounding boxes.
|
||||
* @param[out] chosen_lp_classIds : set of indeces that indicate the classes of each box of the license plate that has been chosen by engine (ie the one with the highest confidence)
|
||||
* @param[out] chosen_lp_confidences : detection confidences of the corresponding boxes
|
||||
* @param[out] chosen_lp_boxes : A set of bounding boxes of the license plate that has been chosen by engine (ie the one with the highest confidence)
|
||||
*/
|
||||
void two_stage_lpr(const std::list<Yolov5_alpr_onxx_detector*>& detectors, const std::string& image_filename,
|
||||
//double linked lists to separate lps
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<int>>& classes, std::list < std::list<cv::Rect>>& boxes,
|
||||
//all lps in the image given by lpn (as string), lp country ppronenace (as class index) and lp area in the image (cv::Rect)
|
||||
std::list <std::string>& lpns, std::list <int>& lp_country_class, std::list < cv::Rect>& lp_rois,
|
||||
//detection inside the chosen lp
|
||||
std::list<int>& chosen_lp_classIds, std::list<float>& chosen_lp_confidences, std::list<cv::Rect>& chosen_lp_boxes
|
||||
);
|
||||
//two stage lpn detection : first a global nn detects lpn of a free flow vehicle, then a second nn focuses and reads the lpn of the previously detected lpn.
|
||||
void two_stage_lpr(const std::list<Yolov5_alpr_onxx_detector*>& freeflow_detectors, const std::list<Yolov5_alpr_onxx_detector*>& parking_detectors
|
||||
, Plates_types_classifier& plates_types_classifier,
|
||||
const std::string& image_filename,
|
||||
//double linked lists to separate lps
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<int>>& classes, std::list < std::list<cv::Rect>>& boxes,
|
||||
//all lps in the image given by lpn (as string), lp country ppronenace (as class index) and lp area in the image (cv::Rect)
|
||||
std::list <std::string>& lpns, std::list <int>& lp_country_class, std::list < cv::Rect>& lp_rois,
|
||||
//detection inside the chosen lp
|
||||
std::list<int>& chosen_lp_classIds, std::list<float>& chosen_lp_confidences, std::list<cv::Rect>& chosen_lp_boxes
|
||||
);
|
||||
void two_stage_lpr(Yolov5_alpr_onxx_detector& freeflow_detector, Yolov5_alpr_onxx_detector& parking_detector
|
||||
, Plates_types_classifier& plates_types_classifier,
|
||||
const std::string& image_filename,
|
||||
//double linked lists to separate lps
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<int>>& classes, std::list < std::list<cv::Rect>>& boxes,
|
||||
//all lps in the image given by lpn (as string), lp country ppronenace (as class index) and lp area in the image (cv::Rect)
|
||||
std::list <std::string>& lpns, std::list <int>& lp_country_class, std::list < cv::Rect>& lp_rois,
|
||||
//detection inside the chosen lp
|
||||
std::list<int>& chosen_lp_classIds, std::list<float>& chosen_lp_confidences, std::list<cv::Rect>& chosen_lp_boxes
|
||||
);
|
||||
//two stage lpn detection : first a global nn detects lpn of a free flow vehicle, then a second nn focuses and reads the lpn of the previously detected lpn.
|
||||
void two_stage_lpr(const std::list<Yolov5_alpr_onxx_detector*>& freeflow_detectors, const std::list<Yolov5_alpr_onxx_detector*>& parking_detectors
|
||||
, Plates_types_classifier& plates_types_classifier,
|
||||
const cv::Mat& frame,
|
||||
//double linked lists to separate lps
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<int>>& classes, std::list < std::list<cv::Rect>>& boxes,
|
||||
//all lps in the image given by lpn (as string), lp country ppronenace (as class index) and lp area in the image (cv::Rect)
|
||||
std::list <std::string>& lpns, std::list <int>& lp_country_class, std::list < cv::Rect>& lp_rois,
|
||||
//detection inside the chosen lp
|
||||
std::list<int>& chosen_lp_classIds, std::list<float>& chosen_lp_confidences, std::list<cv::Rect>& chosen_lp_boxes
|
||||
);
|
||||
//two stage lpn detection : first a global nn detects lpn of a free flow vehicle, then a second nn focuses and reads the lpn of the previously detected lpn.
|
||||
void two_stage_lpr(Yolov5_alpr_onxx_detector& freeflow_detector, Yolov5_alpr_onxx_detector& parking_detector
|
||||
, Plates_types_classifier& plates_types_classifier,
|
||||
const cv::Mat& frame,
|
||||
//double linked lists to separate lps
|
||||
std::list < std::list<float>>& confidences, std::list < std::list<int>>& classes, std::list < std::list<cv::Rect>>& boxes,
|
||||
//all lps in the image given by lpn (as string), lp country ppronenace (as class index) and lp area in the image (cv::Rect)
|
||||
std::list <std::string>& lpns, std::list <int>& lp_country_class, std::list < cv::Rect>& lp_rois,
|
||||
//detection inside the chosen lp
|
||||
std::list<int>& chosen_lp_classIds, std::list<float>& chosen_lp_confidences, std::list<cv::Rect>& chosen_lp_boxes
|
||||
);
|
||||
#endif // !defined(YOLOV5_ALPR_ONNX_DETECTOR)
|
||||
@@ -0,0 +1,5 @@
|
||||
// pch.cpp: source file corresponding to the pre-compiled header
|
||||
|
||||
#include "pch.h"
|
||||
|
||||
// When you are using pre-compiled headers, this source file is necessary for compilation to succeed.
|
||||
@@ -0,0 +1,13 @@
|
||||
// pch.h: This is a precompiled header file.
|
||||
// Files listed below are compiled only once, improving build performance for future builds.
|
||||
// This also affects IntelliSense performance, including code completion and many code browsing features.
|
||||
// However, files listed here are ALL re-compiled if any one of them is updated between builds.
|
||||
// Do not add files here that you will be updating frequently as this negates the performance advantage.
|
||||
|
||||
#ifndef PCH_H
|
||||
#define PCH_H
|
||||
|
||||
// add headers that you want to pre-compile here
|
||||
#include "framework.h"
|
||||
|
||||
#endif //PCH_H
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
// Line.h: interface for the C_Line class.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
*/
|
||||
// Levenshtein.cpp: implementation of the Levenshtein class.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include "../include/Levenshtein.h"
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// Construction/Destruction
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
Levenshtein::Levenshtein()
|
||||
{
|
||||
}
|
||||
Levenshtein::~Levenshtein()
|
||||
{
|
||||
}
|
||||
//****************************************************
|
||||
// Compute Levenshtein distance
|
||||
// This code is based on the work by Michael Gilleland
|
||||
//
|
||||
// It uses dynamic arrays which allows any std::string size.
|
||||
//****************************************************
|
||||
int Levenshtein::Get2 (char const *s, char const *t)
|
||||
{
|
||||
return Get2(s, strlen(s), t, strlen(t) );
|
||||
}
|
||||
int Levenshtein::Get2 (const char *s, size_t n, const char* t, size_t dst)
|
||||
{
|
||||
int *d; // pointer to matrix
|
||||
size_t i; // iterates through s
|
||||
size_t j; // iterates through t
|
||||
//char s_i; // ith character of s
|
||||
char t_j; // jth character of t
|
||||
int cost; // cost
|
||||
int result; // result
|
||||
int cell; // contents of target cell
|
||||
int above; // contents of cell immediately above
|
||||
int left; // contents of cell immediately to left
|
||||
int diag; // contents of cell immediately above and to left
|
||||
size_t sz; // number of cells in matrix
|
||||
// Step 1
|
||||
if (n == 0) {
|
||||
return static_cast<int>(dst);
|
||||
}
|
||||
if (dst == 0) {
|
||||
return static_cast<int>(n);
|
||||
}
|
||||
sz = (n+1) * (dst+1) * sizeof (int);
|
||||
d = static_cast<int*>(malloc(sz));
|
||||
// Step 2
|
||||
for (i = 0; i <= n; i++) {
|
||||
PutAt (d, i, 0, n, static_cast<int>(i));
|
||||
}
|
||||
for (j = 0; j <= dst; j++) {
|
||||
PutAt (d, 0, j, n, static_cast<int>(j));
|
||||
}
|
||||
// Step 3
|
||||
for (i = 1; i <= n; i++) {
|
||||
char s_i; // ith character of s
|
||||
s_i = s[i-1];
|
||||
// Step 4
|
||||
for (j = 1; j <= dst; j++) {
|
||||
t_j = t[j-1];
|
||||
// Step 5
|
||||
if (s_i == t_j) {
|
||||
cost = 0;
|
||||
}
|
||||
else {
|
||||
cost = 1;
|
||||
}
|
||||
// Step 6
|
||||
above = GetAt (d,i-1,j, n);
|
||||
left = GetAt (d,i, j-1, n);
|
||||
diag = GetAt (d, i-1,j-1, n);
|
||||
cell = Minimum (above + 1, left + 1, diag + cost);
|
||||
PutAt (d, i, j, n, cell);
|
||||
}
|
||||
}
|
||||
// Step 7
|
||||
result = GetAt (d, n, dst, n);
|
||||
free (d);
|
||||
return result;
|
||||
}
|
||||
/*****************************************************
|
||||
* Second implementation of the Levenshtein algorithm.
|
||||
* A static array is used with length MAXLINE. Make
|
||||
* sure that your strings are no longer! Otherwise use
|
||||
* the algorithm above.
|
||||
*/
|
||||
#define MAXLINE 128
|
||||
int Levenshtein::Get(const char *a, const char *b)
|
||||
{
|
||||
return Get(a, strlen(a), b, strlen(b));
|
||||
}
|
||||
int Levenshtein::Get(const char *a, size_t aLen, const char *b, size_t bLen)
|
||||
{
|
||||
int arr[MAXLINE][MAXLINE];
|
||||
int i,j,l,dst,n,add;
|
||||
// MAXLINE is the limit! If the strings are longer use the other implementation
|
||||
if (aLen > MAXLINE || bLen > MAXLINE)
|
||||
{
|
||||
return Get2(a, aLen, b, bLen);
|
||||
}
|
||||
for (i=0;i<=aLen;i++)
|
||||
{
|
||||
arr[0][i]=i;
|
||||
}
|
||||
for (j=0;j<=bLen;j++)
|
||||
{
|
||||
arr[j][0]=j;
|
||||
}
|
||||
for (j=1;j<=bLen;j++)
|
||||
{
|
||||
for (i=1;i<=aLen;i++)
|
||||
{
|
||||
if (a[i-1] == b[j-1])
|
||||
{
|
||||
add=0;
|
||||
}
|
||||
else
|
||||
{
|
||||
add=1;
|
||||
}
|
||||
dst = 1+arr[j-1][i];
|
||||
l = 1+arr[j][i-1];
|
||||
n = add+arr[j-1][i-1];
|
||||
arr[j][i] = (dst < l ? (dst < n ? dst : n): (l < n ? l : n));
|
||||
}
|
||||
}
|
||||
return arr[bLen][aLen];
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
//************************************************************************
|
||||
/*
|
||||
************************************************************************
|
||||
// Copyright (C) 2003-2006, LPReditor SARL, all rights reserved.
|
||||
// author : Raphael Poulenard.
|
||||
************************************************************************
|
||||
// Line.h: interface for the C_Line class.
|
||||
//
|
||||
This program is free software : you can redistribute itand /or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
||||
GNU General Public License for more details.
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
*/
|
||||
// Line.cpp: implementation of the C_Line class.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
#include "Line.h"
|
||||
#ifdef _WINDOWS
|
||||
#endif //_WINDOWS
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
// Construction/Destruction
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
C_Line::C_Line():a(0.0f),b(0.0f)
|
||||
{
|
||||
}
|
||||
//constructeur a partir de deux pts
|
||||
C_Line::C_Line(const cv::Point2f& A,const cv::Point2f& B)
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
//assert(A.x!=B.x);
|
||||
#endif //_DEBUG
|
||||
if(fabsf(A.x-B.x)<FLT_EPSILON) {
|
||||
a=(A.y-B.y)/(A.x-B.x+FLT_EPSILON);
|
||||
b=0.0f;
|
||||
}
|
||||
else {a=(A.y-B.y)/(A.x-B.x);
|
||||
b=A.y-(a*A.x);
|
||||
}
|
||||
}
|
||||
C_Line::C_Line(const C_Line & line_)
|
||||
{
|
||||
a=line_.a;
|
||||
b=line_.b;
|
||||
}
|
||||
C_Line::C_Line(const float & a_,const float & b_):a(a_),b(b_)
|
||||
{
|
||||
}
|
||||
C_Line::~C_Line()
|
||||
{
|
||||
}
|
||||
//retourne le pt d'inter de C_Line avec la dte horiz y=ordonnee
|
||||
float C_Line::get_abs(const int ordonnee) const
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
assert(a!=0.0f);
|
||||
#endif //_DEBUG
|
||||
return (ordonnee-b)/a;
|
||||
}
|
||||
float C_Line::get_abs(const float ordonnee) const
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
assert(a!=0.0f);
|
||||
#endif //_DEBUG
|
||||
return (ordonnee-b)/a;
|
||||
}
|
||||
//retourne l'image de abscisse par la fonction affi,e definie par la droite
|
||||
int C_Line::get_image_entiere(const int abscisse) const
|
||||
{
|
||||
float ordonnee(a*abscisse+b);
|
||||
int ordonne_floor=static_cast<int>(floorf(ordonnee));
|
||||
if (ordonnee-ordonne_floor>0.5f) {
|
||||
//le point le plus proche de la droite est situe au dessus.
|
||||
ordonne_floor++;
|
||||
#ifdef _DEBUG
|
||||
assert(ordonne_floor-ordonnee<0.5f);
|
||||
#endif //_DEBUG
|
||||
}
|
||||
return ordonne_floor;
|
||||
}
|
||||
//retourne l'image de abscisse par la fonction affi,e definie par la droite
|
||||
float C_Line::get_image(const float & abscisse) const
|
||||
{
|
||||
return a*abscisse+b;
|
||||
}
|
||||
//retourne l'image de abscisse par la fonction affi,e definie par la droite
|
||||
float C_Line::get_image(const int abscisse) const
|
||||
{
|
||||
return a*abscisse+b;
|
||||
}
|
||||
bool C_Line::is_nearly_the_same(const C_Line & right_op) const
|
||||
{
|
||||
return (fabs(a-right_op.a)<FLT_EPSILON
|
||||
&& fabs(b-right_op.b)<FLT_EPSILON);
|
||||
}
|
||||
bool C_Line::is_nearly_horiz() const
|
||||
{
|
||||
return (a+0.000001>0.0 && a<0.000001);
|
||||
}
|
||||
void C_Line::reset()
|
||||
{
|
||||
a=0.0f;b=0.0f;
|
||||
}
|
||||
float C_Line::dist_y(const cv::Point& pt) const
|
||||
{
|
||||
//true if the line doesnot have too large slope and not large ordonnee a l'origine
|
||||
if (is_valid()) {
|
||||
return fabsf(pt.y - a * static_cast<float>(pt.x) - b);
|
||||
}
|
||||
else return .0f;
|
||||
}
|
||||
float C_Line::dist_y(const cv::Point2f& pt) const
|
||||
{
|
||||
//true if the line doesnot have too large slope and not large ordonnee a l'origine
|
||||
if (is_valid()) {
|
||||
return fabsf(pt.y - a * static_cast<float>(pt.x) - b);
|
||||
}
|
||||
else return .0f;
|
||||
}
|
||||
//retourne la distance du point
|
||||
// sa projection verticale de la droite
|
||||
float C_Line::difference_y(const cv::Point2f& pt) const
|
||||
{
|
||||
return (pt.y - a * pt.x - b);
|
||||
}
|
||||
//returns the distance of the center point of a box from its vertical projection on the line
|
||||
float C_Line::dist_y_from_center(const cv::Rect& box) const
|
||||
{
|
||||
cv::Point2f pt(box.x + box.width / 2.0f, box.y + box.height / 2.0f);
|
||||
return dist_y(pt);
|
||||
}
|
||||
float C_Line::difference_y_from_center(const cv::Rect& box) const
|
||||
{
|
||||
cv::Point2f pt(box.x + box.width / 2.0f, box.y + box.height / 2.0f);
|
||||
return difference_y(pt);
|
||||
}
|
||||
float C_Line::dist_y_from_center(const std::list<cv::Rect>& boxes) const
|
||||
{
|
||||
float dist = 0.0f;
|
||||
std::list<cv::Rect>::const_iterator it(boxes.begin());
|
||||
while (it != boxes.end()) {
|
||||
dist += dist_y_from_center(*it);
|
||||
it++;
|
||||
}
|
||||
return dist;
|
||||
}
|
||||
float C_Line::difference_y_from_center(const std::list<cv::Rect>& boxes) const
|
||||
{
|
||||
float dist = 0.0f;
|
||||
std::list<cv::Rect>::const_iterator it(boxes.begin());
|
||||
while (it != boxes.end()) {
|
||||
dist += difference_y_from_center(*it);
|
||||
it++;
|
||||
}
|
||||
return dist;
|
||||
}
|
||||
//returns sum of distances of a list of points from their vertical projections on the line
|
||||
float C_Line::dist_y(const std::list<cv::Point2f>& points) const
|
||||
{
|
||||
float dist = 0.0f;
|
||||
std::list<cv::Point2f>::const_iterator it(points.begin());
|
||||
while (it != points.end()) {
|
||||
dist += dist_y(*it);
|
||||
it++;
|
||||
}
|
||||
return dist;
|
||||
}
|
||||
float C_Line::dist_y(const std::list<cv::Point>& points) const
|
||||
{
|
||||
float dist = 0.0f;
|
||||
std::list<cv::Point>::const_iterator it(points.begin());
|
||||
while (it != points.end()) {
|
||||
dist += dist_y(*it);
|
||||
it++;
|
||||
}
|
||||
return dist;
|
||||
}
|
||||
float C_Line::error(const std::list<cv::Rect>& boxes) const
|
||||
{
|
||||
if (boxes.size()) {
|
||||
return dist_y_from_center(boxes) / static_cast<float>(boxes.size());
|
||||
}
|
||||
else return 0.0f;
|
||||
}
|
||||
float C_Line::error(const std::list<cv::Point2f>& points) const
|
||||
{
|
||||
if (points.size()) {
|
||||
return dist_y(points) / static_cast<float>(points.size());
|
||||
}
|
||||
else return 0.0f;
|
||||
}
|
||||
@@ -0,0 +1,367 @@
|
||||
/*
|
||||
// Line.h: interface for the C_Line class.
|
||||
//
|
||||
*/
|
||||
#include "ONNX_detector.h"
|
||||
#include <iostream>
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/imgproc.hpp>
|
||||
#include <opencv2/dnn/dnn.hpp>
|
||||
#include <filesystem>
|
||||
OnnxDetector::OnnxDetector(Ort::Env& env_, const ORTCHAR_T* model_path, const Ort::SessionOptions& options) : env(env_), sessionOptions(options), session(env_, model_path, options) {
|
||||
dump();
|
||||
}
|
||||
OnnxDetector::OnnxDetector(Ort::Env& env_, const void* model_data, size_t model_data_length, const Ort::SessionOptions& options) : env(env_),sessionOptions(options),
|
||||
session(env_, model_data, model_data_length, options)
|
||||
{
|
||||
dump();
|
||||
}
|
||||
void OnnxDetector::dump() const {
|
||||
//std::cout << "Available execution providers:\n";
|
||||
//for (const auto& s : Ort::GetAvailableProviders()) std::cout << '\t' << s << '\n';
|
||||
Ort::AllocatorWithDefaultOptions allocator;
|
||||
size_t numInputNodes = session.GetInputCount();
|
||||
size_t numOutputNodes = session.GetOutputCount();
|
||||
//std::cout << "Number of Input Nodes: " << numInputNodes << std::endl;
|
||||
//std::cout << "Number of Output Nodes: " << numOutputNodes << std::endl;
|
||||
//const char* inputName = session.GetInputName(0, allocator);
|
||||
//std::cout << "Input Name: " << inputName << std::endl;
|
||||
Ort::TypeInfo inputTypeInfo = session.GetInputTypeInfo(0);
|
||||
auto inputTensorInfo = inputTypeInfo.GetTensorTypeAndShapeInfo();
|
||||
ONNXTensorElementDataType inputType = inputTensorInfo.GetElementType();
|
||||
//std::cout << "Input Type: " << inputType << std::endl;
|
||||
std::vector<int64_t> inputDims = inputTensorInfo.GetShape();
|
||||
//std::cout << "Input Dimensions: " << inputDims << std::endl;
|
||||
//for (size_t i = 0; i < inputDims.size() - 1; i++)
|
||||
// std::cout << inputDims[i] << std::endl;
|
||||
//std::cout << std::endl;
|
||||
//const char* outputName = session.GetOutputName(0, allocator);
|
||||
//std::cout << "Output Name: " << outputName << std::endl;
|
||||
Ort::TypeInfo outputTypeInfo = session.GetOutputTypeInfo(0);
|
||||
auto outputTensorInfo = outputTypeInfo.GetTensorTypeAndShapeInfo();
|
||||
ONNXTensorElementDataType outputType = outputTensorInfo.GetElementType();
|
||||
//std::cout << "Output Type: " << outputType << std::endl;
|
||||
std::vector<int64_t> outputDims = outputTensorInfo.GetShape();//1 25200 41
|
||||
#ifdef _DEBUG
|
||||
if (std::string(outputName) == "modelOutput" && std::string(inputName) == "modelInput"
|
||||
|| std::string(outputName) == "platesTypeOutput" && std::string(inputName) == "inputImage") {
|
||||
assert(inputDims.size() == 4);
|
||||
assert(inputDims[0] == 1);
|
||||
assert(inputDims[1] == 3);
|
||||
assert(inputDims[2] == 80);
|
||||
assert(inputDims[3] == 200);
|
||||
for (size_t i = 0; i < inputDims.size(); i++)
|
||||
std::cout << inputDims[i] << std::endl;
|
||||
assert(outputDims.size() == 2);
|
||||
assert(outputDims[0] == 1);
|
||||
assert(outputDims[1] == 502);//502 types of lps
|
||||
}
|
||||
else {
|
||||
assert(outputDims.size() == 3);
|
||||
assert(outputDims[0] == 1);
|
||||
assert(outputDims[2] == 103);// 0,1,2,3 ->box,4->confidence,1 -> output classes = 36 characters+pays 61 + 1 vehicle= 98 classes =4+1+36+61+1=103
|
||||
}
|
||||
#endif //_DEBUG
|
||||
// std::cout << "Output Dimensions: " << std::endl;
|
||||
// for (size_t i = 0; i < outputDims.size(); i++)
|
||||
// std::cout << outputDims[i] << std::endl;
|
||||
// std::cout << std::endl;
|
||||
}
|
||||
//returns the maximum size of input image (ie width or height of dnn input layer)
|
||||
int64_t OnnxDetector::max_image_size() const {
|
||||
std::vector<std::vector<Detection>> result;
|
||||
cv::Mat resizedImageRGB, resizedImage, preprocessedImage;
|
||||
Ort::TypeInfo inputTypeInfo = session.GetInputTypeInfo(0);
|
||||
auto inputTensorInfo = inputTypeInfo.GetTensorTypeAndShapeInfo();
|
||||
std::vector<int64_t> inputDims = inputTensorInfo.GetShape();
|
||||
int64_t max_size = inputDims.at(2);// width;
|
||||
if (max_size < inputDims.at(3))//height
|
||||
max_size = inputDims.at(3);
|
||||
return max_size;
|
||||
}
|
||||
std::vector<std::vector<Detection>>
|
||||
OnnxDetector::Run(const cv::Mat& img, float conf_threshold, float iou_threshold, bool preserve_aspect_ratio) {
|
||||
std::vector<std::vector<Detection>> result;
|
||||
cv::Mat resizedImageRGB, resizedImage, preprocessedImage;
|
||||
Ort::TypeInfo inputTypeInfo = session.GetInputTypeInfo(0);
|
||||
auto inputTensorInfo = inputTypeInfo.GetTensorTypeAndShapeInfo();
|
||||
std::vector<int64_t> inputDims = inputTensorInfo.GetShape();
|
||||
//cv::Mat img = cv::imread(imageFilepath, cv::ImreadModes::IMREAD_COLOR);
|
||||
int channels_ = img.channels();
|
||||
if (
|
||||
img.size().width &&
|
||||
img.size().height && ((channels_ == 1) || (channels_ == 3) || (channels_ == 4))) {
|
||||
if (channels_ == 1) {
|
||||
cv::cvtColor(img, resizedImageRGB,
|
||||
cv::ColorConversionCodes::COLOR_GRAY2RGB);
|
||||
}
|
||||
else if (channels_ == 4) {
|
||||
cv::cvtColor(img, resizedImageRGB,
|
||||
cv::ColorConversionCodes::COLOR_BGRA2RGB);
|
||||
}
|
||||
else if (channels_ == 3) {
|
||||
int type = img.type();
|
||||
cv::cvtColor(img, resizedImageRGB,
|
||||
cv::ColorConversionCodes::COLOR_BGR2RGB);
|
||||
}
|
||||
float pad_w = -1.0f, pad_h = -1.0f, scale = -1.0f;
|
||||
if (preserve_aspect_ratio) {
|
||||
// keep the original image for visualization purpose
|
||||
std::vector<float> pad_info = LetterboxImage(resizedImageRGB, resizedImageRGB, cv::Size(static_cast<int>(inputDims.at(2)), static_cast<int>(inputDims.at(3))));
|
||||
//pad_w is the left (and also right) border width in the square image feeded to the model
|
||||
pad_w = pad_info[0];
|
||||
pad_h = pad_info[1];
|
||||
scale = pad_info[2];
|
||||
}
|
||||
else {
|
||||
cv::resize(resizedImageRGB, resizedImageRGB,
|
||||
cv::Size(static_cast<int>(inputDims.at(2)), static_cast<int>(inputDims.at(3))),
|
||||
cv::InterpolationFlags::INTER_CUBIC);
|
||||
}
|
||||
resizedImageRGB.convertTo(resizedImage, CV_32FC3, 1.0f / 255.0f);
|
||||
// HWC to CHW
|
||||
cv::dnn::blobFromImage(resizedImage, preprocessedImage);
|
||||
int64_t inputTensorSize = vectorProduct(inputDims);
|
||||
std::vector<float> inputTensorValues(inputTensorSize);
|
||||
inputTensorValues.assign(preprocessedImage.begin<float>(),
|
||||
preprocessedImage.end<float>());
|
||||
Ort::TypeInfo outputTypeInfo = session.GetOutputTypeInfo(0);
|
||||
auto outputTensorInfo = outputTypeInfo.GetTensorTypeAndShapeInfo();
|
||||
ONNXTensorElementDataType outputType = outputTensorInfo.GetElementType();//1
|
||||
#ifdef _DEBUG
|
||||
assert(outputType == 1);
|
||||
#endif //_DEBUG
|
||||
std::vector<int64_t> outputDims = outputTensorInfo.GetShape();//1 25200 41
|
||||
#ifdef _DEBUG
|
||||
assert(outputDims.size() == 3);
|
||||
assert(outputDims[0] == 1);
|
||||
//assert(outputDims[1] == 25200);
|
||||
assert(outputDims[2] == 103);// 0,1,2,3 ->box,4->confidence,1 -> output classes = 36 characters+pays 61 + 1 vehicle= 98 classes =4+1+36+61+1=103
|
||||
#endif //_DEBUG
|
||||
int64_t outputTensorSize = vectorProduct(outputDims);
|
||||
std::vector<float> outputTensorValues(outputTensorSize);
|
||||
Ort::AllocatorWithDefaultOptions allocator;
|
||||
std::vector<const char*> inputNames;
|
||||
std::vector<Ort::AllocatedStringPtr> inputNodeNameAllocatedStrings; // <-- newly added
|
||||
std::vector<const char*> outputNames;
|
||||
std::vector<Ort::AllocatedStringPtr> outputNodeNameAllocatedStrings; // <-- newly added
|
||||
inputNames.clear();
|
||||
outputNames.clear();
|
||||
|
||||
auto inputName = session.GetInputNameAllocated(0, allocator);//session.GetInputName(0, allocator);
|
||||
inputNodeNameAllocatedStrings.push_back(std::move(inputName));
|
||||
inputNames.push_back(inputNodeNameAllocatedStrings.back().get());
|
||||
|
||||
|
||||
auto outputName = session.GetOutputNameAllocated(0, allocator);
|
||||
outputNodeNameAllocatedStrings.push_back(std::move(outputName));
|
||||
outputNames.push_back(outputNodeNameAllocatedStrings.back().get());
|
||||
|
||||
std::vector<Ort::Value> inputTensors;
|
||||
std::vector<Ort::Value> outputTensors;
|
||||
Ort::MemoryInfo memoryInfo = Ort::MemoryInfo::CreateCpu(
|
||||
OrtAllocatorType::OrtArenaAllocator, OrtMemType::OrtMemTypeDefault);
|
||||
inputTensors.push_back(Ort::Value::CreateTensor<float>(
|
||||
memoryInfo, inputTensorValues.data(), inputTensorSize, inputDims.data(),
|
||||
inputDims.size()));
|
||||
outputTensors.push_back(Ort::Value::CreateTensor<float>(
|
||||
memoryInfo, outputTensorValues.data(), outputTensorSize,
|
||||
outputDims.data(), outputDims.size()));
|
||||
// https://github.com/microsoft/onnxruntime/blob/rel-1.6.0/include/onnxruntime/core/session/onnxruntime_cxx_api.h#L353
|
||||
session.Run(Ort::RunOptions{ nullptr }, inputNames.data(), inputTensors.data(), 1, outputNames.data(), outputTensors.data(), 1);
|
||||
size_t dimensionsCount = outputTensorInfo.GetDimensionsCount();//3
|
||||
#ifdef _DEBUG
|
||||
assert(dimensionsCount == 3);
|
||||
#endif //_DEBUG
|
||||
float* output = outputTensors[0].GetTensorMutableData<float>(); // output of onnx runtime ->>> 1,25200,85
|
||||
size_t size = outputTensors[0].GetTensorTypeAndShapeInfo().GetElementCount(); // 1x25200x85=2142000
|
||||
int64_t dimensions = outputDims[2]; // 0,1,2,3 ->box,4->confidence,5-85 -> output classes = 35 characters+pays 60 = 95 classes confidence
|
||||
#ifdef _DEBUG
|
||||
assert(dimensions >= 41);// 0,1,2,3 ->box,4->confidence,5-85 -> output classes = 35 characters+pays 60 = 95 classes confidence
|
||||
#endif //_DEBUG
|
||||
const cv::Size& out_size = cv::Size(static_cast<int>(inputDims[3]), static_cast<int>(inputDims[3]));
|
||||
std::vector<std::vector<Detection>> detections;
|
||||
if (preserve_aspect_ratio) {
|
||||
// keep the original image for visualization purpose
|
||||
detections = (
|
||||
PostProcessing(
|
||||
output, // output of onnx runtime ->>> 1,25200,85
|
||||
dimensionsCount,
|
||||
size, // 1x25200x85=2142000
|
||||
static_cast<int>(dimensions),
|
||||
//pad_w is the left (and also right) border width in the square image feeded to the model
|
||||
pad_w, pad_h, scale, img.size(),
|
||||
conf_threshold, iou_threshold));
|
||||
}
|
||||
else {
|
||||
detections = (PostProcessing(
|
||||
output, // output of onnx runtime ->>> 1,25200,85
|
||||
dimensionsCount,
|
||||
size, // 1x25200x85=2142000
|
||||
static_cast<int>(dimensions),
|
||||
static_cast<float>(out_size.width), static_cast<float>(out_size.height), img.size(),
|
||||
conf_threshold, iou_threshold));
|
||||
}
|
||||
#ifdef _DEBUG
|
||||
std::list<cv::Rect> true_boxes; std::list<int> classesId;
|
||||
std::vector<Detection>::const_iterator it(detections[0].begin());
|
||||
while (it != detections[0].end()) {
|
||||
true_boxes.push_back(it->bbox);
|
||||
classesId.push_back(it->class_idx);
|
||||
it++;
|
||||
}
|
||||
#endif //_DEBUG
|
||||
return detections;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
std::list<std::vector<std::vector<Detection>>>
|
||||
OnnxDetector::Run(const cv::Mat& img
|
||||
//, float conf_threshold
|
||||
, float iou_threshold
|
||||
) {
|
||||
std::list<std::vector<std::vector<Detection>>> result;
|
||||
cv::Mat resizedImageRGB, resizedImage, preprocessedImage;
|
||||
Ort::TypeInfo inputTypeInfo = session.GetInputTypeInfo(0);
|
||||
auto inputTensorInfo = inputTypeInfo.GetTensorTypeAndShapeInfo();
|
||||
std::vector<int64_t> inputDims = inputTensorInfo.GetShape();
|
||||
//cv::Mat img = cv::imread(imageFilepath, cv::ImreadModes::IMREAD_COLOR);
|
||||
int channels_ = img.channels();
|
||||
if (
|
||||
img.size().width &&
|
||||
img.size().height && ((channels_ == 1) || (channels_ == 3) || (channels_ == 4))) {
|
||||
if (channels_ == 1) {
|
||||
cv::cvtColor(img, resizedImageRGB,
|
||||
cv::ColorConversionCodes::COLOR_GRAY2RGB);
|
||||
}
|
||||
else if (channels_ == 4) {
|
||||
cv::cvtColor(img, resizedImageRGB,
|
||||
cv::ColorConversionCodes::COLOR_BGRA2RGB);
|
||||
}
|
||||
else if (channels_ == 3) {
|
||||
int type = img.type();
|
||||
cv::cvtColor(img, resizedImageRGB,
|
||||
cv::ColorConversionCodes::COLOR_BGR2RGB);
|
||||
}
|
||||
bool preserve_aspect_ratio = true;
|
||||
float pad_w = -1.0f, pad_h = -1.0f, scale = -1.0f;
|
||||
// keep the original image for visualization purpose
|
||||
std::vector<float> pad_info = LetterboxImage(resizedImageRGB, resizedImageRGB, cv::Size(static_cast<int>(inputDims.at(2)), static_cast<int>(inputDims.at(3))));
|
||||
//pad_w is the left (and also right) border width in the square image feeded to the model
|
||||
pad_w = pad_info[0];
|
||||
pad_h = pad_info[1];
|
||||
scale = pad_info[2];
|
||||
resizedImageRGB.convertTo(resizedImage, CV_32FC3, 1.0f / 255.0f);
|
||||
// HWC to CHW
|
||||
cv::dnn::blobFromImage(resizedImage, preprocessedImage);
|
||||
int64_t inputTensorSize = vectorProduct(inputDims);
|
||||
std::vector<float> inputTensorValues(inputTensorSize);
|
||||
inputTensorValues.assign(preprocessedImage.begin<float>(),
|
||||
preprocessedImage.end<float>());
|
||||
Ort::TypeInfo outputTypeInfo = session.GetOutputTypeInfo(0);
|
||||
auto outputTensorInfo = outputTypeInfo.GetTensorTypeAndShapeInfo();
|
||||
ONNXTensorElementDataType outputType = outputTensorInfo.GetElementType();//1
|
||||
#ifdef _DEBUG
|
||||
assert(outputType == 1);
|
||||
#endif //_DEBUG
|
||||
std::vector<int64_t> outputDims = outputTensorInfo.GetShape();//1 25200 41
|
||||
#ifdef _DEBUG
|
||||
assert(outputDims.size() == 3);
|
||||
assert(outputDims[0] == 1);
|
||||
//assert(outputDims[1] == 25200);
|
||||
assert(outputDims[2] == 103);// 0,1,2,3 ->box,4->confidence,1 -> output classes = 36 characters+pays 61 + 1 vehicle= 98 classes =4+1+36+61+1=103
|
||||
#endif //_DEBUG
|
||||
int64_t outputTensorSize = vectorProduct(outputDims);
|
||||
std::vector<float> outputTensorValues(outputTensorSize);
|
||||
Ort::AllocatorWithDefaultOptions allocator;
|
||||
|
||||
std::vector<const char*> inputNames;
|
||||
std::vector<Ort::AllocatedStringPtr> inputNodeNameAllocatedStrings; // <-- newly added
|
||||
std::vector<const char*> outputNames;
|
||||
std::vector<Ort::AllocatedStringPtr> outputNodeNameAllocatedStrings; // <-- newly added
|
||||
inputNames.clear();
|
||||
outputNames.clear();
|
||||
|
||||
auto inputName = session.GetInputNameAllocated(0, allocator);//session.GetInputName(0, allocator);
|
||||
inputNodeNameAllocatedStrings.push_back(std::move(inputName));
|
||||
inputNames.push_back(inputNodeNameAllocatedStrings.back().get());
|
||||
|
||||
|
||||
auto outputName = session.GetOutputNameAllocated(0, allocator);
|
||||
outputNodeNameAllocatedStrings.push_back(std::move(outputName));
|
||||
outputNames.push_back(outputNodeNameAllocatedStrings.back().get());
|
||||
|
||||
|
||||
std::vector<Ort::Value> inputTensors;
|
||||
std::vector<Ort::Value> outputTensors;
|
||||
Ort::MemoryInfo memoryInfo = Ort::MemoryInfo::CreateCpu(
|
||||
OrtAllocatorType::OrtArenaAllocator, OrtMemType::OrtMemTypeDefault);
|
||||
inputTensors.push_back(Ort::Value::CreateTensor<float>(
|
||||
memoryInfo, inputTensorValues.data(), inputTensorSize, inputDims.data(),
|
||||
inputDims.size()));
|
||||
outputTensors.push_back(Ort::Value::CreateTensor<float>(
|
||||
memoryInfo, outputTensorValues.data(), outputTensorSize,
|
||||
outputDims.data(), outputDims.size()));
|
||||
// https://github.com/microsoft/onnxruntime/blob/rel-1.6.0/include/onnxruntime/core/session/onnxruntime_cxx_api.h#L353
|
||||
session.Run(Ort::RunOptions{ nullptr }, inputNames.data(), inputTensors.data(), 1, outputNames.data(), outputTensors.data(), 1);
|
||||
size_t dimensionsCount = outputTensorInfo.GetDimensionsCount();//3
|
||||
#ifdef _DEBUG
|
||||
assert(dimensionsCount == 3);
|
||||
#endif //_DEBUG
|
||||
float* output = outputTensors[0].GetTensorMutableData<float>(); // output of onnx runtime ->>> 1,25200,85
|
||||
size_t size = outputTensors[0].GetTensorTypeAndShapeInfo().GetElementCount(); // 1x25200x85=2142000
|
||||
int64_t dimensions = outputDims[2]; // 0,1,2,3 ->box,4->confidence,5-85 -> output classes = 35 characters+pays 60 = 95 classes confidence
|
||||
#ifdef _DEBUG
|
||||
assert(dimensions >= 41);// 0,1,2,3 ->box,4->confidence,5-85 -> output classes = 35 characters+pays 60 = 95 classes confidence
|
||||
#endif //_DEBUG
|
||||
const cv::Size& out_size = cv::Size(static_cast<int>(inputDims[3]), static_cast<int>(inputDims[3]));
|
||||
const int nb_passes = 10;
|
||||
const float conf_threshold_min = 0.1f;
|
||||
for (int conf_threshold_step = 0; conf_threshold_step < nb_passes; conf_threshold_step++)
|
||||
{
|
||||
float conf_threshold = conf_threshold_min + static_cast<float>(conf_threshold_step) * (1.0f - 2.0f * conf_threshold_min) / (static_cast<float>(nb_passes));
|
||||
// keep the original image for visualization purpose
|
||||
std::vector<std::vector<Detection>> current_detections = (
|
||||
PostProcessing(
|
||||
output, // output of onnx runtime ->>> 1,25200,85
|
||||
dimensionsCount,
|
||||
size, // 1x25200x85=2142000
|
||||
static_cast<int>(dimensions),
|
||||
//pad_w is the left (and also right) border width in the square image feeded to the model
|
||||
pad_w, pad_h, scale, img.size(),
|
||||
conf_threshold, iou_threshold));
|
||||
result.push_back(current_detections);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
//non max suppession algorithm to select boxes
|
||||
void nms(const std::vector<cv::Rect>& srcRects, std::vector<cv::Rect>& resRects, std::vector<int>& resIndexs, float thresh) {
|
||||
resRects.clear();
|
||||
const size_t size = srcRects.size();
|
||||
if (!size) return;
|
||||
// Sort the bounding boxes by the bottom - right y - coordinate of the bounding box
|
||||
std::multimap<int, size_t> idxs;
|
||||
for (size_t i = 0; i < size; ++i) {
|
||||
idxs.insert(std::pair<int, size_t>(srcRects[i].br().y, i));
|
||||
}
|
||||
// keep looping while some indexes still remain in the indexes list
|
||||
while (idxs.size() > 0) {
|
||||
// grab the last rectangle
|
||||
auto lastElem = --std::end(idxs);
|
||||
const cv::Rect& last = srcRects[lastElem->second];
|
||||
resIndexs.push_back(static_cast<int>(lastElem->second));
|
||||
resRects.push_back(last);
|
||||
idxs.erase(lastElem);
|
||||
for (auto pos = std::begin(idxs); pos != std::end(idxs); ) {
|
||||
// grab the current rectangle
|
||||
const cv::Rect& current = srcRects[pos->second];
|
||||
float intArea = static_cast<float>((last & current).area());
|
||||
float unionArea = last.area() + current.area() - intArea;
|
||||
float overlap = intArea / unionArea;
|
||||
// if there is sufficient overlap, suppress the current bounding box
|
||||
if (overlap > thresh) pos = idxs.erase(pos);
|
||||
else ++pos;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
//*
|
||||
// StatSommesX_Y_H_dbl.cpp: implementation of the C_SumsRegLineXYHDbl class.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
#ifndef GITHUB
|
||||
#include "StatSommesX_Y_H_dbl.h"
|
||||
#include <cmath>
|
||||
#include <climits>
|
||||
#ifdef _WINDOWS
|
||||
#endif //_WINDOWS
|
||||
#ifdef _DEBUG
|
||||
#undef THIS_FILE
|
||||
static char THIS_FILE[] = __FILE__;
|
||||
#define new DEBUG_NEW
|
||||
#endif
|
||||
inline float divise_par_zero(const float& numerateur, // Modif PN passage de float en double
|
||||
const bool denominateur_positif = true) {
|
||||
if (denominateur_positif) {
|
||||
if (numerateur > 0.0f) {
|
||||
if (numerateur < FLT_EPSILON) return 0.0f;
|
||||
else return FLT_MAX;
|
||||
}
|
||||
else {
|
||||
if (FLT_EPSILON + numerateur > 0.0f) return 0.0f;
|
||||
else return -FLT_MAX;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (numerateur > 0.0f) {
|
||||
if (numerateur < FLT_EPSILON) return 0.0f;
|
||||
else return -FLT_MAX;
|
||||
}
|
||||
else {
|
||||
if (FLT_EPSILON + numerateur > 0.0f) return 0.0f;
|
||||
else return FLT_MAX;
|
||||
}
|
||||
}
|
||||
};
|
||||
C_SumsRegLineXYHDbl::C_SumsRegLineXYHDbl() : somme_x(0.0f)
|
||||
, somme_y(0.0f)
|
||||
, produit_xy(0.0f)
|
||||
, somme_carre_x(0.0f)
|
||||
, somme_hauteurs(0)
|
||||
{
|
||||
}
|
||||
C_SumsRegLineXYHDbl::C_SumsRegLineXYHDbl(const int somme_hauteurs_) : somme_x(0.0f)
|
||||
, somme_y(0.0f)
|
||||
, produit_xy(0.0f)
|
||||
, somme_carre_x(0.0f)
|
||||
, somme_hauteurs(somme_hauteurs_)
|
||||
{
|
||||
}
|
||||
C_SumsRegLineXYHDbl::~C_SumsRegLineXYHDbl()
|
||||
{
|
||||
}
|
||||
#ifdef _DEBUG
|
||||
bool C_SumsRegLineXYHDbl::debug(const float& somme_x_,
|
||||
const float& somme_y_,
|
||||
const float& produit_xy_,
|
||||
const float& somme_carre_x_) const
|
||||
{
|
||||
#ifdef LPR_DOUBLE_PRECISION
|
||||
return (fabsf(somme_x - somme_x_) < FLT_EPSILON
|
||||
&& fabsf(somme_y - somme_y_) < FLT_EPSILON
|
||||
&& fabsf((produit_xy - produit_xy_)) < FLT_EPSILON
|
||||
&& fabsf((somme_carre_x - somme_carre_x_)) < FLT_EPSILON
|
||||
);
|
||||
#else // LPR_DOUBLE_PRECISION
|
||||
bool ok = fabsf(somme_x - somme_x_) < FLT_EPSILON
|
||||
&& fabsf(somme_y - somme_y_) < FLT_EPSILON;
|
||||
if (produit_xy + produit_xy_ > FLT_EPSILON)
|
||||
ok = ok && fabsf((produit_xy - produit_xy_)) < FLT_EPSILON * (produit_xy + produit_xy_);
|
||||
else ok = ok && fabsf((produit_xy - produit_xy_)) < FLT_EPSILON;
|
||||
if (somme_carre_x + somme_carre_x_ > FLT_EPSILON)
|
||||
ok = ok && fabsf((somme_carre_x - somme_carre_x_)) < FLT_EPSILON * (somme_carre_x + somme_carre_x_);
|
||||
else ok = ok && fabsf((somme_carre_x - somme_carre_x_)) < FLT_EPSILON;
|
||||
return ok;
|
||||
#endif // LPR_DOUBLE_PRECISION
|
||||
}
|
||||
bool C_SumsRegLineXYHDbl::debug(const float& somme_x_,
|
||||
const float& somme_y_,
|
||||
const float& produit_xy_,
|
||||
const float& somme_carre_x_,
|
||||
const int heigth_) const
|
||||
{
|
||||
/*
|
||||
return (somme_x== somme_x_ && somme_y==somme_y_ &&
|
||||
produit_xy==produit_xy_ && somme_carre_x==somme_carre_x_ && somme_hauteurs==heigth_);
|
||||
*/
|
||||
#ifdef LPR_DOUBLE_PRECISION
|
||||
return (fabsf(somme_x - somme_x_) < FLT_EPSILON
|
||||
&& fabsf(somme_y - somme_y_) < FLT_EPSILON
|
||||
&& fabsf((produit_xy - produit_xy_)) < FLT_EPSILON
|
||||
&& fabsf((somme_carre_x - somme_carre_x_)) < FLT_EPSILON
|
||||
&& somme_hauteurs == heigth_);
|
||||
#else // LPR_DOUBLE_PRECISION
|
||||
return (fabsf(somme_x - somme_x_) < FLT_EPSILON
|
||||
&& fabsf(somme_y - somme_y_) < FLT_EPSILON
|
||||
&& fabsf((produit_xy - produit_xy_)) < FLT_EPSILON * (produit_xy + produit_xy_)
|
||||
&& fabsf((somme_carre_x - somme_carre_x_)) < FLT_EPSILON * (somme_carre_x + somme_carre_x_)
|
||||
&& somme_hauteurs == heigth_);
|
||||
#endif // LPR_DOUBLE_PRECISION
|
||||
}
|
||||
#endif //_DEBUG
|
||||
float C_SumsRegLineXYHDbl::pente(const int nb_elements) const
|
||||
{//calcul de la moyenne des xi
|
||||
//calcul de la moyenne des yi
|
||||
#ifdef _DEBUG
|
||||
assert(nb_elements > 1);
|
||||
#endif //_DEBUG
|
||||
if (nb_elements == 1) return 0.0f;
|
||||
else if (nb_elements > 1) {
|
||||
float moyenne_x = somme_x / nb_elements;
|
||||
float moyenne_y = somme_y / nb_elements;
|
||||
//calcul de la std_deviation(X)
|
||||
float variance = (somme_carre_x)-somme_x * moyenne_x;
|
||||
if (fabsf(variance) < FLT_EPSILON) return FLT_MAX;
|
||||
//calcul de la Covariance(X,Y)
|
||||
float Covariance = (produit_xy)-moyenne_x * somme_y;
|
||||
//calcul de la pente p=Covariance(X,Y)/variance(X)
|
||||
#ifdef _DEBUG
|
||||
assert(variance > -FLT_EPSILON);
|
||||
#endif //_DEBUG
|
||||
float pente_;
|
||||
if (variance != 0.0f) pente_ = Covariance / variance;
|
||||
else pente_ = divise_par_zero(Covariance);
|
||||
#ifdef _DEBUG
|
||||
const float pi = 3.1415926535897932384626433832795f;
|
||||
#ifdef _DEBUG
|
||||
assert(atanf(pente_) <= pi / 2 && (atanf(pente_) + pi / 2) > -FLT_EPSILON);
|
||||
#endif //_DEBUG
|
||||
#endif
|
||||
return pente_;
|
||||
}
|
||||
else return 0.0f;
|
||||
}
|
||||
cv::Point2f C_SumsRegLineXYHDbl::barycenter(const int nb_points)
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
assert(nb_points > 1);
|
||||
#endif //_DEBUG
|
||||
if (nb_points <= 0) return cv::Point2f(FLT_MAX, FLT_MAX);
|
||||
else if (nb_points == 0) {
|
||||
float moyenne_x = somme_x;
|
||||
float moyenne_y = somme_y;
|
||||
return cv::Point2f(moyenne_x, moyenne_y);
|
||||
}
|
||||
else {
|
||||
#ifdef _DEBUG
|
||||
assert(nb_points > 1);
|
||||
#endif //_DEBUG
|
||||
float moyenne_x = somme_x / nb_points;
|
||||
float moyenne_y = somme_y / nb_points;
|
||||
return cv::Point2f(moyenne_x, moyenne_y);
|
||||
}
|
||||
}
|
||||
C_Line C_SumsRegLineXYHDbl::regression_line(const int nb_elements)
|
||||
{
|
||||
if (nb_elements == 0) return C_Line();
|
||||
else if (nb_elements == 1) return C_Line(0.0, somme_y);
|
||||
else if (nb_elements > 1) {
|
||||
float moyenne_x = somme_x / nb_elements;
|
||||
float moyenne_y = somme_y / nb_elements;
|
||||
//calcul de la std_deviation(X)
|
||||
float variance = (somme_carre_x)-somme_x * moyenne_x;
|
||||
if (fabsf(variance) < FLT_EPSILON) return C_Line(FLT_MAX, FLT_MAX);
|
||||
//calcul de la Covariance(X,Y)
|
||||
float Covariance = (produit_xy)-moyenne_x * somme_y;
|
||||
//calcul de la pente_ p=Covariance(X,Y)/variance(X)
|
||||
float pente_ = Covariance / variance;
|
||||
//calcul du coefficient q ( y=px+q )
|
||||
float ordonnee_origine = moyenne_y - pente_ * moyenne_x;
|
||||
#ifdef _DEBUG
|
||||
const float pi = 3.1415926535897932384626433832795f;
|
||||
assert(atanf(pente_) <= pi / 2 && (atanf(pente_) + pi / 2) > -FLT_EPSILON);
|
||||
#endif //_DEBUG
|
||||
C_Line regression_line(pente_, ordonnee_origine);
|
||||
#ifdef _DEBUG
|
||||
//calcul de la moyenne des xi
|
||||
//calcul de la moyenne des yi
|
||||
//ce sont les coordonnees du centre de gravit du fond principal
|
||||
float moyenne_x_ = somme_x / nb_elements;
|
||||
float moyenne_y_ = somme_y / nb_elements;
|
||||
//calcul de la std_deviation(X)
|
||||
float variance_ = (somme_carre_x - somme_x * moyenne_x_);
|
||||
//calcul de la Covariance(X,Y)
|
||||
float Covariance_ = (produit_xy - moyenne_x_ * somme_y);
|
||||
//calcul de la pente p=Covariance(X,Y)/variance(X)
|
||||
#ifdef _DEBUG
|
||||
assert(variance > -FLT_EPSILON);
|
||||
#endif //_DEBUG
|
||||
float pente__;
|
||||
if (variance_ > FLT_EPSILON) pente__ = Covariance_ / variance_;
|
||||
else {
|
||||
pente__ = divise_par_zero(Covariance_);//calcul du coefficient q ( y=px+q )
|
||||
}
|
||||
float ordonnee_origine_ = moyenne_y_ - pente__ * moyenne_x_;
|
||||
#ifdef _DEBUG
|
||||
assert(fabsf(regression_line.a - pente__) < FLT_EPSILON && fabsf(regression_line.b - ordonnee_origine_) < 0.001f);
|
||||
#endif //_DEBUG
|
||||
float diff_ = regression_line.a - pente__;
|
||||
diff_ = regression_line.b - ordonnee_origine_;
|
||||
#endif //_DEBUG
|
||||
return regression_line;
|
||||
}
|
||||
else return C_Line();
|
||||
}
|
||||
bool C_SumsRegLineXYHDbl::operator ==(const C_SumsRegLineXYHDbl& right_op) const
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
double somme_x_r = right_op.somme_x;
|
||||
double somme_y_r = right_op.somme_y;
|
||||
double produit_xy_r = right_op.produit_xy;
|
||||
double somme_carre_x_r = right_op.somme_carre_x;
|
||||
double produit_xy_ = produit_xy;
|
||||
double somme_carre_x_ = somme_carre_x;
|
||||
assert(fabsf(somme_x - right_op.somme_x) < FLT_EPSILON
|
||||
&& fabsf(somme_y - right_op.somme_y) < FLT_EPSILON
|
||||
&& somme_hauteurs == right_op.somme_hauteurs);
|
||||
#ifdef LPR_DOUBLE_PRECISION
|
||||
assert(fabsf((produit_xy - right_op.produit_xy)) <
|
||||
FLT_EPSILON * (produit_xy + right_op.produit_xy)
|
||||
&& fabsf((somme_carre_x - right_op.somme_carre_x)) < FLT_EPSILON *
|
||||
(somme_carre_x + right_op.somme_carre_x));
|
||||
#else // LPR_DOUBLE_PRECISION
|
||||
assert(fabsf((produit_xy - right_op.produit_xy)) / (produit_xy + right_op.produit_xy) < FLT_EPSILON
|
||||
&& fabsf((somme_carre_x - right_op.somme_carre_x)) / (somme_carre_x + right_op.somme_carre_x) < FLT_EPSILON);
|
||||
#endif // LPR_DOUBLE_PRECISION
|
||||
float dif = fabsf(somme_x - right_op.somme_x);
|
||||
dif = fabsf(somme_y - right_op.somme_y);
|
||||
dif = fabsf((produit_xy - right_op.produit_xy));
|
||||
dif = fabsf((somme_carre_x - right_op.somme_carre_x));
|
||||
#endif
|
||||
#ifdef LPR_DOUBLE_PRECISION
|
||||
return (fabsf(somme_x - right_op.somme_x) < FLT_EPSILON
|
||||
&& fabsf(somme_y - right_op.somme_y) < FLT_EPSILON
|
||||
&& fabsf((produit_xy - right_op.produit_xy)) < FLT_EPSILON * fabsf((produit_xy + right_op.produit_xy))
|
||||
&& fabsf((somme_carre_x - right_op.somme_carre_x)) < FLT_EPSILON * (somme_carre_x + right_op.somme_carre_x)
|
||||
&& somme_hauteurs == right_op.somme_hauteurs);
|
||||
#else // LPR_DOUBLE_PRECISION
|
||||
return (fabsf(somme_x - right_op.somme_x) < FLT_EPSILON
|
||||
&& fabsf(somme_y - right_op.somme_y) < FLT_EPSILON
|
||||
&& fabsf((produit_xy - right_op.produit_xy)) < FLT_EPSILON * (produit_xy + right_op.produit_xy)
|
||||
&& fabsf((somme_carre_x - right_op.somme_carre_x)) < FLT_EPSILON * (somme_carre_x + right_op.somme_carre_x)
|
||||
&& somme_hauteurs == right_op.somme_hauteurs);
|
||||
#endif // LPR_DOUBLE_PRECISION
|
||||
}
|
||||
void C_SumsRegLineXYHDbl::clear()
|
||||
{
|
||||
somme_x = 0.0f;
|
||||
somme_y = 0.0f;
|
||||
produit_xy = 0.0f;
|
||||
somme_carre_x = 0.0f;
|
||||
somme_hauteurs = 0;
|
||||
}
|
||||
#endif //GITHUB
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,171 @@
|
||||
/*
|
||||
// Line.h: interface for the C_Line class.
|
||||
*/
|
||||
#include "../include/utils_image_file.h"
|
||||
#include <opencv2/opencv.hpp>
|
||||
#include <filesystem>
|
||||
#include "../include/utils_alpr_detect.h"
|
||||
#define NUMBER_OF_CARACTERS_LATIN_NUMBERPLATE 36
|
||||
|
||||
/**
|
||||
@brief
|
||||
returns the true license plate number out of a filename
|
||||
you must place the true license plate number in the image filename this way : number+underscore+license plate number,
|
||||
for instance filename 0000000001_3065WWA34.jpg will be interpreted as an image with the license plate 3065WWA34 in it.
|
||||
@param filename: the image filename that contains in it the true registration number
|
||||
@return the lpn contained in the image filename
|
||||
@see
|
||||
*/
|
||||
std::string getTrueLPN(const std::string& filename, const bool& vrai_lpn_after_underscore)
|
||||
{
|
||||
std::string analysing_string = filename;
|
||||
if (analysing_string == "")
|
||||
return std::string();
|
||||
char sep_underscore = '_';
|
||||
size_t index = 0;
|
||||
index = (analysing_string.find(sep_underscore));
|
||||
if (index != -1)
|
||||
{
|
||||
std::string subanalysing_string;//la sous chaine
|
||||
if (!vrai_lpn_after_underscore) {
|
||||
subanalysing_string = analysing_string.substr(0, index);//la sous chaine
|
||||
}
|
||||
else {
|
||||
subanalysing_string = analysing_string.substr(index + 1, analysing_string.length() - (index + 1));//la sous chaine
|
||||
}
|
||||
if (could_be_lpn(subanalysing_string))
|
||||
return subanalysing_string;
|
||||
else return std::string();
|
||||
}
|
||||
else {
|
||||
if (could_be_lpn(filename))
|
||||
return filename;
|
||||
else return std::string();
|
||||
}
|
||||
}
|
||||
/**
|
||||
@brief
|
||||
//checks if the characters contained in lpn are compatible with the alphabet
|
||||
@param lpn: the registration of the vehicle as a string
|
||||
@return
|
||||
@see
|
||||
*/
|
||||
//extracts from a test directory all images files
|
||||
void load_images_filenames(const std::string& dir, std::list<std::string>& image_filenames)
|
||||
{
|
||||
std::filesystem::path p(dir);
|
||||
std::vector<std::filesystem::directory_entry> v; // To save the file names in a vector.
|
||||
if (is_directory(p))
|
||||
{
|
||||
const std::string dir_path = p.string();
|
||||
std::filesystem::directory_iterator b(p), e;
|
||||
for (auto i = b; i != e; ++i)
|
||||
{
|
||||
if (std::filesystem::is_regular_file(*i)) {
|
||||
std::filesystem::path fe = i->path().extension();
|
||||
std::string extension = fe.string();
|
||||
if (extension == ".bmp" || extension == ".BMP" || extension == ".jpg" || extension == ".JPG" || extension == ".jpeg")
|
||||
{
|
||||
std::filesystem::path p_(i->path());
|
||||
//if you want to select images that have the true license plate number in the image filename
|
||||
const bool select_images_with_lpn = true;
|
||||
if (select_images_with_lpn) {
|
||||
bool vrai_lpn_after_underscore = true;
|
||||
//returns the true license plate number out of a filename
|
||||
//you must place the true license plate number in the image filename this way : number + underscore + license plate number,
|
||||
//for instance filename 0000000001_3065WWA34.jpg will be interpreted as an image with the license plate 3065WWA34 in it.
|
||||
std::string ExactLPN(getTrueLPN(p_.stem().string(), vrai_lpn_after_underscore));
|
||||
if (ExactLPN.size() > 3 && ExactLPN.size() < 11) {
|
||||
image_filenames.push_back(i->path().string());
|
||||
}
|
||||
}
|
||||
else {//take all images files -- output stats impossible
|
||||
image_filenames.push_back(i->path().string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
@brief
|
||||
//return the ascii character that corresponds to index class output by the dnn
|
||||
@param classe : integer index = class identifier, output by the object detection dnn
|
||||
@return an ascii character
|
||||
@see
|
||||
*/
|
||||
char get_char(const int classe) {
|
||||
char _LATIN_LETTERS_LATIN_DIGITS[NUMBER_OF_CARACTERS_LATIN_NUMBERPLATE] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
|
||||
'Y', 'Z','0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
|
||||
if (classe >= 0 && classe < NUMBER_OF_CARACTERS_LATIN_NUMBERPLATE)
|
||||
return _LATIN_LETTERS_LATIN_DIGITS[classe];
|
||||
else return '?';
|
||||
}
|
||||
//retourne l'index du caractere LPChar
|
||||
int get_index(const char LPChar)
|
||||
{
|
||||
switch (LPChar) {
|
||||
case 'A': {return 0; } break;
|
||||
case 'B': {return 1; } break;
|
||||
case 'C': {return 2; } break;
|
||||
case 'D': {return 3; } break;
|
||||
case 'E': {return 4; } break;
|
||||
case 'F': {return 5; } break;
|
||||
case 'G': {return 6; } break;
|
||||
case 'H': {return 7; } break;
|
||||
case 'I': {return 8; } break;
|
||||
case 'J': {return 9; } break;
|
||||
case 'K': {return 10; } break;
|
||||
case 'L': {return 11; } break;
|
||||
case 'M': {return 12; } break;
|
||||
case 'N': {return 13; } break;
|
||||
case 'O': {return 14; } break;
|
||||
case 'P': {return 15; } break;
|
||||
case 'Q': {return 16; } break;
|
||||
case 'R': {return 17; } break;
|
||||
case 'S': {return 18; } break;
|
||||
case 'T': {return 19; } break;
|
||||
case 'U': {return 20; } break;
|
||||
case 'V': {return 21; } break;
|
||||
case 'W': {return 22; } break;
|
||||
case 'X': {return 23; } break;
|
||||
case 'Y': {return 24; } break;
|
||||
case 'Z': {return 25; } break;
|
||||
case '0': {return 26; } break;
|
||||
case '1': {return 27; } break;
|
||||
case '2': {return 28; } break;
|
||||
case '3': {return 29; } break;
|
||||
case '4': {return 30; } break;
|
||||
case '5': {return 31; } break;
|
||||
case '6': {return 32; } break;
|
||||
case '7': {return 33; } break;
|
||||
case '8': {return 34; } break;
|
||||
case '9': {return 35; } break;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
//checks if the characters contained in lpn are compatible with the alphabet
|
||||
/**
|
||||
@brief
|
||||
//checks if the characters contained in lpn are compatible with the alphabet
|
||||
@param lpn: the registration of the vehicle as a string
|
||||
@return
|
||||
@see
|
||||
*/
|
||||
bool could_be_lpn(const std::string& lpn) {
|
||||
char _LATIN_LETTERS_LATIN_DIGITS[NUMBER_OF_CARACTERS_LATIN_NUMBERPLATE] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J','K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
|
||||
'Y', 'Z','0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
|
||||
std::string::const_iterator it(lpn.begin());
|
||||
std::list<char> chars;
|
||||
while (it != lpn.end()) {
|
||||
int i;
|
||||
for (i = 0; i < NUMBER_OF_CARACTERS_LATIN_NUMBERPLATE; i++) {
|
||||
if (*it == _LATIN_LETTERS_LATIN_DIGITS[i]) break;
|
||||
}
|
||||
if (i < NUMBER_OF_CARACTERS_LATIN_NUMBERPLATE) {
|
||||
it++;
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,196 @@
|
||||
#include "ANSByteTrack.h"
|
||||
#include "boost/property_tree/ptree.hpp"
|
||||
#include "boost/property_tree/json_parser.hpp"
|
||||
#include "boost/foreach.hpp"
|
||||
#include "boost/optional.hpp"
|
||||
#include <map>
|
||||
namespace ANSCENTER {
|
||||
|
||||
template <typename T>
|
||||
T get_data(const boost::property_tree::ptree& pt, const std::string& key)
|
||||
{
|
||||
T ret;
|
||||
if (boost::optional<T> data = pt.get_optional<T>(key))
|
||||
{
|
||||
ret = data.get();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
std::vector<ByteTrack::Object> GetByteTrackInputs(const boost::property_tree::ptree& pt)
|
||||
{
|
||||
std::vector<ByteTrack::Object> inputs_ref;
|
||||
inputs_ref.clear();
|
||||
BOOST_FOREACH(const boost::property_tree::ptree::value_type & child, pt.get_child("results"))
|
||||
{
|
||||
const boost::property_tree::ptree& result = child.second;
|
||||
const auto class_id = get_data<int>(result, "class_id");
|
||||
const auto prob = get_data<float>(result, "prob");
|
||||
const auto x = get_data<float>(result, "x");
|
||||
const auto y = get_data<float>(result, "y");
|
||||
const auto width = get_data<float>(result, "width");
|
||||
const auto height = get_data<float>(result, "height");
|
||||
const auto left = get_data<float>(result, "left");
|
||||
const auto top = get_data<float>(result, "top");
|
||||
const auto right = get_data<float>(result, "right");
|
||||
const auto bottom = get_data<float>(result, "bottom");
|
||||
const auto object_id = get_data<std::string>(result, "object_id");
|
||||
ByteTrack::Object temp{ ByteTrack::Rect(x, y, width, height),
|
||||
class_id,prob,left,top,right,bottom,object_id };
|
||||
inputs_ref.push_back(temp);
|
||||
}
|
||||
return inputs_ref;
|
||||
}
|
||||
ANSByteTrack::ANSByteTrack() {
|
||||
_licenseValid = false;
|
||||
CheckLicense();
|
||||
tracker.update_parameters(30, 30, 0.5, 0.6, 0.8);
|
||||
}
|
||||
|
||||
ANSByteTrack::~ANSByteTrack() {
|
||||
|
||||
}
|
||||
bool ANSByteTrack::Destroy() {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ANSByteTrack::UpdateParameters(const std::string& trackerParameters) {
|
||||
// Use JSON Boost to parse paramter from trackerParameters
|
||||
try {
|
||||
int frameRate, trackBuffer;
|
||||
double trackThreshold, highThreshold, matchThresold;
|
||||
bool autoFrameRate;
|
||||
frameRate = 15;
|
||||
trackBuffer = 300;
|
||||
trackThreshold = 0.5;
|
||||
highThreshold = 0.1;
|
||||
matchThresold = 0.95;
|
||||
autoFrameRate = true;
|
||||
if (!trackerParameters.empty()) {
|
||||
std::stringstream ss;
|
||||
ss << trackerParameters;
|
||||
boost::property_tree::ptree pt;
|
||||
boost::property_tree::read_json(ss, pt);
|
||||
auto rootNode = pt.get_child("parameters");
|
||||
|
||||
auto childNode = rootNode.get_child("frame_rate");
|
||||
frameRate = childNode.get_value<int>();
|
||||
|
||||
childNode = rootNode.get_child("track_buffer");
|
||||
trackBuffer = childNode.get_value<int>();
|
||||
|
||||
childNode = rootNode.get_child("track_threshold");
|
||||
trackThreshold = childNode.get_value<float>();
|
||||
|
||||
childNode = rootNode.get_child("high_threshold");
|
||||
highThreshold = childNode.get_value<float>();
|
||||
|
||||
childNode = rootNode.get_child("match_thresold");
|
||||
matchThresold = childNode.get_value<float>();
|
||||
|
||||
// Optional: auto frame rate estimation
|
||||
if (auto optNode = rootNode.get_child_optional("auto_frame_rate")) {
|
||||
autoFrameRate = optNode->get_value<int>() != 0;
|
||||
}
|
||||
}
|
||||
tracker.update_parameters(frameRate, trackBuffer, trackThreshold, highThreshold, matchThresold, autoFrameRate);
|
||||
return true;
|
||||
}
|
||||
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogFatal("ANSByteTrack::UpdateParameters", e.what(), __FILE__, __LINE__);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
std::string ANSByteTrack::Update(int modelId, const std::string& detectionData) {
|
||||
if (!_licenseValid) {
|
||||
this->_logger.LogFatal("ANSByteTrack::Update", "Invalid license", __FILE__, __LINE__);
|
||||
return "";
|
||||
}
|
||||
try {
|
||||
boost::property_tree::ptree root;
|
||||
boost::property_tree::ptree trackingObjects;
|
||||
boost::property_tree::ptree pt;
|
||||
//1. Get input
|
||||
std::stringstream ss;
|
||||
ss << detectionData;
|
||||
boost::property_tree::read_json(ss, pt);
|
||||
std::vector<ByteTrack::Object> objects = GetByteTrackInputs(pt);
|
||||
//2. Do tracking
|
||||
const auto outputs = tracker.update(objects);
|
||||
if (outputs.size() > 0) {
|
||||
for (int i = 0; i < outputs.size(); i++) {
|
||||
boost::property_tree::ptree trackingNode;
|
||||
trackingNode.put("model_id", modelId);
|
||||
trackingNode.put("track_id", outputs[i]->getTrackId());
|
||||
trackingNode.put("class_id", outputs[i]->class_id);
|
||||
trackingNode.put("prob", outputs[i]->getScore());
|
||||
trackingNode.put("x", outputs[i]->getRect().tlwh[0]);
|
||||
trackingNode.put("y", outputs[i]->getRect().tlwh[1]);
|
||||
trackingNode.put("width", outputs[i]->getRect().tlwh[2]);
|
||||
trackingNode.put("height", outputs[i]->getRect().tlwh[3]);
|
||||
trackingNode.put("left", outputs[i]->left);
|
||||
trackingNode.put("top", outputs[i]->top);
|
||||
trackingNode.put("right", outputs[i]->right);
|
||||
trackingNode.put("bottom", outputs[i]->bottom);
|
||||
trackingNode.put("valid", outputs[i]->isActivated());
|
||||
trackingNode.put("object_id", outputs[i]->object_id);
|
||||
// Add this node to the list.
|
||||
trackingObjects.push_back(std::make_pair("", trackingNode));
|
||||
}
|
||||
}
|
||||
//3. Convert result
|
||||
root.add_child("results", trackingObjects);
|
||||
std::ostringstream stream;
|
||||
boost::property_tree::write_json(stream, root,false);
|
||||
std::string st = stream.str();
|
||||
return st;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogFatal("ANSByteTrack::Update", e.what(), __FILE__, __LINE__);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<TrackerObject> ANSByteTrack::UpdateTracker(int modelId, const std::vector<TrackerObject>& detectionObjects) {
|
||||
if (!_licenseValid) {
|
||||
this->_logger.LogFatal("ANSByteTrack::UpdateTracker", "Invalid license", __FILE__, __LINE__);
|
||||
return std::vector<TrackerObject>();
|
||||
}
|
||||
try {
|
||||
std::vector<TrackerObject> trackingResults;
|
||||
std::vector<ByteTrack::Object> objects;
|
||||
for (const auto& detObj : detectionObjects) {
|
||||
ByteTrack::Object temp{ ByteTrack::Rect(detObj.x, detObj.y, detObj.width, detObj.height),
|
||||
detObj.class_id,detObj.prob,detObj.left,detObj.top,detObj.right,detObj.bottom,detObj.object_id };
|
||||
objects.push_back(temp);
|
||||
}
|
||||
//2. Do tracking
|
||||
const auto outputs = tracker.update(objects);
|
||||
if (outputs.size() > 0) {
|
||||
for (int i = 0; i < outputs.size(); i++) {
|
||||
TrackerObject trackObj;
|
||||
trackObj.track_id = outputs[i]->getTrackId();
|
||||
trackObj.class_id = outputs[i]->class_id;
|
||||
trackObj.prob = outputs[i]->getScore();
|
||||
trackObj.x = outputs[i]->getRect().tlwh[0];
|
||||
trackObj.y = outputs[i]->getRect().tlwh[1];
|
||||
trackObj.width = outputs[i]->getRect().tlwh[2];
|
||||
trackObj.height = outputs[i]->getRect().tlwh[3];
|
||||
trackObj.left = outputs[i]->left;
|
||||
trackObj.top = outputs[i]->top;
|
||||
trackObj.right = outputs[i]->right;
|
||||
trackObj.bottom = outputs[i]->bottom;
|
||||
trackObj.object_id = outputs[i]->object_id;
|
||||
trackingResults.push_back(trackObj);
|
||||
}
|
||||
}
|
||||
return trackingResults;
|
||||
}
|
||||
catch (const std::exception& e) {
|
||||
this->_logger.LogFatal("ANSByteTrack::UpdateTracker", e.what(), __FILE__, __LINE__);
|
||||
return std::vector<TrackerObject>();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef ANSBYTETRACK_H
|
||||
#define ANSBYTETRACK_H
|
||||
#include "ANSMOT.h"
|
||||
#include "BYTETracker.h"
|
||||
namespace ANSCENTER {
|
||||
class ANSMOT_API ANSByteTrack :public ANSMOT
|
||||
{
|
||||
private:
|
||||
ByteTrack::BYTETracker tracker;
|
||||
public:
|
||||
~ANSByteTrack();
|
||||
ANSByteTrack();
|
||||
[[nodiscard]] bool UpdateParameters(const std::string& trackerParameters) override;
|
||||
[[nodiscard]] std::string Update(int modelId, const std::string& detectionData) override;
|
||||
[[nodiscard]] std::vector<TrackerObject> UpdateTracker(int modelId, const std::vector<TrackerObject>& detectionObjects) override;
|
||||
[[nodiscard]] bool Destroy() override;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user