I'm trying to make my app take a picture when certain objects is detected. In this case, the object is "Person" and still can't get it to work. The app detects the object but doesn't take a picture. I'm using TFlite for this app.
Here's my code to make take the picture. When the text value from the bounding box turns "Person" it will take the picture or that's what I thought.
if (Detected == "Person") {
controller.stopImageStream();
XFile image = controller.takePicture();
image.saveTo(image.path);
// ignore: use_build_context_synchronously
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => DisplayPictureScreen(
imagePath: image.path,
),
),
);
}
This is my whole bounding box code
// ignore_for_file: prefer_typing_uninitialized_variables, no_leading_underscores_for_local_identifiers
import 'package:camera/camera.dart';
import 'package:camera_test/detection/l_camera.dart';
import 'package:flutter/material.dart';
import 'dart:math' as math;
class BoundingBox extends StatelessWidget {
final List<dynamic> results;
final int previewH;
final int previewW;
final double screenH;
final double screenW;
const BoundingBox(
this.results, this.previewH, this.previewW, this.screenH, this.screenW,
{super.key});
get controller => null;
@override
Widget build(BuildContext context) {
List<Widget> _renderBox() {
//daripada result akan ubah bounding box
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;
final String Detected = "${re["detectedClass"]}";
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;
}
if (Detected == "Person") {
controller.stopImageStream();
XFile image = controller.takePicture();
image.saveTo(image.path);
// ignore: use_build_context_synchronously
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => DisplayPictureScreen(
imagePath: image.path,
),
),
);
}
return Positioned(
left: math.max(0, x),
top: math.max(0, y),
width: w,
height: h,
child: Container(
padding: const EdgeInsets.only(top: 5.0, left: 5.0),
decoration: BoxDecoration(
border: Border.all(
color: const Color.fromARGB(255, 77, 255, 0),
width: 3.0,
),
),
child: Text(
"${re["detectedClass"]} ${(re["confidenceInClass"] * 100).toStringAsFixed(0)}%",
style: const TextStyle(
color: Color.fromARGB(255, 77, 255, 0),
fontSize: 14.0,
fontWeight: FontWeight.bold,
),
),
),
);
}).toList();
}
return Stack(
children: _renderBox(),
);
}
}