Embedding machine learning models into Android App (Part 1)

The main idea: After saving the model, load the model through Flask (a lightweight web development framework that allows us to quickly implement a website or web service using Python language), and then the App calls the web interface.

This article mainly records loading the trained SVM model to the Web through Flask.

It is mainly divided into the following steps:

1. Configure Flask

Method 1: pip install flask

Method 2: Deploy in pycharm (I personally prefer to use this method)

Search for flask, then install package. After success, you will see flask in the package view of the project dependencies (second line in the above figure). 

2. Save the SVM model;

Here the svm model is placed in TrainingModel.py.

Key code:

Pay attention to the problem of reading data type! There was an error message AttributeError: 'str' object has no attribute 'predict'. The data type was wrong. It took me two days to correct it (crying).

3. Build a Web interface;

You can build a Web interface in Pycharm - templates/page.html.

 

I won’t go into too much detail about the specific interface content, just adjust it according to your own needs.

4. Load the model into the web interface.

Use App.py to achieve this.

Key code:

 Through the above two .py and one .html, the model can be loaded into the web interface through Flask.

Note: .html is located under templates, and the two .py are located under venv.

Guess you like

Origin blog.csdn.net/weixin_58222015/article/details/130304690