Compare commits
3
Commits
f329fab62d
..
tw
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c60a14b6ac | ||
|
|
4381ae78f8 | ||
|
|
81e04e370a |
No files matched your search
Whitespace-only changes.
@@ -36,22 +36,5 @@ dependencies {
|
|||||||
testImplementation 'junit:junit:4.13.2'
|
testImplementation 'junit:junit:4.13.2'
|
||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
||||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
||||||
|
|
||||||
|
|
||||||
// OpenCV (from: https://github.com/QuickBirdEng/opencv-android)
|
|
||||||
def opencv_version = "4.5.3.0"
|
|
||||||
implementation "com.quickbirdstudios:opencv:${opencv_version}"
|
|
||||||
|
|
||||||
|
|
||||||
// Required for CameraX
|
|
||||||
def camerax_version = "1.2.2"
|
|
||||||
implementation "androidx.camera:camera-core:${camerax_version}"
|
|
||||||
implementation "androidx.camera:camera-camera2:${camerax_version}"
|
|
||||||
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
|
|
||||||
implementation "androidx.camera:camera-video:${camerax_version}"
|
|
||||||
implementation "androidx.camera:camera-view:${camerax_version}"
|
|
||||||
implementation "androidx.camera:camera-extensions:${camerax_version}"
|
|
||||||
|
|
||||||
|
|
||||||
implementation 'com.jjoe64:graphview:4.2.2'
|
implementation 'com.jjoe64:graphview:4.2.2'
|
||||||
}
|
}
|
||||||
@@ -2,10 +2,8 @@
|
|||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
<uses-feature android:name="android.hardware.camera"/>
|
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
|
||||||
<uses-permission android:name="android.permission.CAMERA"/>
|
<uses-permission android:name="android.permission.CAMERA"/>
|
||||||
<uses-permission android:name="android.permission.RECORD_AUDIO" />
|
|
||||||
|
|
||||||
<!--<uses-permission android:name="android.permission.BLUETOOTH"/>-->
|
<!--<uses-permission android:name="android.permission.BLUETOOTH"/>-->
|
||||||
<!--<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>-->
|
<!--<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>-->
|
||||||
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
package com.example.ueberwachungssystem.Detection;
|
|
||||||
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import java.util.Calendar;
|
|
||||||
|
|
||||||
/** Detection Report Class */
|
|
||||||
public class DetectionReport {
|
|
||||||
public String timeStamp;
|
|
||||||
public String detectionType;
|
|
||||||
public float detectedValue;
|
|
||||||
public boolean detectionState;
|
|
||||||
|
|
||||||
public DetectionReport(boolean detectionState, String detectionType, float detectedAmplitude) {
|
|
||||||
this.timeStamp = String.valueOf(Calendar.getInstance().getTime());
|
|
||||||
this.detectionType = detectionType;
|
|
||||||
this.detectedValue = detectedAmplitude;
|
|
||||||
this.detectionState = detectionState;
|
|
||||||
|
|
||||||
//this.detectorID = detectorID;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/** Get Detection Report in String format */
|
|
||||||
public String toString() {
|
|
||||||
String state = "State: " + "[" + this.detectionState + "]";
|
|
||||||
String time = "Time: " + "[" + this.timeStamp + "]";
|
|
||||||
String type = "Type: " + "[" + this.detectionType + "]";
|
|
||||||
String value = "Value: " + "[" + this.detectedValue + "]";
|
|
||||||
|
|
||||||
return String.join("\t", state, time, type, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Debug Report */
|
|
||||||
public void log(String tag) {
|
|
||||||
Log.d(tag, this.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
package com.example.ueberwachungssystem.Detection;
|
|
||||||
|
|
||||||
import android.os.CountDownTimer;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
|
|
||||||
|
|
||||||
abstract public class Detector {
|
|
||||||
private OnDetectionListener listener;
|
|
||||||
private boolean isDetecting = false;
|
|
||||||
private boolean extendViolation = false;
|
|
||||||
|
|
||||||
// Countdown parameters
|
|
||||||
private final int COUNTDOWN_TIME = 5000; // milliseconds
|
|
||||||
private final int COUNTDOWN_POLLING_TIME = 100; // milliseconds
|
|
||||||
|
|
||||||
/** Constructor - takes context of current activity */
|
|
||||||
public Detector() {}
|
|
||||||
|
|
||||||
|
|
||||||
/** On Detection Listener - runs when violation is reported */
|
|
||||||
public interface OnDetectionListener {
|
|
||||||
void onDetection(@NonNull DetectionReport detectionReport);
|
|
||||||
}
|
|
||||||
public void setOnDetectionListener(@NonNull OnDetectionListener listener) {
|
|
||||||
this.listener = listener;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Triggers onDetectionListener - call this to trigger violation/alarm */
|
|
||||||
public void reportViolation(String detectionType, float amplitude) {
|
|
||||||
if (listener != null) {
|
|
||||||
if (!isDetecting) {
|
|
||||||
isDetecting = true;
|
|
||||||
DetectionReport detectionReport = new DetectionReport(true, detectionType, amplitude);
|
|
||||||
listener.onDetection(detectionReport);
|
|
||||||
startDetectionTimer(detectionType, amplitude);
|
|
||||||
} else {
|
|
||||||
extendViolation = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
isDetecting = false;
|
|
||||||
extendViolation = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void startDetectionTimer(String detectionType, float amplitude) {
|
|
||||||
isDetecting = true;
|
|
||||||
new CountDownTimer((long) COUNTDOWN_TIME, COUNTDOWN_POLLING_TIME) {
|
|
||||||
@Override
|
|
||||||
public void onTick(long millisUntilFinished) {
|
|
||||||
if (extendViolation) {
|
|
||||||
extendViolation = false;
|
|
||||||
startDetectionTimer(detectionType, amplitude);
|
|
||||||
this.cancel();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void onFinish() {
|
|
||||||
isDetecting = false;
|
|
||||||
DetectionReport detectionReport = new DetectionReport(false, detectionType, amplitude);
|
|
||||||
listener.onDetection(detectionReport);
|
|
||||||
}
|
|
||||||
}.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void extendViolation(){
|
|
||||||
this.extendViolation = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Starts Detection (abstract method: needs to be overridden in child class) */
|
|
||||||
public abstract void startDetection();
|
|
||||||
|
|
||||||
/** Stops Detection (abstract method: needs to be overridden in child class) */
|
|
||||||
public abstract void stopDetection();
|
|
||||||
}
|
|
||||||
@@ -1,441 +0,0 @@
|
|||||||
package com.example.ueberwachungssystem.Detection;
|
|
||||||
|
|
||||||
import android.Manifest;
|
|
||||||
import android.annotation.SuppressLint;
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.Context;
|
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.graphics.Bitmap;
|
|
||||||
import android.graphics.ImageFormat;
|
|
||||||
import android.media.Image;
|
|
||||||
import android.os.CountDownTimer;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.view.Surface;
|
|
||||||
import android.widget.ImageView;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.camera.core.CameraSelector;
|
|
||||||
import androidx.camera.core.ExperimentalGetImage;
|
|
||||||
import androidx.camera.core.ImageAnalysis;
|
|
||||||
import androidx.camera.core.ImageProxy;
|
|
||||||
import androidx.camera.core.Preview;
|
|
||||||
import androidx.camera.core.VideoCapture;
|
|
||||||
import androidx.camera.lifecycle.ProcessCameraProvider;
|
|
||||||
import androidx.camera.view.PreviewView;
|
|
||||||
import androidx.core.app.ActivityCompat;
|
|
||||||
import androidx.core.content.ContextCompat;
|
|
||||||
import androidx.lifecycle.LifecycleOwner;
|
|
||||||
|
|
||||||
import com.google.common.util.concurrent.ListenableFuture;
|
|
||||||
|
|
||||||
import org.opencv.android.OpenCVLoader;
|
|
||||||
import org.opencv.android.Utils;
|
|
||||||
import org.opencv.core.Core;
|
|
||||||
import org.opencv.core.CvType;
|
|
||||||
import org.opencv.core.Mat;
|
|
||||||
import org.opencv.core.MatOfPoint;
|
|
||||||
import org.opencv.core.Scalar;
|
|
||||||
import org.opencv.core.Size;
|
|
||||||
import org.opencv.imgproc.Imgproc;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.ExecutionException;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Video Detector inherits some methods from abstract Detector class (more info there)
|
|
||||||
* USE FROM MAIN ACTIVITY:
|
|
||||||
* VideoDetector vd = new VideoDetector(this);
|
|
||||||
* */
|
|
||||||
|
|
||||||
|
|
||||||
@ExperimentalGetImage
|
|
||||||
public class VideoDetector extends Detector {
|
|
||||||
|
|
||||||
// Calling Activity
|
|
||||||
private final Context context;
|
|
||||||
|
|
||||||
// Permission handling
|
|
||||||
private static final int PERMISSION_REQUEST_CODE = 3691;
|
|
||||||
|
|
||||||
// Camera Provider
|
|
||||||
private ProcessCameraProvider cameraProvider;
|
|
||||||
private final ImageAnalysis imageAnalysis;
|
|
||||||
private final VideoCapture videoCapture;
|
|
||||||
private final Preview preview;
|
|
||||||
|
|
||||||
// Logic
|
|
||||||
private boolean isDetecting = false;
|
|
||||||
private boolean isRecording = false;
|
|
||||||
private boolean allowReportViolation = false;
|
|
||||||
|
|
||||||
// Image Processing
|
|
||||||
private Mat previousImage = null;
|
|
||||||
|
|
||||||
// Debugging
|
|
||||||
private ImageView inputImageView = null;
|
|
||||||
private ImageView outputImageView = null;
|
|
||||||
|
|
||||||
|
|
||||||
// Recording
|
|
||||||
private final String outputName = "video.mp4";
|
|
||||||
|
|
||||||
|
|
||||||
// Parameters
|
|
||||||
private static final float ALARM_THRESHOLD = 0.5f; // Percent of pixels changed
|
|
||||||
private static final long START_DELAY = 20000; // milliseconds
|
|
||||||
private static final android.util.Size IMAGE_RES = new android.util.Size(640, 480);
|
|
||||||
|
|
||||||
|
|
||||||
private enum UseCase {
|
|
||||||
ImageAnalysis,
|
|
||||||
Preview,
|
|
||||||
VideoCapture
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/** Constructor */
|
|
||||||
public VideoDetector(Context context) {
|
|
||||||
super();
|
|
||||||
this.context = context;
|
|
||||||
this.imageAnalysis = setupImageAnalysis();
|
|
||||||
this.videoCapture = setupVideoCapture();
|
|
||||||
this.preview = new Preview.Builder().build();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Get State of the Detector */
|
|
||||||
public boolean isRunning() {
|
|
||||||
return isDetecting;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/** Starts the Video Detection */
|
|
||||||
@Override
|
|
||||||
public void startDetection() {
|
|
||||||
// Check States
|
|
||||||
if (isDetecting)
|
|
||||||
return;
|
|
||||||
// Return On Request Permissions
|
|
||||||
if (!hasPermissions()) {
|
|
||||||
getPermissions();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Open CV startup check
|
|
||||||
if (!OpenCVLoader.initDebug()) {
|
|
||||||
Log.e("OpenCV", "Unable to load OpenCV!");
|
|
||||||
return;
|
|
||||||
} else
|
|
||||||
Log.d("OpenCV", "OpenCV loaded Successfully!");
|
|
||||||
// Get Process Camera Provider and start
|
|
||||||
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
|
||||||
cameraProviderFuture.addListener(() -> {
|
|
||||||
try {
|
|
||||||
cameraProvider = cameraProviderFuture.get();
|
|
||||||
isDetecting = true;
|
|
||||||
bindCameraProvider(UseCase.ImageAnalysis);
|
|
||||||
} catch (ExecutionException | InterruptedException e) {}
|
|
||||||
}, ContextCompat.getMainExecutor(context));
|
|
||||||
|
|
||||||
startViolationTimer();
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Stops the Video Detection */
|
|
||||||
@Override
|
|
||||||
public void stopDetection() {
|
|
||||||
if (!isDetecting || imageAnalysis == null)
|
|
||||||
return;
|
|
||||||
cameraProvider.unbind(imageAnalysis);
|
|
||||||
isDetecting = false;
|
|
||||||
allowReportViolation = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Permission handling */
|
|
||||||
private boolean hasPermissions() {
|
|
||||||
return ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
|
|
||||||
ContextCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
|
|
||||||
}
|
|
||||||
private void getPermissions() {
|
|
||||||
if (!hasPermissions())
|
|
||||||
ActivityCompat.requestPermissions((Activity) context, new String[]{android.Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO}, PERMISSION_REQUEST_CODE);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Binds the Luminosity Analyzer (configure and run Analysis) */
|
|
||||||
private void bindCameraProvider(UseCase useCase) {
|
|
||||||
// Specify which Camera to use
|
|
||||||
CameraSelector cameraSelector = new CameraSelector.Builder().requireLensFacing(CameraSelector.LENS_FACING_BACK).build();
|
|
||||||
cameraProvider.unbindAll();
|
|
||||||
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, videoCapture);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/** Start delay until Violation Report is allowed */
|
|
||||||
private void startViolationTimer() {
|
|
||||||
new CountDownTimer((long) (START_DELAY), 100) {
|
|
||||||
@Override
|
|
||||||
public void onTick(long millisUntilFinished) {
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void onFinish() {
|
|
||||||
allowReportViolation = true;
|
|
||||||
}
|
|
||||||
}.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private ImageAnalysis setupImageAnalysis() {
|
|
||||||
// Configure and create Image Analysis
|
|
||||||
ImageAnalysis.Builder builder = new ImageAnalysis.Builder();
|
|
||||||
builder.setTargetResolution(IMAGE_RES);
|
|
||||||
builder.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST);
|
|
||||||
builder.setOutputImageFormat(ImageAnalysis.OUTPUT_IMAGE_FORMAT_YUV_420_888);
|
|
||||||
ImageAnalysis imageAnalysis = builder.build();
|
|
||||||
// Set Analyzer
|
|
||||||
imageAnalysis.setAnalyzer(ContextCompat.getMainExecutor(context), imageProxy -> {
|
|
||||||
if (imageProxy.getFormat() == ImageFormat.YUV_420_888) {
|
|
||||||
Image image = imageProxy.getImage();
|
|
||||||
assert image != null;
|
|
||||||
|
|
||||||
// Violation Handling
|
|
||||||
Mat processed = processImage(imageProxy);
|
|
||||||
int n = OpenCVHelper.countNonZeroPixels(processed);
|
|
||||||
int pixelCount = image.getWidth() * image.getHeight();
|
|
||||||
float percentChanged = (float) n / pixelCount;
|
|
||||||
|
|
||||||
// Violation Condition
|
|
||||||
if (percentChanged * 100 > ALARM_THRESHOLD) {
|
|
||||||
if (allowReportViolation)
|
|
||||||
reportViolation("Video", n);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
imageProxy.close();
|
|
||||||
});
|
|
||||||
return imageAnalysis;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressLint("RestrictedApi")
|
|
||||||
private VideoCapture setupVideoCapture() {
|
|
||||||
return new VideoCapture.Builder()
|
|
||||||
.setTargetRotation(Surface.ROTATION_0)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressLint("RestrictedApi")
|
|
||||||
public void startRecording() {
|
|
||||||
// Check States
|
|
||||||
if (isRecording){
|
|
||||||
extendViolation();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return On Request Permissions
|
|
||||||
if (!hasPermissions()) {
|
|
||||||
getPermissions();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
final ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(context);
|
|
||||||
cameraProviderFuture.addListener(() -> {
|
|
||||||
try {
|
|
||||||
cameraProvider = cameraProviderFuture.get();
|
|
||||||
isRecording = true;
|
|
||||||
bindCameraProvider(UseCase.VideoCapture);
|
|
||||||
|
|
||||||
File vidFile = new File(context.getFilesDir() + "/" + outputName);
|
|
||||||
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
videoCapture.startRecording(
|
|
||||||
new VideoCapture.OutputFileOptions.Builder(vidFile).build(),
|
|
||||||
context.getMainExecutor(),
|
|
||||||
new VideoCapture.OnVideoSavedCallback() {
|
|
||||||
@Override
|
|
||||||
public void onVideoSaved(@NonNull VideoCapture.OutputFileResults outputFileResults) {
|
|
||||||
isRecording = false;
|
|
||||||
Toast.makeText(context, "recording saved", Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) {
|
|
||||||
isRecording = false;
|
|
||||||
Toast.makeText(context, "recording failed", Toast.LENGTH_SHORT).show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
} catch (ExecutionException | InterruptedException e) {}
|
|
||||||
}, ContextCompat.getMainExecutor(context));
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressLint("RestrictedApi")
|
|
||||||
public void stopRecording(){
|
|
||||||
videoCapture.stopRecording();
|
|
||||||
cameraProvider.unbind(videoCapture);
|
|
||||||
isRecording = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Process Image to be used for Motion Detection */
|
|
||||||
private Mat processImage(ImageProxy imageProxy){
|
|
||||||
if (imageProxy == null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
// Image Transformation
|
|
||||||
Mat imageMat = OpenCVHelper.extractYChannel(imageProxy);
|
|
||||||
|
|
||||||
// Show Input Image
|
|
||||||
if (inputImageView != null)
|
|
||||||
OpenCVHelper.debugMat(imageMat, inputImageView);
|
|
||||||
|
|
||||||
// Preprocess Image
|
|
||||||
Mat preprocessed = imageMat;
|
|
||||||
preprocessed = OpenCVHelper.addGaussianBlur(preprocessed, new Size(21, 21));
|
|
||||||
preprocessed = OpenCVHelper.addBlur(preprocessed, new Size(3, 3));
|
|
||||||
|
|
||||||
if (previousImage == null) {
|
|
||||||
previousImage = preprocessed;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Process Image
|
|
||||||
Mat processed = preprocessed.clone();
|
|
||||||
processed = OpenCVHelper.thresholdPixels(processed, previousImage, 25);
|
|
||||||
processed = OpenCVHelper.dilateBinaryMat(processed, new Size(3,3));
|
|
||||||
processed = OpenCVHelper.dilateBinaryMat(processed, new Size(3,3));
|
|
||||||
processed = OpenCVHelper.thresholdContourArea(processed, 500);
|
|
||||||
|
|
||||||
// Output
|
|
||||||
previousImage = preprocessed.clone();
|
|
||||||
// Show Output Image
|
|
||||||
if (outputImageView != null)
|
|
||||||
OpenCVHelper.debugMat(processed, outputImageView);
|
|
||||||
return processed;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void debugProcessing(@NonNull ImageView inputImageView, @NonNull ImageView outputImageView){
|
|
||||||
this.inputImageView = inputImageView;
|
|
||||||
this.outputImageView = outputImageView;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPreviewView(@NonNull PreviewView previewView) {
|
|
||||||
// Create Preview
|
|
||||||
if (this.preview != null)
|
|
||||||
this.preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private static class OpenCVHelper{
|
|
||||||
private OpenCVHelper() {}
|
|
||||||
|
|
||||||
/** OpenCV helper methods **/
|
|
||||||
private static Mat addGaussianBlur(Mat inputMat, Size kernelSize){
|
|
||||||
Mat outputMat = new Mat();
|
|
||||||
Imgproc.GaussianBlur(inputMat, outputMat, kernelSize, 0);
|
|
||||||
return outputMat;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Mat addBlur(Mat inputMat, Size kernelSize){
|
|
||||||
Mat outputMat = new Mat();
|
|
||||||
Imgproc.blur(inputMat, outputMat, kernelSize);
|
|
||||||
return outputMat;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Mat extractYChannel(@NonNull ImageProxy imgProxy) {
|
|
||||||
Image img = imgProxy.getImage();
|
|
||||||
|
|
||||||
assert img != null;
|
|
||||||
ByteBuffer yBuffer = img.getPlanes()[0].getBuffer();
|
|
||||||
byte[] yData = new byte[yBuffer.remaining()];
|
|
||||||
yBuffer.get(yData);
|
|
||||||
|
|
||||||
Mat yMat = new Mat(img.getHeight(), img.getWidth(), CvType.CV_8UC1);
|
|
||||||
yMat.put(0, 0, yData);
|
|
||||||
|
|
||||||
return yMat;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Mat thresholdPixels(Mat inputMat, Mat previousImage, int threshold){
|
|
||||||
Mat diffImage = new Mat();
|
|
||||||
Core.absdiff(inputMat, previousImage, diffImage);
|
|
||||||
Mat binaryMat = new Mat();
|
|
||||||
Imgproc.threshold(diffImage, binaryMat, threshold, 255, Imgproc.THRESH_BINARY);
|
|
||||||
return binaryMat;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Mat imageProxyToGrayscaleMat(ImageProxy imageProxy) {
|
|
||||||
// Step 1: Extract the image data from ImageProxy
|
|
||||||
ImageProxy.PlaneProxy[] planes = imageProxy.getPlanes();
|
|
||||||
ByteBuffer yBuffer = planes[0].getBuffer();
|
|
||||||
byte[] yData = new byte[yBuffer.remaining()];
|
|
||||||
yBuffer.get(yData);
|
|
||||||
|
|
||||||
// Step 2: Convert the image data to NV21 format
|
|
||||||
int width = imageProxy.getWidth();
|
|
||||||
int height = imageProxy.getHeight();
|
|
||||||
byte[] nv21Data = new byte[width * height * 3 / 2];
|
|
||||||
// Assuming the image format is YUV_420_888
|
|
||||||
System.arraycopy(yData, 0, nv21Data, 0, yData.length);
|
|
||||||
for (int i = yData.length; i < nv21Data.length; i += 2) {
|
|
||||||
nv21Data[i] = yData[i + 1];
|
|
||||||
nv21Data[i + 1] = yData[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Step 3: Create a grayscale Mat from the NV21 data
|
|
||||||
Mat grayscaleMat = new Mat(height, width, CvType.CV_8UC1);
|
|
||||||
grayscaleMat.put(0, 0, nv21Data);
|
|
||||||
|
|
||||||
return grayscaleMat;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static Mat thresholdContourArea(Mat inputMat, float areaThreshold){
|
|
||||||
List<MatOfPoint> contours = new ArrayList<>();
|
|
||||||
Mat hierarchy = new Mat();
|
|
||||||
Imgproc.findContours(inputMat, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
|
|
||||||
|
|
||||||
Mat outputMat = new Mat(inputMat.size(), inputMat.type(), new Scalar(0));
|
|
||||||
// Iterate over the contours and draw only the larger contours on the outputMat
|
|
||||||
for (MatOfPoint contour : contours) {
|
|
||||||
double contourArea = Imgproc.contourArea(contour);
|
|
||||||
if (contourArea > areaThreshold) {
|
|
||||||
Imgproc.drawContours(outputMat, Collections.singletonList(contour), 0, new Scalar(255), -1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Apply the outputMat as a mask to the dilatedImage
|
|
||||||
Mat maskedImage = new Mat();
|
|
||||||
inputMat.copyTo(maskedImage, outputMat);
|
|
||||||
return outputMat;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Mat dilateBinaryMat(Mat inputMat, Size kernelSize){
|
|
||||||
Mat dilatedMat = new Mat();
|
|
||||||
Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_ELLIPSE, kernelSize);
|
|
||||||
Imgproc.dilate(inputMat, dilatedMat, kernel);
|
|
||||||
return dilatedMat;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int countNonZeroPixels(Mat inputImage) {
|
|
||||||
if (inputImage != null)
|
|
||||||
return Core.countNonZero(inputImage);
|
|
||||||
else
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private static void debugMat(Mat mat, ImageView imageView) {
|
|
||||||
if (imageView == null || mat == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
Bitmap bitmap = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888);
|
|
||||||
Utils.matToBitmap(mat, bitmap);
|
|
||||||
|
|
||||||
// Display the bitmap in an ImageView
|
|
||||||
imageView.setImageBitmap(bitmap);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
package com.example.ueberwachungssystem;
|
package com.example.ueberwachungssystem.Detector;
|
||||||
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
+2
-3
@@ -1,6 +1,5 @@
|
|||||||
package com.example.ueberwachungssystem;
|
package com.example.ueberwachungssystem.Detector;
|
||||||
|
|
||||||
import android.app.Activity;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
@@ -22,7 +21,7 @@ abstract public class Detector {
|
|||||||
|
|
||||||
|
|
||||||
/** Triggers onDetectionListener - call this to trigger violation/alarm */
|
/** Triggers onDetectionListener - call this to trigger violation/alarm */
|
||||||
private void reportViolation(String detectorID, String detectionType, float amplitude) {
|
public void reportViolation(String detectorID, String detectionType, float amplitude) {
|
||||||
if (listener != null) {
|
if (listener != null) {
|
||||||
DetectionReport detectionReport = new DetectionReport(detectorID, detectionType, amplitude);
|
DetectionReport detectionReport = new DetectionReport(detectorID, detectionType, amplitude);
|
||||||
listener.onDetection(detectionReport);
|
listener.onDetection(detectionReport);
|
||||||
+27
-51
@@ -1,4 +1,4 @@
|
|||||||
package com.example.ueberwachungssystem;
|
package com.example.ueberwachungssystem.Detector;
|
||||||
|
|
||||||
import static java.lang.Math.*;
|
import static java.lang.Math.*;
|
||||||
|
|
||||||
@@ -10,13 +10,14 @@ import android.media.AudioFormat;
|
|||||||
import android.media.AudioRecord;
|
import android.media.AudioRecord;
|
||||||
import android.media.MediaRecorder;
|
import android.media.MediaRecorder;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.core.app.ActivityCompat;
|
import androidx.core.app.ActivityCompat;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
|
|
||||||
import com.example.ueberwachungssystem.Signalverarbeitung.Complex;
|
import com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex;
|
||||||
import com.example.ueberwachungssystem.Signalverarbeitung.FFT;
|
import com.example.ueberwachungssystem.Detector.Signalverarbeitung.FFT;
|
||||||
import com.example.ueberwachungssystem.logger.Logger;
|
import com.example.ueberwachungssystem.Detector.logger.Logger;
|
||||||
import com.jjoe64.graphview.GraphView;
|
import com.jjoe64.graphview.GraphView;
|
||||||
import com.jjoe64.graphview.series.DataPoint;
|
import com.jjoe64.graphview.series.DataPoint;
|
||||||
import com.jjoe64.graphview.series.LineGraphSeries;
|
import com.jjoe64.graphview.series.LineGraphSeries;
|
||||||
@@ -33,17 +34,11 @@ public class MicrophoneDetector extends Detector {
|
|||||||
private AufnahmeTask aufnahmeTask;
|
private AufnahmeTask aufnahmeTask;
|
||||||
public boolean armed = false;
|
public boolean armed = false;
|
||||||
public int Schwellwert_Alarm = 100;
|
public int Schwellwert_Alarm = 100;
|
||||||
GraphView graph;
|
|
||||||
|
|
||||||
Logger logger;
|
|
||||||
private Activity MainActivityForClass;
|
private Activity MainActivityForClass;
|
||||||
|
|
||||||
public MicrophoneDetector(Context context, Logger MainLogger, GraphView MainGraph) {
|
public MicrophoneDetector(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
MainActivityForClass = (Activity) context;
|
MainActivityForClass = (Activity) context;
|
||||||
logger = MainLogger; //Class uses the same logger as the MainActivity
|
|
||||||
logger.log(this.getClass().getSimpleName() + ".onCreate");
|
|
||||||
graph = MainGraph;
|
|
||||||
|
|
||||||
if (!istZugriffAufMikrofonErlaubt()) {
|
if (!istZugriffAufMikrofonErlaubt()) {
|
||||||
zugriffAufMikrofonAnfordern();
|
zugriffAufMikrofonAnfordern();
|
||||||
@@ -52,7 +47,6 @@ public class MicrophoneDetector extends Detector {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void startDetection() {
|
public void startDetection() {
|
||||||
logger.log(this.getClass().getSimpleName() + ".startDetection");
|
|
||||||
|
|
||||||
if (!istZugriffAufMikrofonErlaubt()) {
|
if (!istZugriffAufMikrofonErlaubt()) {
|
||||||
zugriffAufMikrofonAnfordern();
|
zugriffAufMikrofonAnfordern();
|
||||||
@@ -65,19 +59,17 @@ public class MicrophoneDetector extends Detector {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stopDetection() {
|
public void stopDetection() {
|
||||||
logger.log(this.getClass().getSimpleName() + ".stopDetection");
|
|
||||||
if (aufnahmeTask != null) {
|
if (aufnahmeTask != null) {
|
||||||
aufnahmeTask.cancel(true);
|
aufnahmeTask.cancel(true);
|
||||||
// aufnahmeTask = null; // if aufnahmeTask = null, break in for loop would not work (Nullpointer Exception)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean istZugriffAufMikrofonErlaubt() {
|
private boolean istZugriffAufMikrofonErlaubt() {
|
||||||
if (ContextCompat.checkSelfPermission(MainActivityForClass, android.Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
|
if (ContextCompat.checkSelfPermission(MainActivityForClass, android.Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
|
||||||
logger.log("Zugriff auf Mikrofon ist verboten.");
|
Log.d("0","Zugriff auf Mikrofon ist verboten.");
|
||||||
return false;
|
return false;
|
||||||
} else {
|
} else {
|
||||||
logger.log("Zugriff auf Mikrofon ist erlaubt.");
|
Log.d("0","Zugriff auf Mikrofon ist erlaubt.");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -113,13 +105,8 @@ public class MicrophoneDetector extends Detector {
|
|||||||
|
|
||||||
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, channelConfig, audioFormat, pufferGroesseInBytes);
|
recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRateInHz, channelConfig, audioFormat, pufferGroesseInBytes);
|
||||||
|
|
||||||
// textViewMinPufferGroesseInBytes.setText("" + minPufferGroesseInBytes);
|
Log.d("0","Puffergroeße: "+ minPufferGroesseInBytes + " " + pufferGroesseInBytes);
|
||||||
// textViewPufferGroesseInBytes.setText("" + pufferGroesseInBytes);
|
Log.d("0","Recorder (SR, CH): "+ recorder.getSampleRate() + " " + recorder.getChannelCount());
|
||||||
// textViewAbtastrate.setText("" + recorder.getSampleRate());
|
|
||||||
// textViewAnzahlKanaele.setText("" + recorder.getChannelCount());
|
|
||||||
|
|
||||||
logger.log("Puffergroeße: "+ minPufferGroesseInBytes + " " + pufferGroesseInBytes);
|
|
||||||
logger.log("Recorder (SR, CH): "+ recorder.getSampleRate() + " " + recorder.getChannelCount());
|
|
||||||
|
|
||||||
int anzahlBytesProAbtastwert;
|
int anzahlBytesProAbtastwert;
|
||||||
String s;
|
String s;
|
||||||
@@ -139,7 +126,6 @@ public class MicrophoneDetector extends Detector {
|
|||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
// textViewAudioFormat.setText(s);
|
|
||||||
|
|
||||||
switch (recorder.getChannelConfiguration()) {
|
switch (recorder.getChannelConfiguration()) {
|
||||||
case AudioFormat.CHANNEL_IN_MONO:
|
case AudioFormat.CHANNEL_IN_MONO:
|
||||||
@@ -155,14 +141,12 @@ public class MicrophoneDetector extends Detector {
|
|||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
}
|
}
|
||||||
// textViewKanalKonfiguration.setText(s);
|
|
||||||
logger.log("Konfiguration: "+ s);
|
Log.d("0","Konfiguration: "+ s);
|
||||||
|
|
||||||
int pufferGroesseInAnzahlAbtastwerten = pufferGroesseInBytes / anzahlBytesProAbtastwert;
|
int pufferGroesseInAnzahlAbtastwerten = pufferGroesseInBytes / anzahlBytesProAbtastwert;
|
||||||
int pufferGroesseInMillisekunden = 1000 * pufferGroesseInAnzahlAbtastwerten / recorder.getSampleRate();
|
int pufferGroesseInMillisekunden = 1000 * pufferGroesseInAnzahlAbtastwerten / recorder.getSampleRate();
|
||||||
|
|
||||||
// textViewPufferGroesseInAnzahlAbtastwerte.setText("" + pufferGroesseInAnzahlAbtastwerten);
|
|
||||||
// textViewPufferGroesseInMillisekunden.setText("" + pufferGroesseInMillisekunden);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -178,7 +162,7 @@ public class MicrophoneDetector extends Detector {
|
|||||||
|
|
||||||
//Kalibrierung
|
//Kalibrierung
|
||||||
try {
|
try {
|
||||||
Thread.sleep(3000);
|
Thread.sleep(3000); // Time to lay down the phone
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -195,20 +179,20 @@ public class MicrophoneDetector extends Detector {
|
|||||||
}
|
}
|
||||||
kalibierWert = kalibierWert/i;
|
kalibierWert = kalibierWert/i;
|
||||||
|
|
||||||
|
// __Part of FFT__
|
||||||
// Complex[] zeitSignal = new Complex[puffer.length];
|
// Complex[] zeitSignal = new Complex[puffer.length];
|
||||||
// for (int j = 0; j < puffer.length; j++) {
|
// for (int j = 0; j < puffer.length; j++) {
|
||||||
// zeitSignal[j] = new Complex(puffer[j], 0);
|
// zeitSignal[j] = new Complex(puffer[j], 0);
|
||||||
// }
|
// }
|
||||||
// Complex[] spektrum = FFT.fft(zeitSignal);
|
// Complex[] spektrum = FFT.fft(zeitSignal);
|
||||||
double[] spektrum = calculateFFT(puffer);
|
// double[] spektrum = calculateFFT(puffer);
|
||||||
DataPoint AddPoint;
|
// DataPoint AddPoint;
|
||||||
// LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[]{});
|
// LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[]{});
|
||||||
// for (i = 0; i < spektrum.length; i++) {
|
// for (i = 0; i < spektrum.length; i++) {
|
||||||
// AddPoint = new DataPoint(i, spektrum[i]);
|
// AddPoint = new DataPoint(i, spektrum[i]);
|
||||||
// series.appendData(AddPoint, true, spektrum.length);
|
// series.appendData(AddPoint, true, spektrum.length);
|
||||||
// }
|
// }
|
||||||
// graph.addSeries(series);
|
// graph.addSeries(series);
|
||||||
// logger.log(spektrum.toString());
|
|
||||||
|
|
||||||
for (; ; ) {
|
for (; ; ) {
|
||||||
if (aufnahmeTask.isCancelled()) {
|
if (aufnahmeTask.isCancelled()) {
|
||||||
@@ -217,15 +201,14 @@ public class MicrophoneDetector extends Detector {
|
|||||||
int n = recorder.read(puffer, 0, puffer.length);
|
int n = recorder.read(puffer, 0, puffer.length);
|
||||||
Verarbeitungsergebnis ergebnis = verarbeiten(puffer, n);
|
Verarbeitungsergebnis ergebnis = verarbeiten(puffer, n);
|
||||||
anzahlVerarbeitet += n;
|
anzahlVerarbeitet += n;
|
||||||
|
// __Part of FFT__
|
||||||
|
// spektrum = calculateFFT(puffer);
|
||||||
|
// LineGraphSeries<DataPoint> newseries = new LineGraphSeries<DataPoint>(new DataPoint[]{});
|
||||||
|
// for (i = 0; i < spektrum.length; i++) {
|
||||||
|
// AddPoint = new DataPoint(i, spektrum[i]);
|
||||||
|
// newseries.appendData(AddPoint, true, spektrum.length);
|
||||||
|
// }
|
||||||
|
|
||||||
spektrum = calculateFFT(puffer);
|
|
||||||
LineGraphSeries<DataPoint> newseries = new LineGraphSeries<DataPoint>(new DataPoint[]{});
|
|
||||||
for (i = 0; i < spektrum.length; i++) {
|
|
||||||
AddPoint = new DataPoint(i, spektrum[i]);
|
|
||||||
newseries.appendData(AddPoint, true, spektrum.length);
|
|
||||||
}
|
|
||||||
graph.removeAllSeries();
|
|
||||||
graph.addSeries(newseries);
|
|
||||||
zaehlerZeitMessung++;
|
zaehlerZeitMessung++;
|
||||||
if (zaehlerZeitMessung == maxZaehlerZeitMessung) {
|
if (zaehlerZeitMessung == maxZaehlerZeitMessung) {
|
||||||
long time = System.currentTimeMillis();
|
long time = System.currentTimeMillis();
|
||||||
@@ -242,7 +225,7 @@ public class MicrophoneDetector extends Detector {
|
|||||||
publishProgress(ergebnis);
|
publishProgress(ergebnis);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Thread.sleep(100);
|
Thread.sleep(10);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@@ -265,7 +248,6 @@ public class MicrophoneDetector extends Detector {
|
|||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
if (daten[i] > max) {
|
if (daten[i] > max) {
|
||||||
max = daten[i];
|
max = daten[i];
|
||||||
//max = 20 * log10(abs(daten[i]) / 32768);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -282,20 +264,16 @@ public class MicrophoneDetector extends Detector {
|
|||||||
@Override
|
@Override
|
||||||
protected void onProgressUpdate(Verarbeitungsergebnis... progress) {
|
protected void onProgressUpdate(Verarbeitungsergebnis... progress) {
|
||||||
super.onProgressUpdate(progress);
|
super.onProgressUpdate(progress);
|
||||||
// textViewMaxAmp.setText("" + progress[0].maxAmp);
|
|
||||||
// textViewVerarbeitungsrate.setText("" + progress[0].verarbeitungsrate);
|
|
||||||
float maxAmpPrint = round(20*log10(abs(progress[0].maxAmp/1.0)));
|
float maxAmpPrint = round(20*log10(abs(progress[0].maxAmp/1.0)));
|
||||||
float kalibierWertPrint = round(20*log10(abs(kalibierWert)));
|
float kalibierWertPrint = round(20*log10(abs(kalibierWert)));
|
||||||
logger.overwriteLastlog("VR, Max, Kal:" + progress[0].verarbeitungsrate + ", " + maxAmpPrint
|
Log.d("0","VR, Max, Kal:" + progress[0].verarbeitungsrate + ", " + maxAmpPrint
|
||||||
+ " dB, " + kalibierWertPrint + " dB");
|
+ " dB, " + kalibierWertPrint + " dB");
|
||||||
|
|
||||||
if (progress[0].maxAmp >= Schwellwert_Alarm+kalibierWert && armed == true) {
|
if (progress[0].maxAmp >= Schwellwert_Alarm+kalibierWert && armed == true) {
|
||||||
armed = false;
|
armed = false;
|
||||||
detectionReport = new DetectionReport("Mic1", "Audio", maxAmpPrint);
|
detectionReport = new DetectionReport("Mic1", "Audio", maxAmpPrint);
|
||||||
logger.log("");
|
reportViolation("Mic1", "Audio", maxAmpPrint);
|
||||||
logger.log("Alarm!");
|
Log.d("1",detectionReport.toString());
|
||||||
logger.log(detectionReport.toString());
|
|
||||||
logger.log("");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -325,7 +303,6 @@ public class MicrophoneDetector extends Detector {
|
|||||||
y = FFT.fft(complexSignal);
|
y = FFT.fft(complexSignal);
|
||||||
|
|
||||||
mMaxFFTSample = 0.0;
|
mMaxFFTSample = 0.0;
|
||||||
// mPeakPos = 0;
|
|
||||||
for(int i = 0; i < (mNumberOfFFTPoints/2); i++)
|
for(int i = 0; i < (mNumberOfFFTPoints/2); i++)
|
||||||
{
|
{
|
||||||
absSignal[i] = y[i].abs();
|
absSignal[i] = y[i].abs();
|
||||||
@@ -333,7 +310,6 @@ public class MicrophoneDetector extends Detector {
|
|||||||
// if(absSignal[i] > mMaxFFTSample)
|
// if(absSignal[i] > mMaxFFTSample)
|
||||||
// {
|
// {
|
||||||
// mMaxFFTSample = absSignal[i];
|
// mMaxFFTSample = absSignal[i];
|
||||||
// // mPeakPos = i;
|
|
||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
+11
-11
@@ -1,4 +1,4 @@
|
|||||||
package com.example.ueberwachungssystem.Signalverarbeitung;
|
package com.example.ueberwachungssystem.Detector.Signalverarbeitung;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@@ -12,7 +12,7 @@ public class Complex {
|
|||||||
im = imag;
|
im = imag;
|
||||||
}
|
}
|
||||||
|
|
||||||
// return a string representation of the invoking com.example.ueberwachungssystem.Signalverarbeitung.Complex object
|
// return a string representation of the invoking com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object
|
||||||
public String toString() {
|
public String toString() {
|
||||||
if (im == 0) return re + "";
|
if (im == 0) return re + "";
|
||||||
if (re == 0) return im + "i";
|
if (re == 0) return im + "i";
|
||||||
@@ -30,7 +30,7 @@ public class Complex {
|
|||||||
return Math.atan2(im, re);
|
return Math.atan2(im, re);
|
||||||
}
|
}
|
||||||
|
|
||||||
// return a new com.example.ueberwachungssystem.Signalverarbeitung.Complex object whose value is (this + b)
|
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is (this + b)
|
||||||
public Complex plus(Complex b) {
|
public Complex plus(Complex b) {
|
||||||
Complex a = this; // invoking object
|
Complex a = this; // invoking object
|
||||||
double real = a.re + b.re;
|
double real = a.re + b.re;
|
||||||
@@ -38,7 +38,7 @@ public class Complex {
|
|||||||
return new Complex(real, imag);
|
return new Complex(real, imag);
|
||||||
}
|
}
|
||||||
|
|
||||||
// return a new com.example.ueberwachungssystem.Signalverarbeitung.Complex object whose value is (this - b)
|
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is (this - b)
|
||||||
public Complex minus(Complex b) {
|
public Complex minus(Complex b) {
|
||||||
Complex a = this;
|
Complex a = this;
|
||||||
double real = a.re - b.re;
|
double real = a.re - b.re;
|
||||||
@@ -46,7 +46,7 @@ public class Complex {
|
|||||||
return new Complex(real, imag);
|
return new Complex(real, imag);
|
||||||
}
|
}
|
||||||
|
|
||||||
// return a new com.example.ueberwachungssystem.Signalverarbeitung.Complex object whose value is (this * b)
|
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is (this * b)
|
||||||
public Complex times(Complex b) {
|
public Complex times(Complex b) {
|
||||||
Complex a = this;
|
Complex a = this;
|
||||||
double real = a.re * b.re - a.im * b.im;
|
double real = a.re * b.re - a.im * b.im;
|
||||||
@@ -59,12 +59,12 @@ public class Complex {
|
|||||||
return new Complex(alpha * re, alpha * im);
|
return new Complex(alpha * re, alpha * im);
|
||||||
}
|
}
|
||||||
|
|
||||||
// return a new com.example.ueberwachungssystem.Signalverarbeitung.Complex object whose value is the conjugate of this
|
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is the conjugate of this
|
||||||
public Complex conjugate() {
|
public Complex conjugate() {
|
||||||
return new Complex(re, -im);
|
return new Complex(re, -im);
|
||||||
}
|
}
|
||||||
|
|
||||||
// return a new com.example.ueberwachungssystem.Signalverarbeitung.Complex object whose value is the reciprocal of this
|
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is the reciprocal of this
|
||||||
public Complex reciprocal() {
|
public Complex reciprocal() {
|
||||||
double scale = re * re + im * im;
|
double scale = re * re + im * im;
|
||||||
return new Complex(re / scale, -im / scale);
|
return new Complex(re / scale, -im / scale);
|
||||||
@@ -85,22 +85,22 @@ public class Complex {
|
|||||||
return a.times(b.reciprocal());
|
return a.times(b.reciprocal());
|
||||||
}
|
}
|
||||||
|
|
||||||
// return a new com.example.ueberwachungssystem.Signalverarbeitung.Complex object whose value is the complex exponential of this
|
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is the complex exponential of this
|
||||||
public Complex exp() {
|
public Complex exp() {
|
||||||
return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
|
return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
|
||||||
}
|
}
|
||||||
|
|
||||||
// return a new com.example.ueberwachungssystem.Signalverarbeitung.Complex object whose value is the complex sine of this
|
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is the complex sine of this
|
||||||
public Complex sin() {
|
public Complex sin() {
|
||||||
return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im));
|
return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im));
|
||||||
}
|
}
|
||||||
|
|
||||||
// return a new com.example.ueberwachungssystem.Signalverarbeitung.Complex object whose value is the complex cosine of this
|
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is the complex cosine of this
|
||||||
public Complex cos() {
|
public Complex cos() {
|
||||||
return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im));
|
return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im));
|
||||||
}
|
}
|
||||||
|
|
||||||
// return a new com.example.ueberwachungssystem.Signalverarbeitung.Complex object whose value is the complex tangent of this
|
// return a new com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex object whose value is the complex tangent of this
|
||||||
public Complex tan() {
|
public Complex tan() {
|
||||||
return sin().divides(cos());
|
return sin().divides(cos());
|
||||||
}
|
}
|
||||||
+3
-5
@@ -1,12 +1,10 @@
|
|||||||
package com.example.ueberwachungssystem.Signalverarbeitung;
|
package com.example.ueberwachungssystem.Detector.Signalverarbeitung;
|
||||||
// Source: https://introcs.cs.princeton.edu/java/97data/FFT.java.html
|
// Source: https://introcs.cs.princeton.edu/java/97data/FFT.java.html
|
||||||
|
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* Compilation: javac FFT.java
|
* Compilation: javac FFT.java
|
||||||
* Execution: java FFT n
|
* Execution: java FFT n
|
||||||
* Dependencies: com.example.ueberwachungssystem.Signalverarbeitung.Complex.java
|
* Dependencies: com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex.java
|
||||||
*
|
*
|
||||||
* Compute the FFT and inverse FFT of a length n complex sequence
|
* Compute the FFT and inverse FFT of a length n complex sequence
|
||||||
* using the radix 2 Cooley-Tukey algorithm.
|
* using the radix 2 Cooley-Tukey algorithm.
|
||||||
@@ -158,7 +156,7 @@ public class FFT {
|
|||||||
return y;
|
return y;
|
||||||
}
|
}
|
||||||
|
|
||||||
// display an array of com.example.ueberwachungssystem.Signalverarbeitung.Complex numbers to standard output
|
// display an array of com.example.ueberwachungssystem.Detector.Signalverarbeitung.Complex numbers to standard output
|
||||||
public static void show(Complex[] x, String title) {
|
public static void show(Complex[] x, String title) {
|
||||||
System.out.println(title);
|
System.out.println(title);
|
||||||
System.out.println("-------------------");
|
System.out.println("-------------------");
|
||||||
@@ -2,72 +2,64 @@ package com.example.ueberwachungssystem;
|
|||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.camera.core.ExperimentalGetImage;
|
|
||||||
import androidx.camera.view.PreviewView;
|
|
||||||
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.ImageView;
|
import android.widget.Switch;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.ToggleButton;
|
|
||||||
|
|
||||||
import com.example.ueberwachungssystem.Detection.DetectionReport;
|
import com.example.ueberwachungssystem.Detector.DetectionReport;
|
||||||
import com.example.ueberwachungssystem.Detection.Detector;
|
import com.example.ueberwachungssystem.Detector.Detector;
|
||||||
import com.example.ueberwachungssystem.Detection.VideoDetector;
|
import com.example.ueberwachungssystem.Detector.MicrophoneDetector;
|
||||||
import com.example.ueberwachungssystem.logger.Logger;
|
|
||||||
import com.jjoe64.graphview.GraphView;
|
import com.jjoe64.graphview.GraphView;
|
||||||
|
|
||||||
@ExperimentalGetImage
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
|
||||||
|
|
||||||
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
private TextView tv_log;
|
||||||
|
MicrophoneDetector Mic;
|
||||||
|
private Switch TglBtn_Mic;
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
tv_log = (TextView) findViewById(R.id.tv_log); //Set textview for showing logged content
|
||||||
|
Log.d("Main",this.getClass().getSimpleName() + ".onCreate");
|
||||||
ImageView inputImageView = findViewById(R.id.inputImageView);
|
Mic = new MicrophoneDetector(this);
|
||||||
ImageView outputImageView = findViewById(R.id.outputImageView);
|
setupMic();
|
||||||
PreviewView previewView = findViewById(R.id.previewView);
|
Mic.setOnDetectionListener(new Detector.OnDetectionListener() {
|
||||||
|
|
||||||
|
|
||||||
// Mic stuff
|
|
||||||
TextView textView= (TextView) findViewById(R.id.textView); //Set textview for showing logged content
|
|
||||||
Logger logger = new Logger(this.getClass().getSimpleName(), textView, "");
|
|
||||||
GraphView graph = (GraphView) findViewById(R.id.graph);
|
|
||||||
MicrophoneDetector mic = new MicrophoneDetector(this, logger, graph);
|
|
||||||
mic.startDetection();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// video Stuff
|
|
||||||
VideoDetector vd = new VideoDetector(this);
|
|
||||||
//vd.setPreviewView(previewView);
|
|
||||||
vd.debugProcessing(inputImageView, outputImageView);
|
|
||||||
vd.setOnDetectionListener(new Detector.OnDetectionListener() {
|
|
||||||
@Override
|
@Override
|
||||||
public void onDetection(@NonNull DetectionReport detectionReport) {
|
public void onDetection(@NonNull DetectionReport detectionReport) {
|
||||||
Log.d("onDetection", detectionReport.toString());
|
Log.d("Main", "Detection erkannt!");
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
vd.startDetection();
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onResume() {
|
||||||
|
super.onResume();
|
||||||
|
Log.d("Main",this.getClass().getSimpleName() + ".onResume");
|
||||||
|
}
|
||||||
|
|
||||||
ToggleButton toggleButton = findViewById(R.id.toggleButton);
|
@Override
|
||||||
toggleButton.setOnClickListener(new View.OnClickListener() {
|
public void onPause() {
|
||||||
|
super.onPause();
|
||||||
|
Log.d("Main",this.getClass().getSimpleName() + ".onPause");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setupMic() {
|
||||||
|
TglBtn_Mic = (Switch) findViewById(R.id.TglBtn_Mic);
|
||||||
|
TglBtn_Mic.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
if (toggleButton.isChecked())
|
if (v == TglBtn_Mic) {
|
||||||
{
|
Log.d("Main","onClick toggleButtonThread " + TglBtn_Mic.isChecked());
|
||||||
//vd.startDetection();
|
if (TglBtn_Mic.isChecked()) {
|
||||||
vd.startRecording();
|
Mic.startDetection();
|
||||||
}
|
} else {
|
||||||
else {
|
Mic.stopDetection();
|
||||||
//vd.stopDetection();
|
}
|
||||||
vd.stopRecording();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,61 +0,0 @@
|
|||||||
package com.example.ueberwachungssystem.logger;
|
|
||||||
|
|
||||||
import android.util.Log;
|
|
||||||
import android.widget.TextView;
|
|
||||||
|
|
||||||
import java.io.PrintWriter;
|
|
||||||
import java.io.StringWriter;
|
|
||||||
|
|
||||||
public class Logger {
|
|
||||||
private TextView textView;
|
|
||||||
private StringBuffer sb = new StringBuffer();
|
|
||||||
private String tag;
|
|
||||||
private int lengthOfLastLog = 0;
|
|
||||||
private boolean overwrite = false;
|
|
||||||
|
|
||||||
public Logger(String tag, TextView textView, String logInitText) {
|
|
||||||
this.tag = tag;
|
|
||||||
this.textView = textView;
|
|
||||||
sb.append(logInitText);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void log(String s) {
|
|
||||||
overwrite = false;
|
|
||||||
Log.d(tag, s);
|
|
||||||
sb.append(s).append("\n");
|
|
||||||
if (textView != null) {
|
|
||||||
textView.setText(sb.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void overwriteLastlog(String s) {
|
|
||||||
Log.d(tag, s);
|
|
||||||
lengthOfLastLog = s.length();
|
|
||||||
if (overwrite)
|
|
||||||
{
|
|
||||||
sb.setLength(sb.length() - (lengthOfLastLog + 1));
|
|
||||||
}
|
|
||||||
sb.append(s).append("\n");
|
|
||||||
overwrite = true;
|
|
||||||
if (textView != null) {
|
|
||||||
textView.setText(sb.toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void log(Exception e) {
|
|
||||||
StringWriter sw = new StringWriter();
|
|
||||||
e.printStackTrace(new PrintWriter(sw));
|
|
||||||
log(sw.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void clearLog() {
|
|
||||||
sb.setLength(0);
|
|
||||||
if (textView != null) {
|
|
||||||
textView.setText("");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getLoggedText() {
|
|
||||||
return sb.toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,56 +1,26 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||||
xmlns:tools="http://schemas.android.com/tools"
|
xmlns:tools="http://schemas.android.com/tools"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:layout_gravity="center"
|
|
||||||
android:gravity="top"
|
|
||||||
android:orientation="vertical"
|
|
||||||
tools:context=".MainActivity">
|
tools:context=".MainActivity">
|
||||||
|
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/textView"
|
android:id="@+id/tv_log"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="100dp"
|
|
||||||
android:text="TextView" />
|
|
||||||
|
|
||||||
<androidx.camera.view.PreviewView
|
|
||||||
android:id="@+id/previewView"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:backgroundTint="@android:color/black"/>
|
android:text="Log"
|
||||||
|
|
||||||
<ToggleButton
|
|
||||||
android:id="@+id/toggleButton"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:text="ToggleButton" />
|
|
||||||
|
|
||||||
<ImageView
|
|
||||||
android:id="@+id/inputImageView"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
tools:srcCompat="@tools:sample/avatars" />
|
|
||||||
|
|
||||||
<ImageView
|
|
||||||
android:id="@+id/outputImageView"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="1dp"
|
|
||||||
tools:srcCompat="@tools:sample/avatars" />
|
|
||||||
|
|
||||||
<com.jjoe64.graphview.GraphView
|
|
||||||
android:id="@+id/graph"
|
|
||||||
android:layout_width="match_parent"
|
|
||||||
android:layout_height="200dip"
|
|
||||||
android:layout_marginStart="16dp"
|
|
||||||
android:layout_marginEnd="16dp"
|
|
||||||
app:layout_constraintBottom_toBottomOf="parent"
|
|
||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintTop_toTopOf="parent"
|
app:layout_constraintTop_toBottomOf="@+id/TglBtn_Mic" />
|
||||||
app:layout_constraintVertical_bias="0.499" />
|
|
||||||
|
|
||||||
|
<Switch
|
||||||
|
android:id="@+id/TglBtn_Mic"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="Mic"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
|
app:layout_constraintTop_toTopOf="parent" />
|
||||||
|
|
||||||
</LinearLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
Reference in new issue
Block a user