在视频捕捉期间跟踪/检测面部。我已经能够捕获并存储视频了。现在我在其中添加面部检测。我现在正在视频捕捉期间检测脸部。 如何检查facedetect是否正常工作。我可以做出哪些改变:
public class AndroidVideoCaptureExample extends Activity {
private Camera mCamera;
private int vWidth, vHeight;
private CameraPreview mPreview;
private MediaRecorder mediaRecorder;
private Button capture, switchCamera;
private Context myContext;
private LinearLayout cameraPreview;
private boolean cameraFront = false;
private int desiredwidth = 640, desiredheight = 360;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
myContext = this;
initialize();
}
private int findFrontFacingCamera() {
int cameraId = -1;
// Search for the front facing camera
int numberOfCameras = Camera.getNumberOfCameras();
for (int i = 0; i < numberOfCameras; i++) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
cameraId = i;
cameraFront = true;
break;
}
}
return cameraId;
}
public void onResume() {
super.onResume();
if (!hasCamera(myContext)) {
Toast toast = Toast.makeText(myContext, "Sorry, your phone does not have a camera!", Toast.LENGTH_LONG);
toast.show();
finish();
}
if (mCamera == null) {
// if the front facing camera does not exist
if (findFrontFacingCamera() < 0) {
Toast.makeText(this, "No front facing camera found.", Toast.LENGTH_LONG).show();
switchCamera.setVisibility(View.GONE);
}
mCamera = Camera.open(findFrontFacingCamera());
mCamera.setFaceDetectionListener(new FaceDetectionListener());
mCamera.setDisplayOrientation(90);
mPreview.refreshCamera(mCamera);
startFaceDetection();
}
}
public void initialize() {
cameraPreview = (LinearLayout) findViewById(R.id.camera_preview);
mPreview = new CameraPreview(myContext, mCamera);
cameraPreview.addView(mPreview);
capture = (Button) findViewById(R.id.button_capture);
capture.setOnClickListener(captrureListener);
}
@Override
protected void onPause() {
super.onPause();
// when on Pause, release camera in order to be used from other
// applications
releaseCamera();
}
private boolean hasCamera(Context context) {
// check if the device has camera
if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
return true;
} else {
return false;
}
}
boolean recording = false;
OnClickListener captrureListener = new OnClickListener() {@Override
public void onClick(View v) {
if (recording) {
// stop recording and release camera
mediaRecorder.stop(); // stop the recording
releaseMediaRecorder(); // release the MediaRecorder object
Toast.makeText(AndroidVideoCaptureExample.this, "Video captured!", Toast.LENGTH_LONG).show();
Toast.makeText(AndroidVideoCaptureExample.this, vWidth + "BY" + vHeight, Toast.LENGTH_LONG).show();
recording = false;
} else {
if (!prepareMediaRecorder()) {
Toast.makeText(AndroidVideoCaptureExample.this, "Failed in prepareMediaRecorder()!\n - Ended -", Toast.LENGTH_LONG).show();
finish();
}
// work on UiThread for better performance
runOnUiThread(new Runnable() {
public void run() {
// If there are stories, add them to the table
try {
mediaRecorder.start();
} catch (final Exception ex) {
// Log.i("---","Exception in thread");
}
}
});
recording = true;
}
}
};
private void releaseMediaRecorder() {
if (mediaRecorder != null) {
mediaRecorder.reset(); // clear recorder configuration
mediaRecorder.release(); // release the recorder object
mediaRecorder = null;
mCamera.lock(); // lock camera for later use
}
}
private boolean prepareMediaRecorder() {
List < Camera.Size > videosizes = mCamera.getParameters().getSupportedVideoSizes();
Camera.Size videosize = videosizes.get(1);
//Camera.Size optimalPreviewSize = getOptimalPreviewSize(previewsizes, desiredwidth, desiredheight);
Camera.Size optimalVideoSize = getOptimalPreviewSize(videosize, desiredwidth, desiredheight);
vWidth = optimalVideoSize.width; //mCamera.getParameters().getPreviewSize().width;
vHeight = optimalVideoSize.height; //mCamera.getParameters().getPreviewSize().height;
mediaRecorder = new MediaRecorder();
mCamera.unlock();
mediaRecorder.setCamera(mCamera);
//startFaceDetection();
//mPreview.setPreviewSize(optimalPreviewSize.width, optimalPreviewSize.height);
//mCamera.unlock();
//mMediaRecorder = new MediaRecorder();
//mMediaRecorder.setCamera(mCamera);
//mMediaRecorder.setVideoSize(optimalVideoSize.width, optimalVideoSize.height);
//myCamera.setParameters(p);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));
mediaRecorder.setVideoEncodingBitRate(512 * 1000);
mediaRecorder.setVideoFrameRate(15);
mediaRecorder.setVideoSize(optimalVideoSize.width, optimalVideoSize.height);
mediaRecorder.setOutputFile("/sdcard/myvideo.mp4");
mediaRecorder.setMaxDuration(600000); // Set max duration 60 sec.
mediaRecorder.setMaxFileSize(50000000); // Set max file size 50M
try {
mediaRecorder.prepare();
} catch (IllegalStateException e) {
releaseMediaRecorder();
return false;
} catch (IOException e) {
releaseMediaRecorder();
return false;
}
return true;
}
private void releaseCamera() {
// stop and release camera
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
private Camera.Size getOptimalPreviewSize(Camera.Size sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.2;
double targetRatio = (double) w / h;
if (sizes == null) return null;
Camera.Size optimalSize = null;
double minDiff = Double.MAX_VALUE;
int targetHeight = h;
// Try to find an size match aspect ratio and size
Camera.Size size = sizes;
Log.d("Camera", "Checking size " + size.width + "w " + size.height + "h");
double ratio = (double) size.width / size.height;
if (Math.abs(ratio - targetRatio) <= ASPECT_TOLERANCE) 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;
if (Math.abs(size.height - targetHeight) < minDiff) {
optimalSize = size;
minDiff = Math.abs(size.height - targetHeight);
}
}
return optimalSize;
}
public void startFaceDetection() {
// Try starting Face Detection
Camera.Parameters params = mCamera.getParameters();
// start face detection only *after* preview has started
if (params.getMaxNumDetectedFaces() > 0) {
// camera supports face detection, so can start it:
mCamera.startFaceDetection();
}
}
这是面部检测监听器类:
public class FaceDetectionListener implements Camera.FaceDetectionListener {
@Override
public void onFaceDetection(Camera.Face[] faces, Camera c)
{
if (faces.length > 0){
Log.d("FaceDetection", "face detected: " + faces.length +
" Face 1 Location X: " + faces[0].rect.centerX() +
"Y: " + faces[0].rect.centerY());
}
}
}
答案 0 :(得分:-2)
我使用Mobile Vision API进行人脸检测实时视频,你可以搜索Mobile Vision API进行人脸检测,它会工作正常, 但是我没有任何方法可以捕获“检测到的脸部”并将此检测到的脸部保存在“实时视频”库中