LeonardoITCode
LeonardoITCode

Reputation: 1

Device Orientation Incorrect on Flutter/Camera Plugin 0.5.3

I'm new to both Flutter and Dart, and I'm trying to use the Camera Plugin on XIAOMI MI 9 SE (ANDROID) to detect objects on my TensorFlow Lite model. Everything works in Portrait mode, but when I turn the smartphone on Landscape view the preview doesn't rotate, keep the portrait' orientation and appears incorrect.

I tried use other versions of the Camera Plugin and it was no use.

detectObjectOnFrame Code:

         ` Tflite.detectObjectOnFrame(
            bytesList: img.planes.map((plane) {
              return plane.bytes;
            }).toList(),
            model: widget.model == yolo ? "YOLO" : "SSDMobileNet",
            imageHeight: img.height,
            imageWidth: img.width,
            imageMean: widget.model == yolo ? 0 : 127.5,
            imageStd: widget.model == yolo ? 255.0 : 127.5,
            numResultsPerClass: 1,
            threshold: widget.model == yolo ? 0.2 : 0.4,
          ).then((recognitions) {
            int endTime = new DateTime.now().millisecondsSinceEpoch;
            print("Detection took ${endTime - startTime}");

            widget.setRecognitions(recognitions, img.height, img.width);

            isDetecting = false;`

CAMERA.DART file:

final List cameras;
final Callback setRecognitions;
final String model;

Camera(this.cameras, this.model, this.setRecognitions);

_CameraState createState() => new _CameraState();
}

class _CameraState extends State {
CameraController controller;
bool isDetecting = false;

void initState() {
super.initState();

if (widget.cameras == null || widget.cameras.length < 1) {
  print('No camera is found');
} else {
  controller = new CameraController(
    widget.cameras[0],
    ResolutionPreset.high,
  );
  controller.initialize().then((_) {
    if (!mounted) {
      return;
    }
    setState(() {});

    controller.startImageStream((CameraImage img) {
      if (!isDetecting) {
        isDetecting = true;

        int startTime = new DateTime.now().millisecondsSinceEpoch;

        if (widget.model == mobilenet) {
          ..........
           .......... (Ignored code)

            isDetecting = false;
          });
        } else {
          Tflite.detectObjectOnFrame(
           ..........
           .......... (Ignored code)

          });
        }
      }
    });
  });
}
}

void dispose() {
controller?.dispose();
super.dispose();
}

Widget build(BuildContext context) {
if (controller == null || !controller.value.isInitialized) {
return Container();
}

var tmp = MediaQuery.of(context).size;
var screenH = math.max(tmp.height, tmp.width);
var screenW = math.min(tmp.height, tmp.width);
tmp = controller.value.previewSize;
var previewH = math.max(tmp.height, tmp.width);
var previewW = math.min(tmp.height, tmp.width);
var screenRatio = screenH / screenW;
var previewRatio = previewH / previewW;

return OverflowBox(
  maxHeight:
      screenRatio > previewRatio ? screenH : screenW / previewW * previewH,
  maxWidth:
      screenRatio > previewRatio ? screenH / previewH * previewW : screenW,
  child: CameraPreview(controller),
);
}
}

HOME.DART:

setState(() {
_model = model;
});
loadModel();
}

setRecognitions(recognitions, imageHeight, imageWidth) {
setState(() {
_recognitions = recognitions;
_imageHeight = imageHeight;
_imageWidth = imageWidth;
});
}

@override
Widget build(BuildContext context) {
Size screen = MediaQuery.of(context).size;
return Scaffold(
body: _model == ""
? Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
RaisedButton(
child: const Text(yolo),
onPressed: () => onSelect(yolo),
),
],
),
)
: Stack(
children: [
Camera(
widget.cameras,
_model,
setRecognitions,
),
BndBox(
_recognitions == null ? [] : _recognitions,
math.max(_imageHeight, _imageWidth),
math.min(_imageHeight, _imageWidth),
screen.height,
screen.width,
_model),
],
),
);
}
}

Pubspec.yaml:

flutter_test:
sdk: flutter

camera: ^0.5.3
tflite: 1.0.4

This code give me incorrect image on Landscape view, the preview doesn't turn together with smartphone.

I appreciated any help. Thanks.

Upvotes: 0

Views: 1819

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57203

In Landscape mode, you can put a RotatedBox around CameraPreview. It should not change the input to TFlite: the frames will arrive to detectObjectOnFrame() in landscape, so if there is some special rotation involved, you should disable it.

Upvotes: 1

Related Questions