Flask基础

本文介绍了Python的argparse模块用于命令行参数解析的基本用法,包括设置默认值和处理多个参数。同时,展示了Flask框架的应用,如创建简单的图像上传和处理服务,以及如何接收和处理POST请求。
摘要由CSDN通过智能技术生成

1. argparse模块——命令行解析

argparse基本用法1https://blog.csdn.net/Zhang_Chen_/article/details/88212022

传递多个参数、默认参数……https://zhuanlan.zhihu.com/p/56922793

1.1多个参数、默认参数:

import argparse
parser = argparse.ArgumentParser(description='姓名')
parser.add_argument('--family', type=str, default='张',help='姓')
parser.add_argument('--name', type=str, default='三', help='名')
args = parser.parse_args()
#打印姓名
print(args.family+args.name)

 python demo.py 

张三

python demo.py --family=李

李三

2. Flask

Flask框架详解,传递参数、重定位……(flask扩展)icon-default.png?t=N2N8https://blog.csdn.net/shifengboy/article/details/114274271?app_version=5.15.2&csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22114274271%22%2C%22source%22%3A%22m0_59115974%22%7D&utm_source=app

2.1 flask是什么

2.2 实例1

Flask server

"""
Simple app to upload an image via a web form 
and view the inference results on the image in the browser.
"""
import argparse
import io
import os
import cv2
import base64
import time
from PIL import Image
import json
import numpy as np
from flask import send_file
from flask import Flask, render_template, request, redirect
app = Flask(__name__)


def cv2_base64(img):
    img = cv2.imencode('.jpg', img)[1]
    image_code = str(base64.b64encode(img))[2:-1]
    return image_code

@app.route("/", methods=["GET", "POST"])
def predict():
    if request.method == "POST":
        cap = cv2.VideoCapture(0)
        time.sleep(0.5)
        i=0
        while i<=5:
            ret, frame = cap.read()
            i=i+1
        frame = cv2.flip(frame, 0)  #上下颠倒
        img = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
        cap.release()

        img_pil = Image.fromarray(img)
        img_io = io.BytesIO()
        img_pil.save(img_io, format="PNG")
        img_io.seek(0)        
        return send_file(img_io, mimetype="image/png", max_age=0)
    if request.method == "GET":   # 直接返回
        return "Hello World!洪大量是大帅哥22222"

# 将图片信息和数据信息打包成字典返回
@app.route("/hello", methods=["GET", "POST"])
def predict1():
    if request.method == "POST":
        cap = cv2.VideoCapture(0)
        time.sleep(0.5)
        i=0
        while i<=5:
            ret, frame = cap.read()
            i=i+1
        img = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
        cap.release()
        img_base64 = cv2_base64(img)
        senddict = {}
        senddict['picturecontent'] = img_base64
        senddict['time'] = time.time()
        senddict_json = json.dumps(senddict)
        return senddict_json

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Flask app exposing facenet models")
    parser.add_argument("--port", default=5000, type=int, help="port number")
    args = parser.parse_args()
    app.run(host="0.0.0.0", port=args.port)  # debug=True causes Restarting with stat
import json
import pprint
import requests
import cv2
import base64
import numpy as np

def base64_to_cv2(base64_code):
    img_data = base64.b64decode(base64_code)
    img_array = np.fromstring(img_data, np.uint8)
    img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
    return img

DETECTION_URL = "http://127.0.0.1:5000/hello"
response = requests.post(DETECTION_URL)
print(response.content)
dict_json = response.content
dict = json.loads(dict_json)
print(dict)

imgbase64, time = dict['picturecontent'], dict['time']
img = base64_to_cv2(imgbase64)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
cv2.imshow('1234',img)
cv2.waitKey(0)

3. Html文件

login.html

<html>
   <body>
      
      <form action = "http://localhost:5000/login" method = "post">
         <p>Enter Name:</p>
         <p><input type = "text" name = "nm" /></p>
         <p><input type = "submit" value = "submit" /></p>
      </form>
      
   </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值