Ich würde Ihnen empfehlen, es so einfach wie möglich und in dieser Situation zu halten Möglichkeiten nur nutzen nativen iOS, es zu tun, wenn es genug ist. Die Hauptklasse ist CIDetector
der CoreImage
Rahmen. Im Folgenden sind die wichtigsten Methoden
// create CIDetector object with CIDetectorTypeFace type
CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace
context:nil
options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}];
// give it CIImage and receive an array with CIFaceFeature objects
NSArray *features = [detector featuresInImage:newImage];
Nach Apple- docs CIFaceFeature
nächsten Eigenschaften
@interface CIFaceFeature : CIFeature
@property (readonly, assign) CGRect bounds;
@property (readonly, assign) BOOL hasLeftEyePosition;
@property (readonly, assign) CGPoint leftEyePosition;
@property (readonly, assign) BOOL hasRightEyePosition;
@property (readonly, assign) CGPoint rightEyePosition;
@property (readonly, assign) BOOL hasMouthPosition;
@property (readonly, assign) CGPoint mouthPosition;
@property (readonly, assign) BOOL hasTrackingID;
@property (readonly, assign) int trackingID;
@property (readonly, assign) BOOL hasTrackingFrameCount;
@property (readonly, assign) int trackingFrameCount;
@property (readonly, assign) BOOL hasFaceAngle;
@property (readonly, assign) float faceAngle;
@property (readonly, assign) BOOL hasSmile;
@property (readonly, assign) BOOL leftEyeClosed;
@property (readonly, assign) BOOL rightEyeClosed;
@end
Auch dort enthält eine große Raywenderlich article über GCD, die Gesichtserkennung implementiert verfügt und hier ist seine Final project. Es findet die Augenposition der Menschen und überlagert sie mit einigen lustigen Augen.
Und schließlich Teil des Codes aus dem Projekt und den Screenshot daraus.
- (UIImage *)faceOverlayImageFromImage:(UIImage *)image
{
CIDetector* detector = [CIDetector detectorOfType:CIDetectorTypeFace
context:nil
options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}];
// Get features from the image
CIImage* newImage = [CIImage imageWithCGImage:image.CGImage];
NSArray *features = [detector featuresInImage:newImage];
UIGraphicsBeginImageContext(image.size);
CGRect imageRect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
//Draws this in the upper left coordinate system
[image drawInRect:imageRect blendMode:kCGBlendModeNormal alpha:1.0f];
CGContextRef context = UIGraphicsGetCurrentContext();
for (CIFaceFeature *faceFeature in features) {
CGRect faceRect = [faceFeature bounds];
CGContextSaveGState(context);
// CI and CG work in different coordinate systems, we should translate to
// the correct one so we don't get mixed up when calculating the face position.
CGContextTranslateCTM(context, 0.0, imageRect.size.height);
CGContextScaleCTM(context, 1.0f, -1.0f);
if ([faceFeature hasLeftEyePosition]) {
CGPoint leftEyePosition = [faceFeature leftEyePosition];
CGFloat eyeWidth = faceRect.size.width/kFaceBoundsToEyeScaleFactor;
CGFloat eyeHeight = faceRect.size.height/kFaceBoundsToEyeScaleFactor;
CGRect eyeRect = CGRectMake(leftEyePosition.x - eyeWidth/2.0f,
leftEyePosition.y - eyeHeight/2.0f,
eyeWidth,
eyeHeight);
[self drawEyeBallForFrame:eyeRect];
}
if ([faceFeature hasRightEyePosition]) {
CGPoint leftEyePosition = [faceFeature rightEyePosition];
CGFloat eyeWidth = faceRect.size.width/kFaceBoundsToEyeScaleFactor;
CGFloat eyeHeight = faceRect.size.height/kFaceBoundsToEyeScaleFactor;
CGRect eyeRect = CGRectMake(leftEyePosition.x - eyeWidth/2.0f,
leftEyePosition.y - eyeHeight/2.0f,
eyeWidth,
eyeHeight);
[self drawEyeBallForFrame:eyeRect];
}
CGContextRestoreGState(context);
}
UIImage *overlayImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return overlayImage;
}
- (void)drawEyeBallForFrame:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(context, rect);
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillPath(context);
CGFloat x, y, eyeSizeWidth, eyeSizeHeight;
eyeSizeWidth = rect.size.width * kRetinaToEyeScaleFactor;
eyeSizeHeight = rect.size.height * kRetinaToEyeScaleFactor;
x = arc4random_uniform((rect.size.width - eyeSizeWidth));
y = arc4random_uniform((rect.size.height - eyeSizeHeight));
x += rect.origin.x;
y += rect.origin.y;
CGFloat eyeSize = MIN(eyeSizeWidth, eyeSizeHeight);
CGRect eyeBallRect = CGRectMake(x, y, eyeSize, eyeSize);
CGContextAddEllipseInRect(context, eyeBallRect);
CGContextSetFillColorWithColor(context, [UIColor blackColor].CGColor);
CGContextFillPath(context);
}
Hoffen, dass es helfen wird