Preserve offline geometry, marking, replay and evaluation pipelines with source manifests and synthetic tests. Research qualification does not activate these methods as production navigation.
56 lines
3.6 KiB
Python
56 lines
3.6 KiB
Python
"""Template hypotheses in metric object coordinates; never site coordinates."""
|
|
import numpy as np,cv2,shapely
|
|
from shapely.geometry import Polygon,LineString
|
|
from shapely.ops import unary_union
|
|
from shapely.affinity import rotate,translate
|
|
from scipy.optimize import minimize
|
|
|
|
def template(kind,L,W,stroke):
|
|
# x is longitudinal; all dimensions fitted to image evidence, not prescribed law.
|
|
stem=stroke;hl=min(L*.3,W*1.8);root=L-hl
|
|
straight=Polygon([(0,-stem/2),(root,-stem/2),(root,-W/2),(L,0),(root,W/2),(root,stem/2),(0,stem/2)])
|
|
if kind=='straight_arrow':return straight
|
|
if kind=='letter_A':
|
|
outer=LineString([(0,-W/2),(L,0),(0,W/2)]).buffer(stroke/2,cap_style=2,join_style=2)
|
|
bar=LineString([(L*.3,-W*.35),(L*.3,W*.35)]).buffer(stroke/2,cap_style=2)
|
|
return unary_union([outer,bar])
|
|
sign=-1 if 'left' in kind else 1
|
|
# Branch shares a stem; constrained right-angle turn and a triangular head.
|
|
branch=LineString([(0,0),(L*.52,0),(L*.52,sign*W*.62)]).buffer(stroke/2,cap_style=2,join_style=2)
|
|
head=Polygon([(L*.52-W*.28,sign*W*.52),(L*.52+W*.28,sign*W*.52),(L*.52,sign*W)])
|
|
return unary_union([branch,head,straight] if kind.startswith('straight_') else [branch,head])
|
|
|
|
def fit_symbol(p):
|
|
if p.geom_type!='Polygon' or p.area<.25:return None
|
|
points=np.array(p.exterior.coords)[:,:2];rect=cv2.minAreaRect(points.astype('float32'));corners=cv2.boxPoints(rect);edges=np.roll(corners,-1,axis=0)-corners;k=np.argmax(np.linalg.norm(edges,axis=1));u=edges[k]/np.linalg.norm(edges[k]);L=np.linalg.norm(edges[k]);W=min(rect[1]);origin=np.array(rect[0]);ang=np.arctan2(u[1],u[0]);fill=p.area/(L*W)
|
|
if not (1.2<L<9 and .35<W<3 and L/W>1.2 and .12<fill<.72):return None
|
|
# A fixed-resolution local evidence patch avoids manipulating full scene pixels.
|
|
cell=.04;lo=np.array(p.bounds[:2])-.6;hi=np.array(p.bounds[2:])+.6;shape=np.ceil((hi-lo)/cell).astype(int)[::-1]
|
|
if max(shape)>350:return None
|
|
target=np.zeros(shape,np.uint8)
|
|
def draw(poly):
|
|
out=np.zeros(shape,np.uint8)
|
|
for part in ([poly] if poly.geom_type=='Polygon' else getattr(poly,'geoms',[])):
|
|
if part.geom_type!='Polygon':continue
|
|
cv2.fillPoly(out,[np.rint((np.array(part.exterior.coords)[:,:2]-lo)/cell).astype('int32')],1)
|
|
for hole in part.interiors:cv2.fillPoly(out,[np.rint((np.array(hole.coords)[:,:2]-lo)/cell).astype('int32')],0)
|
|
return out
|
|
target=draw(p);edge=cv2.morphologyEx(target,cv2.MORPH_GRADIENT,np.ones((3,3),np.uint8));distance=cv2.distanceTransform(1-edge,cv2.DIST_L2,3)*cell
|
|
variants=['straight_arrow','letter_A','left_arrow','right_arrow','straight_left_arrow','straight_right_arrow'];best=None
|
|
for kind in variants:
|
|
for flip in [0,np.pi]:
|
|
a=ang+flip;uu=np.array([np.cos(a),np.sin(a)]);base=origin-uu*L/2
|
|
x0=np.array([base[0],base[1],a,L,W,.13]);bounds=[(base[0]-.4,base[0]+.4),(base[1]-.4,base[1]+.4),(a-.16,a+.16),(L*.85,L*1.15),(W*.75,W*1.25),(.07,.22)]
|
|
def polygon(x):
|
|
m=template(kind,x[3],x[4],x[5]);m=rotate(m,x[2],origin=(0,0),use_radians=True);return translate(m,x[0],x[1])
|
|
def loss(x):
|
|
m=draw(polygon(x));inter=(m&target).sum();union=(m|target).sum();return 1-inter/max(union,1)+.05*abs(x[3]/L-1)
|
|
score=loss(x0)
|
|
if kind!='letter_A' and (W<.60 or W/x0[5]<3.5):continue
|
|
if kind=='letter_A' and not p.interiors:continue
|
|
if score>.8:continue
|
|
fit=minimize(loss,x0,method='Powell',bounds=bounds,options=dict(maxiter=35,xtol=.003,ftol=.003));m=polygon(fit.x);r=draw(m);iou=float((r&target).sum()/max((r|target).sum(),1));recall=float((r&target).sum()/max(target.sum(),1))
|
|
if best is None or iou>best[1]:best=(m,iou,kind,recall,fit.x.tolist())
|
|
if best and best[1]>=.68 and best[3]>=.77:return best
|
|
return None
|