Files
nanocode/web-opencv/miniappB/gen_samples.py
T
Ubuntu 32b43f85bd a
2026-07-03 16:58:28 +08:00

68 lines
2.7 KiB
Python

"""Generate test sample images for PaperRecognizer."""
from PIL import Image, ImageDraw
import struct, subprocess, json
def make_strip(w, h, b_rect, s_rect, color, blocks, colors):
img = Image.new('RGB', (w, h), (235, 235, 235))
draw = ImageDraw.Draw(img)
draw.rectangle(b_rect, fill=color)
draw.rectangle(s_rect, fill=color)
for r, c in zip(blocks, colors):
draw.rectangle(r, fill=c)
return img
# Warm colors only (no blue/green to avoid mask bleeding)
WARM = [(200,130,90),(215,160,110),(185,120,80),(225,170,120),
(195,140,100),(210,150,105),(180,125,95),(220,165,115),
(190,135,100),(205,145,85),(230,175,125),(195,130,90),
(215,155,130),(200,145,115)]
BLUE = (0, 50, 200)
GREEN = (26, 136, 68)
W, H = 800, 300
CY = H // 2
def add_blocks(rect_l, rect_r, slices, first_pos, step_ratio, n_blocks, bw):
"""Calculate block positions between rect_l and rect_r."""
dx = rect_r[0] - rect_l[2]
step = dx / slices
fo = int(step * first_pos)
so = int(step * step_ratio)
blocks = []
for i in range(n_blocks):
x1 = rect_l[2] + fo + so * i
x2 = x1 + bw
if x2 >= rect_r[0]: break
blocks.append((x1, CY - 12, x2, CY + 12))
return blocks
# === Sample 1: 14S — big locator 50x25 (aspect 2:1) ===
big = (80, CY-12, 129, CY+12) # 50x25
# For area ratio ~3.33 with 50x25 big: small area = 50*25/3.33 = 375
# Small 25x15 area = 375 → ratio = 1250/375 = 3.33
small = (550, CY-7, 574, CY+7) # 25x15
big_w = big[2]-big[0]; big_h = big[3]-big[1]
small_w = small[2]-small[0]; small_h = small[3]-small[1]
print(f"S1 big={big_w}x{big_h} small={small_w}x{small_h} area_ratio={(big_w*big_h)/(small_w*small_h):.2f}")
blocks1 = add_blocks(big, small, 50, 2.25, 3.5, 14, 18)
img1 = make_strip(W, H, big, small, BLUE, blocks1, WARM[:len(blocks1)])
img1.save('samples/sample1.jpg', quality=95)
print(f" blocks={len(blocks1)}")
# === Sample 2: 11L — big locator 60x20 (aspect 3:1) ===
# Area ratio ~5.0: small area = 60*20/5.0 = 240
# Small 20x12 area = 240 → ratio = 1200/240 = 5.0
big2 = (620, CY-10, 679, CY+10) # 60x20
small2 = (330, CY-6, 349, CY+6) # 20x12
big_w2 = big2[2]-big2[0]; big_h2 = big2[3]-big2[1]
small_w2 = small2[2]-small2[0]; small_h2 = small2[3]-small2[1]
print(f"S2 big={big_w2}x{big_h2} small={small_w2}x{small_h2} area_ratio={(big_w2*big_h2)/(small_w2*small_h2):.2f}")
# Check aspect: 60/20=3.0 ✓, BIG_LOCATOR_RATIO_LONG=3.0 within 0.35 tol
# Check small aspect: 20/12=1.667 ✓, SMALL_LOCATOR_RATIO=1.667
blocks2 = add_blocks(small2, big2, 39, 2.25, 3.5, 11, 20)
img2 = make_strip(W, H, big2, small2, GREEN, blocks2, WARM[:len(blocks2)])
img2.save('samples/sample2.jpg', quality=95)
print(f" blocks={len(blocks2)}")