1. A brief overview of Tensorflow (TF):
Tensorflow (TF) is a library developed by Google and is an open source library used to calculate arithmetic using data flow graph in which nodes are mathematical calculations and edges represent data flow. Whether.
Benefits that this library brings:
- Integrates many Machine Learning libraries.
- Has good compatibility and expansion.
- Recognize pronunciation and translate automatically.
- Email classification of Gmail.
- Recognize faces in photos.
TensorFlow is characterized by processing all types of data that can be represented as data flow graphs or low levels such as handwriting processing.
2. Put TF into the Flutter app:
2.1 Add data to TF model:
You can go to https://teachablemachine.withgoogle.com/train to add your data.
Select to create Image Project.
To be able to get the data you download from this page: https://www.kaggle.com/tongpython/cat-and-dog/data , then upload to the model above for each type that you set and click Train model
for machine to conduct learning.
Then proceed to Export model
and Download
to device.
Unzip the two files above and proceed to add assets
of Flutter project.
2.2 Code UI Detech Animal:
build.gradle
to android’s build.gradle
add the following 2 lines.
It will disable the compression of files called tflite
and lite
.
Add the 2 image_picker
and tflite
libraries to the pupspec.yaml
directory
Let’s proceed with the coding. In the main.dart
file, write the function to put the models we trained earlier into Tflite.loadModel()
to save the model.
1 2 3 4 5 6 7 | // Data được load vào model của TF. loadModel() async{ await Tflite.loadModel( model: "assets/model_unquant.tflite", labels: "assets/labels.txt"); } |
Next we add a function to get the image from the phone.
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 | // Lấy image từ máy. chooseImage() async{ var image = await ImagePicker.pickImage(source: ImageSource.gallery); if(image == null) { return null; } setState(() { _isLodaing = true; _image = image; }); runModelonImage(image); } //Đưa ảnh vào trong TFlite phân tích với model đã train. runModelonImage(File image) async { var output = await Tflite.runModelOnImage( path: image.path, numResults: 2, imageMean: 127.5, imageStd: 127.5, threshold: 0.5 ); setState(() { _isLodaing = false; _outputs = output; }); } |
Add the image display and Button get photos from the machine.
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 | class _HomePageState extends State<HomePage> { bool _isLodaing = false; File _image; List _outputs; @override void initState() { // TODO: implement initState super.initState(); _isLodaing = true; loadModel().then((value){ setState(() { _isLodaing = false; }); }); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text("Detech Animals"), ), body: _isLodaing ? Container( alignment: Alignment.center, child: CircularProgressIndicator(), ) : Container( width: MediaQuery.of(context).size.width, child: Column( crossAxisAlignment: CrossAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ _image == null ? Container() : Image.file(_image), SizedBox(height: 20,), _outputs != null ? Text( "${_outputs[0]["label"]}", // Hiển thị kết quả test với 1 là Dog và 0 là Cat. style: TextStyle( color: Colors.black, fontSize: 20.0, background: Paint()..color = Colors.white, ), ) : Container() ], ), ), floatingActionButton: FloatingActionButton( onPressed: () { chooseImage(); }, child: Icon(Icons.image), ), ); } |
2.3 Demo try:
If you select a cat image, it will say 0 Cat
.
If you select a dog image, you will notice it is 1 Dog
.
Conclude:
I have instructed you to make a simple Detect animal app using Tensorflow, but there are still some disadvantages such as: If you choose an image that is not a dog or a cat, you may get a wrong result because of the lack of Data Training.
Hope you enjoyed this article.
Link source code.