#import <UIKit / UIKit.h>
@interface RootViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
IBOutlet UIImageView * imageView;
IBOutlet UILabel * label;
CIDetector * detector;
}
@end
- ( IBAction ) onImport : ( id ) sender
{
UIImagePickerController * vc = [ [ UIImagePickerController alloc ] init ] ;
vc.delegate = self;
vc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[ self presentModalViewController : vc animated : YES ] ;
[ vc release ] ;
}
- ( IBAction ) onRecognize : ( id ) sender
{
detector = [ CIDetector detectorOfType : CIDetectorTypeFace context : nil options : [ NSDictionary dictionaryWithObject : CIDetectorAccuracyHigh forKey : CIDetectorAccuracy ] ] ;
NSDate * date = [ NSDate date ] ;
NSArray * features = [ detector featuresInImage :
[ [ [ CIImage alloc ] initWithCGImage : imageView.image.CGImage ] autorelease ]
] ;
NSTimeInterval ti = fabs ( [ date timeIntervalSinceNow ] ) ;
label.text = [ NSString stringWithFormat : @ "Time:% 0.3f \ n Faces:% i" , ti, [ features count ] ] ;
UIGraphicsBeginImageContext ( imageView.image.size ) ;
CGContextRef ctx = UIGraphicsGetCurrentContext ( ) ;
CGContextDrawImage ( ctx, CGRectMake ( 0 , 0 , imageView.image.size.width, imageView.image.size.height ) , imageView.image.CGImage ) ;
for ( CIFeature * feature in features )
{
CGRect r = feature.bounds;
CGContextSetStrokeColor ( ctx, CGColorGetComponents ( [ UIColor yellowColor ] .CGColor ) ) ;
CGContextSetLineWidth ( ctx, 1.0f ) ;
CGContextBeginPath ( ctx ) ;
CGContextAddRect ( ctx, r ) ;
CGContextClosePath ( ctx ) ;
CGContextStrokePath ( ctx ) ;
}
imageView.image = [ UIImage imageWithCGImage : UIGraphicsGetImageFromCurrentImageContext ( ) .CGImage scale : 1.0f orientation : UIImageOrientationDownMirrored ] ;
UIGraphicsEndImageContext ( ) ;
}
Source: https://habr.com/ru/post/131121/
All Articles