關於圖片壓縮:imageview的大小與image的大小不一致,識別的時候,是按照image的大小進行計算,通過重繪,使imageview與image保持一致。 關於坐標Y軸翻轉:屏幕的坐標原點即(0,0)點在左上角,識別圖片的坐標原點在左下角。 運行結果 控制台輸出結果 ...
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; imageView.image = [UIImage imageNamed:@"timg.jpeg"]; [self.view addSubview:imageView]; CGFloat width = imageView.image.size.width; CGFloat height = imageView.image.size.height; CGFloat sWidth = imageView.bounds.size.width; CGFloat sHeight = imageView.bounds.size.height; CGFloat x = width/sWidth; CGFloat y = height/sHeight; // 根據image修改imageview的frame if (x > y) { imageView.frame = CGRectMake(0, (sHeight-height/x)/2, sWidth, height/x); }else{ imageView.frame = CGRectMake((sWidth-width/y)/2, 0, width/y, sHeight); } // 壓縮image UIGraphicsBeginImageContext(CGSizeMake(imageView.bounds.size.width , imageView.bounds.size.height)); [imageView.image drawInRect:CGRectMake(0, 0, imageView.bounds.size.width, imageView.bounds.size.height)]; UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); imageView.image = scaledImage; //識別 dispatch_async(dispatch_get_global_queue(0, 0), ^{ CIImage *cImage = [CIImage imageWithCGImage:imageView.image.CGImage]; // 設置識別模式 NSDictionary *opts = [NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh forKey:CIDetectorAccuracy]; /* Lower accuracy, higher performance */ //CORE_IMAGE_EXPORT NSString* const CIDetectorAccuracyLow NS_AVAILABLE(10_7, 5_0); /* Lower performance, higher accuracy */ //CORE_IMAGE_EXPORT NSString* const CIDetectorAccuracyHigh NS_AVAILABLE(10_7, 5_0); CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:opts]; NSArray *features = [detector featuresInImage:cImage]; if ([features count] == 0) { dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"檢測失敗"); }); return ; } for (CIFaceFeature *feature in features) { // 是否微笑 BOOL smile = feature.hasSmile; NSLog(smile ? @"微笑" : @"沒微笑"); // 眼睛是否睜開 BOOL leftEyeClosed = feature.leftEyeClosed; BOOL rightEyeClosed = feature.rightEyeClosed; NSLog(leftEyeClosed ? @"左眼沒睜開" : @"左眼睜開"); NSLog(rightEyeClosed ? @"右眼沒睜開" : @"右眼睜開"); // 獲取臉部frame CGRect rect = feature.bounds; rect.origin.y = imageView.bounds.size.height - rect.size.height - rect.origin.y;// Y軸旋轉180度 faceRect = rect; NSLog(@"臉 %@",NSStringFromCGRect(rect)); // 左眼 if (feature.hasLeftEyePosition) { CGPoint eye = feature.leftEyePosition; eye.y = imageView.bounds.size.height - eye.y;// Y軸旋轉180度 NSLog(@"左眼 %@",NSStringFromCGPoint(eye)); } // 右眼 if (feature.hasRightEyePosition) { CGPoint eye = feature.rightEyePosition; eye.y = imageView.bounds.size.height - eye.y;// Y軸旋轉180度 NSLog(@"右眼 %@",NSStringFromCGPoint(eye)); } // 嘴 if (feature.hasMouthPosition) { CGPoint mouth = feature.mouthPosition; mouth.y = imageView.bounds.size.height - mouth.y;// Y軸旋轉180度 NSLog(@"嘴 %@",NSStringFromCGPoint(mouth)); } } dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"檢測完成"); UIView *view = [[UIView alloc] initWithFrame:faceRect]; view.backgroundColor = [UIColor blueColor]; view.alpha = 0.3; [imageView addSubview:view]; }); }); }
關於圖片壓縮:imageview的大小與image的大小不一致,識別的時候,是按照image的大小進行計算,通過重繪,使imageview與image保持一致。
關於坐標Y軸翻轉:屏幕的坐標原點即(0,0)點在左上角,識別圖片的坐標原點在左下角。
運行結果
控制台輸出結果