我正在关注 Pro iOS 5 Augmented Reality 书中的面部识别应用程序示例。我什至下载了源代码......我从那里运行它并且他的代码仍然存在问题。这是问题所在:它在分配一个数组时崩溃,该数组将 CGImage 的 featuresInImage 用于检测人脸的 CIDetector。从日志中......似乎这个方法被多次调用......我正在使用cocos2d_chipmunk所以我使用的是CSScene。请注意,这个崩溃是一个EXC_BAD_ACCESS (code=1, address=0x4499923c)
帮助吗?
- (void)facialRecognitionRequest:(UIImage *)image {
//NSLog(@"Image is: %f by %f", image.size.width, image.size.height);
if (!isProcessingRequest) {
isProcessingRequest = YES;
//NSLog(@"Detecting Faces");
NSArray* arr = [detector featuresInImage:[CIImage imageWithCGImage:[image CGImage]]]; // CRASHES HERE
if ([arr count] > 0) {
//NSLog(@"Faces found.");
for (int i = 0; i < 1; i++) { //< [arr count]; i++) {
CIFaceFeature *feature = [arr objectAtIndex:i];
double xPosition = (feature.leftEyePosition.x + feature.rightEyePosition.x+feature.mouthPosition.x)/(3*image.size.width) ;
double yPosition = (feature.leftEyePosition.y + feature.rightEyePosition.y+feature.mouthPosition.y)/(3*image.size.height);
double dist = sqrt(pow((feature.leftEyePosition.x - feature.rightEyePosition.x),2)+pow((feature.leftEyePosition.y - feature.rightEyePosition.y),2))/image.size.width;
yPosition += dist;
CGSize size = [[CCDirector sharedDirector] winSize];
pumpkin.opacity = 255;
pumpkin.scale = 5*(size.width*dist)/256.0;
//int randomPumpkin = ((arc4random() % 10) + 5);
[pumpkin setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"pumpkin%d.png", pumpkin_count + 4]]];
CCMoveTo *moveAction = [CCMoveTo actionWithDuration:0 position:ccp((size.width * (xPosition)), (size.height * ((yPosition))))];
[pumpkin runAction:moveAction];
}
} else {
pumpkin.opacity = 0;
}
}
isProcessingRequest = NO;
}
分配 CIDetector:
- (id)init {
if (self = [super init]) {
// ....... other stuff here
NSDictionary *detectorOptions = [NSDictionary dictionaryWithObjectsAndKeys:CIDetectorAccuracyLow, CIDetectorAccuracy, nil];
self.detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:detectorOptions]; // CIDetector instance named detector is my property
}
return self;
}
我试过:CGImage *theCGImage = [图像 CGImage]; NSLog(@"theCGImage: %@", theCGImage);
CIImage *theCIImage = [CIImage imageWithCGImage:theCGImage];
NSLog(@"theCIImage: %@", theCIImage);
NSArray* arr = [detector featuresInImage:theCIImage];
NSLog(@"arr: %@", arr);
结果如下:
2012-04-15 19:08:25.136 Ch8[981:609f] tmpCGImage: <CGImage 0x1f689c00>
2012-04-15 19:08:25.143 Ch8[981:609f] tmpCIImage: <CIImage: 0x1f687970 extent [0 0 480 360]>
2012-04-15 19:08:25.282 Ch8[981:609f] arr: (
"<CIFaceFeatureInternal: 0x1f58e080>"
)
我也尝试过启用NSZombies
但仍然没有运气......有什么想法吗?