Skip to content

Commit 449293d

Browse files
committed
❄️ restructred code
1 parent 7ac295c commit 449293d

File tree

8 files changed

+120
-154
lines changed

8 files changed

+120
-154
lines changed

lib/app/colors.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import 'package:flutter/material.dart';
2+
3+
const Color white = Colors.white;
4+
const Color red = Colors.red;
5+
const Color yellow = Colors.yellow;
6+
const Color green = Colors.green;
7+
const Color black = Colors.black;
8+
const Color yellowAccent = Colors.yellowAccent;
9+
const Color transparent = Colors.transparent;
10+
const Color greenAccent = Colors.greenAccent;

lib/app/icons.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import 'package:flutter/material.dart';
2+
3+
const IconData exit = Icons.exit_to_app;
4+
const IconData cameraFront = Icons.camera_front;
5+
const IconData cameraRear = Icons.camera_rear;
6+
const IconData code = Icons.code;
7+
const IconData imageIcon = Icons.image;

lib/app/strings.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const String withMask = "with_mask";
2+
const String withoutMask = "without_mask";
3+
const String labelString = "label";
4+
const String confidenceString = "confidence";
5+
const String dontShake = "Don\'t Shake your mobile";
6+
const String wearingMask = "Wearing mask";
7+
const String noMask = "No mask";
8+
const String pickIFG = "Pick Image from gallery";
9+
const String selectImageP = "Select an Image to Show Preview";
10+
const String gilroy = "Gilroy";
11+
const String noCamera = 'Looks like there are no cameras';
12+
const String codeString = "Code";
13+
const String liveCamera = "Live Camera";
14+
const String title = 'Face Mask Detection';
15+
const String fromGallery = "From Gallery";
16+
17+
const String illustrator = "assets/images/illustrator.jpeg";
18+
const String urlString =
19+
"https://github.com/shashiben/flutter-face-mask-detection";

lib/ui/camera_screen.dart

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import 'dart:math';
22

33
import 'package:camera/camera.dart';
4+
import 'package:face_mask_detection/app/colors.dart';
5+
import 'package:face_mask_detection/app/icons.dart';
6+
import 'package:face_mask_detection/app/strings.dart';
47
import 'package:face_mask_detection/services/tensorflow_services.dart';
5-
import 'package:face_mask_detection/utils/overlay.dart' as ol;
8+
import 'package:face_mask_detection/widgets/overlay.dart' as overlay;
69
import 'package:flutter/material.dart';
710

811
class CameraPage extends StatefulWidget {
@@ -17,7 +20,7 @@ class _CameraPageState extends State<CameraPage> with WidgetsBindingObserver {
1720

1821
bool _rear = false;
1922
List<CameraDescription> _cameras = [];
20-
List<dynamic> _recognitions;
23+
List<dynamic> _recognitions = [];
2124

2225
loadModel() async {
2326
_services.loadModel();
@@ -55,7 +58,7 @@ class _CameraPageState extends State<CameraPage> with WidgetsBindingObserver {
5558
void _setupCamera() async {
5659
_cameras = await availableCameras();
5760
if (_cameras == null || _cameras.isEmpty) {
58-
print('Looks like there are no cameras');
61+
print(noCamera);
5962
} else {
6063
_controller = CameraController(
6164
_cameras[_rear ? 0 : 1],
@@ -135,30 +138,22 @@ class _CameraPageState extends State<CameraPage> with WidgetsBindingObserver {
135138
: screenW,
136139
child: CameraPreview(_controller),
137140
),
138-
ol.Overlay(
141+
overlay.Overlay(
139142
results: _recognitions ?? <dynamic>[],
140143
),
141-
Positioned(
142-
right: 30,
143-
bottom: MediaQuery.of(context).size.height / 2,
144-
child: Transform.rotate(
145-
angle: -pi / 2,
146-
child: Text(_recognitions[0]["confidence"].toString())
147-
),
148-
),
149144
Positioned(
150145
bottom: 30,
151146
left: 30,
152147
child: Container(
153148
padding: EdgeInsets.all(15),
154149
decoration: BoxDecoration(
155-
borderRadius: BorderRadius.circular(80), color: Colors.green),
150+
borderRadius: BorderRadius.circular(80), color: green),
156151
child: GestureDetector(
157152
onTap: () async => _switchCameraLens(),
158153
child: Icon(
159-
_rear ? Icons.camera_front : Icons.camera_rear,
154+
_rear ? cameraFront : cameraRear,
160155
size: 30,
161-
color: Colors.white,
156+
color: white,
162157
)),
163158
),
164159
),
@@ -168,13 +163,13 @@ class _CameraPageState extends State<CameraPage> with WidgetsBindingObserver {
168163
child: Container(
169164
padding: EdgeInsets.all(15),
170165
decoration: BoxDecoration(
171-
borderRadius: BorderRadius.circular(80), color: Colors.red),
166+
borderRadius: BorderRadius.circular(80), color: red),
172167
child: GestureDetector(
173168
onTap: () => Navigator.pop(context),
174169
child: Icon(
175-
Icons.exit_to_app,
170+
exit,
176171
size: 30,
177-
color: Colors.white,
172+
color: white,
178173
)),
179174
),
180175
),

lib/ui/homepage.dart

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import 'package:face_mask_detection/app/colors.dart';
2+
import 'package:face_mask_detection/app/icons.dart';
3+
import 'package:face_mask_detection/app/strings.dart';
14
import 'package:face_mask_detection/ui/camera_screen.dart';
25
import 'package:face_mask_detection/ui/local_storage.dart';
36
import 'package:flutter/material.dart';
@@ -13,31 +16,31 @@ class HomeScreen extends StatelessWidget {
1316
actions: [
1417
IconButton(
1518
icon: Icon(
16-
Icons.code,
17-
color: Colors.green,
19+
code,
20+
color: green,
1821
size: 25,
19-
semanticLabel: "Code",
22+
semanticLabel: codeString,
2023
),
2124
onPressed: () {
2225
gotoWebPage();
2326
},
2427
)
2528
],
26-
backgroundColor: Colors.yellow.withOpacity(0.6),
29+
backgroundColor: yellow.withOpacity(0.6),
2730
title: Text(
28-
'Face Mask Detection',
29-
style: TextStyle(color: Colors.black, fontFamily: "Gilroy"),
31+
title,
32+
style: TextStyle(color: black, fontFamily: gilroy),
3033
),
3134
),
3235
body: Container(
3336
height: MediaQuery.of(context).size.height,
3437
decoration: BoxDecoration(
35-
color: Colors.yellow.withOpacity(0.4),
36-
border: Border.all(color: Colors.yellow, width: 5)),
38+
color: yellow.withOpacity(0.4),
39+
border: Border.all(color: yellow, width: 5)),
3740
child: Column(
3841
mainAxisAlignment: MainAxisAlignment.center,
3942
children: <Widget>[
40-
Image.asset("assets/images/illustrator.jpeg"),
43+
Image.asset(illustrator),
4144
SizedBox(
4245
height: 30,
4346
),
@@ -49,14 +52,14 @@ class HomeScreen extends StatelessWidget {
4952
child: Container(
5053
padding: EdgeInsets.all(10),
5154
decoration: BoxDecoration(
52-
color: Colors.yellow,
55+
color: yellow,
5356
borderRadius: BorderRadius.circular(15),
54-
border: Border.all(width: 3, color: Colors.white)),
57+
border: Border.all(width: 3, color: white)),
5558
child: Text(
56-
"Live camera",
59+
liveCamera,
5760
style: TextStyle(
58-
color: Colors.green,
59-
fontFamily: "Gilroy",
61+
color: green,
62+
fontFamily: gilroy,
6063
fontSize: 15,
6164
fontWeight: FontWeight.w200),
6265
),
@@ -73,14 +76,14 @@ class HomeScreen extends StatelessWidget {
7376
child: Container(
7477
padding: EdgeInsets.fromLTRB(15, 10, 15, 10),
7578
decoration: BoxDecoration(
76-
color: Colors.yellow,
79+
color: yellow,
7780
borderRadius: BorderRadius.circular(15),
78-
border: Border.all(width: 3, color: Colors.white)),
81+
border: Border.all(width: 3, color: white)),
7982
child: Text(
80-
"From Gallery",
83+
fromGallery,
8184
style: TextStyle(
82-
color: Colors.green,
83-
fontFamily: "Gilroy",
85+
color: green,
86+
fontFamily: gilroy,
8487
fontSize: 15,
8588
fontWeight: FontWeight.w200),
8689
),
@@ -93,11 +96,10 @@ class HomeScreen extends StatelessWidget {
9396
}
9497

9598
gotoWebPage() async {
96-
const url = "https://github.com/shashiben/flutter-face-mask-detection";
97-
if (await canLaunch(url)) {
98-
await launch(url);
99+
if (await canLaunch(urlString)) {
100+
await launch(urlString);
99101
} else {
100-
throw "Could not launch $url";
102+
throw "Could not launch $urlString";
101103
}
102104
}
103105
}

lib/ui/local_storage.dart

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
import 'dart:io';
2+
import 'package:face_mask_detection/app/colors.dart';
3+
import 'package:face_mask_detection/app/icons.dart';
4+
import 'package:face_mask_detection/app/strings.dart';
25
import 'package:face_mask_detection/services/tensorflow_services.dart';
36
import 'package:flutter/material.dart';
47
import 'package:image_picker/image_picker.dart';
@@ -42,11 +45,11 @@ class _LocalStorageState extends State<LocalStorage> {
4245
}
4346

4447
predictImage(File image) async {
45-
var recognitions =await _tensorFlowServices.predictImage(image);
48+
var recognitions = await _tensorFlowServices.predictImage(image);
4649
setState(() {
4750
_loading = false;
4851
_recognitions = recognitions;
49-
text = _recognitions[0]["label"];
52+
text = _recognitions[0][labelString];
5053
});
5154
}
5255

@@ -55,21 +58,21 @@ class _LocalStorageState extends State<LocalStorage> {
5558
return Scaffold(
5659
floatingActionButton: FloatingActionButton(
5760
child: Icon(
58-
Icons.image,
59-
color: Colors.yellow,
61+
imageIcon,
62+
color: yellow,
6063
),
61-
tooltip: "Pick Image from gallery",
62-
backgroundColor: Colors.green,
64+
tooltip: pickIFG,
65+
backgroundColor: green,
6366
onPressed: selectImage,
6467
),
6568
body: _loading
6669
? Container(
67-
color: Colors.yellowAccent,
70+
color: yellowAccent,
6871
alignment: Alignment.center,
6972
child: CircularProgressIndicator(),
7073
)
7174
: Container(
72-
color: Colors.yellow.withOpacity(0.4),
75+
color: yellow.withOpacity(0.4),
7376
width: MediaQuery.of(context).size.width,
7477
child: Column(
7578
crossAxisAlignment: CrossAxisAlignment.center,
@@ -78,11 +81,9 @@ class _LocalStorageState extends State<LocalStorage> {
7881
_image == null
7982
? Container(
8083
child: Text(
81-
"Select an Image to Show Preview",
84+
selectImageP,
8285
style: TextStyle(
83-
fontSize: 20,
84-
color: Colors.green,
85-
fontFamily: "Gilroy"),
86+
fontSize: 20, color: green, fontFamily: gilroy),
8687
),
8788
)
8889
: Image.file(
@@ -97,11 +98,9 @@ class _LocalStorageState extends State<LocalStorage> {
9798
? Text(
9899
"$text",
99100
style: TextStyle(
100-
color: text == "without_mask"
101-
? Colors.red
102-
: Colors.green,
101+
color: text == withoutMask ? red : green,
103102
fontSize: 20.0,
104-
fontFamily: "Gilroy"),
103+
fontFamily: gilroy),
105104
)
106105
: Container()
107106
],

lib/widgets/confidence_meter.dart

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)