Files
NODEDC_MISSION_CORE/simulation/urban-plan-research/systematic/observation_tiles.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

79 lines
3.8 KiB
Python

"""Bounded independent native-tile work; no map or cross-tile state."""
from pathlib import Path
import hashlib,json,time
import numpy as np,cv2
from shapely.geometry import mapping
from scipy.ndimage import distance_transform_edt
from layer_common import sample
from fit_scene import world
from search_views import SearchViews
from primary_line_observations import extract
from paint_observations import collect as paint_observations
from catalog_observations import collect as catalog_observations
from compact_observations import collect as compact_observations
from crossing_discovery import primitives
def initialize():
global views,labels,low,high,road,inside,outside
cv2.setNumThreads(2)
cv2.setRNGSeed(2026)
with np.load('/evidence_plan/fields.npz')as z:
labels=z['labels'];low,high=z['bounds']
road=labels==1
inside=distance_transform_edt(road)*.1
outside=distance_transform_edt(~road)*.1
tiles=[t for t in json.loads(Path('/survey/tile-index.json').read_text())['tiles']if t['status']=='surveyed']
views=SearchViews(tiles,labels,low,high,None)
def collect_tile(tile):
name=tile['id'];t0=time.monotonic()
g=views.geometry(name);rgb,vehicle=views.inputs(name);size=rgb.shape[0];mpp=60/size
def xy(uv):return world((np.asarray(uv)+.5)*1200/size-.5,g)
yy,xx=np.mgrid[:1200,:1200];points=world(np.c_[xx.ravel(),yy.ravel()],g)
allowed1200=(sample(inside,points,low,high)>.25).reshape(1200,1200)&(vehicle<.2)&(g['alpha']>.65)
allowed=cv2.resize(allowed1200.astype('uint8'),(size,size),interpolation=cv2.INTER_NEAREST)>0
stages={};tic=time.monotonic()
obs,rec=extract(rgb,allowed,xy,name,mpp)
stages['linear']=time.monotonic()-tic;tic=time.monotonic()
path=Path('/evidence_plan/evidence')/(name+'.npz')
source=hashlib.sha256(path.read_bytes()).hexdigest()
with np.load(path)as z:marking=z['road marking'].astype('float32');cross=z['crosswalk'].astype('float32')
pc,cc=paint_observations(rgb,vehicle,g,marking,cross,road,low,high,tile,road_dist=outside)
paint=[dict(geometry=mapping(p),properties=props)for p,props in pc]
crossbars=[dict(geometry=mapping(p),properties=props)for p,props in cc]
stages['appearance']=time.monotonic()-tic;tic=time.monotonic()
glyphs=catalog_observations(rgb,vehicle,g,labels,low,high,name)
stages['catalog']=time.monotonic()-tic;tic=time.monotonic()
compact=compact_observations(rgb,vehicle,g,labels,low,high,name)
stages['compact']=time.monotonic()-tic;tic=time.monotonic()
seeds=primitives(rgb,cross,vehicle,g,labels,low,high)
stages['crossing']=time.monotonic()-tic
report=dict(tile=name,seconds=time.monotonic()-t0,detectors=stages,linear=len(obs),paint=len(pc),crossbars=len(cc))
return dict(lines=obs,paint=paint,crossbars=crossbars,glyphs=glyphs,compact=compact,seeds=seeds,report=report,source=source)
def ordered_tiles(tiles,workers=4):
"""At most eight submitted tiles; collect in the original source order."""
if type(workers)is not int or not 1<=workers<=4:
raise ValueError('Observation workers must be an integer from one to four')
if workers==1:
initialize()
for tile in tiles:yield collect_tile(tile)
return
from concurrent.futures import ProcessPoolExecutor
from multiprocessing import get_context
from collections import deque
pending=deque();source=iter(tiles)
with ProcessPoolExecutor(max_workers=workers,mp_context=get_context('spawn'),initializer=initialize)as pool:
for _ in range(workers*2):
tile=next(source,None)
if tile is None:break
pending.append(pool.submit(collect_tile,tile))
while pending:
result=pending.popleft().result()
tile=next(source,None)
if tile is not None:pending.append(pool.submit(collect_tile,tile))
yield result