Skip to content

Commit 67ac052

Browse files
cursoragentznsoftm
andcommitted
Cut ring-angle search to eight starts and publish the web-page detector.
The coarser search is faster and more accurate on the hold-out than the 24-step refine. A fixed real-page test split is recorded, and the detector finetuned on those pages trims false boxes only slightly. Co-authored-by: Daniel <znsoft@163.com>
1 parent 4e805b0 commit 67ac052

21 files changed

Lines changed: 1259 additions & 128 deletions

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ CPU 上的印章检测与识别。覆盖中文圆形公章、椭圆章、方形
1212
| 几何 | 圆/椭圆极坐标展开,方章按格切开,矩形取水平带 | 无参数 |
1313
| SealRec | 灰度 32×384,CNN-CTC,无 RNN | 6,169,425 参数(2,817 类),ONNX 约 24MB |
1414

15-
`models/metrics.json` 是合成页和 ReST 留出集混在一起的 hold-out,不是 ICDAR 官方 test。检测 mAP50 0.798,检测召回 0.999。标题整串命中 0.427(合成页 0.522,ReST 0.212)。圆章对齐条带整串 0.764(146/191),整页 0.251。Paddle 验证条带的公司名(第一个数字之前)命中 0.465,整串只有 0.113。自由姓名格条带 0.375,合成方章条带和整页方章仍是 0。训练机 CPU 上整页约 729 ms。
15+
`models/metrics.json` 是合成页和 ReST 留出集混在一起的 hold-out,不是 ICDAR 官方 test。检测 mAP50 0.804,检测召回 0.997。标题整串命中 0.513(合成页 0.645,ReST 0.216)。圆章整页 0.339,对齐条带仍是 0.764(146/191)。外文公司章整页 0.898,财务章 0.547,发票章 0.757,公证章 0.779,椭圆 0.315,矩形章 0.830,方章仍是 0。Paddle 验证条带的公司名命中仍是 0.465,整串 0.113。合成方章条带仍是 0/137。训练机 CPU 上整页约 217 ms。网页真实测试集 52 页:类无关 precision@0.5 从 0.024 到 0.033,召回从 0.345 到 0.379,mAP50 从 0.076 到 0.032。
1616

1717
## 目录
1818

‎cpp/src/engine.cpp‎

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -259,43 +259,38 @@ std::pair<std::string, float> rapidstamp_engine::read_layout(const RsImage& crop
259259
const int grids[4][2] = {{2, 1}, {3, 1}, {4, 1}, {2, 2}};
260260
for (const auto& item : grids) {
261261
auto decoded = recognize(grid_unroll(crop, item[0], item[1], config.rec_h, config.rec_w));
262-
if (decoded.second > best.second) best = decoded;
262+
float penalty = 0.05f * std::abs(utf8_len(decoded.first) - item[0] * item[1]);
263+
best = prefer_reading(best, {decoded.first, decoded.second - penalty});
263264
}
264265
return best;
265266
}
266267
RsImage source = layout == "circle" ? pad_to_square(crop, 255) : crop;
267-
struct Reading {
268-
float start;
269-
std::string text;
270-
float score;
268+
std::vector<float> starts;
269+
auto push_unique = [&](float start) {
270+
float wrapped = std::fmod(start, 360.f);
271+
if (wrapped < 0.f) wrapped += 360.f;
272+
int key = static_cast<int>(std::lround(wrapped)) % 360;
273+
if (key < 0) key += 360;
274+
for (float existing : starts) {
275+
int other = static_cast<int>(std::lround(existing)) % 360;
276+
if (other < 0) other += 360;
277+
if (other == key) return;
278+
}
279+
starts.push_back(wrapped);
271280
};
272-
std::vector<Reading> readings;
273-
int steps = std::max(1, config.angle_steps);
274-
auto push = [&](float start) {
281+
// Eight starts. A 15 degree grid lets a rotated fragment outscore the title.
282+
int steps = config.angle_steps >= 8 ? 8 : std::max(1, config.angle_steps);
283+
for (int step = 0; step < steps; ++step) push_unique(step * (360.f / static_cast<float>(steps)));
284+
push_unique(config.start_deg);
285+
float guess = 0.f;
286+
float gap = 0.f;
287+
if (estimate_ring_start(source, config.r_in, config.r_out, guess, gap) && gap >= 40.f) push_unique(guess);
288+
std::pair<std::string, float> best{"", -1.f};
289+
for (float start : starts) {
275290
auto decoded = recognize(polar_unwrap(source, config.rec_w, config.rec_h, config.r_in, config.r_out, start,
276291
config.span_deg));
277-
readings.push_back(Reading{start, decoded.first, decoded.second});
278-
};
279-
for (int step = 0; step < steps; ++step) push(step * (360.f / static_cast<float>(steps)));
280-
push(config.start_deg);
281-
std::vector<Reading> ranked = readings;
282-
std::sort(ranked.begin(), ranked.end(), [](const Reading& a, const Reading& b) { return a.score > b.score; });
283-
int refined = 0;
284-
std::vector<int> seen;
285-
for (const Reading& item : ranked) {
286-
int key = static_cast<int>(std::lround(item.start)) % 360;
287-
if (key < 0) key += 360;
288-
if (std::find(seen.begin(), seen.end(), key) != seen.end()) continue;
289-
seen.push_back(key);
290-
refined += 1;
291-
if (refined > 2) break;
292-
for (int delta = -12; delta <= 12; delta += 3) {
293-
if (delta == 0) continue;
294-
push(item.start + static_cast<float>(delta));
295-
}
292+
best = prefer_reading(best, decoded);
296293
}
297-
std::pair<std::string, float> best{"", -1.f};
298-
for (const Reading& item : readings) best = prefer_reading(best, {item.text, item.score});
299294
return best;
300295
}
301296

‎cpp/src/geometry.cpp‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,59 @@ std::vector<RsDet> decode_detections(const float* pred, int cells, int num_class
255255
return kept;
256256
}
257257

258+
bool estimate_ring_start(const RsImage& src, float r_in, float r_out, float& start_deg, float& gap_deg) {
259+
// Same 96×96 histogram as python rapidstamp.geometry.estimate_ring_start.
260+
RsImage small = resize_bilinear(src, 96, 96);
261+
constexpr int kBins = 72;
262+
int counts[kBins] = {};
263+
int ink_n = 0;
264+
const double cx = (small.width - 1) / 2.0;
265+
const double cy = (small.height - 1) / 2.0;
266+
for (int y = 0; y < small.height; ++y) {
267+
for (int x = 0; x < small.width; ++x) {
268+
double dx = (static_cast<double>(x) - cx) / std::max(cx, 1e-6);
269+
double dy = (static_cast<double>(y) - cy) / std::max(cy, 1e-6);
270+
double radius = std::sqrt(dx * dx + dy * dy);
271+
if (radius < r_in || radius > r_out + 0.02) continue;
272+
const uint8_t* px = small.pixel(x, y);
273+
int red = px[0];
274+
int green = small.channels > 1 ? px[1] : px[0];
275+
int blue = small.channels > 2 ? px[2] : px[0];
276+
bool ink = (red > 120 && red > green + 30 && red > blue + 30) ||
277+
(blue > 100 && blue > red + 20 && blue > green + 10) || (red < 85 && green < 85 && blue < 85);
278+
if (!ink) continue;
279+
double angle = std::atan2(dy, dx) * 180.0 / 3.14159265358979323846;
280+
if (angle < 0) angle += 360.0;
281+
int bin = static_cast<int>(angle / 360.0 * kBins);
282+
if (bin < 0) bin = 0;
283+
if (bin >= kBins) bin = kBins - 1;
284+
counts[bin] += 1;
285+
ink_n += 1;
286+
}
287+
}
288+
gap_deg = 0.f;
289+
if (ink_n < 25) return false;
290+
int best = 0;
291+
int best_end = 0;
292+
int run = 0;
293+
for (int index = 0; index < kBins * 2; ++index) {
294+
if (counts[index % kBins] == 0) {
295+
run += 1;
296+
if (run > best && run <= kBins) {
297+
best = run;
298+
best_end = index;
299+
}
300+
} else {
301+
run = 0;
302+
}
303+
}
304+
gap_deg = static_cast<float>(best * (360.0 / kBins));
305+
if (best < 8) return false;
306+
int start_bin = (best_end + 1) % kBins;
307+
start_deg = static_cast<float>(start_bin * (360.0 / kBins));
308+
return true;
309+
}
310+
258311
bool load_key_values(const std::string& path, std::vector<std::pair<std::string, std::string>>& out) {
259312
std::ifstream input(path);
260313
if (!input) return false;

‎cpp/src/geometry.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ RsImage polar_unwrap(const RsImage& src, int out_w, int out_h, float r_in, float
3838
float span_deg);
3939
RsImage grid_unroll(const RsImage& src, int rows, int cols, int out_h, int out_w);
4040
RsImage hline_band(const RsImage& src, int out_h, int out_w);
41+
// Largest ink gap on the ring. False when the gap is under 40 degrees.
42+
bool estimate_ring_start(const RsImage& src, float r_in, float r_out, float& start_deg, float& gap_deg);
4143

4244
void gray01(const RsImage& src, std::vector<float>& dst);
4345

‎docs/data.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,6 @@ python training/scripts/collect_web_seals.py \
6666

6767
脚本会问 Bing、百度和 360 图片。Google 图片搜索会 302 跳转,这里不抓。这台机器上百度直接拒绝爬虫。中文查询和 “official round stamp on document” 上 Bing 回的是空页;英文的 notary、corporate seal 查询 Bing 有结果。2026-10-16 这一批留下 401 张:360 图片搜索 350 张,Bing 51 张,百度 0 张。来源页的版权仍归原站,这些图只做内部训练和评测。清单里的 `model_text` 是现有模型在原图上的自动结果。这一版平均每张标出十几个框,环文经常塌成同一小撮字,不能直接当评测或训练标签。`spot_check` 只覆盖抽看过的 9 张。
6868

69+
固定划分写在 `training/manifests/web_seals_split.jsonl`,不随新增文件重抽:sha256 前 8 位转成整数后模 10,小于 3 为测试,其余可训练;拼图只进训练。文章页和商品图整类排除,不用作空负样本,因为页面上往往还有章,标成空会教检测器把章压掉。只有审成「无关」的页是空负样本。框是红蓝墨连通域,再对上审阅时给的大致区域;彩色页上没有红蓝墨的框丢掉。当前清单是训练 137 页、测试 52 页、排除 191 页、没有可用框而跳过 21 页。测试集上的文字几乎是空的,整串命中不以这 52 页为准。图片在 `/home/mayong/rapidstamp/real/web`(训练)和 `/home/mayong/rapidstamp/real/web_test`(测试,目录名是 `det/val`,这样评测脚本不用改)。
70+
6971
ReST 的测试图没有转写,脚本不使用 `test_images.zip`。有标注的 5,000 张里大约 10% 按 id 留作验证。检测框是贴回文档页的整枚裁图;标题多边形只说明字在哪,不当作印章外框。

0 commit comments

Comments
 (0)