65 行
6.6 KiB
C++
65 行
6.6 KiB
C++
// 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 {
|
|
NSDictionary *settings=tools[@"mosaic"];if(![settings[@"enabled"]boolValue])return src;
|
|
FxRect image=src.imagePixelBounds,bounds=src.tilePixelBounds;
|
|
if(memcmp(&image,&bounds,sizeof(FxRect))!=0){fail(error,@"局部马赛克需要完整源画面。");return nil;}
|
|
BOOL face=[settings[@"source"]isEqual:@"face"];
|
|
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;
|
|
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];
|
|
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];
|
|
if(!input||!buffer||!output||!pipeline){fail(error,@"局部马赛克缓冲区不可用。");return nil;}
|
|
// Size is in 1080-line reference pixels, stable under proxy/viewer scaling.
|
|
uint32_t block=(uint32_t)MAX(2,round([settings[@"size"]doubleValue]*input.height/1080.));
|
|
id<MTLCommandBuffer>cb=[entry[@"queue"]commandBuffer];id<MTLComputeCommandEncoder>ce=[cb computeCommandEncoder];
|
|
[ce setComputePipelineState:pipeline];[ce setTexture:input atIndex:0];[ce setTexture:output atIndex:1];[ce setBuffer:buffer offset:0 atIndex:0];[ce setBytes:&header length:sizeof(header) atIndex:1];[ce setBytes:&block length:sizeof(block) atIndex:2];
|
|
[ce dispatchThreads:MTLSizeMake(input.width,input.height,1)threadsPerThreadgroup:MTLSizeMake(16,16,1)];[ce endEncoding];[cb commit];[cb waitUntilCompleted];
|
|
if(cb.status!=MTLCommandBufferStatusCompleted){fail(error,cb.error.localizedDescription);return nil;}
|
|
DYExportTile *tile=[[[DYExportTile alloc]initWithTexture:output registry:registry]autorelease];[tile setBounds:bounds origin:src.imageOrigin];[tile setPixelAspect:src.pixelAspect];[tile inheritColorSpace:src.colorSpace];return tile;
|
|
}
|