Woop Woop

This commit is contained in:
2021-11-16 16:22:32 -06:00
parent 08c4610e74
commit 9942c66e0c
4 changed files with 137 additions and 55 deletions

19
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,19 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "reprecord-app",
"request": "launch",
"type": "dart"
},
{
"name": "reprecord-app (profile mode)",
"request": "launch",
"type": "dart",
"flutterMode": "profile"
}
]
}

View File

@@ -1,10 +1,13 @@
import 'dart:async'; import 'dart:async';
import 'dart:ffi'; import 'dart:ffi';
import 'package:camera/camera.dart'; import 'dart:ui';
import 'dart:typed_data'; import 'dart:typed_data';
import 'dart:math';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'package:image/image.dart' as img;
List<CameraDescription> cameras = []; List<CameraDescription> cameras = [];
Future<void> main() async { Future<void> main() async {
@@ -26,16 +29,16 @@ class _CameraAppState extends State<CameraApp> {
var image_height = 0; var image_height = 0;
var posy = 0.0; var posy = 0.0;
var posx = 0.0; var posx = 0.0;
var count = 0.0; var count = 0;
static const alpha = 0.99; var frameNumber = 0;
static const thresh = 25; static const alpha = 0.90;
Uint8List? previousImage; static const thresh = 50;
Uint8List? diff;
Uint8List? dst;
Uint64List? diffValues;
Float32List? background;
Float64List? diffWeightsX;
var previousImage = <int>[];
var gray = <int>[];
var diff = <int>[];
var dst = <int>[];
var background = <double>[];
@override @override
void initState() { void initState() {
@@ -67,8 +70,15 @@ class _CameraAppState extends State<CameraApp> {
appBar: AppBar( appBar: AppBar(
title: const Text('Home'), title: const Text('Home'),
), ),
body: Column(children: <Widget>[ body: Stack(children: <Widget>[
CameraPreview(cameraController), diffView(),
Container(
height: image_height.toDouble(),
width: image_width.toDouble(),
child: CustomPaint(
painter: OpenPainter(posx, posy),
),
),
]), ]),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
child: const Icon(Icons.plus_one), child: const Icon(Icons.plus_one),
@@ -81,6 +91,17 @@ class _CameraAppState extends State<CameraApp> {
); );
} }
Widget diffView() {
return Transform.rotate(
angle: 0.0, //pi * -0.5
child: diff.isEmpty
? Container()
: Image.memory(Uint8List.fromList(img.encodeJpg((img.Image.fromBytes(
image_width, image_height, diff,
format: img.Format.luminance))))),
);
}
void startStream() { void startStream() {
final CameraController? cameraController = controller; final CameraController? cameraController = controller;
@@ -99,56 +120,62 @@ class _CameraAppState extends State<CameraApp> {
}); });
} }
void handleImage(CameraImage src) { void handleImage(CameraImage src) async {
diffWeightsX = Float64List(src.width); if (previousImage.isEmpty) {
diffValues = Uint64List(src.width); previousImage = List.filled(src.planes[0].bytes.length, 0);
gray = List.filled(src.planes[0].bytes.length, 0);
diff = List.filled(src.planes[0].bytes.length, 0);
dst = List.filled(src.planes[0].bytes.length, 0);
background = List.filled(src.planes[0].bytes.length, 0.0);
if (previousImage == null || background == null) {
previousImage = Uint8List(src.planes[0].bytes.length);
dst = Uint8List(src.planes[0].bytes.length);
diff = Uint8List(src.planes[0].bytes.length);
background = Float32List(src.planes[0].bytes.length);
image_width = src.width; image_width = src.width;
image_height = src.height; image_height = src.height;
} else { } else {
for (var i = 0; i < image_height; i++) { if (frameNumber % 10 == 0) {
for (var j = 0; j < image_width; j++) { gray = src.planes[0].bytes;
//Background calculation
background![i + j] = (background![i + j] * alpha +
src.planes[0].bytes[i + j].toDouble() * (1.0 - alpha));
//Difference calculation count = 0;
diff![i + j] = (background![i + j].toInt() - src.planes[0].bytes[i + j]).abs(); var xVal = 0.0;
if(diff![i + j] > thresh) { var yVal = 0.0;
diffWeightsX![i] += diff![i + j] * j;
diffValues![i] += diff![i + j]; for (var i = 0; i < gray.length; i++) {
background[i] = background[i] * alpha + gray[i] * (1.0 - alpha);
diff[i] = (gray[i] - background[i].toInt()).abs();
dst[i] = background[i].toInt();
if (diff[i] < thresh) {
diff[i] = 0;
} else {
//diff[i] = 255;
xVal += diff[i] * ((i % image_width) + 1);
yVal += diff[i] * ((i % image_height) + 1);
count += diff[i];
} }
} }
}
// Processing
//Average X Position // End Processing
posx = 0; previousImage = gray.toList();
posy = 0; if (count != 0) {
var currentCount = 0; posx = xVal / count;
var weightsY = 0; posy = yVal / count;
for(var i = 0; i < diffWeightsX!.length; i++){ print("$count ${posx.toInt()} ${posy.toInt()}");
if(diffValues![i] > 0){
posx += (diffWeightsX![i]/diffValues![i])/diffWeightsX!.length;
weightsY += diffValues![i] * i;
currentCount += diffValues![i];
} }
setState(() {});
} }
frameNumber += 1;
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;
} }
} }
} }
class OpenPainter extends CustomPainter {
double posx;
double posy;
OpenPainter(this.posx, this.posy);
@override
void paint(Canvas canvas, Size size) {
canvas.drawCircle(Offset(posx, posy), 10,
Paint()..color = Color.fromARGB(255, 0, 255, 0));
}
@override
bool shouldRepaint(CustomPainter oldDelegate) => true;
}

View File

@@ -1,6 +1,13 @@
# Generated by pub # Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile # See https://dart.dev/tools/pub/glossary#lockfile
packages: packages:
archive:
dependency: transitive
description:
name: archive
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.6"
async: async:
dependency: transitive dependency: transitive
description: description:
@@ -71,6 +78,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.3.2" version: "0.3.2"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
fake_async: fake_async:
dependency: transitive dependency: transitive
description: description:
@@ -107,6 +121,13 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
image:
dependency: "direct main"
description:
name: image
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.8"
js: js:
dependency: transitive dependency: transitive
description: description:
@@ -149,6 +170,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.11.1" version: "1.11.1"
petitparser:
dependency: transitive
description:
name: petitparser
url: "https://pub.dartlang.org"
source: hosted
version: "4.4.0"
plugin_platform_interface: plugin_platform_interface:
dependency: transitive dependency: transitive
description: description:
@@ -231,6 +259,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.0" version: "2.1.0"
xml:
dependency: transitive
description:
name: xml
url: "https://pub.dartlang.org"
source: hosted
version: "5.3.1"
sdks: sdks:
dart: ">=2.14.0 <3.0.0" dart: ">=2.14.0 <3.0.0"
flutter: ">=2.5.0" flutter: ">=2.5.0"

View File

@@ -11,6 +11,7 @@ dependencies:
camera: ^0.9.4+3 camera: ^0.9.4+3
flutter: flutter:
sdk: flutter sdk: flutter
image: ^3.0.8
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: