V0.4.0加入锁定二维码
This commit is contained in:
@@ -1,55 +1,51 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Synthetic visual-path smoke test for the 4-QR optical page and QFTPAIR2.
|
||||
Requires: qrcode, numpy, opencv-python, pyzbar/libzbar.
|
||||
This is intentionally not a substitute for a real display/camera test.
|
||||
"""
|
||||
import base64, json, os, time
|
||||
"""Synthetic QFT2/Base45 single-QR acquisition/ROI smoke test. Not a physical camera test."""
|
||||
import binascii, os, struct, time
|
||||
import cv2
|
||||
import numpy as np
|
||||
import qrcode
|
||||
from pyzbar.pyzbar import decode as zdecode, ZBarSymbol
|
||||
from qrcode.constants import ERROR_CORRECT_L, ERROR_CORRECT_M
|
||||
from qrcode.constants import ERROR_CORRECT_L
|
||||
|
||||
B45="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"
|
||||
def b45(data):
|
||||
out=[]; i=0
|
||||
while i+1 < len(data):
|
||||
v=data[i]*256+data[i+1]; out += [B45[v%45],B45[(v//45)%45],B45[v//2025]]; i+=2
|
||||
if i<len(data):
|
||||
v=data[i]; out += [B45[v%45],B45[v//45]]
|
||||
return ''.join(out)
|
||||
|
||||
def make_qr(text: str, ec, target: int):
|
||||
qr = qrcode.QRCode(version=None, error_correction=ec, box_size=1, border=4)
|
||||
qr.add_data(text); qr.make(fit=True)
|
||||
mat = np.asarray(qr.get_matrix(), dtype=np.uint8)
|
||||
scale = max(1, target // mat.shape[0])
|
||||
return ((1 - np.kron(mat, np.ones((scale, scale), np.uint8))) * 255).astype(np.uint8), qr.version
|
||||
payload=os.urandom(450)
|
||||
body=b"QFT2"+bytes([2,2])+struct.pack(">QIIH",0x1020304050607080,7,1000,len(payload))+payload
|
||||
raw=body+struct.pack(">I",binascii.crc32(body)&0xffffffff)
|
||||
text="QFT2:"+b45(raw)
|
||||
qr=qrcode.QRCode(version=None,error_correction=ERROR_CORRECT_L,box_size=1,border=4)
|
||||
qr.add_data(text); qr.make(fit=True)
|
||||
assert qr.version <= 15, qr.version
|
||||
mat=np.asarray(qr.get_matrix(),dtype=np.uint8)
|
||||
# Make the large single code fill ~70% of the 480px analysis height.
|
||||
scale=max(1,340//mat.shape[0])
|
||||
symbol=((1-np.kron(mat,np.ones((scale,scale),np.uint8)))*255).astype(np.uint8)
|
||||
canvas=np.full((480,640),255,np.uint8)
|
||||
side=symbol.shape[0]; x=max(8,(640-side)//2); y=max(8,(480-side)//2)
|
||||
canvas[y:y+side,x:x+side]=symbol
|
||||
rot=cv2.getRotationMatrix2D((320,240),3.0,1.0)
|
||||
frame=cv2.warpAffine(canvas,rot,(640,480),borderValue=255)
|
||||
frame=cv2.GaussianBlur(frame,(3,3),0.35)
|
||||
expected=text.encode()
|
||||
found=zdecode(frame,symbols=[ZBarSymbol.QRCODE]); assert any(r.data==expected for r in found)
|
||||
r=next(r for r in found if r.data==expected)
|
||||
pad=int(max(r.rect.width,r.rect.height)*0.08)
|
||||
l=max(0,r.rect.left-pad); t=max(0,r.rect.top-pad); rr=min(frame.shape[1],r.rect.left+r.rect.width+pad); bb=min(frame.shape[0],r.rect.top+r.rect.height+pad)
|
||||
roi=frame[t:bb,l:rr]
|
||||
assert any(z.data==expected for z in zdecode(roi,symbols=[ZBarSymbol.QRCODE]))
|
||||
area=roi.size/frame.size; assert area < 0.80
|
||||
|
||||
# Four data QR symbols at the same density used by v0.3 (about 1243 chars/frame).
|
||||
texts = ["QFT1:" + base64.urlsafe_b64encode(os.urandom(928)).decode().rstrip("=") for _ in range(4)]
|
||||
canvas = np.full((960, 960), 255, np.uint8)
|
||||
gutter = 15
|
||||
cell = (960 - gutter * 3) // 2
|
||||
for idx, text in enumerate(texts):
|
||||
img, version = make_qr(text, ERROR_CORRECT_L, cell)
|
||||
row, col = divmod(idx, 2)
|
||||
x = gutter + col * (cell + gutter) + (cell - img.shape[1]) // 2
|
||||
y = gutter + row * (cell + gutter) + (cell - img.shape[0]) // 2
|
||||
canvas[y:y+img.shape[0], x:x+img.shape[1]] = img
|
||||
started = time.perf_counter()
|
||||
found = {r.data.decode() for r in zdecode(canvas, symbols=[ZBarSymbol.QRCODE])}
|
||||
elapsed = time.perf_counter() - started
|
||||
assert found == set(texts), (len(found), len(texts))
|
||||
|
||||
# Pairing QR: downscale + rotate + slight blur to exercise acquisition margin.
|
||||
v2 = {
|
||||
"v":2,"s":"qft_test_session_128bit_XY","n":"DESKTOP-QFT","p":39393,
|
||||
"e":["http://192.168.137.1:39393"],
|
||||
"w":["QFT-DESKTOP-abc123","3xamplePassw0rd"],
|
||||
}
|
||||
raw = json.dumps(v2, separators=(",", ":"), ensure_ascii=False).encode()
|
||||
pair_text = "QFTPAIR2:" + base64.urlsafe_b64encode(raw).decode().rstrip("=")
|
||||
pair, pair_ver = make_qr(pair_text, ERROR_CORRECT_M, 520)
|
||||
pair = cv2.resize(pair, (260, 260), interpolation=cv2.INTER_NEAREST)
|
||||
rot = cv2.getRotationMatrix2D((130, 130), 7.0, 1.0)
|
||||
pair = cv2.warpAffine(pair, rot, (300, 300), borderValue=255)
|
||||
pair = cv2.GaussianBlur(pair, (3, 3), 0.5)
|
||||
started2 = time.perf_counter()
|
||||
pair_found = [r.data.decode() for r in zdecode(pair, symbols=[ZBarSymbol.QRCODE])]
|
||||
elapsed2 = time.perf_counter() - started2
|
||||
assert pair_text in pair_found
|
||||
print(f"PASS synthetic QR decode: 4/4 optical QR v{version} decoded from 960px page in {elapsed*1000:.1f} ms; "
|
||||
f"QFTPAIR2 QR-M v{pair_ver} decoded after 7deg rotate+blur in {elapsed2*1000:.1f} ms")
|
||||
def avg_ms(img,loops=40):
|
||||
started=time.perf_counter()
|
||||
for _ in range(loops): zdecode(img,symbols=[ZBarSymbol.QRCODE])
|
||||
return (time.perf_counter()-started)*1000/loops
|
||||
full_ms=avg_ms(frame); roi_ms=avg_ms(roi)
|
||||
assert roi_ms < full_ms*1.15
|
||||
print(f"PASS synthetic QFT2 lock: QR-L v{qr.version}, {len(text)} chars; full={full_ms:.1f}ms, lockedROI={roi_ms:.1f}ms, ROI area={area*100:.1f}%")
|
||||
|
||||
Reference in New Issue
Block a user