Skip to content

Instantly share code, notes, and snippets.

@ghaffaru
Created July 25, 2021 21:25
Show Gist options
  • Select an option

  • Save ghaffaru/14650389caa88f7b11b378c771428ad6 to your computer and use it in GitHub Desktop.

Select an option

Save ghaffaru/14650389caa88f7b11b378c771428ad6 to your computer and use it in GitHub Desktop.
Send image file to server captured from camera
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:image_picker/image_picker.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Center(
child: Text('Cat Dog Classification'),
),
),
body: Page()),
);
}
}
class Page extends StatefulWidget {
@override
_PageState createState() => _PageState();
}
class _PageState extends State<Page> {
File imageFile;
String predicted;
@override
void initState() {
// TODO: implement initState
predicted = '';
super.initState();
}
void openImagePicker(BuildContext context) {
showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return Container(
height: 150,
padding: EdgeInsets.all(10),
child: Column(
children: <Widget>[
Text(
'Pick an image',
style: TextStyle(fontWeight: FontWeight.bold),
),
SizedBox(
height: 10.0,
),
FlatButton(
child: Text(
'Use Camera',
style: TextStyle(color: Colors.blue),
),
onPressed: () {
getImage(context, ImageSource.camera);
},
),
FlatButton(
child: Text(
'Use Gallery',
style: TextStyle(color: Colors.blue),
),
onPressed: () {
getImage(context, ImageSource.gallery);
},
),
],
),
);
});
}
void getImage(BuildContext context, ImageSource source) {
ImagePicker.pickImage(
source: source,
maxWidth: 400,
).then((File image) {
setState(() {
imageFile = image;
});
Navigator.pop(context);
uploadImage(imageFile);
});
}
uploadImage(File image, {String imagePath}) async {
final imageUploadRequest = http.MultipartRequest(
'POST', Uri.parse('https://cat-dog-clf-dl.herokuapp.com/api/classify'));
final file = await http.MultipartFile.fromPath(
'image',
image.path,
);
// imageUploadRequest.fields.addAll(other)
imageUploadRequest.files.add(file);
var streamedResponse = await imageUploadRequest.send();
var response = await http.Response.fromStream(streamedResponse);
print(json.decode(response.body)['predicted']);
setState(() {
predicted = json.decode(response.body)['predicted'];
});
}
@override
Widget build(BuildContext context) {
return Column(children: <Widget>[
SizedBox(
height: 20,
),
OutlineButton(
borderSide: BorderSide(color: Colors.lightBlue),
onPressed: () {
openImagePicker(context);
},
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Icon(Icons.camera_alt),
SizedBox(
width: 5.0,
),
Text('Upload an image')
],
),
),
SizedBox(
height: 10.0,
),
imageFile == null
? Text('Please choose an image')
: Image.file(
imageFile,
fit: BoxFit.cover,
height: 300.0,
alignment: Alignment.topCenter,
width: MediaQuery.of(context).size.width,
),
SizedBox(
height: 10.0,
),
imageFile == null
? Text('')
: Text(
'This is a $predicted',
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20),
),
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment