-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.dart
152 lines (142 loc) · 4.83 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:image_picker/image_picker.dart';
import 'dart:developer';
import 'dart:io';
import 'dart:convert';
import 'package:http/http.dart' as http;
void main() {
runApp(const ImagePickerApp());
}
class ImagePickerApp extends StatefulWidget {
const ImagePickerApp({Key? key}) : super(key: key);
@override
State<ImagePickerApp> createState() => _ImagePickerAppState();
}
class _ImagePickerAppState extends State<ImagePickerApp> {
File? _image;
String? _response;
Map<String, dynamic>? _prediction;
Future getImage(ImageSource source) async {
try {
final image = await ImagePicker().pickImage(
source: source, imageQuality: 40, maxHeight: 250, maxWidth: 250);
if (image == null) return;
final tempImage = File(image.path);
String base64Image = base64Encode(tempImage.readAsBytesSync());
Uri url = Uri.parse('https://flutter-ml.onrender.com/imgprocess');
final response = await http.post(
url,
body: jsonEncode({
'image': base64Image,
}),
headers: {
'Content-Type': 'application/json',
},
);
setState(() {
_image = tempImage;
_response = response.body;
// assign to _response a specific key from the response body
try {
_prediction = jsonDecode(_response!);
} catch (e) {
log('Failed to decode JSON: $base64Image');
_prediction = {'output': 'Failed to decode JSON'};
}
;
});
} on PlatformException catch (e) {
log('Failed to pick image: $e');
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.teal,
textTheme: GoogleFonts.poppinsTextTheme(),
),
home: Scaffold(
appBar: AppBar(
title: Center(
child: Text('Flutter + Machine Learning',
style: GoogleFonts.poppins())),
),
body: Center(
child: Column(
children: [
const Padding(
padding:
EdgeInsets.only(top: 20, bottom: 30, left: 30, right: 30),
child: Center(
child: Text(
'📷 Take a picture of a hand-written digit between 0 and 9\n\n🔢 The digit must be centered and in black color\n\n🧮 The app will try to predict the digit (as best as it could 😁)',
style: TextStyle(fontSize: 12),
),
),
),
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
_image != null
? Column(
children: [
Image.file(_image!,
width: 250, height: 250, fit: BoxFit.cover),
const SizedBox(height: 10),
Text('Predicted Value: ${_prediction!['output']}',
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
)),
],
)
: Container(
width: 250,
height: 250,
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(10)),
child: Icon(Icons.image_outlined,
size: 100, color: Colors.grey[600]),
),
const SizedBox(height: 20),
CustomButton(
title: 'Pick from Gallery',
icon: Icons.image_outlined,
onClick: () => getImage(ImageSource.gallery),
),
CustomButton(
title: 'Capture image',
icon: Icons.camera_alt_outlined,
onClick: () => getImage(ImageSource.camera),
),
const SizedBox(height: 40),
const Text('Made by:'),
const Text('Reeman Singh | BSIT-2A'),
],
),
],
),
),
),
);
}
}
Widget CustomButton({
required String title,
required IconData icon,
required VoidCallback onClick,
}) {
return Container(
width: 200,
child: ElevatedButton(
onPressed: onClick,
child: Row(children: [
Icon(icon),
const SizedBox(width: 15),
Text(title),
])));
}