Implement adaptive inspector, mask clock repair, and selectable face mosaic; validation pending

这个提交包含在:
mac 于 2026-11-06 15:56:42 +08:00 提交
1 父节点 f05cf05451
当前提交 3b4c612edf
19 个文件已更改
+245 -103

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

+42 -4
查看文件
@@ -1,3 +1,37 @@
// Privacy needs face boxes only. Keep the more expensive 76-point landmark
// request exclusively for skin protection. Reuse boxes only for identical pixels.
- (NSDictionary*)mosaicFaceSnapshot {@synchronized(_previewLock){return [[_mosaicFaceSnapshot retain]autorelease];}}
- (NSData*)mosaicFaces:(FxImageTile*)source time:(NSDictionary*)time error:(NSError**)error {
NSMutableDictionary *entry=[self device:source.deviceRegistryID error:error];if(!entry)return nil;
id<MTLTexture>texture=[source metalTextureForDevice:entry[@"device"]];if(!texture)return nil;
if(!_faceContext)_faceContext=[[CIContext contextWithMTLDevice:entry[@"device"] options:@{kCIContextCacheIntermediates:@NO}]retain];
CGColorSpaceRef linear=CGColorSpaceCreateWithName(kCGColorSpaceLinearSRGB);
CIImage *input=[CIImage imageWithMTLTexture:texture options:@{kCIImageColorSpace:(id)linear}];CGColorSpaceRelease(linear);if(!input)return nil;
if(source.imageOrigin==kFxImageOrigin_TOP_LEFT)input=[input imageByApplyingTransform:CGAffineTransformMake(1,0,0,-1,0,input.extent.size.height)];
CGFloat scale=MIN(1.,640./MAX(input.extent.size.width,input.extent.size.height));input=[input imageByApplyingTransform:CGAffineTransformMakeScale(scale,scale)];
CGColorSpaceRef srgb=CGColorSpaceCreateWithName(kCGColorSpaceSRGB);CGImageRef cg=[_faceContext createCGImage:input fromRect:input.extent format:kCIFormatRGBA8 colorSpace:srgb];CGColorSpaceRelease(srgb);if(!cg)return nil;
CFDataRef raster=CGDataProviderCopyData(CGImageGetDataProvider(cg));BOOL same=raster&&[_mosaicRaster isEqual:(NSData*)raster];
NSMutableArray *boxes=[NSMutableArray array];
if(same){[boxes addObjectsFromArray:[self mosaicFaceSnapshot][@"boxes"]?:@[]];}
else{
VNDetectFaceRectanglesRequest *request=[VNDetectFaceRectanglesRequest new];request.revision=VNDetectFaceRectanglesRequestRevision3;
VNImageRequestHandler *handler=[[VNImageRequestHandler alloc]initWithCGImage:cg orientation:kCGImagePropertyOrientationUp options:@{}];
BOOL ok=[handler performRequests:@[request] error:error];
if(ok)for(VNFaceObservation *face in request.results){CGRect box=face.boundingBox;if(boxes.count>=DYSharedMaskLimit||face.confidence<.5||box.size.width<.015||box.size.height<.015)continue;
[boxes addObject:@[@(CGRectGetMidX(box)),@(1-CGRectGetMidY(box)),@(box.size.width*1.2),@(box.size.height*1.25)]];
}
[handler release];[request release];
if(!ok){if(raster)CFRelease(raster);CGImageRelease(cg);return nil;}
[boxes sortUsingComparator:^NSComparisonResult(NSArray *a,NSArray *b){NSComparisonResult x=[a[0]compare:b[0]];return x==NSOrderedSame?[a[1]compare:b[1]]:x;}];
std::vector<DYToolMaskEntry> entries;
for(NSArray *box in boxes){DYToolMaskEntry m{};m.section=-102;m.mask.parameters=dyDefaultMaskParameters();auto &p=m.mask.parameters;p.kind=DYMaskRectangle;p.centerX=[box[0]floatValue];p.centerY=[box[1]floatValue];p.width=[box[2]floatValue];p.height=[box[3]floatValue];p.feather=0;entries.push_back(m);}
[_mosaicEntries release];_mosaicEntries=[[NSData alloc]initWithBytes:entries.data() length:entries.size()*sizeof(DYToolMaskEntry)];
[_mosaicRaster release];_mosaicRaster=raster?[(NSData*)raster copy]:nil;
}
if(raster)CFRelease(raster);CGImageRelease(cg);
@synchronized(_previewLock){[_mosaicFaceSnapshot release];_mosaicFaceSnapshot=[@{@"boxes":boxes,@"time":time?:@{}}copy];}
return _mosaicEntries;
}
// Runs on the original frame before grading, then follows the same spatial
// transforms as the picture. Geometry has already been evaluated at render time.
- (FxImageTile*)sourceByApplyingMosaic:(NSDictionary*)tools source:(FxImageTile*)src registry:(uint64_t)registry error:(NSError**)error {
@@ -5,13 +39,17 @@
FxRect image=src.imagePixelBounds,bounds=src.tilePixelBounds;
if(memcmp(&image,&bounds,sizeof(FxRect))!=0){fail(error,@"局部马赛克需要完整源画面。");return nil;}
BOOL face=[settings[@"source"]isEqual:@"face"];
NSDictionary *mask=tools[@"mosaicRenderMask"]?:DYSharedMaskWithID(tools,settings[@"maskID"]);
if(!face&&(!mask||mask[@"stampSource"]||![mask[@"enabled"]boolValue]))return src;
NSMutableArray *masks=[NSMutableArray array];
NSArray *selected=tools[@"mosaicRenderMasks"];
if(!selected){NSMutableArray *raw=[NSMutableArray array];for(NSString *ID in settings[@"maskIDs"]?: (settings[@"maskID"]?@[settings[@"maskID"]]:@[])){NSDictionary *m=DYSharedMaskWithID(tools,ID);if(m)[raw addObject:m];}selected=tools[@"mosaicRenderMask"]?@[tools[@"mosaicRenderMask"]]:raw;}
for(NSDictionary *mask in selected)if(!mask[@"stampSource"]&&[mask[@"enabled"]boolValue]){NSMutableDictionary *m=[[mask mutableCopy]autorelease];m[@"combine"]=@0;[masks addObject:m];}
if(!face&&!masks.count)return src;
NSMutableDictionary *entry=[self device:registry error:error];if(!entry)return nil;
if(face)[self prepareFaceSupport:src];else{[_faceEntries release];_faceEntries=nil;}
NSData *faces=face?[self mosaicFaces:src time:tools[@"renderTime"] error:error]:nil;if(face&&!faces){fail(error,@"当前帧人脸识别失败,请重试或改用跟踪蒙版。");return nil;}
DYToolMaskHeader header{};header.section=face?-102:0;
id<MTLDevice>device=entry[@"device"];id<MTLTexture>input=[src metalTextureForDevice:device];
id<MTLBuffer>buffer=[self maskBuffer:face?@{}:@{@"primary":@[mask]} tile:src device:device header:&header];
NSData *skinEntries=_faceEntries;_faceEntries=faces;
id<MTLBuffer>buffer=[self maskBuffer:face?@{}:@{@"primary":masks} tile:src device:device header:&header];_faceEntries=skinEntries;
MTLTextureDescriptor *desc=[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA32Float width:input.width height:input.height mipmapped:NO];desc.storageMode=MTLStorageModePrivate;desc.usage=MTLTextureUsageShaderRead|MTLTextureUsageShaderWrite;
id<MTLTexture>output=[[device newTextureWithDescriptor:desc]autorelease];
id<MTLComputePipelineState>pipeline=[self managedKernel:@"dyLocalMosaic" entry:entry error:error];