Skip to content

Commit

Permalink
refactor facedetection and add autoclose parameter to camera2 imageli…
Browse files Browse the repository at this point in the history
…stener
  • Loading branch information
pedroSG94 committed Oct 10, 2023
1 parent 3036969 commit 64a2b98
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ allprojects {
}
}
dependencies {
implementation 'com.github.pedroSG94.RootEncoder:library:2.2.8'
implementation 'com.github.pedroSG94.RootEncoder:library:2.2.9'
}
```
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ android {
dependencies {
implementation project(':library')
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.google.android.material:material:1.9.0'
implementation 'com.google.android.material:material:1.10.0'
}
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pedro.streamer">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ apply plugin: 'org.jetbrains.dokka'
buildscript {
ext.kotlin_version = '1.9.10'
ext.library_group = "com.github.pedroSG94"
ext.version_code = 228
ext.version_name = "2.2.8"
ext.version_code = 229
ext.version_name = "2.2.9"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import android.view.View;

import com.pedro.encoder.Frame;
import com.pedro.encoder.input.video.facedetector.FaceDetectorCallback;
import com.pedro.encoder.input.video.facedetector.UtilsKt;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -79,11 +81,6 @@ public class Camera1ApiManager implements Camera.PreviewCallback, Camera.FaceDet
private CameraCallbacks cameraCallbacks;
private final int focusAreaSize = 100;

//Face detector
public interface FaceDetectorCallback {
void onGetFaces(Camera.Face[] faces, Rect scaleSensor, int sensorOrientation);
}

private final int sensorOrientation = 0;
//Value obtained from Camera.Face documentation api about bounds
private final Rect faceSensorScale = new Rect(-1000, -1000, 1000, 1000);
Expand Down Expand Up @@ -734,7 +731,7 @@ public boolean isFaceDetectionEnabled() {

@Override
public void onFaceDetection(Camera.Face[] faces, Camera camera) {
if (faceDetectorCallback != null) faceDetectorCallback.onGetFaces(faces, faceSensorScale, sensorOrientation);
if (faceDetectorCallback != null) faceDetectorCallback.onGetFaces(UtilsKt.mapCamera1Faces(faces), faceSensorScale, sensorOrientation);
}

private Rect calculateFocusArea(float x, float y, float previewWidth, float previewHeight) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.pedro.encoder.input.video.facedetector.FaceDetectorCallback;
import com.pedro.encoder.input.video.facedetector.UtilsKt;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -81,7 +84,7 @@ public class Camera2ApiManager extends CameraDevice.StateCallback {
private SurfaceView surfaceView;
private TextureView textureView;
private Surface surfaceEncoder; //input surfaceEncoder from videoEncoder
private CameraManager cameraManager;
private final CameraManager cameraManager;
private Handler cameraHandler;
private CameraCaptureSession cameraCaptureSession;
private boolean prepared = false;
Expand All @@ -99,11 +102,6 @@ public class Camera2ApiManager extends CameraDevice.StateCallback {
private final Semaphore semaphore = new Semaphore(0);
private CameraCallbacks cameraCallbacks;

//Face detector
public interface FaceDetectorCallback {
void onGetFaces(Face[] faces, Rect scaleSensor, int sensorOrientation);
}

public interface ImageCallback {
void onImageAvailable(Image image);
}
Expand Down Expand Up @@ -787,8 +785,8 @@ private void prepareFaceDetectionCallback() {
public void onCaptureCompleted(@NonNull CameraCaptureSession session,
@NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
Face[] faces = result.get(CaptureResult.STATISTICS_FACES);
if (faceDetectorCallback != null) {
faceDetectorCallback.onGetFaces(faces, faceSensorScale, sensorOrientation);
if (faceDetectorCallback != null && faces != null) {
faceDetectorCallback.onGetFaces(UtilsKt.mapCamera2Faces(faces), faceSensorScale, sensorOrientation);
}
}
};
Expand Down Expand Up @@ -1008,7 +1006,7 @@ public void closeCamera(boolean resetSurface) {
running = false;
}

public void addImageListener(int width, int height, int format, int maxImages, ImageCallback listener) {
public void addImageListener(int width, int height, int format, int maxImages, boolean autoClose, ImageCallback listener) {
boolean wasRunning = running;
closeCamera(false);
if (wasRunning) closeCamera(false);
Expand All @@ -1020,7 +1018,7 @@ public void addImageListener(int width, int height, int format, int maxImages, I
Image image = reader.acquireLatestImage();
if (image != null) {
listener.onImageAvailable(image);
image.close();
if (autoClose) image.close();
}
}, new Handler(imageThread.getLooper()));
if (wasRunning) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.pedro.encoder.input.video.facedetector

import android.graphics.Point
import android.graphics.Rect

/**
* Created by pedro on 10/10/23.
*/
data class Face(
val id: Int?, //depend if device support it, if not supported the value could be -1
val leftEye: Point?, //depend if device support it
val rightEye: Point?, //depend if device support it
val mouth: Point?, //depend if device support it
val rect: Rect,
val score: Int //range 1 to 100
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.pedro.encoder.input.video.facedetector

import android.graphics.Rect

/**
* Created by pedro on 10/10/23.
*/
interface FaceDetectorCallback {
fun onGetFaces(faces: Array<Face>, scaleSensor: Rect?, sensorOrientation: Int)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
@file:Suppress("DEPRECATION")

package com.pedro.encoder.input.video.facedetector

import android.os.Build
import androidx.annotation.RequiresApi
import android.hardware.Camera.Face as Camera1Face
import android.hardware.camera2.params.Face as Camera2Face

/**
* Created by pedro on 10/10/23.
*/

fun Camera1Face.toFace(): Face {
return Face(
id = id,
leftEye = leftEye,
rightEye = rightEye,
mouth = mouth,
rect = rect,
score = score
)
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
fun Camera2Face.toFace(): Face {
return Face(
id = id,
leftEye = leftEyePosition,
rightEye = rightEyePosition,
mouth = mouthPosition,
rect = bounds,
score = score
)
}

fun mapCamera1Faces(faces: Array<Camera1Face>): Array<Face> {
return faces.map { it.toFace() }.toTypedArray()
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
fun mapCamera2Faces(faces: Array<Camera2Face>): Array<Face> {
return faces.map { it.toFace() }.toTypedArray()
}
4 changes: 2 additions & 2 deletions library/src/main/java/com/pedro/library/base/Camera1Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import androidx.annotation.RequiresApi;

import com.pedro.encoder.EncoderErrorCallback;
import com.pedro.encoder.Frame;
import com.pedro.encoder.audio.AudioEncoder;
import com.pedro.encoder.audio.GetAacData;
import com.pedro.encoder.input.audio.CustomAudioEffect;
Expand All @@ -47,6 +46,7 @@
import com.pedro.encoder.input.video.CameraHelper;
import com.pedro.encoder.input.video.CameraOpenException;
import com.pedro.encoder.input.video.GetCameraData;
import com.pedro.encoder.input.video.facedetector.FaceDetectorCallback;
import com.pedro.encoder.utils.CodecUtil;
import com.pedro.encoder.video.FormatVideoEncoder;
import com.pedro.encoder.video.GetVideoData;
Expand Down Expand Up @@ -198,7 +198,7 @@ public void setFpsListener(FpsListener.Callback callback) {
/**
* @return true if success, false if fail (not supported or called before start camera)
*/
public boolean enableFaceDetection(Camera1ApiManager.FaceDetectorCallback faceDetectorCallback) {
public boolean enableFaceDetection(FaceDetectorCallback faceDetectorCallback) {
return cameraManager.enableFaceDetection(faceDetectorCallback);
}

Expand Down
10 changes: 7 additions & 3 deletions library/src/main/java/com/pedro/library/base/Camera2Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import androidx.annotation.RequiresApi;

import com.pedro.encoder.EncoderErrorCallback;
import com.pedro.encoder.Frame;
import com.pedro.encoder.audio.AudioEncoder;
import com.pedro.encoder.audio.GetAacData;
import com.pedro.encoder.input.audio.CustomAudioEffect;
Expand All @@ -48,6 +47,7 @@
import com.pedro.encoder.input.video.CameraCallbacks;
import com.pedro.encoder.input.video.CameraHelper;
import com.pedro.encoder.input.video.CameraOpenException;
import com.pedro.encoder.input.video.facedetector.FaceDetectorCallback;
import com.pedro.encoder.utils.CodecUtil;
import com.pedro.encoder.video.FormatVideoEncoder;
import com.pedro.encoder.video.GetVideoData;
Expand Down Expand Up @@ -211,7 +211,7 @@ public void setFpsListener(FpsListener.Callback callback) {
/**
* @return true if success, false if fail (not supported or called before start camera)
*/
public boolean enableFaceDetection(Camera2ApiManager.FaceDetectorCallback faceDetectorCallback) {
public boolean enableFaceDetection(FaceDetectorCallback faceDetectorCallback) {
return cameraManager.enableFaceDetection(faceDetectorCallback);
}

Expand Down Expand Up @@ -1035,7 +1035,11 @@ public RecordController.Status getRecordStatus() {
}

public void addImageListener(int width, int height, int format, int maxImages, Camera2ApiManager.ImageCallback listener) {
cameraManager.addImageListener(width, height, format, maxImages, listener);
cameraManager.addImageListener(width, height, format, maxImages, true, listener);
}

public void addImageListener(int width, int height, int format, int maxImages, boolean autoClose, Camera2ApiManager.ImageCallback listener) {
cameraManager.addImageListener(width, height, format, maxImages, autoClose, listener);
}

public void addImageListener(int format, int maxImages, Camera2ApiManager.ImageCallback listener) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import java.util.regex.Pattern
*
* Class to create request to server and parse response from server.
*/
class CommandsManager {
open class CommandsManager {

var host: String? = null
private set
Expand Down

0 comments on commit 64a2b98

Please sign in to comment.