-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSmileDetector.m
68 lines (54 loc) · 2.41 KB
/
SmileDetector.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// CameraCapture.m
// MoodChanger
//
// Created by Austin Feight on 2/2/14.
//
//
#import "SmileDetector.h"
#import "MCUtility.h"
@implementation SmileDetector
- (id)init {
if (self = [super init]) {
_faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace
context:nil
options:@{CIDetectorAccuracy: CIDetectorAccuracyHigh}];
}
return self;
}
#pragma mark - API Functionality
#pragma mark - API Session Management
- (void)setDetectionAccuracy:(NSString *)accuracy {
if (![accuracy isEqualToString:CIDetectorAccuracyHigh] && ![accuracy isEqualToString:CIDetectorAccuracyLow]) {
return;
}
_faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace
context:nil
options:@{CIDetectorAccuracy: accuracy}];
}
#pragma mark - Output Buffer Handling
// called asynchronously as the capture output is capturing sample buffers, this method asks the face detector (if on)
// to detect features and for each draw the red square in a layer and set appropriate orientation
-(void)captureOutput:(AVCaptureOutput *)captureOutput didDropSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
}
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
// got an image
CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate);
CIImage *ciImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer options:(__bridge NSDictionary *)attachments];
if (attachments) {
CFRelease(attachments);
}
int exifOrientation = [MCUtility exifOrientation:_isUsingFrontFacingCamera];
NSArray *features = [_faceDetector featuresInImage:ciImage options:@{ CIDetectorSmile: @YES,
CIDetectorEyeBlink: @YES,
CIDetectorImageOrientation: [NSNumber numberWithInt:exifOrientation] }];
for (CIFaceFeature *feat in features) {
if ([feat hasSmile]) {
NSLog(@"Smiling");
} else {
NSLog(@"Face is there, not detecting smiles");
}
}
}
@end