Use CIDetector for face recognition and cidetector for face recognition

Source: Internet
Author: User

Use CIDetector for face recognition and cidetector for face recognition
Use CIDetector for face recognition by Wu xueying
Use CIDetector to add images for face recognition: UIImage * image = [UIImage imageNamed: @ "face.jpg"]; UIImageView * testImage = [[UIImageView alloc] initWithImage: image]; [testImage setTransform: CGAffineTransformMakeScale (1,-1)]; [[UIApplication sharedApplication] delegate]. window setTransform: CGAffineTransformMakeScale (1,-1)]; [testImage setFrame: CGRectMake (0, 0, testImage. image. size. width, testImage. image. size. height)]; [self. view addSubview: testImage]; recognition image: CIImage * ciimage = [CIImage imageWithCGImage: image. CGImage]; NSDictionary * opts = [NSDictionary dictionaryWithObject: Rule forKey: role]; CIDetector * detector = [CIDetector detectorOfType: Condition context: nil options: opts]; NSArray * features = [detector featuresInImage: ciimage]; mark the face, eyes, and mouth: for (CIFaceFeature * faceFeature in features) {}// mark the face CGFloat faceWidth = faceFeature. bounds. size. width; UIView * faceView = [[UIView alloc] initWithFrame: faceFeature. bounds]; faceView. layer. borderWidth = 1; faceView. layer. borderColor = [[UIColor redColor] CGColor]; [self. view addSubview: faceView]; // mark the left eye if (faceFeature. hasLeftEyePosition) {UIView * leftEyeView = [[UIView alloc] initWithFrame: CGRectMake (faceFeature. leftEyePosition. x-faceWidth * 0.15,
FaceFeature. leftEyePosition. y-faceWidth * 0.15, faceWidth * 0.3, faceWidth * 0.3)]; [leftEyeView setBackgroundColor: [[UIColor blueColor] colorWithAlphaComponent: 0.3]; [leftEyeView setCenter: faceFeature. leftEyePosition]; leftEyeView. layer. cornerRadius = faceWidth * 0.15; [self. view addSubview: leftEyeView];} // mark the right eye if (faceFeature. hasRightEyePosition) {UIView * leftEye = [[UIView alloc] initWithFrame: CGRectMake (faceFeature. rightEyePosition. x-faceWidth * 0.15, faceFeature. rightEyePosition. y-faceWidth * 0.15, faceWidth * 0.3, faceWidth * 0.3)]; [leftEye setBackgroundColor: [UIColor blueColor] colorWithAlphaComponent: 0.3]; [leftEye setCenter: faceFeature. rightEyePosition]; leftEye. layer. cornerRadius = faceWidth * 0.15; [self. view addSubview: leftEye];} // mark the mouth if (faceFeature. hasMouthPosition) {UIView * mouth = [[UIView alloc] initWithFrame: CGRectMake (faceFeature. mouthPosition. x-faceWidth * 0.2,
FaceFeature. mouthPosition. y-faceWidth * 0.2, faceWidth * 0.4, faceWidth * 0.4)]; [mouth setBackgroundColor: [UIColor greenColor] colorWithAlphaComponent: 0.3]; [mouth setCenter: faceFeature. mouthPosition]; mouth. layer. cornerRadius = faceWidth * 0.2; [self. view addSubview: mouth];}
Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.