|
05、python的web服务Flask框架搭建
开始搭建python的flask web接口:
pip install flask-restful
Successfully installed Flask-1.1.1 Jinja2-2.11.1 MarkupSafe-1.1.1 Werkzeug-1.0.0 aniso8601-8.0.0 click-7.0 flask-restful-0.3.8 itsdangerous-1.1.0
- # -*- coding: utf-8 -*-
- __author__ = u'东方耀 微信:dfy_88888'
- __date__ = '2020/2/19 上午9:57'
- __product__ = 'PyCharm'
- __filename__ = 'face_web_main'
- from flask import Flask, request
- from flask_restful import Resource, Api
- import os
- import darknet as dn
- # python dfy_python/dfy_test_darknet_img_video.py 终端这样运行才行
- dn.set_gpu(0)
- net = dn.load_net("cfg/yolov3-tiny-widerface-test.cfg", "backup_widerface_tiny/yolov3-tiny-widerface_8000.weights",
- 0)
- meta = dn.load_meta("cfg/tiny-widerface.data")
- app = Flask(__name__)
- api = Api(app)
- class Quotes(Resource):
- def get(self):
- return {
- 'William Shakespeare': {
- 'quote': ['Love all,trust a few,do wrong to none',
- 'Some are born great, some achieve greatness, and some greatness thrust upon them.']
- },
- 'Linus': {
- 'quote': ['Talk is cheap. Show me the code.']
- }
- }
- api.add_resource(Quotes, '/')
- @app.route('/hello')
- def hello_world():
- return '<h1>Hello World!</h1>'
- @app.route('/upload', methods=['POST', 'GET'])
- def upload_img():
- # return 'upload_success'
- # 从客户端 浏览器 或 app 或 微信小程序 上传过来
- f = request.files.get('file')
- upload_path = os.path.join("./results/tmp." + f.filename.split(".")[-1])
- # 注意:实际部署的时候 需要把图片的名字设置为上传来的名字一样 并发非常高时
- # secure_filename(f.filename)) #注意:没有的文件夹一定要先创建,不然会提示没有该路径
- print('图片上传的路径:', upload_path)
- f.save(upload_path)
- return upload_path
- @app.route('/face_detect')
- def face_detect():
- # http://127.0.0.1:8888/face_detect?url=testfiles/img01.jpg
- img_url = request.args.get('url')
- print('img_url:', img_url)
- # img_url = 'tmp/2.jpg'
- # img_url = '/home/dfy888/py3_flask_works/tmp/WechatIMG231.jpeg'
- if img_url == '' or img_url is None:
- return '<h2>img url is null</h2>'
- r = dn.detect(net, meta, img_url)
- return str(r)
- if __name__ == '__main__':
- # 在终端这样运行才行:python dfy_python/face_web_main.py
- app.run(host='127.0.0.1', port=8888, debug=True)
- # app.run(debug=True)
复制代码
|
-
|