From 08c4610e7418612ecdca155d4fc654ceac107513 Mon Sep 17 00:00:00 2001 From: RobViren Date: Mon, 15 Nov 2021 15:44:46 -0600 Subject: [PATCH] moment work --- lib/main.dart | 54 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/lib/main.dart b/lib/main.dart index 20fb1fc..66dbfaa 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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 { 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 { } 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 { 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; }