日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人工智能 > pytorch >内容正文

pytorch

iOS人脸识别

發(fā)布時(shí)間:2024/4/14 pytorch 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 iOS人脸识别 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

//

//? ViewController.m

//? 02-人臉識(shí)別

//

//? Created by Romeo on 15/9/25.

//? Copyright ? 2015年 itheima. All rights reserved.

//

?

#import "ViewController.h"

#import "FaceppAPI.h"

#import "UIView+AutoLayout.h"

?

@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

?

@property (weak, nonatomic) IBOutlet UIImageView *imageVIew;

?

@end

?

@implementation ViewController

?

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? // Do any additional setup after loading the view, typically from a nib.

}

?

/**

?UIImagePickerControllerSourceTypePhotoLibrary,? ? ? 照片庫

?UIImagePickerControllerSourceTypeCamera,? ? ? ? ? ? 相機(jī)

?UIImagePickerControllerSourceTypeSavedPhotosAlbum ? 相冊(cè)

?*/

?

#pragma mark 相冊(cè)

?

- (IBAction)selectPhotoClick:(id)sender {

? ? //1. 首先判斷是否可用

? ? if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

? ? ? ? return;

? ? }

?? ?

? ? //2. 創(chuàng)建選擇控制器

? ? UIImagePickerController *picker = [UIImagePickerController new];

?? ?

? ? //3. 設(shè)置類型

? ? picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

?? ?

? ? //4. 設(shè)置代理

? ? picker.delegate = self;

?? ?

? ? //5. 彈出

? ? [self presentViewController:picker animated:YES completion:nil];

}

?

#pragma mark 相機(jī)

?

- (IBAction)selectCameClick:(id)sender {

?? ?

? ? //1. 首先判斷是否可用

? ? if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

? ? ? ? return;

? ? }

?? ?

? ? //2. 創(chuàng)建選擇控制器

? ? UIImagePickerController *picker = [UIImagePickerController new];

?? ?

? ? //3. 設(shè)置類型

? ? picker.sourceType = UIImagePickerControllerSourceTypeCamera;

?? ?

? ? //4. 設(shè)置代理

? ? picker.delegate = self;

?? ?

? ? //5. 彈出

? ? [self presentViewController:picker animated:YES completion:nil];

}

?

#pragma mark Picker代理方法

//點(diǎn)擊照片的時(shí)候調(diào)用

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info

{

? ? //1. 獲取選擇的圖像

? ? UIImage *image = info[UIImagePickerControllerOriginalImage];

?? ?

? ? //orientation 0? --> 在做人臉識(shí)別, 一定要方向?yàn)?

?? ?

? ? //2. 校正方向

? ? image = [self fixOrientation:image];

?? ?

? ? //3. 開始檢測(cè)

? ? NSData *data = UIImageJPEGRepresentation(image, 0.5);

//? ? UIImagePNGRepresentation(<#UIImage * _Nonnull image#>)

?? ?

? ? //detection/detect 檢測(cè)一張照片中的人臉信息(臉部位置、年齡、種族、性別等等)

? ? FaceppResult *result = [[FaceppAPI detection] detectWithURL:nil orImageData:data];

?? ?

? ? //4. 獲取性別,年齡

?? ?

? ? NSDictionary *attributeDict = result.content[@"face"][0][@"attribute"];

? ? NSString *ageValue = attributeDict[@"age"][@"value"];

? ? NSString *sexValue = [attributeDict[@"gender"][@"value"] isEqualToString:@"Male"] ? @"男性" : @"女性";

?? ?

? ? //5. 獲取臉部位置 (比例值)

? ? NSDictionary *positionDict = result.content[@"face"][0][@"position"];

?? ?

? ? CGFloat h = [positionDict[@"height"] floatValue];

? ? CGFloat w = [positionDict[@"width"] floatValue];

? ? CGFloat x = [positionDict[@"center"][@"x"] floatValue] - w * 0.5;

? ? CGFloat y = [positionDict[@"center"][@"y"] floatValue] - h * 0.5;

?? ?

? ? //6. 畫圖--> 畫臉到 image 上

?? ?

? ? //6.1 開啟圖像圖形上下文

? ? UIGraphicsBeginImageContextWithOptions( image.size, NO, 0);

?? ?

? ? //6.2 將原圖先繪制到底部, 從(0, 0)點(diǎn)開始繪制

? ? [image drawAtPoint:CGPointZero];

?? ?

? ? //6.3 畫素材圖像到圖形上下文中

? ? UIImage *aImage = [UIImage imageNamed:@"cang"];

?? ?

? ? // 根據(jù)原圖的大小比例來計(jì)算自己的位置

? ? CGFloat imageW = image.size.width;

? ? CGFloat imageH = image.size.height;

? ? [aImage drawInRect:CGRectMake(x * 0.01 * imageW , y * 0.01 * imageH, w * 0.01 * imageW, h * 0.01 * imageH)];

?? ?

? ? // 320 ? ? 50% ? ? 160

? ? //50 * 0.01 * 320 ? = 160

?? ?

? ? //6.4 合成新的圖像

? ? UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

?? ?

? ? //6.5 關(guān)閉圖像上下文

? ? UIGraphicsEndImageContext();

?? ?

? ? //7. 在控制器顯示

?? ?

? ? //刪除原有約束,然后重新添加

?? ?

? ? // 刪除原有約束

? ? [self.imageVIew autoRemoveConstraintsAffectingView];

?? ?

? ? // 設(shè)置居中

? ? [self.imageVIew autoCenterInSuperview];

?? ?

? ? // 設(shè)置寬高 --> 需要根據(jù)圖片的比例來確定

? ? //960 * 1080

?? ?

? ? // 假如我們需要寬度: 屏幕總寬度

?? ?

? ? // iPhone5 : 320

? ? // iPhone6 : 375

? ? CGFloat scale = newImage.size.width / [UIScreen mainScreen].bounds.size.width;

?? ?

?? ?

? ? //Dimension : 尺寸 --> 寬高

? ? [self.imageVIew autoSetDimension: ALDimensionWidth toSize:newImage.size.width / scale];

? ? [self.imageVIew autoSetDimension:ALDimensionHeight toSize:newImage.size.height / scale];

?? ?

? ? self.imageVIew.image = newImage;

?? ?

? ? //8. 實(shí)現(xiàn)了代理方法一定要取消

? ? [picker dismissViewControllerAnimated:YES completion:nil];

}

?

?

#pragma mark 方向校正

/**

?1. 先將頭部朝上

?2. 將鏡像反轉(zhuǎn)

?3. 重新合成輸出

?*/

?

- (UIImage*)fixOrientation:(UIImage*)aImage

{

?? ?

? ? // No-op if the orientation is already correct

? ? if (aImage.imageOrientation == UIImageOrientationUp)

? ? ? ? return aImage;

?? ?

? ? // We need to calculate the proper transformation to make the image upright.

? ? // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.

? ? CGAffineTransform transform = CGAffineTransformIdentity;

?? ?

? ? switch (aImage.imageOrientation) {

? ? ? ? case UIImageOrientationDown:

? ? ? ? case UIImageOrientationDownMirrored:

? ? ? ? ? ? transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);

? ? ? ? ? ? transform = CGAffineTransformRotate(transform, M_PI);

? ? ? ? ? ? break;

?? ? ? ? ? ?

? ? ? ? case UIImageOrientationLeft:

? ? ? ? case UIImageOrientationLeftMirrored:

? ? ? ? ? ? transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);

? ? ? ? ? ? transform = CGAffineTransformRotate(transform, M_PI_2);

? ? ? ? ? ? break;

?? ? ? ? ? ?

? ? ? ? case UIImageOrientationRight:

? ? ? ? case UIImageOrientationRightMirrored:

? ? ? ? ? ? transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);

? ? ? ? ? ? transform = CGAffineTransformRotate(transform, -M_PI_2);

? ? ? ? ? ? break;

? ? ? ? default:

? ? ? ? ? ? break;

? ? }

?? ?

? ? switch (aImage.imageOrientation) {

? ? ? ? case UIImageOrientationUpMirrored:

? ? ? ? case UIImageOrientationDownMirrored:

? ? ? ? ? ? transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);

? ? ? ? ? ? transform = CGAffineTransformScale(transform, -1, 1);

? ? ? ? ? ? break;

?? ? ? ? ? ?

? ? ? ? case UIImageOrientationLeftMirrored:

? ? ? ? case UIImageOrientationRightMirrored:

? ? ? ? ? ? transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);

? ? ? ? ? ? transform = CGAffineTransformScale(transform, -1, 1);

? ? ? ? ? ? break;

? ? ? ? default:

? ? ? ? ? ? break;

? ? }

?? ?

? ? // Now we draw the underlying CGImage into a new context, applying the transform

? ? // calculated above.

? ? CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CGImageGetBitsPerComponent(aImage.CGImage), 0,

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CGImageGetColorSpace(aImage.CGImage),

?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? CGImageGetBitmapInfo(aImage.CGImage));

? ? CGContextConcatCTM(ctx, transform);

? ? switch (aImage.imageOrientation) {

? ? ? ? case UIImageOrientationLeft:

? ? ? ? case UIImageOrientationLeftMirrored:

? ? ? ? case UIImageOrientationRight:

? ? ? ? case UIImageOrientationRightMirrored:

? ? ? ? ? ? CGContextDrawImage(ctx, CGRectMake(0, 0, aImage.size.height, aImage.size.width), aImage.CGImage);

? ? ? ? ? ? break;

?? ? ? ? ? ?

? ? ? ? default:

? ? ? ? ? ? CGContextDrawImage(ctx, CGRectMake(0, 0, aImage.size.width, aImage.size.height), aImage.CGImage);

? ? ? ? ? ? break;

? ? }

?? ?

? ? // And now we just create a new UIImage from the drawing context

? ? CGImageRef cgimg = CGBitmapContextCreateImage(ctx);

? ? UIImage* img = [UIImage imageWithCGImage:cgimg];

? ? CGContextRelease(ctx);

? ? CGImageRelease(cgimg);

? ? return img;

}

?

@end

?

轉(zhuǎn)載于:https://www.cnblogs.com/cfl911014/p/5276361.html

總結(jié)

以上是生活随笔為你收集整理的iOS人脸识别的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。