Skip to content

Instantly share code, notes, and snippets.

@BoHellgren
BoHellgren / dogcamera1.dart
Last active January 11, 2020 14:36
Dog Camera step 1
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:camera/camera.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
@BoHellgren
BoHellgren / dogcamera2.dart
Last active January 7, 2020 09:23
Dog camera step 2
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:camera/camera.dart';
import 'package:soundpool/soundpool.dart';
import 'dart:ui' as ui;
import 'package:flutter/foundation.dart';
import 'package:flutter/rendering.dart';
void main() => runApp(MyApp());
@BoHellgren
BoHellgren / dogcamera3.dart
Last active January 7, 2020 14:07
Dog camera step 3
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:camera/camera.dart';
import 'package:soundpool/soundpool.dart';
import 'dart:ui' as ui;
import 'dart:typed_data';
import 'package:image/image.dart' as imglib;
void main() => runApp(MyApp());
@BoHellgren
BoHellgren / dogcamera4.dart
Last active May 20, 2020 07:38
Dog camera step 4
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:camera/camera.dart';
import 'package:soundpool/soundpool.dart';
import 'dart:ui' as ui;
import 'dart:typed_data';
import 'package:image/image.dart' as imglib;
import 'package:tflite/tflite.dart';
void main() => runApp(MyApp());
@BoHellgren
BoHellgren / dogcamera5.dart
Last active January 21, 2020 09:03
Dog camera step 5
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:camera/camera.dart';
import 'package:soundpool/soundpool.dart';
import 'dart:ui' as ui;
import 'dart:typed_data';
import 'package:image/image.dart' as imglib;
import 'package:flutter/foundation.dart';
import 'package:tflite/tflite.dart';
import 'dart:io';
@BoHellgren
BoHellgren / ButtonsPainter.dart
Created January 6, 2020 17:52
ButtonsPainter
class ButtonsPainter extends CustomPainter {
ui.Image buttonImage;
ButtonsPainter(this.buttonImage);
@override
void paint(Canvas canvas, Size size) {
var paint = Paint();
// First paint black field around buttons with low opacity
paint.color = Colors.black.withOpacity(0.1);
Rect rect =
@BoHellgren
BoHellgren / _convertCameraImage.dart
Created January 7, 2020 13:13
_convertCameraImage function
static imglib.Image _convertCameraImage(CameraImage image) {
int width = image.width;
int height = image.height;
// imglib -> Image package from https://pub.dartlang.org/packages/image
var img = imglib.Image(width, height); // Create Image buffer
const int hexFF = 0xFF000000;
final int uvyButtonStride = image.planes[1].bytesPerRow;
final int uvPixelStride = image.planes[1].bytesPerPixel;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
@BoHellgren
BoHellgren / _findDog.dart
Last active January 8, 2020 09:37
_findDog
Future<Map> _findDog(CameraImage image) async {
List resultList = await Tflite.detectObjectOnFrame(
bytesList: image.planes.map((plane) {
return plane.bytes;
}).toList(),
model: "SSDMobileNet",
imageHeight: image.height,
imageWidth: image.width,
imageMean: 127.5,
imageStd: 127.5,
@BoHellgren
BoHellgren / RectPainter.dart
Created January 8, 2020 09:40
RectPainter
class RectPainter extends CustomPainter {
Map rect;
RectPainter(this.rect);
@override
void paint(Canvas canvas, Size size) {
if (rect != null) {
final paint = Paint();
paint.color = Colors.yellow;
paint.style = PaintingStyle.stroke;
paint.strokeWidth = 2.0;
@BoHellgren
BoHellgren / _classifyDog.dart
Created January 8, 2020 16:09
_classifyDog
Future<String> _classifyDog(imglib.Image croppedImage) async {
while (_tfliteBusy) await Future.delayed(Duration(milliseconds: 100));
_tfliteBusy = true;
Uint8List croppedPng = imglib.encodePng(croppedImage);
try {
File(_tempPath).deleteSync();
} catch (e) {
print(e);
}