您的位置:首页 > 运维架构

[置顶] 使用opencv人脸识别对比两张人脸图片

2017-08-02 09:31 507 查看
先上效果图:



最近在做opencv进行人脸识别的项目,一直有一个问题没有解决,如图



这里就是采用





每次提示都是这里出错,后来去google了,说是javacv环境没有搭建。

接下来就是开发的步骤:

1、就搭建javacv环境,在android studio中的导入jar包如下:



tips:这里安装之后还需要给手机安装opencvmanager,之后我会慢慢更新如何不需要安装opencvmanager就可以直接使用,还在学习中。。。

2、给manifest文件添加权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


因为我是要读取手机存储的图片。

3、写布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.uj.myapplication.Main2Activity">

<ImageView
android:id="@+id/iv_show1"
android:layout_width="match_parent"
android:layout_height="200dp" />
<ImageView
android:id="@+id/iv_show2"
android:layout_width="match_parent"
android:layout_height="200dp" />

<TextView
android:id="@+id/cmp"
android:text="相似度"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>


就是两个简单的图片加载和一个显示相似度的文本框

4、整个activity方法

package com.example.uj.myapplication;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.bytedeco.javacpp.opencv_core;
import org.bytedeco.javacpp.opencv_core.IplImage;

import static org.bytedeco.javacpp.helper.opencv_imgproc.cvCalcHist;
import static org.bytedeco.javacpp.opencv_core.CV_HIST_ARRAY;
import static org.bytedeco.javacpp.opencv_imgcodecs.CV_LOAD_IMAGE_GRAYSCALE;
import static org.bytedeco.javacpp.opencv_imgcodecs.cvLoadImage;
import static org.bytedeco.javacpp.opencv_imgproc.CV_COMP_CORREL;
import static org.bytedeco.javacpp.opencv_imgproc.cvCompareHist;
import static org.bytedeco.javacpp.opencv_imgproc.cvNormalizeHist;

public class Main2Activity extends AppCompatActivity {

private ImageView imageViewShow1;
private ImageView imageViewShow2;
private TextView tv_cmp;
/**
* 第一张人脸的文件名
*/
private static final String FACE1 = "face1";
/**
* 第二张人脸的文件名
*/
private static final String FACE2 = "face2";

private String path1 = "sdcard/FaceDetect/12.jpg";
private String path2 = "sdcard/FaceDetect/13.jpg";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

imageViewShow1 = (ImageView) findViewById(R.id.iv_show1);
imageViewShow2 = (ImageView) findViewById(R.id.iv_show2);
tv_cmp = (TextView) findViewById(R.id.cmp);

imageViewShow1.setImageBitmap(BitmapFactory.decodeFile(path1));
imageViewShow2.setImageBitmap(BitmapFactory.decodeFile(path2));

double result = CmpPic(path1,path2);

tv_cmp.setText("相似度为: "+result);
Toast.makeText(this, "相似度为: "+result, Toast.LENGTH_SHORT).show();

}

/**
* 特征对比
* 对比的两张图片必须是灰度图
*
* @param file1 人脸特征
* @param file2 人脸特征
* @return 相似度
*/
public double CmpPic(String file1, String file2) {

int l_bins = 20;
int hist_size[] = {l_bins};

float v_ranges[] = {0, 100};
float ranges[][] = {v_ranges};

opencv_core.IplImage Image1 = cvLoadImage(file1, CV_LOAD_IMAGE_GRAYSCALE);
opencv_core.IplImage Image2 = cvLoadImage(file2, CV_LOAD_IMAGE_GRAYSCALE);

IplImage imageArr1[] = {Image1};
IplImage imageArr2[] = {Image2};

opencv_core.CvHistogram Histogram1 = opencv_core.CvHistogram.create(1, hist_size, CV_HIST_ARRAY, ranges, 1);
opencv_core.CvHistogram Histogram2 = opencv_core.CvHistogram.create(1, hist_size, CV_HIST_ARRAY, ranges, 1);

cvCalcHist(imageArr1, Histogram1, 0, null);
cvCalcHist(imageArr2, Histogram2, 0, null);

cvNormalizeHist(Histogram1, 100.0);
cvNormalizeHist(Histogram2, 100.0);

return cvCompareHist(Histogram1, Histogram2, CV_COMP_CORREL);
}

/**
* 提取特征
*
* @param context  Context
* @param fileName 文件名
* @return 特征图片
*/
public Bitmap getImage(Context context, String fileName) {
String filePath = getFilePath(context, fileName);
if (TextUtils.isEmpty(filePath)) {
return null;
} else {
return BitmapFactory.decodeFile(filePath);
}
}

/**
* 获取人脸特征路径
*
* @param fileName 人脸特征的图片的名字
* @return 路径
*/
private String getFilePath(Context context, String fileName) {
if (TextUtils.isEmpty(fileName)) {
return null;
}
// 内存路径
//        return context.getApplicationContext().getFilesDir().getPath() + fileName + ".jpg";
// 内存卡路径 需要SD卡读取权限
return Environment.getExternalStorageDirectory() + "/FaceDetect/" + fileName + ".jpg";
}

}


这里就是加载两张本地的图片进行对比,其他的都还没有添加进去,还有待改进。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: