Fixed Grey Stripes and Rotation Problems
This commit is contained in:
parent
6954d38143
commit
493fa8ca83
@ -6,15 +6,21 @@ import android.app.Activity;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.graphics.ImageFormat;
|
import android.graphics.ImageFormat;
|
||||||
|
import android.graphics.Rect;
|
||||||
|
import android.hardware.SensorManager;
|
||||||
import android.media.Image;
|
import android.media.Image;
|
||||||
import android.os.CountDownTimer;
|
import android.os.CountDownTimer;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.view.Display;
|
||||||
|
import android.view.OrientationEventListener;
|
||||||
import android.view.Surface;
|
import android.view.Surface;
|
||||||
|
import android.view.WindowManager;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.camera.core.AspectRatio;
|
||||||
import androidx.camera.core.CameraSelector;
|
import androidx.camera.core.CameraSelector;
|
||||||
import androidx.camera.core.ExperimentalGetImage;
|
import androidx.camera.core.ExperimentalGetImage;
|
||||||
import androidx.camera.core.ImageAnalysis;
|
import androidx.camera.core.ImageAnalysis;
|
||||||
@ -57,8 +63,8 @@ public class VideoDetector extends Detector {
|
|||||||
|
|
||||||
// Camera Provider
|
// Camera Provider
|
||||||
private ProcessCameraProvider cameraProvider;
|
private ProcessCameraProvider cameraProvider;
|
||||||
private final ImageAnalysis imageAnalysis;
|
private ImageAnalysis imageAnalysis;
|
||||||
private final VideoCapture videoCapture;
|
private VideoCapture videoCapture;
|
||||||
private final Preview preview;
|
private final Preview preview;
|
||||||
|
|
||||||
// Logic
|
// Logic
|
||||||
@ -73,9 +79,7 @@ public class VideoDetector extends Detector {
|
|||||||
private ImageView inputImageView = null;
|
private ImageView inputImageView = null;
|
||||||
private ImageView outputImageView = null;
|
private ImageView outputImageView = null;
|
||||||
|
|
||||||
|
private int rotation = 0;
|
||||||
// Recording
|
|
||||||
private final String outputName = "video.mp4";
|
|
||||||
|
|
||||||
|
|
||||||
// Parameters
|
// Parameters
|
||||||
@ -120,6 +124,9 @@ public class VideoDetector extends Detector {
|
|||||||
getPermissions();
|
getPermissions();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
imageAnalysis = setupImageAnalysis();
|
||||||
|
|
||||||
// Open CV startup check
|
// Open CV startup check
|
||||||
if (!OpenCVLoader.initDebug()) {
|
if (!OpenCVLoader.initDebug()) {
|
||||||
Log.e("OpenCV", "Unable to load OpenCV!");
|
Log.e("OpenCV", "Unable to load OpenCV!");
|
||||||
@ -191,6 +198,7 @@ public class VideoDetector extends Detector {
|
|||||||
builder.setTargetResolution(IMAGE_RES);
|
builder.setTargetResolution(IMAGE_RES);
|
||||||
builder.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST);
|
builder.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST);
|
||||||
builder.setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_YUV_420_888);
|
builder.setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_YUV_420_888);
|
||||||
|
builder.setTargetRotation(Surface.ROTATION_90);
|
||||||
ImageAnalysis imageAnalysis = builder.build();
|
ImageAnalysis imageAnalysis = builder.build();
|
||||||
// Set Analyzer
|
// Set Analyzer
|
||||||
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(context), imageProxy -> {
|
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(context), imageProxy -> {
|
||||||
@ -200,6 +208,7 @@ public class VideoDetector extends Detector {
|
|||||||
|
|
||||||
// Violation Handling
|
// Violation Handling
|
||||||
Mat processed = processImage(imageProxy);
|
Mat processed = processImage(imageProxy);
|
||||||
|
|
||||||
int n = OpenCVHelper.countNonZeroPixels(processed);
|
int n = OpenCVHelper.countNonZeroPixels(processed);
|
||||||
int pixelCount = image.getWidth() * image.getHeight();
|
int pixelCount = image.getWidth() * image.getHeight();
|
||||||
float percentChanged = (float) n / pixelCount;
|
float percentChanged = (float) n / pixelCount;
|
||||||
@ -217,8 +226,9 @@ public class VideoDetector extends Detector {
|
|||||||
|
|
||||||
@SuppressLint("RestrictedApi")
|
@SuppressLint("RestrictedApi")
|
||||||
private VideoCapture setupVideoCapture() {
|
private VideoCapture setupVideoCapture() {
|
||||||
|
int rotation = getRotation();
|
||||||
return new VideoCapture.Builder()
|
return new VideoCapture.Builder()
|
||||||
.setTargetRotation(Surface.ROTATION_0)
|
.setTargetRotation(rotation)
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,6 +245,8 @@ public class VideoDetector extends Detector {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
videoCapture = setupVideoCapture();
|
||||||
|
|
||||||
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
||||||
cameraProviderFuture.addListener(() -> {
|
cameraProviderFuture.addListener(() -> {
|
||||||
try {
|
try {
|
||||||
@ -307,6 +319,7 @@ public class VideoDetector extends Detector {
|
|||||||
// Show Output Image
|
// Show Output Image
|
||||||
if (outputImageView != null)
|
if (outputImageView != null)
|
||||||
OpenCVHelper.debugMat(processed, outputImageView);
|
OpenCVHelper.debugMat(processed, outputImageView);
|
||||||
|
|
||||||
return processed;
|
return processed;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -329,4 +342,10 @@ public class VideoDetector extends Detector {
|
|||||||
// Return the timestamp as a string
|
// Return the timestamp as a string
|
||||||
return currentTime.format(formatter);
|
return currentTime.format(formatter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int getRotation() {
|
||||||
|
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||||
|
Display display = windowManager.getDefaultDisplay();
|
||||||
|
return display.getRotation();
|
||||||
|
}
|
||||||
}
|
}
|
@ -53,6 +53,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
if (toggleButton.isChecked())
|
if (toggleButton.isChecked())
|
||||||
{
|
{
|
||||||
//vd.startDetection();
|
//vd.startDetection();
|
||||||
|
vd.stopDetection();
|
||||||
vd.startRecording();
|
vd.startRecording();
|
||||||
audioRecorder.startRecording();
|
audioRecorder.startRecording();
|
||||||
}
|
}
|
||||||
|
@ -24,15 +24,18 @@
|
|||||||
|
|
||||||
<ImageView
|
<ImageView
|
||||||
android:id="@+id/inputImageView"
|
android:id="@+id/inputImageView"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
tools:srcCompat="@tools:sample/avatars" />
|
tools:srcCompat="@tools:sample/avatars" />
|
||||||
|
|
||||||
<ImageView
|
<ImageView
|
||||||
android:id="@+id/outputImageView"
|
android:id="@+id/outputImageView"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
tools:srcCompat="@tools:sample/avatars" />
|
tools:srcCompat="@tools:sample/avatars" />
|
||||||
|
|
||||||
|
<androidx.camera.view.PreviewView
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
Loading…
x
Reference in New Issue
Block a user