adding basic navbar to the app and fixing camera loading.

This commit is contained in:
Lucas Oskorep
2022-08-28 21:39:43 -04:00
parent c3420067e0
commit 342e567736
3 changed files with 202 additions and 98 deletions
+14 -14
View File
@@ -49,17 +49,17 @@ deploy-android-job:
- MacOS - MacOS
build-ios-debug: #build-ios-debug:
stage: build # stage: build
only: # only:
- branches # - branches
script: # script:
- sh install_tflite.sh -d # - sh install_tflite.sh -d
- flutter build ipa # - flutter build ipa
# artifacts: ## artifacts:
# name: ios-debug ## name: ios-debug
# paths: ## paths:
# - build/app/outputs/bundle/debug/app-debug.aab ## - build/app/outputs/bundle/debug/app-debug.aab
# - build/app/outputs/flutter-apk/app-release.apk ## - build/app/outputs/flutter-apk/app-release.apk
tags: # tags:
- MacOS # - MacOS
+86 -34
View File
@@ -1,6 +1,7 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'dart:isolate'; import 'dart:isolate';
import 'dart:math';
import 'package:camera/camera.dart'; import 'package:camera/camera.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
@@ -34,11 +35,17 @@ class PokeFinder extends StatefulWidget {
class _PokeFinderState extends State<PokeFinder> with WidgetsBindingObserver { class _PokeFinderState extends State<PokeFinder> with WidgetsBindingObserver {
/// true when inference is ongoing /// true when inference is ongoing
///
final double _zoomSliderLogFactor = 1000.0;
bool predicting = false; bool predicting = false;
bool _cameraInitialized = false; bool _cameraReady = false;
bool _classifierInitialized = false; bool _classifierInitialized = false;
bool _saveClassifierImage = false; bool _saveClassifierImage = false;
int cameraIndex = 0; int _cameraIndex = 0;
double _minZoom = 1.0;
double _maxZoom = 1.0;
double _currentZoom = 1.0;
late CameraController cameraController; late CameraController cameraController;
@@ -91,19 +98,41 @@ class _PokeFinderState extends State<PokeFinder> with WidgetsBindingObserver {
options: config.interpreters[0]); options: config.interpreters[0]);
} }
void swapCamera() async {
logger.i(cameras);
logger.i(_cameraIndex);
_cameraIndex += 1;
if (cameras.length <= _cameraIndex) {
_cameraIndex = 0;
}
swapToCamera(cameras[_cameraIndex]);
}
void swapToCamera(CameraDescription cameraDescription) async { void swapToCamera(CameraDescription cameraDescription) async {
setState(() {
_cameraReady = false;
});
_refreshIndicatorKey.currentState?.show();
cameraController = CameraController(cameraDescription, ResolutionPreset.low, cameraController = CameraController(cameraDescription, ResolutionPreset.low,
enableAudio: false); enableAudio: false);
cameraController.initialize().then((_) async { cameraController.initialize().then((_) async {
/// previewSize is size of each image frame captured by controller /// previewSize is size of each image frame captured by controller
/// 352x288 on iOS, 240p (320x240) on Android with ResolutionPreset.low /// 352x288 on iOS, 240p (320x240) on Android with ResolutionPreset.low
await cameraController.startImageStream(onLatestImageAvailable); await cameraController.startImageStream(onLatestImageAvailable);
_maxZoom = await cameraController.getMaxZoomLevel();
_minZoom = await cameraController.getMinZoomLevel();
setState(() { setState(() {
_cameraInitialized = true; _cameraReady = true;
_currentZoom = 1.0;
}); });
}); });
} }
void saveMLImage() async {
logger.i('setting save classifier to true');
_saveClassifierImage = true;
}
/// Callback to receive each frame [CameraImage] perform inference on it /// Callback to receive each frame [CameraImage] perform inference on it
onLatestImageAvailable(CameraImage cameraImage) async { onLatestImageAvailable(CameraImage cameraImage) async {
if (_classifierInitialized) { if (_classifierInitialized) {
@@ -144,44 +173,66 @@ class _PokeFinderState extends State<PokeFinder> with WidgetsBindingObserver {
} }
} }
void swapCamera() async { final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
logger.i(cameras); GlobalKey<RefreshIndicatorState>();
logger.i(cameraIndex);
cameraIndex += 1;
if (cameras.length <= cameraIndex) {
cameraIndex = 0;
}
swapToCamera(cameras[cameraIndex]);
}
void saveMLImage() async {
logger.i('setting save classifier to true');
_saveClassifierImage = true;
}
void setZoom() async {
logger.i(await cameraController.getMinZoomLevel());
logger.i(await cameraController.getMaxZoomLevel());
logger.i(cameraController.getMinZoomLevel());
logger.i(cameraController.getMaxZoomLevel());
logger.i(cameraController.setZoomLevel(0.7));
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// Return empty container while the camera is not initialized
if (!_cameraInitialized) {
return Container();
}
return Column( return Column(
children: [ children: [
AspectRatio( RefreshIndicator(
aspectRatio: 1 / cameraController.value.aspectRatio, key: _refreshIndicatorKey,
child: CameraPreview(cameraController)), onRefresh: () async {
TextButton(onPressed: swapCamera, child: const Text('Change Camera!')), // Replace this delay with the code to be executed during refresh
// and return a Future when code finishes execution.
while (!_cameraReady) {
await Future.delayed(const Duration(milliseconds: 100));
}
return;
},
child: !_cameraReady? Container(width: 100, height: 100,): Stack(
clipBehavior: Clip.none,
alignment: Alignment.center,
children: [
AspectRatio(
aspectRatio: 1 / cameraController.value.aspectRatio,
child: CameraPreview(cameraController)),
Positioned(
top: -10,
child: TextButton(
onPressed: swapCamera,
child: const Text('Change Camera!')),
),
Positioned(
bottom: -10,
child: Row(
children: [
SizedBox(
width: MediaQuery.of(context).size.width - 100,
child: Slider(
min: pow(_minZoom, 1 / _zoomSliderLogFactor)
.toDouble(),
max: pow(_maxZoom, 1 / _zoomSliderLogFactor)
.toDouble(),
divisions: 100,
value: _currentZoom,
onChanged: (double value) {
logger.i('Zoom updated $value');
_currentZoom = value;
cameraController.setZoomLevel(
pow(_currentZoom, _zoomSliderLogFactor)
.toDouble());
})),
Text(
pow(_currentZoom, _zoomSliderLogFactor)
.toStringAsFixed(2),
style: const TextStyle(color: Colors.lightBlue))
// style: const TextStyle(color: Colors.lightBlue))
],
))
])),
TextButton( TextButton(
onPressed: saveMLImage, child: const Text('Save Model Image')), onPressed: saveMLImage, child: const Text('Save Model Image')),
TextButton(onPressed: setZoom, child: const Text('Zoom!'))
], ],
); );
} }
@@ -203,6 +254,7 @@ class _PokeFinderState extends State<PokeFinder> with WidgetsBindingObserver {
break; break;
case AppLifecycleState.resumed: case AppLifecycleState.resumed:
if (!cameraController.value.isStreamingImages) { if (!cameraController.value.isStreamingImages) {
swapToCamera(cameras[_cameraIndex]);
await cameraController.startImageStream(onLatestImageAvailable); await cameraController.startImageStream(onLatestImageAvailable);
} }
break; break;
+102 -50
View File
@@ -24,6 +24,7 @@ class _TensordexHomeState extends State<TensordexHome> {
/// Results from the image classifier /// Results from the image classifier
List<Recognition> results = [Recognition(1, 'NOTHING DETECTED', .5)]; List<Recognition> results = [Recognition(1, 'NOTHING DETECTED', .5)];
Stats stats = Stats(); Stats stats = Stats();
int _selectedNavBarIndex = 0;
/// Scaffold Key /// Scaffold Key
GlobalKey<ScaffoldState> scaffoldKey = GlobalKey(); GlobalKey<ScaffoldState> scaffoldKey = GlobalKey();
@@ -58,60 +59,111 @@ class _TensordexHomeState extends State<TensordexHome> {
}); });
} }
void _onNavBarTapped(int index) {
setState(() {
_selectedNavBarIndex = index;
});
}
static const TextStyle optionStyle =
TextStyle(fontSize: 30, fontWeight: FontWeight.bold);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final List<Widget> _widgetOptions = <Widget>[
Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
PokeFinder(
resultsCallback: resultsCallback, statsCallback: statsCallback),
Results(results, stats),
],
),
const Text(
'Index 1: Seen',
style: optionStyle,
),
const Text(
'Index 2: About',
style: optionStyle,
),
const Text(
'Index 3: Settings',
style: optionStyle,
),
];
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text(widget.title), title: Text(widget.title),
), ),
drawer: Drawer( // drawer: Drawer(
child: ListView( // child: ListView(
// Important: Remove any padding from the ListView. // // Important: Remove any padding from the ListView.
padding: EdgeInsets.zero, // padding: EdgeInsets.zero,
children: [ // children: [
const DrawerHeader( // const DrawerHeader(
decoration: BoxDecoration( // decoration: BoxDecoration(
color: Colors.blue, // color: Colors.blue,
), // ),
child: Text('Drawer Header'), // child: Text('Drawer Header'),
), // ),
ListTile( // ListTile(
title: const Text('Item 1'), // title: const Text('Item 1'),
onTap: () { // onTap: () {
// Update the state of the app. // // Update the state of the app.
// ... // // ...
}, // },
), // ),
ListTile( // ListTile(
title: const Text('Item 2'), // title: const Text('Item 2'),
onTap: () { // onTap: () {
// Update the state of the app. // // Update the state of the app.
// ... // // ...
}, // },
), // ),
], // ],
// ),
// ),
body: Center(
child: _widgetOptions.elementAt(_selectedNavBarIndex),
),
floatingActionButton: GestureDetector(
onLongPress: () {
_incrementCounter();
},
child: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.photo_camera),
), // This trailing comma makes auto-formatting nicer for build methods.
),
bottomNavigationBar: BottomNavigationBar(
backgroundColor: Colors.lightBlue,
type: BottomNavigationBarType.shifting,
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.camera),
label: 'Camera',
backgroundColor: Colors.lightBlue,
), ),
), BottomNavigationBarItem(
body: Center( icon: Icon(Icons.call),
child: Column( label: 'Calls',
mainAxisAlignment: MainAxisAlignment.start, backgroundColor: Colors.deepOrange),
children: <Widget>[ BottomNavigationBarItem(
PokeFinder( icon: Icon(Icons.chat),
resultsCallback: resultsCallback, label: 'Chats',
statsCallback: statsCallback), backgroundColor: Colors.red),
Results(results, stats), BottomNavigationBarItem(
], icon: Icon(Icons.settings),
label: 'Settings',
backgroundColor: Colors.purple,
), ),
), ],
floatingActionButton: GestureDetector( currentIndex: _selectedNavBarIndex,
onLongPress: () { selectedItemColor: Colors.amber,
_incrementCounter(); onTap: _onNavBarTapped,
}, ),
child: FloatingActionButton( );
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.photo_camera),
), // This trailing comma makes auto-formatting nicer for build methods.
));
} }
} }