/**
* 注意:CIFaceFeature 提供的坐標(biāo)都是和CGContext一樣是反方向的,所以以下的畫圖操作都是用了CGContext的方式
*/
- (void)faceRegonized {
CIContext *context = [CIContext contextWithOptions:nil];
UIImage *imageInput = [UIImage imageNamed:@"example"];
CIImage *image = [CIImage imageWithCGImage:imageInput.CGImage];
//設(shè)置識(shí)別參數(shù)
NSDictionary *param = [NSDictionary dictionaryWithObject:CIDetectorAccuracyHigh
forKey:CIDetectorAccuracy];
//聲明一個(gè)CIDetector,并設(shè)定識(shí)別類型
CIDetector* faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace
context:context options:param];//取得識(shí)別結(jié)果
NSArray *detectResult = [faceDetector featuresInImage:image];
//開始一個(gè)畫布,并將畫布原圖畫到畫布中去
UIGraphicsBeginImageContext(image.size);
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
for(CIFaceFeature* faceFeature in detectResult) {
//臉部
UIView* faceView = [[UIView alloc] initWithFrame:faceFeature.bounds];
faceView.layer.borderWidth = 1;
faceView.layer.borderColor = [UIColor orangeColor].CGColor;
CGFloat faceWidth = faceFeature.bounds.size.width;
//CIFaceFeature 所有的屬性
NSLog(@"-------------------------------------\n");
NSLog(@"faceBounds = %@",NSStringFromCGRect(faceFeature.bounds));
NSLog(@"face hasLeftEyePosition = %d",faceFeature.hasLeftEyePosition);
NSLog(@"face leftEyePosition = %@",NSStringFromCGPoint(faceFeature.leftEyePosition));
NSLog(@"face hasRightEyePosition = %d",faceFeature.hasRightEyePosition);
NSLog(@"face rightEyePosition = %@",NSStringFromCGPoint(faceFeature.rightEyePosition));
NSLog(@"face hasMouthPosition = %d",faceFeature.hasMouthPosition);
NSLog(@"face mouthPosition = %@",NSStringFromCGPoint(faceFeature.mouthPosition));
NSLog(@"face hasTrackingID = %d",faceFeature.hasTrackingID);
NSLog(@"face trackingID = %d",faceFeature.trackingID);
NSLog(@"face hasTrackingFrameCount = %d",faceFeature.hasTrackingFrameCount);
NSLog(@"face trackingFrameCount = %d",faceFeature.trackingFrameCount);
NSLog(@"face hasFaceAngle = %d",faceFeature.hasFaceAngle);
NSLog(@"face faceAngle = %f",faceFeature.faceAngle);
NSLog(@"face hasSmile = %d",faceFeature.hasSmile);
NSLog(@"face leftEyeClosed = %d",faceFeature.leftEyeClosed);
NSLog(@"face rightEyeClosed = %d",faceFeature.rightEyeClosed);
NSLog(@"\n-------------------------------------");
//畫一個(gè)矩形出識(shí)別出來的臉部的方位
UIBezierPath *path = [UIBezierPath bezierPath];
CGPoint point = faceFeature.bounds.origin;
CGSize size = faceFeature.bounds.size;
[path moveToPoint:point];
[path addLineToPoint:CGPointMake(point.x + size.width, point.y)];
[path addLineToPoint:CGPointMake(point.x + size.width, point.y + size.height)];
[path addLineToPoint:CGPointMake(point.x, point.y+ size.height)];
[path addLineToPoint:point];
[[UIColor redColor] setStroke];
[path stroke];
[[UIColor redColor] setStroke];
[path stroke];
//左眼
if (faceFeature.hasLeftEyePosition) {
[self drawCycleAtPoint:faceFeature.leftEyePosition withRadius:faceWidth*0.1];
}
//右眼
if (faceFeature.hasRightEyePosition) {
[self drawCycleAtPoint:faceFeature.rightEyePosition withRadius:faceWidth*0.1];
}
//嘴巴
if (faceFeature.hasMouthPosition) {
[self drawCycleAtpoint:faceFeature.mouthPosition withRadius:faceWidth*0.2];
}
}
//獲得所有在原圖中圈出來臉之后最后的圖片,此時(shí)的圖片是反方向的
UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();
//將畫布清空,并把放方向的圖片用CG的方式畫到原圖中去(CG畫出來的東西都是原來的反方向,并不是個(gè)好方法,實(shí)際開發(fā)中用別的方法)
CGContextClearRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height));
CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height), tempImage.CGImage);
//獲得重畫之后的圖片,并結(jié)束畫布
tempImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//將最終的圖片放到View中去
[self.imageView setImage:tempImage];
}
- (void)drawCycleAtPoint:(CGPoint)point withRadius:(CGFloat)radius {
/*畫圓*/
//邊框圓
CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(),1,0,0,1.0);//畫筆線的顏色
CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 1.0);//線的寬度
//void CGContextAddArc(CGContextRef c,CGFloat x, CGFloat y,CGFloat radius,CGFloat startAngle,CGFloat endAngle, int clockwise)1弧度=180°/π (≈57.3°) 度=弧度×180°/π 360°=360×π/180 =2π 弧度
// x,y為圓點(diǎn)坐標(biāo),radius半徑,startAngle為開始的弧度,endAngle為 結(jié)束的弧度,clockwise 0為順時(shí)針,1為逆時(shí)針。
CGContextAddArc(UIGraphicsGetCurrentContext(), point.x, point.y, radius, 0, 2*M_PI, 0); //添加一個(gè)圓
CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathStroke); //繪制路徑
}
系統(tǒng)人臉識(shí)別:iOS 官方人臉檢測(cè)Demo
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 近幾年隨著移動(dòng)設(shè)備硬件設(shè)備越來越優(yōu)各種美顏相機(jī)App應(yīng)運(yùn)而生,美顏、瘦臉、添加掛件等等一系列的功能,這其中的原理一...
- 上兩篇文章介紹了用iOS原生的人臉檢測(cè) 與 Dlib的關(guān)鍵點(diǎn)檢測(cè)來實(shí)現(xiàn)的 人臉關(guān)鍵點(diǎn)識(shí)別,相信已經(jīng)實(shí)現(xiàn)的小伙伴會(huì)感...
- 上篇文章 人臉框檢測(cè) 已經(jīng)介紹了我們?nèi)绻麖囊曨l流中檢測(cè)出人臉位置的信息,基于這些內(nèi)容,我們繼續(xù)向下擴(kuò)展,獲取人臉6...
- 在冬季很多人往往都會(huì)選擇黑白灰三色單品或者大地色系單品來混搭,其實(shí)除了這幾個(gè)百搭色,還是有不少好搭又好看的色調(diào)可以...
- 現(xiàn)在很多人遇到點(diǎn)事情就是順其自然,胖瘦順其自然,不會(huì)去克制自己的食欲和生活習(xí)慣,總是一副喜歡我的人不會(huì)在意我...