Compare commits
No commits in common. "74f0fdb798e54f68101199860a24e9b4d86afac3" and "5d7b949e5434b60c43f40af7b4a91560930894fa" have entirely different histories.
74f0fdb798
...
5d7b949e54
@ -106,9 +106,6 @@ public class AudiodetectionActivity extends AppCompatActivity {
|
|||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
if (isFinishing()){
|
|
||||||
mMicrofonViewModel.recalibrationMicrofonSensor();
|
|
||||||
}
|
|
||||||
mMicrofonViewModel.stopMicrofonSensor();
|
mMicrofonViewModel.stopMicrofonSensor();
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -92,19 +92,14 @@ public class AudiodetectionAndAccelerometerActivity extends AppCompatActivity im
|
|||||||
@Override
|
@Override
|
||||||
public void onChanged(Boolean aBoolean) {
|
public void onChanged(Boolean aBoolean) {
|
||||||
if (aBoolean) {
|
if (aBoolean) {
|
||||||
Toast.makeText(AudiodetectionAndAccelerometerActivity.this, "onChangedTrue", Toast.LENGTH_LONG).show();
|
if (mAudiodetectionAndAccelerometerViewModel.getAccelerometerAlarmDetected()) {
|
||||||
if (mAudiodetectionAndAccelerometerViewModel.getAccelerometerAlarmDetected() && !mAudiodetectionAndAccelerometerViewModel.getMicrofonAlarmDetected()) {
|
|
||||||
mAudiodetectionAndAccelerometerViewModel.updateDevice(mAudiodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mAudiodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "Accelerometer", 10);
|
mAudiodetectionAndAccelerometerViewModel.updateDevice(mAudiodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mAudiodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "Accelerometer", 10);
|
||||||
}
|
}
|
||||||
else if (mAudiodetectionAndAccelerometerViewModel.getMicrofonAlarmDetected() && !mAudiodetectionAndAccelerometerViewModel.getAccelerometerAlarmDetected()) {
|
else if (mAudiodetectionAndAccelerometerViewModel.getMicrofonAlarmDetected()) {
|
||||||
mAudiodetectionAndAccelerometerViewModel.updateDevice(mAudiodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mAudiodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "Audio", (int) mAudiodetectionAndAccelerometerViewModel.getAmplitudeInDB());
|
mAudiodetectionAndAccelerometerViewModel.updateDevice(mAudiodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mAudiodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "Microfon", (int) mAudiodetectionAndAccelerometerViewModel.getAmplitudeInDB());
|
||||||
}
|
|
||||||
else if (mAudiodetectionAndAccelerometerViewModel.getMicrofonAlarmDetected() && mAudiodetectionAndAccelerometerViewModel.getAccelerometerAlarmDetected()) {
|
|
||||||
mAudiodetectionAndAccelerometerViewModel.updateDevice(mAudiodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mAudiodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "2AudioAndAccelerometer", 10);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
Toast.makeText(AudiodetectionAndAccelerometerActivity.this, "onChangedFalse", Toast.LENGTH_LONG).show();
|
|
||||||
mAudiodetectionAndAccelerometerViewModel.updateDevice(mAudiodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mAudiodetectionAndAccelerometerViewModel.getSystemTimeStamp(), false, "AudioAndAccelerometer", 0);
|
mAudiodetectionAndAccelerometerViewModel.updateDevice(mAudiodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mAudiodetectionAndAccelerometerViewModel.getSystemTimeStamp(), false, "AudioAndAccelerometer", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,19 +101,12 @@ public class VideodetectionAndAccelerometerActivity extends AppCompatActivity im
|
|||||||
@Override
|
@Override
|
||||||
public void onChanged(Boolean aBoolean) {
|
public void onChanged(Boolean aBoolean) {
|
||||||
if (aBoolean) {
|
if (aBoolean) {
|
||||||
if (mVideodetectionAndAccelerometerViewModel.getAccelerometerAlarmDetected() && !mVideodetectionAndAccelerometerViewModel.getVideoAlarmDetected()) {
|
mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "Accelerometer", 10);
|
||||||
mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "Accelerometer", 10);
|
|
||||||
}
|
|
||||||
else if (mVideodetectionAndAccelerometerViewModel.getVideoAlarmDetected() && !mVideodetectionAndAccelerometerViewModel.getAccelerometerAlarmDetected()) {
|
|
||||||
mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "Video", 10);
|
|
||||||
}
|
|
||||||
else if (mVideodetectionAndAccelerometerViewModel.getAccelerometerAlarmDetected() && mVideodetectionAndAccelerometerViewModel.getVideoAlarmDetected()) {
|
|
||||||
mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), true, "2VideoAndAccelerometer", 10);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), false, "VideoAndAccelerometer", 0);
|
mVideodetectionAndAccelerometerViewModel.updateDevice(mVideodetectionAndAccelerometerViewModel.getLocalDeviceUUID(), mVideodetectionAndAccelerometerViewModel.getSystemTimeStamp(), false, "Accelerometer", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -3,7 +3,6 @@ package com.example.greenwatch.sensors;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.ImageFormat;
|
import android.graphics.ImageFormat;
|
||||||
import android.media.Image;
|
import android.media.Image;
|
||||||
import android.os.Handler;
|
|
||||||
import android.util.Size;
|
import android.util.Size;
|
||||||
|
|
||||||
import androidx.camera.core.CameraSelector;
|
import androidx.camera.core.CameraSelector;
|
||||||
@ -19,18 +18,9 @@ public class CameraSensor {
|
|||||||
private final MutableLiveData<Boolean> mVideoAlarmDetected = new MutableLiveData<>();
|
private final MutableLiveData<Boolean> mVideoAlarmDetected = new MutableLiveData<>();
|
||||||
private static CameraSensor cameraSensorInstance;
|
private static CameraSensor cameraSensorInstance;
|
||||||
private boolean videoAlarmDetected;
|
private boolean videoAlarmDetected;
|
||||||
private boolean isMotionDetected;
|
|
||||||
private ByteBuffer previousBuffer;
|
private ByteBuffer previousBuffer;
|
||||||
private int previousWidth;
|
private int previousWidth;
|
||||||
private int previousHeight;
|
private int previousHeight;
|
||||||
private final int threshold = 50;
|
|
||||||
private int startX;
|
|
||||||
private int startY;
|
|
||||||
private int endX;
|
|
||||||
private int endY;
|
|
||||||
private static final long ALARM_RESET_DELAY = 5000;
|
|
||||||
private Runnable alarmResetRunnable;
|
|
||||||
private final Handler alarmResetHandler = new Handler();
|
|
||||||
|
|
||||||
private CameraSensor() {
|
private CameraSensor() {
|
||||||
videoAlarmDetected = false;
|
videoAlarmDetected = false;
|
||||||
@ -69,7 +59,7 @@ public class CameraSensor {
|
|||||||
|
|
||||||
if (previousHeight != 0) {
|
if (previousHeight != 0) {
|
||||||
assert currentImage != null;
|
assert currentImage != null;
|
||||||
isMotionDetected = compareFrames(currentImage);
|
videoAlarmDetected = compareFrames(currentImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
assert currentImage != null;
|
assert currentImage != null;
|
||||||
@ -82,18 +72,6 @@ public class CameraSensor {
|
|||||||
|
|
||||||
currentImage.close();
|
currentImage.close();
|
||||||
|
|
||||||
if (isMotionDetected) {
|
|
||||||
|
|
||||||
videoAlarmDetected = true;
|
|
||||||
|
|
||||||
if(alarmResetRunnable != null) {
|
|
||||||
alarmResetHandler.removeCallbacks(alarmResetRunnable);
|
|
||||||
}
|
|
||||||
|
|
||||||
alarmResetRunnable = this::resetAlarmStatus;
|
|
||||||
alarmResetHandler.postDelayed(alarmResetRunnable, ALARM_RESET_DELAY);
|
|
||||||
}
|
|
||||||
|
|
||||||
checkAlarmCondition();
|
checkAlarmCondition();
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -107,76 +85,36 @@ public class CameraSensor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private boolean compareFrames(Image currentImage) {
|
private boolean compareFrames(Image currentImage) {
|
||||||
Image.Plane[] planes = currentImage.getPlanes();
|
|
||||||
ByteBuffer currentBuffer = planes[0].getBuffer();
|
ByteBuffer currentBuffer = currentImage.getPlanes()[0].getBuffer();
|
||||||
|
|
||||||
int currentWidth = currentImage.getWidth();
|
int currentWidth = currentImage.getWidth();
|
||||||
int currentHeight = currentImage.getHeight();
|
int currentHeight = currentImage.getHeight();
|
||||||
int yRowStride = planes[0].getRowStride();
|
|
||||||
int yPixelStride = planes[0].getPixelStride();
|
|
||||||
|
|
||||||
if (previousWidth != currentWidth || previousHeight != currentHeight) {
|
if (previousWidth != currentWidth || previousHeight != currentHeight) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int blockSize = kleinstesQuadrat(previousHeight, previousWidth) / 8;
|
for (int row = 0; row < previousHeight; row++) {
|
||||||
|
for (int col = 0; col < previousWidth; col++) {
|
||||||
|
|
||||||
int numBlocksX = currentWidth / blockSize;
|
int previousIndex = row * previousWidth + col;
|
||||||
int numBlocksY = currentHeight / blockSize;
|
int currentIndex = row * currentWidth + col;
|
||||||
|
|
||||||
for (int blockY = 0; blockY < numBlocksY; blockY++) {
|
int previousPixel = previousBuffer.get(previousIndex) & 0xFF;
|
||||||
for (int blockX = 0; blockX < numBlocksX; blockX++) {
|
int currentPixel = currentBuffer.get(currentIndex) & 0xFF;
|
||||||
startX = blockX * blockSize;
|
|
||||||
startY = blockY * blockSize;
|
|
||||||
endX = startX + blockSize;
|
|
||||||
endY = startY + blockSize;
|
|
||||||
|
|
||||||
float currentLuminance = berechneMittlereLuminanz(currentBuffer, yRowStride, yPixelStride);
|
int pixelDifference = Math.abs(previousPixel - currentPixel);
|
||||||
float previousLuminance = berechneMittlereLuminanz(previousBuffer, yRowStride, yPixelStride);
|
int threshold = 120;
|
||||||
|
|
||||||
int pixelDifference = Math.abs((int) previousLuminance - (int) currentLuminance);
|
|
||||||
|
|
||||||
if (pixelDifference > threshold) {
|
if (pixelDifference > threshold) {
|
||||||
System.out.println(pixelDifference);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private float berechneMittlereLuminanz(ByteBuffer buffer, int rowStride, int pixelStride) {
|
|
||||||
int sumLuminance = 0;
|
|
||||||
int countPixels = 0;
|
|
||||||
|
|
||||||
for (int row = startY; row < endY; row++) {
|
|
||||||
for (int col = startX; col < endX; col++) {
|
|
||||||
int bufferIndex = row * rowStride + col * pixelStride;
|
|
||||||
int currentPixel = buffer.get(bufferIndex) & 0xFF;
|
|
||||||
|
|
||||||
sumLuminance += currentPixel;
|
|
||||||
countPixels++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return (float) sumLuminance / countPixels;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int kleinstesQuadrat(int height, int width) {
|
|
||||||
if (height == width) {
|
|
||||||
return height;
|
|
||||||
} else if (height > width) {
|
|
||||||
return kleinstesQuadrat(height - width, width);
|
|
||||||
} else {
|
|
||||||
return kleinstesQuadrat(height, width - height);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void resetAlarmStatus() {
|
|
||||||
videoAlarmDetected = false;
|
|
||||||
alarmResetRunnable = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void checkAlarmCondition() {
|
public void checkAlarmCondition() {
|
||||||
if (videoAlarmDetected && !mVideoAlarmDetected.getValue()) {
|
if (videoAlarmDetected && !mVideoAlarmDetected.getValue()) {
|
||||||
setMutableLiveDataVideoAlarmDetected();
|
setMutableLiveDataVideoAlarmDetected();
|
||||||
|
@ -19,7 +19,7 @@ import com.example.greenwatch.sensors.MicrofonHelperClasses.Verarbeitungsergebni
|
|||||||
private boolean microfonAlarmDetected = false;
|
private boolean microfonAlarmDetected = false;
|
||||||
private boolean kalibrierung_do = true;
|
private boolean kalibrierung_do = true;
|
||||||
private float threshold = 40;
|
private float threshold = 40;
|
||||||
private float sensitivity = 10;
|
private float sensitivity = 5;
|
||||||
|
|
||||||
private float amplitudeInDB = 0;
|
private float amplitudeInDB = 0;
|
||||||
|
|
||||||
@ -52,8 +52,9 @@ import com.example.greenwatch.sensors.MicrofonHelperClasses.Verarbeitungsergebni
|
|||||||
microfonAlarmDetected = false;
|
microfonAlarmDetected = false;
|
||||||
kalibrierung_do = true;
|
kalibrierung_do = true;
|
||||||
threshold = 40;
|
threshold = 40;
|
||||||
sensitivity = 10;
|
sensitivity = 5;
|
||||||
amplitudeInDB = 0;
|
amplitudeInDB = 0;
|
||||||
|
start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public float getAmplitudeInDB() {
|
public float getAmplitudeInDB() {
|
||||||
|
@ -120,12 +120,12 @@ public class AudiodetectionAndAccelerometerViewModel extends ViewModel implement
|
|||||||
public void onChanged(Boolean aBoolean) {
|
public void onChanged(Boolean aBoolean) {
|
||||||
setAccelerometerAlarmDetected(mAccelerometerAlarmDetected.getValue());
|
setAccelerometerAlarmDetected(mAccelerometerAlarmDetected.getValue());
|
||||||
setMicrofonAlarmDetected(mMicrofonAlarmDetected.getValue());
|
setMicrofonAlarmDetected(mMicrofonAlarmDetected.getValue());
|
||||||
if (microfonAlarmDetected || accelerometerAlarmDetected) {
|
if (microfonAlarmDetected || accelerometerAlarmDetected && !microfondetectionAndAccelerometerAlarmDetected) {
|
||||||
microfondetectionAndAccelerometerAlarmDetected = true;
|
microfondetectionAndAccelerometerAlarmDetected = true;
|
||||||
setMutableLiveDataMicrofondetectionAndAccelerometerAlarmDetected();
|
setMutableLiveDataMicrofondetectionAndAccelerometerAlarmDetected();
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (!microfonAlarmDetected && !accelerometerAlarmDetected) {
|
else if (!microfonAlarmDetected && !accelerometerAlarmDetected && microfondetectionAndAccelerometerAlarmDetected) {
|
||||||
microfondetectionAndAccelerometerAlarmDetected = false;
|
microfondetectionAndAccelerometerAlarmDetected = false;
|
||||||
setMutableLiveDataMicrofondetectionAndAccelerometerAlarmDetected();
|
setMutableLiveDataMicrofondetectionAndAccelerometerAlarmDetected();
|
||||||
}
|
}
|
||||||
|
@ -26,9 +26,7 @@ public class VideodetectionAndAccelerometerViewModel extends ViewModel implement
|
|||||||
private AccelerometerSensor mAccelerometerSensor;
|
private AccelerometerSensor mAccelerometerSensor;
|
||||||
private CameraSensor mCameraSensor;
|
private CameraSensor mCameraSensor;
|
||||||
private DeviceRepository mDeviceRepository;
|
private DeviceRepository mDeviceRepository;
|
||||||
private boolean videoAlarmDetected;
|
private boolean VideodetectionAndAccelerometerAlarmDetected;
|
||||||
private boolean accelerometerAlarmDetected;
|
|
||||||
private boolean videodetectionAndAccelerometerAlarmDetected;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
@ -56,9 +54,7 @@ public class VideodetectionAndAccelerometerViewModel extends ViewModel implement
|
|||||||
if (mVideoAlarmDetected == null) {
|
if (mVideoAlarmDetected == null) {
|
||||||
mVideoAlarmDetected = mCameraSensor.getVideoAlarmDetectedValue();
|
mVideoAlarmDetected = mCameraSensor.getVideoAlarmDetectedValue();
|
||||||
}
|
}
|
||||||
videodetectionAndAccelerometerAlarmDetected = false;
|
VideodetectionAndAccelerometerAlarmDetected = false;
|
||||||
videoAlarmDetected = false;
|
|
||||||
accelerometerAlarmDetected = false;
|
|
||||||
registerAlarmObserver();
|
registerAlarmObserver();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,37 +84,19 @@ public class VideodetectionAndAccelerometerViewModel extends ViewModel implement
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setMutableLiveDataVideodetectionAndAccelerometerAlarmDetected() {
|
private void setMutableLiveDataVideodetectionAndAccelerometerAlarmDetected() {
|
||||||
mVideodetectionAndAccelerometerAlarmDetected.setValue(videodetectionAndAccelerometerAlarmDetected);
|
mVideodetectionAndAccelerometerAlarmDetected.setValue(VideodetectionAndAccelerometerAlarmDetected);
|
||||||
}
|
|
||||||
|
|
||||||
private void setAccelerometerAlarmDetected(boolean accelerometerAlarmDetected) {
|
|
||||||
this.accelerometerAlarmDetected = accelerometerAlarmDetected;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean getAccelerometerAlarmDetected() {
|
|
||||||
return accelerometerAlarmDetected;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setVideoAlarmDetected(boolean videoAlarmDetected) {
|
|
||||||
this.videoAlarmDetected = videoAlarmDetected;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean getVideoAlarmDetected() {
|
|
||||||
return videoAlarmDetected;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Observer<Boolean> observer = new Observer<Boolean>() {
|
Observer<Boolean> observer = new Observer<Boolean>() {
|
||||||
@Override
|
@Override
|
||||||
public void onChanged(Boolean aBoolean) {
|
public void onChanged(Boolean aBoolean) {
|
||||||
setAccelerometerAlarmDetected(mAccelerometerAlarmDetected.getValue());
|
if (mVideoAlarmDetected.getValue() || mAccelerometerAlarmDetected.getValue() && !VideodetectionAndAccelerometerAlarmDetected) {
|
||||||
setVideoAlarmDetected(mVideoAlarmDetected.getValue());
|
VideodetectionAndAccelerometerAlarmDetected = true;
|
||||||
if (videoAlarmDetected || accelerometerAlarmDetected && !videodetectionAndAccelerometerAlarmDetected) {
|
|
||||||
videodetectionAndAccelerometerAlarmDetected = true;
|
|
||||||
setMutableLiveDataVideodetectionAndAccelerometerAlarmDetected();
|
setMutableLiveDataVideodetectionAndAccelerometerAlarmDetected();
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (!videoAlarmDetected && !accelerometerAlarmDetected && videodetectionAndAccelerometerAlarmDetected) {
|
else if (!mVideoAlarmDetected.getValue() && !mAccelerometerAlarmDetected.getValue() && VideodetectionAndAccelerometerAlarmDetected) {
|
||||||
videodetectionAndAccelerometerAlarmDetected = false;
|
VideodetectionAndAccelerometerAlarmDetected = false;
|
||||||
setMutableLiveDataVideodetectionAndAccelerometerAlarmDetected();
|
setMutableLiveDataVideodetectionAndAccelerometerAlarmDetected();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user