Compare commits
2 Commits
e9da712882
...
8cc57ec4d5
Author | SHA1 | Date | |
---|---|---|---|
8cc57ec4d5 | |||
19d1bfe1e3 |
@ -3,7 +3,10 @@
|
|||||||
xmlns:tools="http://schemas.android.com/tools">
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
<uses-feature android:name="android.hardware.camera"/>
|
<uses-feature android:name="android.hardware.camera"/>
|
||||||
<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.WRITE_EXTERNAL_STORAGE" />
|
||||||
|
<uses-feature android:name="android.hardware.Camera"/>
|
||||||
|
|
||||||
<application
|
<application
|
||||||
android:allowBackup="true"
|
android:allowBackup="true"
|
||||||
|
@ -10,17 +10,20 @@ import androidx.camera.core.ExperimentalGetImage;
|
|||||||
import android.Manifest;
|
import android.Manifest;
|
||||||
import android.content.pm.PackageManager;
|
import android.content.pm.PackageManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
import android.widget.ToggleButton;
|
import android.widget.ToggleButton;
|
||||||
|
import android.widget.VideoView;
|
||||||
|
|
||||||
import com.example.ueberwachungssystem.VideoDetection.VideoDetector;
|
import com.example.ueberwachungssystem.VideoDetection.VideoDetector;
|
||||||
|
|
||||||
@ExperimentalGetImage
|
@ExperimentalGetImage
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private static final int CAMERA_PERMISSION_REQUEST_CODE = 101;
|
private static final int CAMERA_PERMISSION_REQUEST_CODE = 100;
|
||||||
|
private static final int RECORD_VIDEO_PERMISSION_REQUEST_CODE = 102;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -55,6 +58,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
vd.stopDetection();
|
vd.stopDetection();
|
||||||
|
textView.setText("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -76,9 +80,9 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE && grantResults.length > 0) {
|
if (requestCode == CAMERA_PERMISSION_REQUEST_CODE && grantResults.length > 0) {
|
||||||
boolean cameraRights = grantResults[0] == PackageManager.PERMISSION_GRANTED;
|
boolean cameraRights = grantResults[0] == PackageManager.PERMISSION_GRANTED;
|
||||||
if (cameraRights) {
|
if (cameraRights) {
|
||||||
Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
|
Toast.makeText(this, "permission granted", Toast.LENGTH_LONG).show();
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
|
Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -32,15 +32,14 @@ public class VideoDetector extends Detector {
|
|||||||
// Preview Camera Image
|
// Preview Camera Image
|
||||||
private PreviewView previewView = null;
|
private PreviewView previewView = null;
|
||||||
// Detect Violation
|
// Detect Violation
|
||||||
private static final int PIXEL_THRESHOLD = 60; // Luminosity (brightness channel of YUV_420_888)
|
private static final float PIXEL_THRESHOLD = 30f; // Luminosity (brightness channel of YUV_420_888)
|
||||||
private static final float ALARM_THRESHOLD = 0.5f; // Percent of pixels changed
|
private static final float ALARM_THRESHOLD = 1f; // Percent of pixels changed
|
||||||
private ByteBuffer previousBuffer = null;
|
private ByteBuffer previousBuffer = null;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Constructor */
|
/** Constructor */
|
||||||
public VideoDetector(Context context) {
|
public VideoDetector(Context context) {
|
||||||
|
|
||||||
super();
|
super();
|
||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
@ -59,6 +58,7 @@ public class VideoDetector extends Detector {
|
|||||||
cameraProvider = cameraProviderFuture.get();
|
cameraProvider = cameraProviderFuture.get();
|
||||||
bindLuminosityAnalysis(cameraProvider);
|
bindLuminosityAnalysis(cameraProvider);
|
||||||
isDetectionRunning = true;
|
isDetectionRunning = true;
|
||||||
|
previousBuffer = null;
|
||||||
} catch (ExecutionException | InterruptedException e) {
|
} catch (ExecutionException | InterruptedException e) {
|
||||||
// No errors need to be handled for this Future. This should never be reached.
|
// No errors need to be handled for this Future. This should never be reached.
|
||||||
}
|
}
|
||||||
@ -117,24 +117,17 @@ public class VideoDetector extends Detector {
|
|||||||
if (previewView != null)
|
if (previewView != null)
|
||||||
preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
preview.setSurfaceProvider(previewView.getSurfaceProvider());
|
||||||
|
|
||||||
|
cameraProvider.unbindAll();
|
||||||
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview);
|
cameraProvider.bindToLifecycle((LifecycleOwner) context, cameraSelector, imageAnalysis, preview);
|
||||||
stopDetection();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Calculate Amount of Pixels changed */
|
/** Calculate Amount of Pixels changed */
|
||||||
private int getChangedPixelCount(Image image) {
|
private int getChangedPixelCount(Image image) {
|
||||||
int width = image.getWidth();
|
|
||||||
int height = image.getHeight();
|
|
||||||
|
|
||||||
Image.Plane[] planes = image.getPlanes();
|
Image.Plane[] planes = image.getPlanes();
|
||||||
ByteBuffer buffer = planes[0].getBuffer();
|
ByteBuffer buffer = planes[0].getBuffer();
|
||||||
|
|
||||||
int yRowStride = image.getPlanes()[0].getRowStride();
|
|
||||||
int yPixelStride = image.getPlanes()[0].getPixelStride();
|
|
||||||
|
|
||||||
int changedPixelCount = 0;
|
|
||||||
|
|
||||||
if (previousBuffer == null) {
|
if (previousBuffer == null) {
|
||||||
previousBuffer = ByteBuffer.allocate(buffer.capacity());
|
previousBuffer = ByteBuffer.allocate(buffer.capacity());
|
||||||
buffer.rewind();
|
buffer.rewind();
|
||||||
@ -143,6 +136,15 @@ public class VideoDetector extends Detector {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int width = image.getWidth();
|
||||||
|
int height = image.getHeight();
|
||||||
|
|
||||||
|
int yRowStride = image.getPlanes()[0].getRowStride();
|
||||||
|
int yPixelStride = image.getPlanes()[0].getPixelStride();
|
||||||
|
|
||||||
|
int changedPixelCount = 0;
|
||||||
|
|
||||||
|
// Count changed pixels
|
||||||
for (int y = 0; y < height; ++y) {
|
for (int y = 0; y < height; ++y) {
|
||||||
for (int x = 0; x < width; x++) {
|
for (int x = 0; x < width; x++) {
|
||||||
int index = (y * yRowStride) + (x * yPixelStride);
|
int index = (y * yRowStride) + (x * yPixelStride);
|
||||||
@ -153,6 +155,7 @@ public class VideoDetector extends Detector {
|
|||||||
changedPixelCount++;
|
changedPixelCount++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset and copy Byte Buffer
|
// Reset and copy Byte Buffer
|
||||||
buffer.rewind();
|
buffer.rewind();
|
||||||
previousBuffer.rewind();
|
previousBuffer.rewind();
|
||||||
|
@ -32,6 +32,8 @@
|
|||||||
android:id="@+id/previewView"
|
android:id="@+id/previewView"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:backgroundTint="@android:color/black"/>
|
android:backgroundTint="@android:color/black">
|
||||||
|
|
||||||
|
</androidx.camera.view.PreviewView>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
Loading…
x
Reference in New Issue
Block a user