Skip to content

Instantly share code, notes, and snippets.

@BoHellgren
BoHellgren / _classifyDog.dart
Last active March 4, 2020 12:25
_classifyDog MLKit version
Future<String> _classifyDog(imglib.Image img) async {
Uint8List png = imglib.encodePng(img);
List<VisionLabel> _cloudLabels =
await labelDetector.detectFromBinary(png, true);
for (int i = 0; i < _cloudLabels.length; i++) {
String foundLabel = _cloudLabels[i].label;
print(foundLabel);
if (isBreed(foundLabel)) {
String conf = (_cloudLabels[i].confidence * 100).toStringAsFixed(0);
print(foundLabel + ' ' + conf);
@BoHellgren
BoHellgren / _findDog.dart
Created March 2, 2020 12:44
_findDog MLKit version
Future<Rect> _findDog(CameraImage image) async {
imglib.Image img = _convertCameraImage(image);
Uint8List png = imglib.encodePng(img);
List<VisionLabel> _onDeviceLabels =
await labelDetector.detectFromBinary(png, false);
bool foundDog = false;
for (int i = 0; i < _onDeviceLabels.length; i++) {
if (_onDeviceLabels[i].label == "Dog") foundDog = true;
}
if (foundDog) {
@BoHellgren
BoHellgren / MiniMLPlugin.java
Created March 2, 2020 09:55
mini_ml java code
package se.ndssoft.mini_ml;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import androidx.annotation.NonNull;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
@BoHellgren
BoHellgren / mini_ml.dart
Created March 2, 2020 09:45
mini_ml dart code
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui';
import 'package:flutter/services.dart';
class MiniMl {
static const MethodChannel _channel =
const MethodChannel('plugins.flutter.io/mini_ml');
@BoHellgren
BoHellgren / main.dart
Last active December 28, 2020 10:35
Final main-dart
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 'breedinfo.dart';
@BoHellgren
BoHellgren / breedinfo.dart
Created January 10, 2020 15:22
breedinfo
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
class BreedInfo extends StatelessWidget {
final String breed;
BreedInfo({Key key, @required this.breed});
@override
Widget build(BuildContext context) {
String url = 'https://en.wikipedia.org/wiki/' + breed;
@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);
}
@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 / _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 / _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++) {