东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 3885|回复: 0

[课堂笔记] 05、python的web服务Flask框架搭建

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14418
QQ
发表于 2020-2-19 11:13:35 | 显示全部楼层 |阅读模式
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


  1. # -*- coding: utf-8 -*-
  2. __author__ = u'东方耀 微信:dfy_88888'
  3. __date__ = '2020/2/19 上午9:57'
  4. __product__ = 'PyCharm'
  5. __filename__ = 'face_web_main'

  6. from flask import Flask, request
  7. from flask_restful import Resource, Api
  8. import os
  9. import darknet as dn
  10. # python dfy_python/dfy_test_darknet_img_video.py 终端这样运行才行

  11. dn.set_gpu(0)
  12. net = dn.load_net("cfg/yolov3-tiny-widerface-test.cfg", "backup_widerface_tiny/yolov3-tiny-widerface_8000.weights",
  13.                   0)
  14. meta = dn.load_meta("cfg/tiny-widerface.data")

  15. app = Flask(__name__)
  16. api = Api(app)


  17. class Quotes(Resource):
  18.     def get(self):
  19.         return {
  20.             'William Shakespeare': {
  21.                 'quote': ['Love all,trust a few,do wrong to none',
  22.                 'Some are born great, some achieve greatness, and some greatness thrust upon them.']
  23.         },
  24.         'Linus': {
  25.             'quote': ['Talk is cheap. Show me the code.']
  26.             }
  27.         }


  28. api.add_resource(Quotes, '/')


  29. @app.route('/hello')
  30. def hello_world():
  31.     return '<h1>Hello World!</h1>'


  32. @app.route('/upload', methods=['POST', 'GET'])
  33. def upload_img():
  34.     # return 'upload_success'
  35.     # 从客户端 浏览器 或 app 或 微信小程序 上传过来
  36.     f = request.files.get('file')
  37.     upload_path = os.path.join("./results/tmp." + f.filename.split(".")[-1])
  38.     # 注意:实际部署的时候 需要把图片的名字设置为上传来的名字一样 并发非常高时
  39.     # secure_filename(f.filename))  #注意:没有的文件夹一定要先创建,不然会提示没有该路径
  40.     print('图片上传的路径:', upload_path)
  41.     f.save(upload_path)
  42.     return upload_path


  43. @app.route('/face_detect')
  44. def face_detect():
  45.     # http://127.0.0.1:8888/face_detect?url=testfiles/img01.jpg
  46.     img_url = request.args.get('url')

  47.     print('img_url:', img_url)
  48.     # img_url = 'tmp/2.jpg'
  49.     # img_url = '/home/dfy888/py3_flask_works/tmp/WechatIMG231.jpeg'
  50.     if img_url == '' or img_url is None:
  51.         return '<h2>img url is null</h2>'
  52.     r = dn.detect(net, meta, img_url)
  53.     return str(r)


  54. if __name__ == '__main__':
  55.     # 在终端这样运行才行:python dfy_python/face_web_main.py
  56.     app.run(host='127.0.0.1', port=8888, debug=True)

  57.     # app.run(debug=True)


复制代码



01.png
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-3-28 22:04 , Processed in 0.194650 second(s), 22 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表