人脸识别(简单代码)

//

//  ViewController.m

//  人脸识别

//

//  Created by yangxiuying on 15/2/15.

//  Copyright (c) 2015年 lanjiying. All rights reserved.

//

#import "ViewController.h"

#define SCREENWIDTH [UIScreen mainScreen].bounds.size.width

#define SCREENHEIGHT [UIScreen mainScreen].bounds.size.height

@interface
ViewController ()

{

//原来的图像

UIImageView * _originleImagView;

//识别出来的图像

UIImageView * newfaceImageView;

}

@property(nonatomic,assign)NSInteger featureCount;

@end

@implementation ViewController

- (void)viewDidLoad {

[super
viewDidLoad];

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

_featureCount =
0;

UIImage * oldFaceImg = [UIImage
imageNamed:@"faces1"];

_originleImagView = [[UIImageView
alloc] initWithFrame:CGRectMake(0,
20, oldFaceImg.size.width, oldFaceImg.size.height)];

[_originleImagView
setImage:oldFaceImg];

_originleImagView.center =
CGPointMake(SCREENWIDTH/2, oldFaceImg.size

.height/2+30);

[self.view
addSubview:_originleImagView];

UIButton * featureButton = [UIButton
buttonWithType:UIButtonTypeCustom];

featureButton.frame =
CGRectMake(0,
0, 120,
50);

featureButton.center =
CGPointMake(SCREENWIDTH/2, oldFaceImg.size.height +
30 + 30);

[featureButton setTitle:[NSString
stringWithFormat:@"识别人脸%ld次",(long)_featureCount]
forState:UIControlStateNormal];

[featureButton setTitle:@"识别的图像为"
forState:UIControlStateSelected];

featureButton.backgroundColor = [UIColor
grayColor];

[featureButton setTitleColor:[UIColor
whiteColor] forState:UIControlStateNormal];

[featureButton setTitleColor:[UIColor
whiteColor] forState:UIControlStateSelected];

[featureButton addTarget:self
action:@selector(faceFetureButtonAction:)
forControlEvents:UIControlEventTouchUpInside];

[self.view
addSubview:featureButton];

newfaceImageView = [[UIImageView
alloc] initWithFrame:CGRectMake(SCREENWIDTH/2, oldFaceImg.size.height
+ 30+ 60 ,
50, 50)];

[self.view
addSubview:newfaceImageView];

}

-(void)faceFetureButtonAction:(UIButton *)button

{

//核心代码进行人脸识别

CIContext * context = [CIContext
contextWithOptions:nil];

UIImage * imageInput = [_originleImagView
image];

CIImage * imageChange = [CIImage
imageWithCGImage:imageInput.CGImage];

//
设置识别的参数

NSDictionary * param = [NSDictionary
dictionaryWithObject:CIDetectorAccuracyHigh
forKey:CIDetectorAccuracy];

//声明CIDetector

CIDetector * faceDetector = [CIDetector
detectorOfType:CIDetectorTypeFace
context:context options:param];

//取得识别结果

NSArray * detectResult = [faceDetector
featuresInImage:imageChange];

UIView * resultView = [[UIView
alloc] initWithFrame:_originleImagView.frame];

[self.view
addSubview:resultView];

for (CIFaceFeature * faceFeature
in detectResult) {

//脸部

UIView * faceView = [[UIView
alloc] initWithFrame:faceFeature.bounds];

faceView.layer.borderWidth =
1;

faceView.layer.borderColor = [UIColor
orangeColor].CGColor;

[resultView
addSubview:faceView];

//左眼

if (faceFeature.hasLeftEyePosition) {

UIView * leftEyeView = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 5,
5)];

[leftEyeView
setCenter:faceFeature.leftEyePosition];

leftEyeView.layer.borderWidth =
1;

leftEyeView.layer.borderColor = [UIColor
orangeColor].CGColor;

[resultView
addSubview:leftEyeView];

}

//右眼

if (faceFeature.hasRightEyePosition) {

UIView * rightEyeView = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 5,
5)];

[rightEyeView
setCenter:faceFeature.rightEyePosition];

rightEyeView.layer.borderColor = [UIColor
orangeColor].CGColor;

rightEyeView.layer.borderWidth =
1;

[resultView
addSubview:rightEyeView];

}

//嘴巴

if (faceFeature.hasMouthPosition) {

UIView * mouthView = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 10,
5)];

[mouthView
setCenter:faceFeature.rightEyePosition];

mouthView.layer.borderColor = [UIColor
orangeColor].CGColor;

mouthView.layer.borderWidth =
1;

[resultView
addSubview:mouthView];

}

[resultView setTransform:CGAffineTransformMakeScale(1, -1)];

if ([detectResult
count] > 0) {

{

CIImage * faceImage = [imageChange
imageByCroppingToRect:[[detectResult objectAtIndex:0]
bounds]];

UIImage * face = [UIImage
imageWithCGImage:[context
createCGImage:faceImage fromRect:faceImage.extent]];

newfaceImageView.image = face;

newfaceImageView.frame =
CGRectMake(SCREENWIDTH/2-40,
300+ 30+
60 ,80 , 80);

_featureCount +=1;

[button setTitle:[NSString
stringWithFormat:@"识别次数%ld次",(long)_featureCount]
forState:UIControlStateNormal];

}

}

}

}

运行效果如下:

时间: 2024-11-07 16:31:44

人脸识别(简单代码)的相关文章

安卓人人脸识别部分代码

package cn.facecore.facecoredemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import cn.facecore.facecoredemo.

iOS人脸识别核心代码(备用)

for (int i = 0; i < 1; i++) { //< [arr count]; i++) { CIFaceFeature *feature = [arr objectAtIndex:i]; double xPosition = (feature.leftEyePosition.x + feature.rightEyePosition.x+feature.mouthPosition.x)/(3*image.size.width) ; double yPosition = (feat

opencv人脸识别代码

opencv人脸识别C++代码 /* * Copyright (c) 2011,2012. Philipp Wagner <bytefish[at]gmx[dot]de>. * Released to public domain under terms of the BSD Simplified license. * * Redistribution and use in source and binary forms, with or without * modification, are

OpenCV人脸识别Eigen算法源码分析

1 理论基础 学习Eigen人脸识别算法需要了解一下它用到的几个理论基础,现总结如下: 1.1 协方差矩阵 首先需要了解一下公式: 共公式可以看出:均值描述的是样本集合的平均值,而标准差描述的则是样本集合的各个样本点到均值的距离之平均.以一个国家国民收入为例,均值反映了平均收入,而均方差/方差则反映了贫富差距,如果两个国家国民收入均值相等,则标准差越大说明国家的国民收入越不均衡,贫富差距较大.以上公式都是用来描述一维数据量的,把方差公式推广到二维,则可得到协方差公式: 协方差表明了两个随机变量之

人脸识别相关分享

人脸识别源代码 ※人脸检测(文章+程序)---技术文档及代码非常全『 人脸检测(文章+程序).rar (1.27 MB) 人脸检测(文章+程序).rar (1.27 MB) 下载次数: 12502 2010-12-21 12:26 』 ※完整的Matlab下人脸检测及识别系统源代码『 Face-Recognition-Detection.rar (393.19 KB) Face-Recognition-Detection.rar (393.19 KB) 下载次数: 11604 2010-12-2

基于百度AI开放平台的人脸识别及语音合成

基于百度AI的人脸识别及语音合成课题 课题需求 (1)人脸识别 在Web界面上传人的照片,后台使用Java技术接收图片,然后对图片进行解码,调用云平台接口识别人脸特征,接收平台返回的人员年龄.性别.颜值等信息,将信息返回到Web界面进行显示. (2)人脸比对 在Web界面上传两张人的照片,后台使用Java技术接收图片,然后对图片进行解码,调用云平台接口比对照片信息,返回相似度. (3)语音识别 在Web页面上传语音文件,判断语音文件格式,如果不是wav格式进行转码处理,然后调用平台接口进行识别,

OpenCV图像处理以及人脸识别

OpenCV基础 OpenCV是一个开源的计算机视觉库.提供了很多图像处理常用的工具 批注:本文所有图片数据都在我的GitHub仓库 读取图片并显示 import numpy as np import cv2 as cv original = cv.imread('../machine_learning_date/forest.jpg') cv.imshow('Original', original) 显示图片某个颜色通道的图像 blue = np.zeros_like(original) bl

Jetson (4)--- 人脸识别(OpenCV安装)

要让摄像头工作,还需要重新编译和安装OpenCV组件,因为Nvidia Jetpack自带安装了一个名为OpenCV4Tegra的OpenCV的特殊闭源版本,该版本针对Jetson进行了优化,并且比开源版本略快.虽然OpenCV4Tegra运行速度比普通OpenCV 2好,但其版本都不支持视频捕获gstreamer,因此我们无法轻松从中获取视频. 您可以使用正确的选项从源代码编译OpenCV3.x支持视频抓取. 我们将使用自编译的OpenCV 3替换OpenCV4Tegra. 推荐使用自动安装脚

dlib人脸识别

dlib人脸识别 1.dlib安装 ? 代码的编写在jupyter notebook中来完成 ? jupyter notebook是一个工具 ? pip install jupyter ------------>安装使用 ? 如何启动: ? 命令行输入:jupyter notebook ? 前提,环境变量配置成功 ? ? dlib安装-------------> pip install dlib ? dlib有不同的版本,最新版本(19.17.0),经过测试,dll包不完备,所以上次我在使用时