feat: WIP unified workspace shared mask objects and OpenCV tracker core

这个提交包含在:
mac 于 2026-11-05 03:16:17 +08:00 提交
1 父节点 78e53b3889
当前提交 ca352248c1
36 个文件已更改
+5298 -164

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

+106
查看文件
@@ -0,0 +1,106 @@
#!/usr/bin/env python3
"""Build the pinned public OpenCV tracking runtime outside the source checkout."""
from pathlib import Path
import argparse, contextlib, fcntl, hashlib, json, os, shutil, subprocess, tarfile, urllib.request
ROOT = Path(__file__).resolve().parent.parent
CACHE = Path('/Users/mac/Library/Caches/CodexBuilds/com.dayu.color/tracking-opencv')
LOCK = json.loads((ROOT/'ThirdParty/OpenCV/source-lock.json').read_text())
def sha(path):
h=hashlib.sha256()
with path.open('rb') as f:
while chunk:=f.read(1024*1024):h.update(chunk)
return h.hexdigest()
def run(args, log):
with log.open('a') as out:
subprocess.run([str(x) for x in args],check=True,stdout=out,stderr=subprocess.STDOUT)
def main():
ap=argparse.ArgumentParser();ap.add_argument('--resources',type=Path);ap.add_argument('--jobs',type=int,default=min(4,os.cpu_count() or 2));args=ap.parse_args()
CACHE.mkdir(parents=True,exist_ok=True)
with (CACHE/'build.lock').open('w') as exclusive:
fcntl.flock(exclusive,fcntl.LOCK_EX)
download=CACHE/'opencv-4.5.1.tar.gz'
if not download.exists():
prior=CACHE.parent/'opencv-tracking/opencv-4.5.1.tar.gz'
if prior.exists() and sha(prior)==LOCK['archiveSHA256']:shutil.move(str(prior),download)
else:
partial=download.with_suffix('.download')
with urllib.request.urlopen(LOCK['archiveURL'],timeout=90) as response, partial.open('wb') as out:
shutil.copyfileobj(response,out)
if partial.stat().st_size!=LOCK['archiveBytes'] or sha(partial)!=LOCK['archiveSHA256']:
raise RuntimeError('OpenCV download identity mismatch; unverified file retained')
partial.replace(download)
if download.stat().st_size!=LOCK['archiveBytes'] or sha(download)!=LOCK['archiveSHA256']:
raise RuntimeError('OpenCV cached source identity mismatch')
source=CACHE/('opencv-'+LOCK['commit'])
if not source.exists():
with tarfile.open(download,'r:gz') as archive:
for member in archive.getmembers():
target=(CACHE/member.name).resolve()
if not target.is_relative_to(CACHE.resolve()) or member.issym() or member.islnk():
raise RuntimeError('Unexpected source archive path/link')
archive.extractall(CACHE)
build=CACHE/'build';prefix=CACHE/'install';log=CACHE/'build.log'
sdk=Path(subprocess.check_output(['xcrun','--show-sdk-path'],text=True).strip())
config=[
'-DCMAKE_POLICY_VERSION_MINIMUM=3.5','-DCMAKE_BUILD_TYPE=Release',
'-DCMAKE_OSX_ARCHITECTURES=arm64','-DCMAKE_OSX_DEPLOYMENT_TARGET=14.6',
'-DBUILD_LIST='+','.join(LOCK['modules']),'-DBUILD_SHARED_LIBS=OFF',
'-DBUILD_TESTS=OFF','-DBUILD_PERF_TESTS=OFF','-DBUILD_EXAMPLES=OFF',
'-DBUILD_opencv_apps=OFF','-DBUILD_DOCS=OFF','-DBUILD_JAVA=OFF',
'-DBUILD_opencv_python2=OFF','-DBUILD_opencv_python3=OFF',
'-DBUILD_opencv_js=OFF','-DBUILD_opencv_gapi=OFF','-DBUILD_opencv_dnn=OFF',
'-DWITH_IPP=OFF','-DWITH_ITT=OFF','-DWITH_OPENCL=OFF','-DWITH_TBB=OFF','-DWITH_CAROTENE=OFF',
'-DWITH_OPENMP=OFF','-DWITH_LAPACK=OFF','-DWITH_EIGEN=OFF',
'-DWITH_VTK=OFF','-DWITH_ADE=OFF','-DWITH_FFMPEG=OFF','-DWITH_GSTREAMER=OFF',
'-DWITH_AVFOUNDATION=OFF','-DWITH_QUIRC=OFF','-DWITH_PROTOBUF=OFF',
'-DWITH_JPEG=OFF','-DWITH_PNG=OFF','-DWITH_TIFF=OFF','-DWITH_WEBP=OFF',
'-DWITH_OPENEXR=OFF','-DWITH_JASPER=OFF','-DWITH_OPENJPEG=OFF',
'-DBUILD_ZLIB=OFF','-DZLIB_INCLUDE_DIR='+str(sdk/'usr/include'),
'-DZLIB_LIBRARY='+str(sdk/'usr/lib/libz.tbd'),
'-DZLIB_LIBRARY_RELEASE='+str(sdk/'usr/lib/libz.tbd'),
'-DOPENCV_FORCE_3RDPARTY_BUILD=OFF','-DOPENCV_GENERATE_PKGCONFIG=OFF',
'-DCMAKE_CXX_STANDARD=17','-DCMAKE_CXX_VISIBILITY_PRESET=hidden',
'-DCMAKE_VISIBILITY_INLINES_HIDDEN=ON','-DCMAKE_INSTALL_PREFIX='+str(prefix)]
configuration=hashlib.sha256(json.dumps(config).encode()).hexdigest()
marker=CACHE/'opencv-build.json'
if not marker.exists() or json.loads(marker.read_text()).get('configuration')!=configuration:
run(['cmake','-S',source,'-B',build,*config],log)
run(['cmake','--build',build,'--parallel',args.jobs],log)
run(['cmake','--install',build],log)
marker.write_text(json.dumps({'configuration':configuration,'commit':LOCK['commit'],'archiveSHA256':LOCK['archiveSHA256']},indent=2)+'\n')
include=CACHE/'include';libs=CACHE/'libs';libs.mkdir(exist_ok=True)
if include.is_symlink():include.unlink()
if not include.exists():include.symlink_to(prefix/'include/opencv4',target_is_directory=True)
libraryNames=['calib3d','features2d','flann','video','imgproc','core']
for name in libraryNames:
src=prefix/'lib'/('libopencv_'+name+'.a');dest=libs/src.name
if not src.exists():raise RuntimeError('Required OpenCV library absent: '+str(src))
if dest.is_symlink():dest.unlink()
if not dest.exists():dest.symlink_to(src)
bridge=ROOT/'tools/tracking/DYOpenCVTracker.cpp';obj=libs/'DYOpenCVTracker.o';runtime=libs/'libDayuOpenCVTracker.a'
identity=hashlib.sha256(bridge.read_bytes()+(ROOT/'大鱼调色/Plugin/MaskCore/DYOpenCVTracker.hpp').read_bytes()+marker.read_bytes()).hexdigest()
bridgeMarker=CACHE/'bridge-build.json'
if not runtime.exists() or not bridgeMarker.exists() or json.loads(bridgeMarker.read_text()).get('identity')!=identity:
run(['xcrun','clang++','-std=c++17','-arch','arm64','-mmacosx-version-min=14.6','-O3','-fvisibility=hidden',
'-I'+str(include),'-c',bridge,'-o',obj],log)
run(['xcrun','libtool','-static','-o',runtime,obj],log)
bridgeMarker.write_text(json.dumps({'identity':identity,'runtimeSHA256':sha(runtime)},indent=2)+'\n')
# Absolute arguments contain no spaces in the fixed build-cache path.
response=[str(runtime),*[str(libs/('libopencv_'+name+'.a')) for name in libraryNames],'-lz','-framework','Accelerate']
(CACHE/'link.rsp').write_text('\n'.join(response)+'\n')
report={'version':LOCK['version'],'commit':LOCK['commit'],'archiveSHA256':LOCK['archiveSHA256'],
'header':str(include),'linkResponse':str(CACHE/'link.rsp'),'libraries':[
{'path':x,'bytes':Path(x).stat().st_size,'sha256':sha(Path(x))} for x in response if x.endswith('.a')],
'sourceApplicationDependency':False}
(CACHE/'build-report.json').write_text(json.dumps(report,indent=2)+'\n')
if args.resources:
args.resources.mkdir(parents=True,exist_ok=True)
for name in ['LICENSE','COPYRIGHT','SOURCE-NOTICES.txt','source-lock.json']:
shutil.copy2(ROOT/'ThirdParty/OpenCV'/name,args.resources/name)
print('OpenCV '+LOCK['version']+' public source runtime ready: '+str(CACHE/'link.rsp'))
if __name__=='__main__':main()
+137
查看文件
@@ -0,0 +1,137 @@
// Independent reconstruction using the public OpenCV API. No commercial source.
#include "../../大鱼调色/Plugin/MaskCore/DYOpenCVTracker.hpp"
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/video/tracking.hpp>
#include <opencv2/calib3d.hpp>
namespace dy {
struct OpenCVTracker::Impl {
cv::Mat previous;
std::vector<cv::Point2f> points, polygon;
std::vector<FlowPoint> mask;
TrackerROI seed{};
TrackResult value{};
cv::Point2f center{};
int frameIndex=0, width=0, height=0;
bool running=false;
static cv::Mat gray8(const GrayFrame& f) {
if(!f.valid())return {};
cv::Mat out(f.height,f.width,CV_8UC1);
for(size_t i=0;i<f.pixels.size();++i) {
float x=f.pixels[i];
if(!std::isfinite(x))return {};
out.data[i]=cv::saturate_cast<uchar>(x*255.f);
}
return out;
}
void detect() {
std::vector<cv::Point> vertices,hull;
for(auto p:polygon)vertices.emplace_back(int(p.x),int(p.y));
points.clear();
if(vertices.size()<3)return;
cv::convexHull(vertices,hull,false,true);
cv::Mat area=cv::Mat::zeros(height,width,CV_8UC1);
cv::fillPoly(area,std::vector<std::vector<cv::Point>>{hull},cv::Scalar(255),cv::LINE_AA);
cv::goodFeaturesToTrack(previous,points,300,.03,10.,area,3,false,.04);
if(!points.empty())cv::cornerSubPix(previous,points,{10,10},{-1,-1},
{cv::TermCriteria::COUNT|cv::TermCriteria::EPS,20,.03});
}
TrackResult fail(TrackFailure why) {
running=false;value.valid=false;value.confidence=0;value.failure=why;return value;
}
};
OpenCVTracker::OpenCVTracker():impl_(std::make_shared<Impl>()){}
OpenCVTracker::~OpenCVTracker()=default;
void OpenCVTracker::setMask(std::vector<FlowPoint> p){impl_->mask=std::move(p);}
const char* OpenCVTracker::version(){return CV_VERSION;}
TrackResult OpenCVTracker::result()const{return impl_->value;}
bool OpenCVTracker::active()const{return impl_->running;}
void OpenCVTracker::stop(){impl_->running=false;impl_->value.valid=false;}
bool OpenCVTracker::reset(const GrayFrame& f,TrackerROI roi) {
auto& t=*impl_;t.running=false;t.frameIndex=1;t.points.clear();t.polygon.clear();
t.value={roi.cx,roi.cy,1,0,0,0,0,false};
if(!f.valid()||!std::isfinite(roi.cx)||!std::isfinite(roi.cy)||
!std::isfinite(roi.width)||!std::isfinite(roi.height)||!std::isfinite(roi.angle)||
roi.cx<0||roi.cx>1||roi.cy<0||roi.cy>1||roi.width<=0||roi.height<=0||roi.width>8||roi.height>8) {
t.fail(TrackFailure::InvalidFrame);return false;
}
try {
t.width=f.width;t.height=f.height;t.seed=roi;t.center={roi.cx*f.width,roi.cy*f.height};
t.previous=Impl::gray8(f);if(t.previous.empty()){t.fail(TrackFailure::InvalidFrame);return false;}
auto outline=t.mask;
if(outline.empty())outline={{-.5f,-.5f},{.5f,-.5f},{.5f,.5f},{-.5f,.5f}};
float c=std::cos(roi.angle),s=std::sin(roi.angle);
for(auto p:outline){float x=p.x*roi.width*f.width,y=p.y*roi.height*f.height;
if(!std::isfinite(x)||!std::isfinite(y)){t.fail(TrackFailure::InvalidFrame);return false;}
t.polygon.emplace_back(t.center.x+c*x-s*y,t.center.y+s*x+c*y);}
t.detect();t.value.features=int(t.points.size());t.value.inliers=t.value.features;
if(t.points.size()<4){t.fail(TrackFailure::InsufficientTexture);return false;}
t.running=true;t.value.valid=true;t.value.confidence=1;return true;
} catch(const cv::Exception&){t.fail(TrackFailure::Backend);return false;}
}
TrackResult OpenCVTracker::track(const GrayFrame& f) {
auto& t=*impl_;
if(!t.running||!f.valid()||f.width!=t.width||f.height!=t.height)return t.fail(TrackFailure::InvalidFrame);
try {
auto next=Impl::gray8(f);if(next.empty())return t.fail(TrackFailure::InvalidFrame);
// The reset detected the seed (index zero). Advancing frames redetect
// on indices 1,2,4… in the corresponding previous image.
if(t.frameIndex<2||t.frameIndex%2==0)t.detect();
t.value.features=int(t.points.size());t.value.matches=0;t.value.inliers=0;t.value.medianError=0;
if(t.points.size()<4)return t.fail(TrackFailure::InsufficientTexture);
std::vector<cv::Point2f> forward,backward;
std::vector<uchar> fs,bs;std::vector<float> fe,be;
cv::TermCriteria termination(cv::TermCriteria::COUNT|cv::TermCriteria::EPS,30,.01);
cv::calcOpticalFlowPyrLK(t.previous,next,t.points,forward,fs,fe,{21,21},3,termination,0,1e-4);
cv::calcOpticalFlowPyrLK(next,t.previous,forward,backward,bs,be,{21,21},3,termination,0,1e-4);
std::vector<cv::Point2f> src,dst,movingSrc,movingDst;
const float motionThreshold=std::max(t.width,t.height)*.01f;
for(size_t i=0;i<t.points.size();++i) {
// Checking both statuses also rejects a failed backward solve.
if(!fs[i]||!bs[i]||cv::norm(backward[i]-t.points[i])>=1.)continue;
src.push_back(t.points[i]);dst.push_back(forward[i]);
if(cv::norm(forward[i]-t.points[i])>=motionThreshold){movingSrc.push_back(t.points[i]);movingDst.push_back(forward[i]);}
}
if(movingSrc.size()>src.size()/2){src.swap(movingSrc);dst.swap(movingDst);}
t.value.matches=int(src.size());
if(src.size()<4)return t.fail(TrackFailure::InsufficientMatches);
for(size_t i=0;i<src.size();++i){src[i]-=t.center;dst[i]-=t.center;}
cv::Mat inliers;
cv::Mat affine=cv::estimateAffine2D(src,dst,inliers,cv::RANSAC,3.,2000,.99,10);
if(affine.empty())return t.fail(TrackFailure::IncoherentMotion);
double a=affine.at<double>(0,0),b=affine.at<double>(0,1),tx=affine.at<double>(0,2);
double c=affine.at<double>(1,0),d=affine.at<double>(1,1),ty=affine.at<double>(1,2);
double det=a*d-b*c,sx=std::hypot(a,c),sy=det/sx,angle=std::atan2(c,a);
if(!cv::checkRange(affine)||!(det>0)||!(sx>0)||!(sy>0))return t.fail(TrackFailure::InvalidTransform);
std::vector<float> errors;int accepted=0;
for(size_t i=0;i<src.size();++i)if(inliers.at<uchar>(int(i))){
++accepted;cv::Point2f q(float(a*src[i].x+b*src[i].y+tx),float(c*src[i].x+d*src[i].y+ty));
errors.push_back(float(cv::norm(q-dst[i])));}
t.value.inliers=accepted;
if(accepted<4)return t.fail(TrackFailure::IncoherentMotion);
std::nth_element(errors.begin(),errors.begin()+errors.size()/2,errors.end());
t.value.medianError=errors[errors.size()/2];
float confidence=std::clamp(float(accepted)/float(src.size())*std::exp(-t.value.medianError/3.f),0.f,1.f);
// Product guards are deliberate: no silent successful empty/degenerate fit.
double totalX=t.value.scaleX*sx,totalY=t.value.scaleY*sy;
auto center=t.center+cv::Point2f(float(tx),float(ty));
if(!std::isfinite(totalX)||!std::isfinite(totalY)||totalX<.1||totalX>10||totalY<.1||totalY>10||
sx<.5||sx>2||sy<.5||sy>2||std::abs(angle)>.6)return t.fail(TrackFailure::InvalidTransform);
if(confidence<.3f)return t.fail(TrackFailure::LowConfidence);
if(center.x<0||center.x>=t.width||center.y<0||center.y>=t.height)return t.fail(TrackFailure::OutsideFrame);
float cosA=float(std::cos(angle)),sinA=float(std::sin(angle));
for(auto& p:t.polygon){auto q=p-t.center;p={center.x+float(sx)*q.x*cosA-float(sy)*q.y*sinA,
center.y+float(sx)*q.x*sinA+float(sy)*q.y*cosA};}
t.center=center;t.value.cx=center.x/t.width;t.value.cy=center.y/t.height;
t.value.scaleX=float(totalX);t.value.scaleY=float(totalY);t.value.scale=std::sqrt(t.value.scaleX*t.value.scaleY);
t.value.rotation+=float(angle);t.value.confidence=confidence;t.value.valid=true;t.value.failure=TrackFailure::None;
t.points.clear();for(size_t i=0;i<dst.size();++i)t.points.push_back(dst[i]+(center-cv::Point2f(float(tx),float(ty))));
t.previous=next;++t.frameIndex;return t.value;
}catch(const cv::Exception&){return t.fail(TrackFailure::Backend);}
}
}
+79
查看文件
@@ -0,0 +1,79 @@
#include "../../大鱼调色/Plugin/MaskCore/DYOpenCVTracker.hpp"
#include <iostream>
#include <string>
#include <chrono>
// Known analytic motion is the oracle. No target app or runtime is used.
static float noise(int x,int y,uint32_t seed=0) {
uint32_t n=uint32_t(x)*73856093u^uint32_t(y)*19349663u^seed;
n^=n>>13;n*=1274126177u;n^=n>>16;return .1f+.8f*float(n&65535)/65535.f;
}
static float texture(float x,float y,uint32_t seed=0) {
float gx=x/3.f,gy=y/3.f;int ix=int(std::floor(gx)),iy=int(std::floor(gy));
float dx=gx-ix,dy=gy-iy;
return (1-dy)*((1-dx)*noise(ix,iy,seed)+dx*noise(ix+1,iy,seed))+
dy*((1-dx)*noise(ix,iy+1,seed)+dx*noise(ix+1,iy+1,seed));
}
static dy::GrayFrame frame(int w,int h,double tx=0,double ty=0,double angle=0,double sx=1,double sy=1,uint32_t seed=0) {
dy::GrayFrame f{w,h,std::vector<float>(size_t(w)*h)};
double cx=w*.5,cy=h*.5,c=std::cos(angle),s=std::sin(angle);
for(int y=0;y<h;y++)for(int x=0;x<w;x++){
double dx=x-cx-tx,dy=y-cy-ty;
f.pixels[y*w+x]=texture(float((c*dx+s*dy)/sx+cx),float((-s*dx+c*dy)/sy+cy),seed);
}return f;
}
int main() {
int failed=0,count=0;std::cout<<"{\"version\":\""<<dy::OpenCVTracker::version()<<"\",\"checks\":[";
auto check=[&](const char*name,bool ok,double error=0){if(count++)std::cout<<",";failed+=!ok;
std::cout<<"{\"name\":\""<<name<<"\",\"pass\":"<<(ok?"true":"false")<<",\"error\":"<<error<<"}";};
check("pinned-public-opencv-version",std::string(dy::OpenCVTracker::version())=="4.5.1");
{
dy::OpenCVTracker t;auto f=frame(640,480);bool started=t.reset(f,{.5,.5,.85,.85,0});
check("300-feature-capacity",started&&t.result().features==300,t.result().features);
auto r=t.track(frame(640,480,2.25,-1.5,.025,1.025,.98));
double error=std::hypot((r.cx-.5)*640-2.25,(r.cy-.5)*480+1.5);
check("translation-rotation-independent-xy-scale",started&&r.valid&&error<.5&&std::abs(r.rotation-.025)<.004&&
std::abs(r.scaleX-1.025)<.005&&std::abs(r.scaleY-.98)<.005,error);
}
{
dy::OpenCVTracker t;auto f=frame(512,384);t.setMask({{.8f,-.5f},{1.8f,-.5f},{1.8f,.5f},{.8f,.5f}});
bool ok=t.reset(f,{.35,.5,.2,.45,0});dy::TrackResult r=t.result();
for(int i=1;i<=12&&ok;++i){r=t.track(frame(512,384,1.25*i,.5*i));ok=r.valid;}
double error=std::hypot((r.cx-.35)*512-15,(r.cy-.5)*384-6);
check("free-path-outside-creation-box-12-frame-subpixel",ok&&error<.7,error);
}
{
dy::OpenCVTracker t;auto f=frame(640,480);bool ok=t.reset(f,{.5,.5,.65,.65,0});
for(int i=0;i<8&&ok;++i)ok=t.track(f).valid;
auto r=t.result();double error=std::hypot((r.cx-.5)*640,(r.cy-.5)*480);
check("periodic-redetection-keeps-static-position",ok&&error<.05,error);
r=t.track(frame(640,480,0,0,0,1,1,0x3344));
check("unrelated-cut-stops-with-last-trusted-position",!r.valid&&!t.active()&&std::abs(r.cx-.5)*640<.05);
dy::GrayFrame flat{640,480,std::vector<float>(640*480,.5f)};
check("flat-mask-rejected",!t.reset(flat,{.5,.5,.7,.7,0}));
}
{
dy::OpenCVTracker t;auto f=frame(640,480);bool ok=t.reset(f,{.5,.5,.9,.9,0});
auto moving=f;
for(int y=0;y<480;++y)for(int x=0;x<640;++x)
moving.pixels[y*640+x]=texture(float(x-(x<460?8:0)),float(y));
auto r=t.track(moving);double error=std::abs((r.cx-.5)*640-8);
check("moving-majority-prefers-subject-over-static-background",ok&&r.valid&&error<.5&&r.matches<r.features*.85,error);
}
{
dy::OpenCVTracker a,b;auto f=frame(640,480);bool ok=a.reset(f,{.3,.4,.3,.3,0})&&b.reset(f,{.7,.6,.3,.3,0});
auto x=a.track(frame(640,480,5,2));auto y=b.track(frame(640,480,-3,-4));
check("independent-tracker-objects-do-not-share-motion",ok&&x.valid&&y.valid&&std::abs((x.cx-.3)*640-5)<.5&&std::abs((y.cx-.7)*640+3)<.5);
auto old=a.result();x=a.track({});check("invalid-frame-preserves-authored-geometry",!x.valid&&x.cx==old.cx&&x.cy==old.cy&&x.rotation==old.rotation);
f.pixels[10]=std::numeric_limits<float>::quiet_NaN();
check("nonfinite-source-rejected",!a.reset(f,{.5,.5,.4,.4,0}));
}
{
auto begin=std::chrono::steady_clock::now();dy::OpenCVTracker t;bool ok=t.reset(frame(3840,2160),{.5,.5,.5,.5,0});
auto r=t.track(frame(3840,2160,8,4));double error=std::hypot((r.cx-.5)*3840-8,(r.cy-.5)*2160-4);
check("4k-complete-source-frame",ok&&r.valid&&error<.5,error);
auto ms=std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now()-begin).count();
std::cerr<<"4K generated images plus tracking: "<<ms<<"ms\n";
}
std::cout<<"],\"pass\":"<<(failed?"false":"true")<<",\"count\":"<<count<<",\"runtimeTargetAppUsed\":false}\n";return failed?1:0;
}