Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added button to switch from front camera to back camera #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Connect your device or start the emulator and run the following code
# change directories
cd Yoga-Guru

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove the changes below!
These have already been merged in the previous PR #9

# gets all the dependencies
flutter get pub

# run the app
flutter run
```
Expand Down
106 changes: 71 additions & 35 deletions lib/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Camera extends StatefulWidget {
class _CameraState extends State<Camera> {
CameraController controller;
bool isDetecting = false;
int indexCamera = 0;

static const platform = const MethodChannel('ondeviceML');

Expand All @@ -40,38 +41,60 @@ class _CameraState extends State<Camera> {
if (!mounted) {
return;
}
setState(() {});

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

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

Tflite.runPoseNetOnFrame(
bytesList: img.planes.map((plane) {
return plane.bytes;
}).toList(),
imageHeight: img.height,
imageWidth: img.width,
numResults: 1,
rotation: -90,
threshold: 0.2,
nmsRadius: 10,
).then((recognitions) {
int endTime = new DateTime.now().millisecondsSinceEpoch;
print("Detection took ${endTime - startTime}");

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

isDetecting = false;
});
}
});
setState((){});
_poseCamera();

});
}
}

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

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

Tflite.runPoseNetOnFrame(
bytesList: img.planes.map((plane) {
return plane.bytes;
}).toList(),
imageHeight: img.height,
imageWidth: img.width,
numResults: 1,
rotation: -90,
threshold: 0.2,
nmsRadius: 10,
).then((recognitions) {
int endTime = new DateTime.now().millisecondsSinceEpoch;
print("Detection took ${endTime - startTime}");

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

isDetecting = false;
});
}
});
}


void _onSwitchCamera() {
indexCamera = indexCamera < widget.cameras.length - 1 ? indexCamera + 1 : 0;

controller = new CameraController(
widget.cameras[indexCamera],
ResolutionPreset.high,
);

controller.initialize().then((_) {
if (!mounted) {
return;
}
setState((){});
_poseCamera();
});
}

@override
void dispose() {
controller?.dispose();
Expand All @@ -93,12 +116,25 @@ class _CameraState extends State<Camera> {
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),
return Stack(
children: <Widget> [
OverflowBox(
maxHeight: screenRatio > previewRatio ? screenH : screenW / previewW * previewH,
maxWidth: screenRatio > previewRatio ? screenH / previewH * previewW : screenW,
child: CameraPreview(controller),
),
Align(
alignment: Alignment.centerRight,
child: FloatingActionButton(
onPressed: () {
_onSwitchCamera();
},
child: Icon(Icons.switch_camera),
backgroundColor: Colors.green,
elevation: 5,
),
)
],
);
}
}
}