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
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>