Browse Source

Fixed Grey Stripes and Rotation Problems

bk_video
Bastian Kohler 1 year ago
parent
commit
493fa8ca83

+ 25
- 6
app/src/main/java/com/example/ueberwachungssystem/Detection/VideoDetector.java View File

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;


// Camera Provider // Camera Provider
private ProcessCameraProvider cameraProvider; private ProcessCameraProvider cameraProvider;
private final ImageAnalysis imageAnalysis;
private final VideoCapture videoCapture;
private ImageAnalysis imageAnalysis;
private VideoCapture videoCapture;
private final Preview preview; private final Preview preview;


// Logic // Logic
private ImageView inputImageView = null; private ImageView inputImageView = null;
private ImageView outputImageView = null; private ImageView outputImageView = null;



// Recording
private final String outputName = "video.mp4";
private int rotation = 0;




// Parameters // Parameters
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!");
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 -> {


// 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;


@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();
} }


return; return;
} }


videoCapture = setupVideoCapture();

final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context); final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
cameraProviderFuture.addListener(() -> { cameraProviderFuture.addListener(() -> {
try { try {
// Show Output Image // Show Output Image
if (outputImageView != null) if (outputImageView != null)
OpenCVHelper.debugMat(processed, outputImageView); OpenCVHelper.debugMat(processed, outputImageView);

return processed; return processed;
} }


// 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();
}
} }

+ 1
- 0
app/src/main/java/com/example/ueberwachungssystem/MainActivity.java View File

if (toggleButton.isChecked()) if (toggleButton.isChecked())
{ {
//vd.startDetection(); //vd.startDetection();
vd.stopDetection();
vd.startRecording(); vd.startRecording();
audioRecorder.startRecording(); audioRecorder.startRecording();
} }

+ 5
- 2
app/src/main/res/layout/activity_main.xml View File



<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…
Cancel
Save