Fix ALPR order issue
This commit is contained in:
+1
Submodule .claude/worktrees/heuristic-lichterman-bda22b added at 66c67dff26
+136
-243
@@ -356,6 +356,7 @@ namespace ANSCENTER {
|
|||||||
constexpr float ROW_SPLIT_MIN_GAP_FACTOR = 0.2f; // maxGap < avgHeight * this => single row
|
constexpr float ROW_SPLIT_MIN_GAP_FACTOR = 0.2f; // maxGap < avgHeight * this => single row
|
||||||
constexpr float ROW_SPLIT_AVGY_FACTOR = 0.4f; // avgY diff must exceed avgHeight * this
|
constexpr float ROW_SPLIT_AVGY_FACTOR = 0.4f; // avgY diff must exceed avgHeight * this
|
||||||
constexpr size_t ROW_SPLIT_MIN_GROUP_SIZE = 2; // minimum chars per row
|
constexpr size_t ROW_SPLIT_MIN_GROUP_SIZE = 2; // minimum chars per row
|
||||||
|
constexpr float Y_CLUSTER_STRONG_FACTOR = 0.7f; // maxYGap > avgHeight * this => Y-cluster reliable, else use PCA
|
||||||
constexpr float DUPLICATE_DIST_THRESHOLD = 5.0f; // pixels: chars closer than this are duplicates
|
constexpr float DUPLICATE_DIST_THRESHOLD = 5.0f; // pixels: chars closer than this are duplicates
|
||||||
constexpr int DUPLICATE_GRID_SIZE = 10; // spatial hash grid cell size in pixels
|
constexpr int DUPLICATE_GRID_SIZE = 10; // spatial hash grid cell size in pixels
|
||||||
constexpr int ASYNC_TIMEOUT_SECONDS = 30; // timeout for async worker threads
|
constexpr int ASYNC_TIMEOUT_SECONDS = 30; // timeout for async worker threads
|
||||||
@@ -1240,9 +1241,10 @@ namespace ANSCENTER {
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
std::string ANSALPR_OD::DetectLicensePlateString(const cv::Mat& lprROI, const std::string& cameraId) {
|
std::string ANSALPR_OD::DetectLicensePlateString(const cv::Mat& lprROI, const std::string& cameraId) {
|
||||||
// No coarse _mutex — _ocrDetector has its own m_inferenceMutex
|
// No coarse _mutex — _ocrDetector has its own m_inferenceMutex.
|
||||||
|
// Single-frame and batch paths now share post-processing via
|
||||||
|
// ProcessSingleOCRResult (PCA-based; robust to plate tilt).
|
||||||
try {
|
try {
|
||||||
// convert lprROI to greyscale if it is not already
|
|
||||||
if (lprROI.empty()) {
|
if (lprROI.empty()) {
|
||||||
this->_logger.LogError("ANSALPR_OD::DetectLicensePlateString", "Input image is empty", __FILE__, __LINE__);
|
this->_logger.LogError("ANSALPR_OD::DetectLicensePlateString", "Input image is empty", __FILE__, __LINE__);
|
||||||
return "";
|
return "";
|
||||||
@@ -1256,174 +1258,9 @@ namespace ANSCENTER {
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::vector<Object> ocrOutput = _ocrDetector->RunInference(grayLprROI, cameraId);
|
std::vector<Object> ocrOutput = _ocrDetector->RunInference(grayLprROI, cameraId);
|
||||||
std::string ocrText = "";
|
ANS_DBG("OCR_PostProc", "cam=%s entry path=single ocr_dets=%zu lpr=%dx%d",
|
||||||
if (ocrOutput.empty()) return ocrText;
|
cameraId.c_str(), ocrOutput.size(), grayLprROI.cols, grayLprROI.rows);
|
||||||
|
return ProcessSingleOCRResult(ocrOutput, cameraId);
|
||||||
//std::cout << "=== OCR Detections ===" << std::endl;
|
|
||||||
//for (size_t i = 0; i < ocrOutput.size(); ++i) {
|
|
||||||
// << "' X=" << ocrOutput[i].box.x
|
|
||||||
// << " Y=" << ocrOutput[i].box.y << std::endl;
|
|
||||||
//}
|
|
||||||
|
|
||||||
// Remove duplicates
|
|
||||||
std::vector<Object> uniqueOutput;
|
|
||||||
for (const auto& obj : ocrOutput) {
|
|
||||||
bool isDuplicate = false;
|
|
||||||
for (const auto& unique : uniqueOutput) {
|
|
||||||
if (std::abs(obj.box.x - unique.box.x) < DUPLICATE_DIST_THRESHOLD &&
|
|
||||||
std::abs(obj.box.y - unique.box.y) < DUPLICATE_DIST_THRESHOLD) {
|
|
||||||
isDuplicate = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!isDuplicate) {
|
|
||||||
uniqueOutput.push_back(obj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//std::cout << "\nAfter removing duplicates: " << uniqueOutput.size() << " chars" << std::endl;
|
|
||||||
if (uniqueOutput.empty()) return ocrText;
|
|
||||||
if (uniqueOutput.size() == 1) return uniqueOutput[0].className;
|
|
||||||
|
|
||||||
// Calculate average character height for threshold
|
|
||||||
float avgHeight = 0;
|
|
||||||
for (const auto& obj : uniqueOutput) {
|
|
||||||
avgHeight += obj.box.height;
|
|
||||||
}
|
|
||||||
avgHeight /= uniqueOutput.size();
|
|
||||||
|
|
||||||
// Calculate linear regression Y = mX + b
|
|
||||||
float sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0;
|
|
||||||
int n = uniqueOutput.size();
|
|
||||||
|
|
||||||
for (const auto& obj : uniqueOutput) {
|
|
||||||
float x = obj.box.x;
|
|
||||||
float y = obj.box.y;
|
|
||||||
sumX += x;
|
|
||||||
sumY += y;
|
|
||||||
sumXY += x * y;
|
|
||||||
sumX2 += x * x;
|
|
||||||
}
|
|
||||||
|
|
||||||
float denominator = n * sumX2 - sumX * sumX;
|
|
||||||
float slope = (std::abs(denominator) > 1e-6f) ? (n * sumXY - sumX * sumY) / denominator : 0.0f;
|
|
||||||
float intercept = (n > 0) ? (sumY - slope * sumX) / n : 0.0f;
|
|
||||||
|
|
||||||
//std::cout << "Linear regression: Y = " << slope << " * X + " << intercept << std::endl;
|
|
||||||
|
|
||||||
// Calculate perpendicular distance from each point to regression line
|
|
||||||
float a = slope;
|
|
||||||
float b = -1.0f;
|
|
||||||
float c = intercept;
|
|
||||||
float normFactor = std::sqrt(a * a + b * b);
|
|
||||||
|
|
||||||
std::vector<std::pair<float, size_t>> distances;
|
|
||||||
for (size_t i = 0; i < uniqueOutput.size(); ++i) {
|
|
||||||
float x = uniqueOutput[i].box.x;
|
|
||||||
float y = uniqueOutput[i].box.y;
|
|
||||||
float dist = (a * x + b * y + c) / normFactor;
|
|
||||||
distances.push_back({ dist, i });
|
|
||||||
}
|
|
||||||
|
|
||||||
// Sort by perpendicular distance
|
|
||||||
std::sort(distances.begin(), distances.end(),
|
|
||||||
[](const std::pair<float, size_t>& a, const std::pair<float, size_t>& b) {
|
|
||||||
return a.first < b.first;
|
|
||||||
});
|
|
||||||
|
|
||||||
// Find largest gap in perpendicular distances
|
|
||||||
float maxGap = 0;
|
|
||||||
size_t splitIdx = distances.size() / 2;
|
|
||||||
|
|
||||||
//std::cout << "\n=== Distance gaps ===" << std::endl;
|
|
||||||
for (size_t i = 1; i < distances.size(); ++i) {
|
|
||||||
float gap = distances[i].first - distances[i - 1].first;
|
|
||||||
//std::cout << "Gap " << i << ": " << gap << std::endl;
|
|
||||||
if (gap > maxGap) {
|
|
||||||
maxGap = gap;
|
|
||||||
splitIdx = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if this is actually a single row
|
|
||||||
// If max gap is too small relative to character height, it's a single row
|
|
||||||
std::vector<std::vector<Object>> rows;
|
|
||||||
|
|
||||||
if (maxGap < avgHeight * ROW_SPLIT_MIN_GAP_FACTOR) {
|
|
||||||
// Single row - all characters on one line
|
|
||||||
rows.resize(1);
|
|
||||||
rows[0] = uniqueOutput;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// Two rows
|
|
||||||
rows.resize(2);
|
|
||||||
|
|
||||||
// Split into two rows based on perpendicular distance
|
|
||||||
for (size_t i = 0; i < distances.size(); ++i) {
|
|
||||||
size_t objIdx = distances[i].second;
|
|
||||||
if (i < splitIdx) {
|
|
||||||
rows[0].push_back(uniqueOutput[objIdx]);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
rows[1].push_back(uniqueOutput[objIdx]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Determine which row is on top (lower average Y = top row)
|
|
||||||
float avgY0 = 0, avgY1 = 0;
|
|
||||||
for (const auto& obj : rows[0]) {
|
|
||||||
avgY0 += obj.box.y;
|
|
||||||
}
|
|
||||||
for (const auto& obj : rows[1]) {
|
|
||||||
avgY1 += obj.box.y;
|
|
||||||
}
|
|
||||||
avgY0 /= rows[0].size();
|
|
||||||
avgY1 /= rows[1].size();
|
|
||||||
|
|
||||||
//std::cout << "Average Y: Row0=" << avgY0 << " Row1=" << avgY1 << std::endl;
|
|
||||||
|
|
||||||
// Swap if needed (top row should be row 0)
|
|
||||||
if (avgY0 > avgY1) {
|
|
||||||
//std::cout << "Swapping rows (Row 0 should be on top)" << std::endl;
|
|
||||||
std::swap(rows[0], rows[1]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//std::cout << "\n=== Rows before X-sorting ===" << std::endl;
|
|
||||||
//for (size_t r = 0; r < rows.size(); ++r) {
|
|
||||||
// std::cout << "Row " << r << " (" << rows[r].size() << " chars): ";
|
|
||||||
// for (const auto& obj : rows[r]) {
|
|
||||||
// }
|
|
||||||
// std::cout << std::endl;
|
|
||||||
//}
|
|
||||||
|
|
||||||
// Sort each row by X
|
|
||||||
for (auto& row : rows) {
|
|
||||||
std::sort(row.begin(), row.end(),
|
|
||||||
[](const Object& a, const Object& b) {
|
|
||||||
return a.box.x < b.box.x;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/*std::cout << "\n=== Rows after X-sorting ===" << std::endl;
|
|
||||||
for (size_t r = 0; r < rows.size(); ++r) {
|
|
||||||
std::cout << "Row " << r << ": ";
|
|
||||||
for (const auto& obj : rows[r]) {
|
|
||||||
}
|
|
||||||
std::cout << std::endl;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
// Concatenate
|
|
||||||
for (const auto& row : rows) {
|
|
||||||
for (const auto& obj : row) {
|
|
||||||
ocrText += obj.className;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//std::cout << "\nFinal text: '" << ocrText << "'" << std::endl;
|
|
||||||
grayLprROI.release(); // Release the grayscale image to free memory
|
|
||||||
std::string processedOcrText = AnalyseLicensePlateText(ocrText);
|
|
||||||
return processedOcrText;
|
|
||||||
}
|
}
|
||||||
catch (std::exception& e) {
|
catch (std::exception& e) {
|
||||||
this->_logger.LogFatal("ANSALPR_OD::DetectLicensePlateString", e.what(), __FILE__, __LINE__);
|
this->_logger.LogFatal("ANSALPR_OD::DetectLicensePlateString", e.what(), __FILE__, __LINE__);
|
||||||
@@ -2917,7 +2754,7 @@ namespace ANSCENTER {
|
|||||||
// Process OCR results — ProcessSingleOCRResult is pure CPU math
|
// Process OCR results — ProcessSingleOCRResult is pure CPU math
|
||||||
// on ~5-20 characters per plate, so thread overhead dominates.
|
// on ~5-20 characters per plate, so thread overhead dominates.
|
||||||
for (size_t i = 0; i < batchSize; ++i) {
|
for (size_t i = 0; i < batchSize; ++i) {
|
||||||
results[i] = ProcessSingleOCRResult(ocrBatchOutputs[i]);
|
results[i] = ProcessSingleOCRResult(ocrBatchOutputs[i], cameraId);
|
||||||
}
|
}
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
@@ -2931,8 +2768,9 @@ namespace ANSCENTER {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// New helper function - thread-safe, no shared state
|
// New helper function - thread-safe, no shared state
|
||||||
std::string ANSALPR_OD::ProcessSingleOCRResult(const std::vector<Object>& ocrOutput) {
|
std::string ANSALPR_OD::ProcessSingleOCRResult(const std::vector<Object>& ocrOutput, const std::string& cameraId) {
|
||||||
if (ocrOutput.empty()) {
|
if (ocrOutput.empty()) {
|
||||||
|
ANS_DBG("OCR_PostProc", "cam=%s empty raw OCR output", cameraId.c_str());
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2978,10 +2816,12 @@ namespace ANSCENTER {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (uniqueOutput.empty()) {
|
if (uniqueOutput.empty()) {
|
||||||
|
ANS_DBG("OCR_PostProc", "cam=%s all dets dropped as duplicates raw=%zu", cameraId.c_str(), ocrOutput.size());
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uniqueOutput.size() == 1) {
|
if (uniqueOutput.size() == 1) {
|
||||||
|
ANS_DBG("OCR_PostProc", "cam=%s single-char plate cls=%s", cameraId.c_str(), uniqueOutput[0].className.c_str());
|
||||||
return AnalyseLicensePlateText(uniqueOutput[0].className);
|
return AnalyseLicensePlateText(uniqueOutput[0].className);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2996,17 +2836,85 @@ namespace ANSCENTER {
|
|||||||
}
|
}
|
||||||
avgHeight /= static_cast<float>(n);
|
avgHeight /= static_cast<float>(n);
|
||||||
|
|
||||||
// ---- Mean center ----
|
// Hybrid row-split strategy:
|
||||||
|
// - If chars cluster cleanly in Y (maxYGap > avgHeight*Y_CLUSTER_STRONG_FACTOR),
|
||||||
|
// trust Y-clustering. Handles flat plates with asymmetric layouts where PCA
|
||||||
|
// gets fooled by cov(x,y) from row-centroid mismatch.
|
||||||
|
// - Otherwise, the plate is either single-row or significantly tilted; fall
|
||||||
|
// back to PCA on character centers to find the true reading direction,
|
||||||
|
// then split rows by perpendicular projection. Handles tilts up to ~45°
|
||||||
|
// where rows interleave in raw Y.
|
||||||
|
//
|
||||||
|
// Failure precedents:
|
||||||
|
// "30E89916" (78x64, top "30E" centered over wider bottom "89916", flat):
|
||||||
|
// PCA invents theta≈33° from layout → "30E16899". Y-cluster works.
|
||||||
|
// "65L149702" (heavily tilted ~28°):
|
||||||
|
// Y-cluster splits rows mid-row at the largest Y gap → "654970L21".
|
||||||
|
// PCA correctly finds reading direction.
|
||||||
|
std::vector<std::pair<float, size_t>> ySorted;
|
||||||
|
ySorted.reserve(n);
|
||||||
|
for (int i = 0; i < n; ++i)
|
||||||
|
ySorted.push_back({ cys[i], static_cast<size_t>(i) });
|
||||||
|
std::sort(ySorted.begin(), ySorted.end());
|
||||||
|
|
||||||
|
float maxYGap = 0.0f;
|
||||||
|
size_t ySplitIdx = ySorted.size();
|
||||||
|
const size_t minGroupSize = ROW_SPLIT_MIN_GROUP_SIZE;
|
||||||
|
for (size_t i = 1; i < ySorted.size(); ++i) {
|
||||||
|
if (i < minGroupSize || (ySorted.size() - i) < minGroupSize)
|
||||||
|
continue;
|
||||||
|
float gap = ySorted[i].first - ySorted[i - 1].first;
|
||||||
|
if (gap > maxYGap) {
|
||||||
|
maxYGap = gap;
|
||||||
|
ySplitIdx = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const float gapThreshold = avgHeight * ROW_SPLIT_MIN_GAP_FACTOR;
|
||||||
|
const float strongYThreshold = avgHeight * Y_CLUSTER_STRONG_FACTOR;
|
||||||
|
const bool useYCluster = (maxYGap > strongYThreshold) && (ySplitIdx < ySorted.size());
|
||||||
|
|
||||||
|
auto sortByXAndConcat = [&](std::vector<size_t>& row, std::string& out) {
|
||||||
|
std::sort(row.begin(), row.end(), [&](size_t a, size_t b) {
|
||||||
|
return uniqueOutput[a].box.x < uniqueOutput[b].box.x;
|
||||||
|
});
|
||||||
|
for (size_t idx : row) out += uniqueOutput[idx].className;
|
||||||
|
};
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// Path A: Y-clustering (flat plates with reliable Y separation)
|
||||||
|
// ============================================================
|
||||||
|
if (useYCluster) {
|
||||||
|
std::vector<size_t> topRow, bottomRow;
|
||||||
|
topRow.reserve(ySplitIdx);
|
||||||
|
bottomRow.reserve(ySorted.size() - ySplitIdx);
|
||||||
|
for (size_t i = 0; i < ySplitIdx; ++i) topRow.push_back(ySorted[i].second);
|
||||||
|
for (size_t i = ySplitIdx; i < ySorted.size(); ++i) bottomRow.push_back(ySorted[i].second);
|
||||||
|
|
||||||
|
std::string ocrText;
|
||||||
|
ocrText.reserve(n);
|
||||||
|
sortByXAndConcat(topRow, ocrText);
|
||||||
|
sortByXAndConcat(bottomRow, ocrText);
|
||||||
|
std::string finalText = AnalyseLicensePlateText(ocrText);
|
||||||
|
|
||||||
|
float avgY0 = 0.0f, avgY1 = 0.0f;
|
||||||
|
for (size_t idx : topRow) avgY0 += cys[idx];
|
||||||
|
for (size_t idx : bottomRow) avgY1 += cys[idx];
|
||||||
|
avgY0 /= static_cast<float>(topRow.size());
|
||||||
|
avgY1 /= static_cast<float>(bottomRow.size());
|
||||||
|
ANS_DBG("OCR_PostProc", "cam=%s Y-cluster two-row n=%d split=%zu/%zu maxYGap=%.2f avgYsep=%.2f raw='%s' final='%s'",
|
||||||
|
cameraId.c_str(), n, topRow.size(), bottomRow.size(),
|
||||||
|
maxYGap, std::abs(avgY1 - avgY0), ocrText.c_str(), finalText.c_str());
|
||||||
|
return finalText;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================================
|
||||||
|
// Path B: PCA (single-row or tilted plates)
|
||||||
|
// ============================================================
|
||||||
float meanX = 0.0f, meanY = 0.0f;
|
float meanX = 0.0f, meanY = 0.0f;
|
||||||
for (int i = 0; i < n; ++i) {
|
for (int i = 0; i < n; ++i) { meanX += cxs[i]; meanY += cys[i]; }
|
||||||
meanX += cxs[i];
|
meanX /= n; meanY /= n;
|
||||||
meanY += cys[i];
|
|
||||||
}
|
|
||||||
meanX /= n;
|
|
||||||
meanY /= n;
|
|
||||||
|
|
||||||
// PCA to find the plate's reading direction.
|
|
||||||
// Robust to large tilt angles unlike OLS regression.
|
|
||||||
float cov_xx = 0.0f, cov_xy = 0.0f, cov_yy = 0.0f;
|
float cov_xx = 0.0f, cov_xy = 0.0f, cov_yy = 0.0f;
|
||||||
for (int i = 0; i < n; ++i) {
|
for (int i = 0; i < n; ++i) {
|
||||||
float dx = cxs[i] - meanX;
|
float dx = cxs[i] - meanX;
|
||||||
@@ -3017,14 +2925,10 @@ namespace ANSCENTER {
|
|||||||
}
|
}
|
||||||
|
|
||||||
float theta = 0.5f * std::atan2(2.0f * cov_xy, cov_xx - cov_yy);
|
float theta = 0.5f * std::atan2(2.0f * cov_xy, cov_xx - cov_yy);
|
||||||
float dirX = std::cos(theta);
|
float dirX = std::cos(theta), dirY = std::sin(theta);
|
||||||
float dirY = std::sin(theta);
|
|
||||||
if (dirX < 0) { dirX = -dirX; dirY = -dirY; }
|
if (dirX < 0) { dirX = -dirX; dirY = -dirY; }
|
||||||
|
float perpX = -dirY, perpY = dirX;
|
||||||
|
|
||||||
float perpX = -dirY;
|
|
||||||
float perpY = dirX;
|
|
||||||
|
|
||||||
// Project centers onto both axes
|
|
||||||
std::vector<float> projAlong(n), projPerp(n);
|
std::vector<float> projAlong(n), projPerp(n);
|
||||||
for (int i = 0; i < n; ++i) {
|
for (int i = 0; i < n; ++i) {
|
||||||
float dx = cxs[i] - meanX;
|
float dx = cxs[i] - meanX;
|
||||||
@@ -3033,84 +2937,73 @@ namespace ANSCENTER {
|
|||||||
projPerp[i] = dx * perpX + dy * perpY;
|
projPerp[i] = dx * perpX + dy * perpY;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Row splitting using perpendicular projections
|
|
||||||
std::vector<std::pair<float, size_t>> perpSorted;
|
std::vector<std::pair<float, size_t>> perpSorted;
|
||||||
perpSorted.reserve(n);
|
perpSorted.reserve(n);
|
||||||
for (int i = 0; i < n; ++i)
|
for (int i = 0; i < n; ++i)
|
||||||
perpSorted.push_back({ projPerp[i], static_cast<size_t>(i) });
|
perpSorted.push_back({ projPerp[i], static_cast<size_t>(i) });
|
||||||
std::sort(perpSorted.begin(), perpSorted.end());
|
std::sort(perpSorted.begin(), perpSorted.end());
|
||||||
|
|
||||||
// Find largest gap with validation:
|
float maxPerpGap = 0.0f;
|
||||||
// 1. Both groups must have >= 2 chars
|
size_t perpSplitIdx = perpSorted.size();
|
||||||
// 2. Groups must be vertically separated (avgY check)
|
|
||||||
float maxGap = 0.0f;
|
|
||||||
size_t splitIdx = perpSorted.size() / 2;
|
|
||||||
const size_t minGroupSize = ROW_SPLIT_MIN_GROUP_SIZE;
|
|
||||||
|
|
||||||
for (size_t i = 1; i < perpSorted.size(); ++i) {
|
for (size_t i = 1; i < perpSorted.size(); ++i) {
|
||||||
float gap = perpSorted[i].first - perpSorted[i - 1].first;
|
|
||||||
if (i < minGroupSize || (perpSorted.size() - i) < minGroupSize)
|
if (i < minGroupSize || (perpSorted.size() - i) < minGroupSize)
|
||||||
continue;
|
continue;
|
||||||
if (gap > maxGap) {
|
float gap = perpSorted[i].first - perpSorted[i - 1].first;
|
||||||
|
if (gap > maxPerpGap) {
|
||||||
float avgY_g1 = 0.0f, avgY_g2 = 0.0f;
|
float avgY_g1 = 0.0f, avgY_g2 = 0.0f;
|
||||||
for (size_t j = 0; j < i; ++j)
|
for (size_t j = 0; j < i; ++j) avgY_g1 += cys[perpSorted[j].second];
|
||||||
avgY_g1 += cys[perpSorted[j].second];
|
for (size_t j = i; j < perpSorted.size(); ++j) avgY_g2 += cys[perpSorted[j].second];
|
||||||
for (size_t j = i; j < perpSorted.size(); ++j)
|
|
||||||
avgY_g2 += cys[perpSorted[j].second];
|
|
||||||
avgY_g1 /= static_cast<float>(i);
|
avgY_g1 /= static_cast<float>(i);
|
||||||
avgY_g2 /= static_cast<float>(perpSorted.size() - i);
|
avgY_g2 /= static_cast<float>(perpSorted.size() - i);
|
||||||
if (std::abs(avgY_g2 - avgY_g1) > avgHeight * ROW_SPLIT_AVGY_FACTOR) {
|
if (std::abs(avgY_g2 - avgY_g1) > avgHeight * ROW_SPLIT_AVGY_FACTOR) {
|
||||||
maxGap = gap;
|
maxPerpGap = gap;
|
||||||
splitIdx = i;
|
perpSplitIdx = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (maxGap < avgHeight * ROW_SPLIT_MIN_GAP_FACTOR) {
|
auto sortByProjAlongAndConcat = [&](std::vector<size_t>& row, std::string& out) {
|
||||||
// Single row - sort by projection along reading direction
|
std::sort(row.begin(), row.end(), [&](size_t a, size_t b) {
|
||||||
std::vector<std::pair<float, size_t>> allProj;
|
return projAlong[a] < projAlong[b];
|
||||||
for (int i = 0; i < n; ++i)
|
});
|
||||||
allProj.push_back({ projAlong[i], static_cast<size_t>(i) });
|
for (size_t idx : row) out += uniqueOutput[idx].className;
|
||||||
std::sort(allProj.begin(), allProj.end());
|
};
|
||||||
|
|
||||||
|
if (maxPerpGap < gapThreshold || perpSplitIdx == perpSorted.size()) {
|
||||||
|
// Single row along PCA axis
|
||||||
|
std::vector<size_t> all(n);
|
||||||
|
for (int i = 0; i < n; ++i) all[i] = static_cast<size_t>(i);
|
||||||
std::string ocrText;
|
std::string ocrText;
|
||||||
ocrText.reserve(n);
|
ocrText.reserve(n);
|
||||||
for (const auto& p : allProj)
|
sortByProjAlongAndConcat(all, ocrText);
|
||||||
ocrText += uniqueOutput[p.second].className;
|
std::string finalText = AnalyseLicensePlateText(ocrText);
|
||||||
return AnalyseLicensePlateText(ocrText);
|
ANS_DBG("OCR_PostProc", "cam=%s PCA single-row n=%d theta=%.3f maxPerpGap=%.2f raw='%s' final='%s'",
|
||||||
|
cameraId.c_str(), n, theta, maxPerpGap, ocrText.c_str(), finalText.c_str());
|
||||||
|
return finalText;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
// Two rows
|
|
||||||
std::vector<std::vector<size_t>> rowIndices(2);
|
|
||||||
for (size_t i = 0; i < perpSorted.size(); ++i) {
|
|
||||||
size_t objIdx = perpSorted[i].second;
|
|
||||||
if (i < splitIdx)
|
|
||||||
rowIndices[0].push_back(objIdx);
|
|
||||||
else
|
|
||||||
rowIndices[1].push_back(objIdx);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ensure row 0 is top row (lower average center-Y)
|
std::vector<size_t> topRow, bottomRow;
|
||||||
float avgY0 = 0.0f, avgY1 = 0.0f;
|
topRow.reserve(perpSplitIdx);
|
||||||
for (auto idx : rowIndices[0]) avgY0 += cys[idx];
|
bottomRow.reserve(perpSorted.size() - perpSplitIdx);
|
||||||
for (auto idx : rowIndices[1]) avgY1 += cys[idx];
|
for (size_t i = 0; i < perpSplitIdx; ++i) topRow.push_back(perpSorted[i].second);
|
||||||
avgY0 /= static_cast<float>(rowIndices[0].size());
|
for (size_t i = perpSplitIdx; i < perpSorted.size(); ++i) bottomRow.push_back(perpSorted[i].second);
|
||||||
avgY1 /= static_cast<float>(rowIndices[1].size());
|
|
||||||
if (avgY0 > avgY1)
|
|
||||||
std::swap(rowIndices[0], rowIndices[1]);
|
|
||||||
|
|
||||||
// Sort each row by projection along reading direction
|
float avgY0 = 0.0f, avgY1 = 0.0f;
|
||||||
std::string ocrText;
|
for (size_t idx : topRow) avgY0 += cys[idx];
|
||||||
ocrText.reserve(n);
|
for (size_t idx : bottomRow) avgY1 += cys[idx];
|
||||||
for (const auto& row : rowIndices) {
|
avgY0 /= static_cast<float>(topRow.size());
|
||||||
std::vector<std::pair<float, size_t>> rowProj;
|
avgY1 /= static_cast<float>(bottomRow.size());
|
||||||
for (auto idx : row)
|
if (avgY0 > avgY1) std::swap(topRow, bottomRow);
|
||||||
rowProj.push_back({ projAlong[idx], idx });
|
|
||||||
std::sort(rowProj.begin(), rowProj.end());
|
std::string ocrText;
|
||||||
for (const auto& p : rowProj)
|
ocrText.reserve(n);
|
||||||
ocrText += uniqueOutput[p.second].className;
|
sortByProjAlongAndConcat(topRow, ocrText);
|
||||||
}
|
sortByProjAlongAndConcat(bottomRow, ocrText);
|
||||||
return AnalyseLicensePlateText(ocrText);
|
std::string finalText = AnalyseLicensePlateText(ocrText);
|
||||||
}
|
ANS_DBG("OCR_PostProc", "cam=%s PCA two-row n=%d theta=%.3f split=%zu/%zu maxPerpGap=%.2f avgYsep=%.2f raw='%s' final='%s'",
|
||||||
|
cameraId.c_str(), n, theta, topRow.size(), bottomRow.size(),
|
||||||
|
maxPerpGap, std::abs(avgY1 - avgY0), ocrText.c_str(), finalText.c_str());
|
||||||
|
return finalText;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ANSALPR_OD::ensureUniquePlateText(std::vector<Object>& results, const std::string& cameraId)
|
void ANSALPR_OD::ensureUniquePlateText(std::vector<Object>& results, const std::string& cameraId)
|
||||||
|
|||||||
@@ -249,7 +249,7 @@ namespace ANSCENTER
|
|||||||
const std::string& cameraId) override;
|
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> 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::vector<std::string> DetectLPColourDetectorBatch(const std::vector<cv::Mat>& lprROIs, const std::string& cameraId);
|
||||||
[[nodiscard]] std::string ProcessSingleOCRResult(const std::vector<Object>& ocrOutput);
|
[[nodiscard]] std::string ProcessSingleOCRResult(const std::vector<Object>& ocrOutput, const std::string& cameraId);
|
||||||
[[nodiscard]] bool Destroy() override;
|
[[nodiscard]] bool Destroy() override;
|
||||||
|
|
||||||
/// Propagate debug flag to all sub-detectors (LPD, OCR, colour)
|
/// Propagate debug flag to all sub-detectors (LPD, OCR, colour)
|
||||||
|
|||||||
@@ -417,7 +417,7 @@ int ANSLPR_OD_VideoTest() {
|
|||||||
std::string licenseKey = "";
|
std::string licenseKey = "";
|
||||||
std::string modelZipFile ="C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ANS_ALPR_v1.2.zip";// "C:\\Projects\\ANSVIS\\Models\\ANS_ALPR_v1.2.zip";// "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ServerOptimised\\ANS_ALPR_v1.1_NVIDIAGeForceRTX4070LaptopGPU.zip";
|
std::string modelZipFile ="C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ANS_ALPR_v1.2.zip";// "C:\\Projects\\ANSVIS\\Models\\ANS_ALPR_v1.2.zip";// "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ServerOptimised\\ANS_ALPR_v1.1_NVIDIAGeForceRTX4070LaptopGPU.zip";
|
||||||
|
|
||||||
std::string videoFilePath = "C:\\ProgramData\\ANSCENTER\\Shared\\day.mp4";//"E:\\Programs\\DemoAssets\\Videos\\ALRP\\PMH\\Day\\day.mp4";//
|
std::string videoFilePath = "E:\\Programs\\DemoAssets\\Videos\\ALRP\\PMH\\Day\\day.mp4";// "C:\\ProgramData\\ANSCENTER\\Shared\\day.mp4";//
|
||||||
std::string lpnResult;
|
std::string lpnResult;
|
||||||
int engineType = 1;
|
int engineType = 1;
|
||||||
double detectionThreshold = 0.5;
|
double detectionThreshold = 0.5;
|
||||||
@@ -701,8 +701,10 @@ int ANSLPR_OD_INDOInferences_FileTest() {
|
|||||||
boost::property_tree::ptree pt;
|
boost::property_tree::ptree pt;
|
||||||
ANSCENTER::ANSALPR* infHandle;
|
ANSCENTER::ANSALPR* infHandle;
|
||||||
std::string licenseKey = "";
|
std::string licenseKey = "";
|
||||||
std::string modelZipFile = "E:\\Programs\\DemoAssets\\ModelsForANSVIS\\ANS_ALPR_IND_v1.1.zip";
|
std::string modelZipFile = "C:\\ProgramData\\ANSCENTER\\ANSVIS Server\\ANSALPR\\ServerOptimised\\ANS_ALPR_v1.4_NVIDIAGeForceRTX4070LaptopGPU.zip";//"E:\\Programs\\DemoAssets\\ModelsForANSVIS\\ANS_ALPR_IND_v1.1.zip";
|
||||||
std::string imageFilePath = "E:\\Programs\\TrainingWorkingStation\\IndoALPR\\Indonesian License Plate Dataset\\data\\train075.jpg";//20250912_213850.717.jpg; 20250912_213850.511.jpg;//20250912_213850.411.jpg;//20250912_213850.261.jpg(65H115912:0.73) cororect (20250912_213850.071.jpg: 65H115833)
|
std::string imageFilePath = "E:\\Programs\\DemoAssets\\Images\\ALPRTest\\alpr_loi\\702.jpg";//"E:\\Programs\\TrainingWorkingStation\\IndoALPR\\Indonesian License Plate Dataset\\data\\train075.jpg";//20250912_213850.717.jpg; 20250912_213850.511.jpg;//20250912_213850.411.jpg;//20250912_213850.261.jpg(65H115912:0.73) cororect (20250912_213850.071.jpg: 65H115833)
|
||||||
|
|
||||||
|
//"E:\\Programs\\DemoAssets\\Images\\ALPRTest\\WrongOrder\\4389.jpg";///
|
||||||
std::string lpnResult;
|
std::string lpnResult;
|
||||||
int engineType = 1;
|
int engineType = 1;
|
||||||
double detectionThreshold = 0.3;
|
double detectionThreshold = 0.3;
|
||||||
@@ -5199,14 +5201,14 @@ int main()
|
|||||||
SetConsoleOutputCP(CP_UTF8);
|
SetConsoleOutputCP(CP_UTF8);
|
||||||
SetConsoleCP(CP_UTF8);
|
SetConsoleCP(CP_UTF8);
|
||||||
#endif
|
#endif
|
||||||
// ANSLPR_OD_INDOInferences_FileTest();
|
//ANSLPR_OD_INDOInferences_FileTest();
|
||||||
//ANSLPR_OD_Inferences_FileTest();
|
//ANSLPR_OD_Inferences_FileTest();
|
||||||
// Disabled — has a hardcoded NVIDIA RTX 4070 TensorRT-optimised zip path
|
// Disabled — has a hardcoded NVIDIA RTX 4070 TensorRT-optimised zip path
|
||||||
// (ServerOptimised\ANS_ALPR_v1.2_NVIDIAGeForceRTX4070LaptopGPU.zip) that
|
// (ServerOptimised\ANS_ALPR_v1.2_NVIDIAGeForceRTX4070LaptopGPU.zip) that
|
||||||
// doesn't exist on AMD/Intel machines. Re-enable after fixing the path
|
// doesn't exist on AMD/Intel machines. Re-enable after fixing the path
|
||||||
// at line 573 if you need the OD folder test.
|
// at line 573 if you need the OD folder test.
|
||||||
//ANSLPR_OD_FolderInferences_Test();
|
//ANSLPR_OD_FolderInferences_Test();
|
||||||
//ANSLPR_OD_VideoTest();
|
ANSLPR_OD_VideoTest();
|
||||||
//ANSLPR_BigSize_VideoTest();
|
//ANSLPR_BigSize_VideoTest();
|
||||||
//ANSLPR_CPU_VideoTest();
|
//ANSLPR_CPU_VideoTest();
|
||||||
//for (int i = 0; i < 100; i++) {
|
//for (int i = 0; i < 100; i++) {
|
||||||
@@ -5220,7 +5222,7 @@ int main()
|
|||||||
//ANSLPR_OD_CPU_VideoTest();
|
//ANSLPR_OD_CPU_VideoTest();
|
||||||
//ALPR_OCR_Test();
|
//ALPR_OCR_Test();
|
||||||
//ALPR_OCR_VideoTest();
|
//ALPR_OCR_VideoTest();
|
||||||
ALPR_OCR_FolderInferences_Test();
|
//ALPR_OCR_FolderInferences_Test();
|
||||||
//ALPR_Helmet_Parallel_VideoTest(); // ANSVIS sim: ALPR + Helmet on same FilePlayer
|
//ALPR_Helmet_Parallel_VideoTest(); // ANSVIS sim: ALPR + Helmet on same FilePlayer
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user