JavaCV实现人脸检测功能

这篇文章主要为大家详细介绍了JavaCV实现人脸检测功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了JavaCV实现人脸检测功能的具体代码,供大家参考,具体内容如下

 /* * Copyright (C) 2010,2011,2012 Samuel Audet * * FacePreview - A fusion of OpenCV's facedetect and Android's CameraPreview samples, *        with JavaCV + JavaCPP as the glue in between. * * This file was based on CameraPreview.java that came with the Samples for * Android SDK API 8, revision 1 and contained the following copyright notice: * * Copyright (C) 2007 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *   http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * * IMPORTANT - Make sure the AndroidManifest.xml file looks like this: * *  *  *    *    *    *    *      *        *          *          *        *      *    *  */ package com.googlecode.javacv.facepreview; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.ImageFormat; import android.graphics.Paint; import android.hardware.Camera; import android.hardware.Camera.Size; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.FrameLayout; import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; import java.util.List; import com.googlecode.javacpp.Loader; import com.googlecode.javacv.cpp.opencv_objdetect; import static com.googlecode.javacv.cpp.opencv_core.*; import static com.googlecode.javacv.cpp.opencv_imgproc.*; import static com.googlecode.javacv.cpp.opencv_objdetect.*; import static com.googlecode.javacv.cpp.opencv_highgui.*; // ---------------------------------------------------------------------- public class FacePreview extends Activity { private FrameLayout layout; private FaceView faceView; private Preview mPreview; @Override protected void onCreate(Bundle savedInstanceState) { // Hide the window title. requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); // Create our Preview view and set it as the content of our activity. try { layout = new FrameLayout(this); faceView = new FaceView(this); mPreview = new Preview(this, faceView); layout.addView(mPreview); layout.addView(faceView); setContentView(layout); } catch (IOException e) { e.printStackTrace(); new AlertDialog.Builder(this).setMessage(e.getMessage()).create().show(); } } } // ---------------------------------------------------------------------- class FaceView extends View implements Camera.PreviewCallback { public static final int SUBSAMPLING_FACTOR = 4; private IplImage grayImage; private CvHaarClassifierCascade classifier; private CvMemStorage storage; private CvSeq faces; public FaceView(FacePreview context) throws IOException { super(context); // Load the classifier file from Java resources. File classifierFile = Loader.extractResource(getClass(), "/com/googlecode/javacv/facepreview/haarcascade_frontalface_alt2.xml", context.getCacheDir(), "classifier", ".xml"); if (classifierFile == null || classifierFile.length() <= 0) { throw new IOException("Could not extract the classifier file from Java resource."); } // Preload the opencv_objdetect module to work around a known bug. Loader.load(opencv_objdetect.class); classifier = new CvHaarClassifierCascade(cvLoad(classifierFile.getAbsolutePath())); classifierFile.delete(); if (classifier.isNull()) { throw new IOException("Could not load the classifier file."); } storage = CvMemStorage.create(); } public void onPreviewFrame(final byte[] data, final Camera camera) { try { Camera.Size size = camera.getParameters().getPreviewSize(); processImage(data, size.width, size.height); camera.addCallbackBuffer(data); } catch (RuntimeException e) { // The camera has probably just been released, ignore. } } protected void processImage(byte[] data, int width, int height) { // First, downsample our image and convert it into a grayscale IplImage int f = SUBSAMPLING_FACTOR; if (grayImage == null || grayImage.width() != width/f || grayImage.height() != height/f) { grayImage = IplImage.create(width/f, height/f, IPL_DEPTH_8U, 1); } int imageWidth = grayImage.width(); int imageHeight = grayImage.height(); int dataStride = f*width; int imageStride = grayImage.widthStep(); ByteBuffer imageBuffer = grayImage.getByteBuffer(); for (int y = 0; y  sizes, int w, int h) { final double ASPECT_TOLERANCE = 0.05; double targetRatio = (double) w / h; if (sizes == null) return null; Size optimalSize = null; double minDiff = Double.MAX_VALUE; int targetHeight = h; // Try to find an size match aspect ratio and size for (Size size : sizes) { double ratio = (double) size.width / size.height; if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; if (Math.abs(size.height - targetHeight)  sizes = parameters.getSupportedPreviewSizes(); Size optimalSize = getOptimalPreviewSize(sizes, w, h); parameters.setPreviewSize(optimalSize.width, optimalSize.height); mCamera.setParameters(parameters); if (previewCallback != null) { mCamera.setPreviewCallbackWithBuffer(previewCallback); Camera.Size size = parameters.getPreviewSize(); byte[] data = new byte[size.width*size.height* ImageFormat.getBitsPerPixel(parameters.getPreviewFormat())/8]; mCamera.addCallbackBuffer(data); } mCamera.startPreview(); } } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持html中文网。

以上就是JavaCV实现人脸检测功能的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java