Skip to content

Instantly share code, notes, and snippets.

@ravindu9701
Created October 19, 2020 17:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ravindu9701/b2ef76f3d0ff83d7a5633f3217a66489 to your computer and use it in GitHub Desktop.
Save ravindu9701/b2ef76f3d0ff83d7a5633f3217a66489 to your computer and use it in GitHub Desktop.
import 'package:flutter/material.dart';
import 'dart:math' as math;
class BndBox extends StatelessWidget {
final List<dynamic> results;
final int previewH;
final int previewW;
final double screenH;
final double screenW;
final String model;
BndBox(this.results, this.previewH, this.previewW, this.screenH, this.screenW,
this.model);
@override
Widget build(BuildContext context) {
List<Widget> _renderBoxes() {
return results.map((re) {
var _x = re["rect"]["x"];
var _w = re["rect"]["w"];
var _y = re["rect"]["y"];
var _h = re["rect"]["h"];
var scaleW, scaleH, x, y, w, h;
if (screenH / screenW > previewH / previewW) {
scaleW = screenH / previewH * previewW;
scaleH = screenH;
var difW = (scaleW - screenW) / scaleW;
x = (_x - difW / 2) * scaleW;
w = _w * scaleW;
if (_x < difW / 2) w -= (difW / 2 - _x) * scaleW;
y = _y * scaleH;
h = _h * scaleH;
} else {
scaleH = screenW / previewW * previewH;
scaleW = screenW;
var difH = (scaleH - screenH) / scaleH;
x = _x * scaleW;
w = _w * scaleW;
y = (_y - difH / 2) * scaleH;
h = _h * scaleH;
if (_y < difH / 2) h -= (difH / 2 - _y) * scaleH;
}
return Positioned(
left: math.max(0, x),
top: math.max(0, y),
width: w,
height: h,
child: Container(
padding: EdgeInsets.only(top: 5.0, left: 5.0),
decoration: BoxDecoration(
border: Border.all(
color: Color.fromRGBO(37, 213, 253, 1.0),
width: 3.0,
),
),
child: Text(
"${re["detectedClass"]} ${(re["confidenceInClass"] * 100).toStringAsFixed(0)}%",
style: TextStyle(
color: Color.fromRGBO(37, 213, 253, 1.0),
fontSize: 14.0,
fontWeight: FontWeight.bold,
),
),
),
);
}).toList();
}
return Stack(
children: _renderBoxes(),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment