moment work

This commit is contained in:
2021-11-15 15:44:46 -06:00
parent 61cb7ef282
commit 08c4610e74

View File

@@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:ffi';
import 'package:camera/camera.dart';
import 'dart:typed_data';
@@ -25,11 +26,16 @@ class _CameraAppState extends State<CameraApp> {
var image_height = 0;
var posy = 0.0;
var posx = 0.0;
var count = 0.0;
static const alpha = 0.99;
static const thresh = 25;
Uint8List? previousImage;
Uint8List? diff;
Uint8List? dst;
Uint64List? diffValues;
Float32List? background;
Float64List? sums;
Float64List? diffWeightsX;
@override
void initState() {
@@ -94,7 +100,8 @@ class _CameraAppState extends State<CameraApp> {
}
void handleImage(CameraImage src) {
sums = Float64List(src.height);
diffWeightsX = Float64List(src.width);
diffValues = Uint64List(src.width);
if (previousImage == null || background == null) {
previousImage = Uint8List(src.planes[0].bytes.length);
@@ -104,27 +111,42 @@ class _CameraAppState extends State<CameraApp> {
image_width = src.width;
image_height = src.height;
} else {
for (var i = 0; i < src.width; i++) {
for (var j = 0; j < src.height; j++) {
for (var i = 0; i < image_height; i++) {
for (var j = 0; j < image_width; j++) {
//Background calculation
background![i + j] = (background![i + j] * 0.99 +
src.planes[0].bytes[i + j].toDouble() * (1.0 - 0.99));
background![i + j] = (background![i + j] * alpha +
src.planes[0].bytes[i + j].toDouble() * (1.0 - alpha));
//Difference calculation
var diff_val =
(background![i + j].toInt() - src.planes[0].bytes[i + j]).abs();
if (diff_val > 50) {
diff![i + j] = diff_val;
} else {
diff![i + j] = 0;
diff![i + j] = (background![i + j].toInt() - src.planes[0].bytes[i + j]).abs();
if(diff![i + j] > thresh) {
diffWeightsX![i] += diff![i + j] * j;
diffValues![i] += diff![i + j];
}
//Center of motion calc
sums[i] +=
}
}
// Processing
print(diff![0]);
//Average X Position
posx = 0;
posy = 0;
var currentCount = 0;
var weightsY = 0;
for(var i = 0; i < diffWeightsX!.length; i++){
if(diffValues![i] > 0){
posx += (diffWeightsX![i]/diffValues![i])/diffWeightsX!.length;
weightsY += diffValues![i] * i;
currentCount += diffValues![i];
}
}
if(currentCount > 0) {
posy = weightsY/currentCount;
}
count = count * alpha + (currentCount/(image_width * image_height * 255) * 100.0) * (1.0 - alpha);
print(count);
// End Processing
previousImage = src.planes[0].bytes;
}