WIP: benchmark mature trackers and bounded occlusion recovery before host integration

这个提交包含在:
mac 于 2026-11-06 19:18:22 +08:00 提交
1 父节点 1f430a164a
当前提交 7aece444b0
7 个文件已更改
+204

没有与搜索条件相匹配的文件

@@ -0,0 +1,51 @@
#pragma once
#include <algorithm>
#include <cmath>
namespace dy {
struct ObjectBox {float cx=.5f,cy=.5f,width=.1f,height=.1f,confidence=0;};
struct ObjectStep {ObjectBox box;bool valid=false,predicted=false,recovered=false;int gap=0;};
// Bounded prediction in analysis-frame order (also valid for reverse passes).
// An unconfirmed tail is never an observed trajectory. Host commit trims it.
class ObjectMotion {
ObjectBox trusted_{},candidate_{};
float vx_=0,vy_=0;
int missing_=0,limit_=20,candidateCount_=0;
bool active_=false;
static bool finite(ObjectBox b){return std::isfinite(b.cx)&&std::isfinite(b.cy)&&std::isfinite(b.width)&&std::isfinite(b.height)&&std::isfinite(b.confidence)&&b.width>.003f&&b.height>.003f&&b.width<=1&&b.height<=1&&b.cx>=0&&b.cx<=1&&b.cy>=0&&b.cy<=1;}
public:
void reset(ObjectBox b,double fps){trusted_=b;vx_=vy_=0;missing_=candidateCount_=0;limit_=std::clamp(int(std::ceil(std::clamp(fps,1.,240.)*.8)),2,192);active_=finite(b);}
int missing()const{return missing_;}
bool active()const{return active_;}
ObjectBox prediction(int ahead=1)const{auto b=trusted_;b.cx+=vx_*(missing_+ahead);b.cy+=vy_*(missing_+ahead);return b;}
ObjectStep advance(ObjectBox measurement,bool observed){
if(!active_)return {};
auto expected=prediction();
float size=std::max(trusted_.width,trusted_.height);
float delta=std::hypot(measurement.cx-expected.cx,measurement.cy-expected.cy);
float scaleX=measurement.width/trusted_.width,scaleY=measurement.height/trusted_.height;
bool good=observed&&finite(measurement)&&measurement.confidence>=(missing_?.70f:.55f)&&
delta<size*(missing_?.65f:.45f)&&scaleX>.65f&&scaleX<1.55f&&scaleY>.65f&&scaleY<1.55f;
if(good&&missing_){
if(candidateCount_&&std::hypot(measurement.cx-candidate_.cx,measurement.cy-candidate_.cy)<size*.25f)++candidateCount_;
else candidateCount_=1;
candidate_=measurement;good=candidateCount_>=2;
}else if(!good)candidateCount_=0;
if(good){
int gap=missing_;float dt=float(gap+1);
float nx=(measurement.cx-trusted_.cx)/dt,ny=(measurement.cy-trusted_.cy)/dt;
float maximum=size*.12f;float length=std::hypot(nx,ny);if(length>maximum){nx*=maximum/length;ny*=maximum/length;}
vx_=gap?nx:.65f*vx_+.35f*nx;vy_=gap?ny:.65f*vy_+.35f*ny;
trusted_=measurement;missing_=candidateCount_=0;return {measurement,true,false,gap>0,gap};
}
++missing_;
if(missing_>limit_||!finite(expected)){active_=false;return {trusted_,false,false,false,missing_};}
// Preview grows conservatively during a gap; committed gaps are later
// reconstructed from both confirmed endpoints, not these guesses.
expected.confidence=.2f*(1.f-float(missing_)/float(limit_+1));
float margin=1.f+.2f*float(missing_)/float(limit_);expected.width*=margin;expected.height*=margin;
return {expected,true,true,false,missing_};
}
};
}
@@ -0,0 +1,72 @@
#pragma once
#import <Vision/Vision.h>
#import <CoreVideo/CoreVideo.h>
#include "DYObjectMotion.hpp"
#include "DYTracker.hpp"
namespace dy {
// Mosaic/object tracking uses small RGB frames and never trains on a lost
// observation. A fresh sequence probes from the last trusted image during gaps.
// All methods are called on the serialized host analysis worker, never UI refresh.
class ObjectTracker {
VNSequenceRequestHandler *sequence_=nil;
VNTrackObjectRequest *request_=nil;
CVPixelBufferRef current_=nullptr,trusted_=nullptr;
ObjectBox trustedBox_{};
ObjectMotion motion_;
TrackerROI seed_{};
int width_=0,height_=0;
bool started_=false;
static CGRect rect(ObjectBox b){return CGRectMake(b.cx-b.width*.5f,1-b.cy-b.height*.5f,b.width,b.height);}
void newSequence(ObjectBox box){
[request_ release];[sequence_ release];sequence_=[VNSequenceRequestHandler new];
request_=[[VNTrackObjectRequest alloc]initWithDetectedObjectObservation:[VNDetectedObjectObservation observationWithBoundingBox:rect(box)]];
request_.trackingLevel=VNRequestTrackingLevelAccurate;
}
bool perform(CVPixelBufferRef image,ObjectBox& box){
NSError *error=nil;BOOL ok=[sequence_ performRequests:@[request_] onCVPixelBuffer:image orientation:kCGImagePropertyOrientationUp error:&error];
VNDetectedObjectObservation *observation=request_.results.firstObject;
if(!ok||!observation)return false;
CGRect b=observation.boundingBox;box={float(CGRectGetMidX(b)),float(1-CGRectGetMidY(b)),float(b.size.width),float(b.size.height),observation.confidence};
request_.inputObservation=observation;return true;
}
bool copyInput(const uint8_t *rgba,int w,int h,bool flip){
if(!rgba||w<16||h<16||w>640||h>640)return false;
if(w!=width_||h!=height_)return false;
CVPixelBufferLockBaseAddress(current_,0);auto *out=(uint8_t*)CVPixelBufferGetBaseAddress(current_);size_t stride=CVPixelBufferGetBytesPerRow(current_);
for(int y=0;y<h;++y){const auto *row=rgba+size_t(flip?h-1-y:y)*w*4;auto *dst=out+y*stride;for(int x=0;x<w;++x){dst[x*4]=row[x*4+2];dst[x*4+1]=row[x*4+1];dst[x*4+2]=row[x*4];dst[x*4+3]=255;}}
CVPixelBufferUnlockBaseAddress(current_,0);return true;
}
public:
ObjectTracker()=default;
ObjectTracker(const ObjectTracker&)=delete;
ObjectTracker& operator=(const ObjectTracker&)=delete;
~ObjectTracker(){stop();}
void stop(){[request_ release];request_=nil;[sequence_ release];sequence_=nil;if(current_)CVPixelBufferRelease(current_);if(trusted_)CVPixelBufferRelease(trusted_);current_=trusted_=nullptr;width_=height_=0;started_=false;}
bool reset(const uint8_t *rgba,int w,int h,bool flip,TrackerROI roi,double fps){
stop();seed_=roi;width_=w;height_=h;
if(w<16||h<16||w>640||h>640)return false;
for(auto p:{&current_,&trusted_})if(CVPixelBufferCreate(kCFAllocatorDefault,w,h,kCVPixelFormatType_32BGRA,(CFDictionaryRef)@{(id)kCVPixelBufferIOSurfacePropertiesKey:@{}},p)!=kCVReturnSuccess){stop();return false;}
trustedBox_={roi.cx,roi.cy,roi.width,roi.height,1};motion_.reset(trustedBox_,fps);if(!motion_.active()||!copyInput(rgba,w,h,flip)){stop();return false;}
newSequence(trustedBox_);ObjectBox first;if(!perform(current_,first)){stop();return false;}
std::swap(current_,trusted_);started_=true;return true;
}
ObjectStep track(const uint8_t *rgba,int w,int h,bool flip){
if(!started_||!copyInput(rgba,w,h,flip))return {};
if(motion_.missing()){
newSequence(trustedBox_);ObjectBox ignored;if(!perform(trusted_,ignored))return {};
}
ObjectBox observed{};bool found=perform(current_,observed);
auto step=motion_.advance(observed,found);
if(step.valid&&!step.predicted){trustedBox_=observed;std::swap(current_,trusted_);}
return step;
}
int pendingFrames()const{return motion_.missing();}
TrackResult result(ObjectStep step)const{
TrackResult r;r.cx=step.box.cx;r.cy=step.box.cy;r.confidence=step.box.confidence;r.valid=step.valid;
// Keep a mosaic's authored aspect and avoid narrowing it across a turn.
r.scale=r.scaleX=r.scaleY=std::max(step.box.width/seed_.width,step.box.height/seed_.height);
r.rotation=0;r.failure=step.valid?TrackFailure::None:TrackFailure::LowConfidence;return r;
}
};
}