flask+pymysql初步实践
软件:pycharm
需要安装:pymysql flask
软件安装
文件夹内部如下所示
其中,templates文件夹内放置网页文件
工程文件如图所示
测试hellow world
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(port=5010, debug=True)
控制台显示
网页详情
测试图像处理程序
app=Flask(__name__)
@app.route('/')
def index():
outname=detect('static/bao.jpg')
return render_template('index2.html',outname=outname)
def detect(filename):
face_cascade = cv2.CascadeClassifier('D:/anaconda/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml')
img = cv2.imread(filename)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
outname='static/face.jpg'
cv2.imwrite(outname,img)
return outname
if __name__=='__main__':
app.run(port=5007, debug=True)
运行结果
测试pymysql连接数据库
用Jupyter测试
import pymysql
wanted=input('请输入关键词:')
db = pymysql.connect("localhost", "root", "yzq1999", "engword", charset='utf8')
cursor = db.cursor()
try:
sql="select * from map_enword where english like '%" + wanted + "%'"
cursor.execute(sql)
rs = cursor.fetchall()
rs = list(rs)
print(rs)
except:
rs = 'db-error'
print('py-db-error')
查询结果
嵌入flask中
app=Flask(__name__)
@app.route('/english')
def english():
db = pymysql.connect("localhost", "root", "yzq1999", "engword", charset='utf8')
cursor = db.cursor()
try:
sql = "select * from map_enword limit 200"
cursor.execute(sql)
rs = cursor.fetchall()
rs = list(rs)
print(rs)
except:
rs = 'db-error'
print('py-db-error')
db.close();
return render_template('english.htm',rs=rs)
if __name__ == '__main__':
app.run(port=5012, debug=True)
并添加serach路由
from flask import Flask,render_template,request
@app.route('/search')
def search():
wanted = request.args.get("wanted", type=str)
if wanted == None:
wanted = 'pineapple'
db = pymysql.connect("localhost", "root", "2017", "engword", charset='utf8')
cursor = db.cursor()
try:
sql = "select * from map_enword where english like '%" + wanted + "%'"
cursor.execute(sql)
rs = cursor.fetchall()
rs = list(rs)
print(rs)
except:
rs = 'db-error'
print('py-db-error')
db.close()
return render_template('english.html',rs=rs)
导向网页english.htm
<html lang="en">
<head>
<meta charset="UTF-8">
<title>英语四六级词表</title>
</head>
<body>
<h1>英语四六级词表</h1>
<form action="/search">
<input type="text" name="wanted" value="" default="apple">
<input type="submit" value="搜索">
</form>
{% for r in rs %}
{{r}}<br>
{% endfor %}
</body>
</html>
测试结果
同理,连接球队数据库测试
测试视频分析程序
代码部分
import os
import cv2
import subprocess
os.chdir('D:\Vedio')
v_path='k37.mp4'
image_save='D:\Pic'
cap=cv2.VideoCapture(v_path)
frame_count=cap.get(cv2.CAP_PROP_FRAME_COUNT)
for i in range(int(frame_count)):
_,img=cap.read()
img=cv2.cvtColor(img,cv2.cv2.COLOR_BGR2GRAY) #cv2.COLOR_RGB2HSV cv2.COLOR_BGR2GRAY
cv2.imwrite('D:\Pic\image{}.jpg'.format(i),img)
切割效果(灰度图像)
量化展示
移植到flask中,注意修改数值传到部分