时间:2021-05-19
本文实例为大家分享了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:///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 < imageHeight; y++) { int dataLine = y*dataStride; int imageLine = y*imageStride; for (int x = 0; x < imageWidth; x++) { imageBuffer.put(imageLine + x, data[dataLine + f*x]); } } IplImage grayImageT = IplImage.create(height/f, width/f, IPL_DEPTH_8U, 1); //cvSaveImage("/storage/emulated/0/Pictures/grayImage.jpg",grayImage); cvTranspose(grayImage,grayImageT); //cvSaveImage("/storage/emulated/0/Pictures/grayImageT.jpg",grayImageT); cvFlip(grayImageT,grayImageT,0); //cvSaveImage("/storage/emulated/0/Pictures/grayImageT_X.jpg",grayImageT); cvFlip(grayImageT,grayImageT,1); //cvSaveImage("/storage/emulated/0/Pictures/grayImageT_Y.jpg",grayImageT); cvClearMemStorage(storage); faces = cvHaarDetectObjects(grayImageT, classifier, storage, 1.1, 3, CV_HAAR_DO_CANNY_PRUNING); postInvalidate(); } @Override protected void onDraw(Canvas canvas) { Paint paint = new Paint(); paint.setColor(Color.RED); paint.setTextSize(20); String s = "FacePreview - This side up."; float textWidth = paint.measureText(s); canvas.drawText(s, (getWidth()-textWidth)/2, 20, paint); if (faces != null) { paint.setStrokeWidth(2); paint.setStyle(Paint.Style.STROKE); float scaleX = (float)getWidth()/grayImage.width(); float scaleY = (float)getHeight()/grayImage.height(); int total = faces.total(); for (int i = 0; i < total; i++) { CvRect r = new CvRect(cvGetSeqElem(faces, i)); int x = r.x(), y = r.y(), w = r.width(), h = r.height(); canvas.drawRect(x*scaleX, y*scaleY, (x+w)*scaleX, (y+h)*scaleY, paint); } } else{ canvas.drawText("meiyoujiancedao", (getWidth()-textWidth)/2, 20, paint); } } } // ---------------------------------------------------------------------- class Preview extends SurfaceView implements SurfaceHolder.Callback { SurfaceHolder mHolder; Camera mCamera; Camera.PreviewCallback previewCallback; Preview(Context context, Camera.PreviewCallback previewCallback) { super(context); this.previewCallback = previewCallback; // Install a SurfaceHolder.Callback so we get notified when the // underlying surface is created and destroyed. mHolder = getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } public void surfaceCreated(SurfaceHolder holder) { // The Surface has been created, acquire the camera and tell it where // to draw. mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT); try { mCamera.setPreviewDisplay(holder); } catch (IOException exception) { mCamera.release(); mCamera = null; // TODO: add more exception handling logic here } } public void surfaceDestroyed(SurfaceHolder holder) { // Surface will be destroyed when we return, so stop the preview. // Because the CameraDevice object is not a shared resource, it's very // important to release it when the activity is paused. mCamera.stopPreview(); mCamera.release(); mCamera = null; } private Size getOptimalPreviewSize(List<Size> 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) < minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } // Cannot find the one match the aspect ratio, ignore the requirement if (optimalSize == null) { minDiff = Double.MAX_VALUE; for (Size size : sizes) { if (Math.abs(size.height - targetHeight) < minDiff) { optimalSize = size; minDiff = Math.abs(size.height - targetHeight); } } } return optimalSize; } public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { // Now that the size is known, set up the camera parameters and begin // the preview. Camera.Parameters parameters = mCamera.getParameters(); List<Size> 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(); } }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
项目中需要实现人脸登陆功能,实现思路为在前端检测人脸,把人脸照片发送到后端识别,返回用户token登陆成功前端调用摄像头使用tracking.js检测视频流中的
OpenCV的人脸检测功能在一般场合还是不错的。而ubuntu正好提供了python-opencv这个包,用它可以方便地实现人脸检测的代码。写代码之前应该先安装
本文实例为大家分享了OpenCV实现人脸检测功能的具体代码,供大家参考,具体内容如下1、HAAR级联检测#include#includeusingnamespa
1.引言在某些场景下,我们不仅需要进行实时人脸检测追踪,还要进行再加工;这里进行摄像头实时人脸检测,并对于实时检测的人脸进行初步提取;单个/多个人脸检测,并依次
retinaface人脸检测算法甜点最近一直了解人脸检测的算法,所以也尝试学多人脸检测框架。所以这里将拿出来和大家分享一下Retinaface与普通的目标检测算