Use flask to build web services and implement computer vision tasks

Preface

        The previous article reviewed how to configure the Raspberry Pi as a remote monitor, and left two questions in the article. This article corresponds to - how to build a transfer station to distribute related tasks.

        Before building the service, I checked relevant information. The back-end frameworks developed based on python language mainly include Django, Flask, and Tornado. Each of these three frameworks has its own characteristics. Django is a large and comprehensive web service framework that has automated backend management functions. Using ORM (Object Relational Mapping), simply defining objects can automatically generate a database structure. Flask is a lightweight web service framework that is friendly to beginners. The core idea when using it is the routing idea. Tornado is a web framework that focuses on fast response. It is characterized by the use of non-blocking services so that it can handle a large number of requests.

Install

        The installation of the Flask framework is very simple and can be achieved using pip in a virtual environment.

use

        In the Flask framework, we can use one thing and one function, and set routing for each function, so that the client can access specific functions through the server's IP address and routing.

        Information interaction is realized through data in json format. The client stores specific operations and parameters in json format and sends them to the server. The server uses the request library in python to parse the json data and extract the required fields. When the server needs to return data, it can be achieved by return jsonfy (field name: data).

question

        Cross domain:

                Whether it is the web or the app, when the IP or port of the client and the server are inconsistent, it cannot be sent normally. This is a cross-domain problem. When encountering this situation, there is no need to solve it on the client side. It is easy to implement using the flask-cors library in the server.

        Request type:

                The allowed request methods need to be declared after routing. The more common ones are post and get. Browser access mostly uses get, and the relevant parameters are placed directly in the URL, which poses certain security risks. Post puts the parameters in the request body.

code

from flask import Flask, url_for, request, jsonify
from utils import send_startPushFlow, send_stopPushFlow
from flask_cors import CORS

app = Flask(__name__)
# 解決跨域問題
CORS(app, resource=r'/*')

@app.route('/testAlarm', methods=['post'])
def handleTestAlarm():
    operation = request.get_json().get("operation")
    if operation == "test alarm":
        if is_alarm:
            # 在app端接受到该消息的时候,调用手机铃声或者震动
            # 向前端主进程发送
            return jsonify(msg="start alarm")
        else:
            return jsonify(msg="")
    else:
        return jsonify(msg="请执行正确操作")
if __name__ == '__main__':
    app.run()

        The above code shows that the app will periodically send a request to the server to determine whether an alarm is needed. The server will return the response json data based on is_alarm, and the app will also determine whether to let the phone ring based on the returned information.

        Because it is a complete project and is still in progress, the details of the deep learning part cannot be made public yet. Subsequent related projects may be open source. If you have friends who want to do a project that separates the front and back and combines it with hardware, you can leave a message and communicate together.

おすすめ

転載: blog.csdn.net/2201_75875170/article/details/132901605