Files
NODEDC_MISSION_CORE/simulation/urban-plan-research/systematic/paint_observations.py
T
DCCONSTRUCTIONS 69dc566665 feat(research): preserve urban geometry pipelines and qualification suites
Preserve offline geometry, marking, replay and evaluation pipelines with source manifests and synthetic tests. Research qualification does not activate these methods as production navigation.
2026-10-29 09:10:00 +03:00

51 lines
3.5 KiB
Python

from pathlib import Path
import numpy as np, cv2, shapely
from shapely.geometry import Polygon
from scipy.ndimage import distance_transform_edt
from layer_common import sample
from fit_scene import world
def collect(im,vehicle,g,marking,cross,road,low,high,tile,road_dist=None,*,span_m=60.0,retain_uncertain=False,valid=None):
# Offline proposal study only: retaining uncertain context never admits an object.
# Defaults preserve the production search and support gates.
if span_m<=0:raise ValueError("span_m must be positive")
paint_candidates=[];cross_candidates=[];name=tile['id'];cell=.1
if road_dist is None:road_dist=distance_transform_edt(~road)*cell
size=im.shape[0];mpp=span_m/size;small=lambda a:cv2.resize(a.astype('float32'),(size,size),interpolation=cv2.INTER_LINEAR)
marking=small(marking);cross=small(cross);vehicles=cv2.dilate((small(vehicle)>.25).astype('uint8'),np.ones((max(3,int(.25/mpp)),)*2,np.uint8))>0
hsv=cv2.cvtColor(im,cv2.COLOR_RGB2HSV);gray=cv2.cvtColor(im,cv2.COLOR_RGB2GRAY);contrast=gray.astype('float32')-cv2.GaussianBlur(gray.astype('float32'),(0,0),.35/mpp)
paint=((hsv[:,:,1]<85)&(gray>115)&(contrast>9))|((hsv[:,:,0]>12)&(hsv[:,:,0]<36)&(hsv[:,:,1]>55)&(gray>100)&(contrast>5))
yy,xx=np.mgrid[:1200,:1200];points=world(np.c_[xx.ravel(),yy.ravel()],g)
road_patch=cv2.resize(sample(road.astype('uint8'),points,low,high).reshape(1200,1200),(size,size),interpolation=cv2.INTER_NEAREST)>0
strong=((hsv[:,:,1]<70)&(gray>130)&(contrast>12))|((hsv[:,:,0]>12)&(hsv[:,:,0]<36)&(hsv[:,:,1]>55)&(gray>100)&(contrast>5))
channels=[('semantic-paint-v1',strong&((marking>.3)|(cross>.3))&~vehicles),
('appearance-paint-v1',paint&(road_patch|retain_uncertain)&~vehicles)]
physical=np.ones((size,size),bool) if valid is None else small(valid)>.65
for contract,candidate in channels:
candidate=candidate&physical
candidate=cv2.morphologyEx(candidate.astype('uint8'),cv2.MORPH_CLOSE,np.ones((max(3,int(.04/mpp)),)*2,np.uint8))
candidate[~physical]=0
contours,hier=cv2.findContours(candidate,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
if hier is None:continue
def xy_native(p):return world((np.asarray(p)+.5)*1200/size-.5,g)
for i,c in enumerate(contours):
if hier[0,i,3]>=0 or cv2.contourArea(c)*mpp*mpp<.06:continue
rect=cv2.minAreaRect(c);short,long=sorted(rect[1]);short*=mpp;long*=mpp
if short<.045 or long<.35 or short>5:continue
ring=cv2.approxPolyDP(c,.035/mpp,True)[:,0];holes=[];j=hier[0,i,2]
while j>=0:
if cv2.contourArea(contours[j])*mpp*mpp>.03:holes.append(xy_native(cv2.approxPolyDP(contours[j],.035/mpp,True)[:,0]))
j=hier[0,j,0]
poly=shapely.make_valid(Polygon(xy_native(ring),holes))
if poly.geom_type!='Polygon':continue
coords=np.array(poly.exterior.coords);supported=np.mean(sample(road_dist,coords,low,high)<.75)
if supported<.5 and not retain_uncertain:continue
rxy=np.rint(rect[0]).astype(int);rxy=np.clip(rxy,0,size-1);is_cross=cross[rxy[1],rxy[0]]>.3
if is_cross and .18<short<1.2 and 1.0<long<12 and long/short>2 and poly.area/(short*long)>.55:
box=Polygon(xy_native(cv2.boxPoints(rect)));cross_candidates.append((box,dict(source_tile=name,subtype='crosswalk',method='parametric-rectangle')))
else:
x,y,bw,bh=cv2.boundingRect(c);patch=np.zeros((bh,bw),np.uint8);cv2.drawContours(patch,[c-np.array([[[x,y]]])],-1,1,-1)
values=marking[y:y+bh,x:x+bw][patch>0]
paint_candidates.append((poly,dict(source_tile=name,proposal_contract=contract,semantic_support=float(np.mean(values)),semantic_peak=float(np.quantile(values,.9)))))
return paint_candidates,cross_candidates