Python+Flask編寫一個(gè)簡單的行人檢測API
前提條件
1.了解Python語言,并會(huì)安裝第三方庫
2.了解Python Web Flask框架
3.了解PyTorch深度學(xué)習(xí)框架
實(shí)驗(yàn)環(huán)境
- Python 3.6.2
- PyTorch 1.7.1
- Flask 1.1.1
- Numpy 1.18.5
- Opencv 3.4.2
- PIL pip3 install pillow
項(xiàng)目結(jié)構(gòu)

相關(guān)說明:
- static:用于存儲(chǔ)靜態(tài)文件,比如css、js和圖片等
- templates:存放模板文件
- upload:用于保存上傳文件
- flask_app.py: 應(yīng)用程序主文件
- predict.py:預(yù)測文件
主要代碼
完整代碼,暫時(shí)沒空整理,如整理完,后續(xù)會(huì)發(fā)布,敬請期待!
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import imp
from flask import request, jsonify, send_from_directory, abort
from werkzeug.utils import secure_filename
from flask import Flask, render_template, jsonify, request
from predict import pre
import time
import os
import base64
app = Flask(__name__)
UPLOAD_FOLDER = 'upload'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
basedir = os.path.abspath(os.path.dirname(__file__))
ALLOWED_EXTENSIONS = set(['txt', 'png', 'jpg', 'xls', 'JPG', 'PNG', 'xlsx', 'gif', 'GIF'])
# 用于判斷文件后綴
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
# 上傳
@app.route('/upload')
def upload_test():
return render_template('upload.html')
@app.route("/api/download/<filename>", methods=['GET'])
def download(filename):
if request.method == "GET":
if os.path.isfile(os.path.join('upload', filename)):
return send_from_directory('upload', filename, as_attachment=True)
abort(404)
# 上傳文件
@app.route('/api/upload', methods=['POST'], strict_slashes=False)
def api_upload():
file_dir = os.path.join(basedir, app.config['UPLOAD_FOLDER'])
if not os.path.exists(file_dir):
os.makedirs(file_dir)
f = request.files['myfile'] # 從表單的file字段獲取文件,myfile為該表單的name值
if f and allowed_file(f.filename): # 判斷是否是允許上傳的文件類型
fname = secure_filename(f.filename)
print(fname)
ext = fname.rsplit('.', 1)[1] # 獲取文件后綴
unix_time = int(time.time())
new_filename = str(unix_time) + '.' + ext # 修改了上傳的文件名
f.save(os.path.join(file_dir, new_filename)) # 保存文件到upload目錄
img_path = os.path.join("upload", new_filename)
print(img_path)
pre_result = pre(img_path)
print(pre_result)
token = base64.b64encode(new_filename.encode('utf-8'))
print(token)
return jsonify({"code": 0, "errmsg": "OK", "token": token, "fileName": "/api/download/" + new_filename,"detect_result:":pre_result})
else:
return jsonify({"code": 1001, "errmsg": "ERROR"})
if __name__ == '__main__':
app.run(host="0.0.0.0",port="5000",threaded=True,debug=False)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link href="{{url_for('static', filename='obj_classification.css')}}" rel="external nofollow" rel="stylesheet" type="text/css" />
<title>圖片識(shí)別--Person</title>
</head>
<body>
<h1>圖片識(shí)別--Person</h1>
<div class="container">
<div class="choose">
<form action="http://IP地址:5000/api/upload" enctype='multipart/form-data' method='POST'>
<input type="file" name="myfile" class="input-new" style="margin-top:20px;" />
<input type="submit" value="識(shí)別圖片" class="button-new" style="margin-top:15px;" />
</form>
</div>
<div class="display">
<img src="{{ url_for('static', filename='images/test.jpg',_t=val1) }}" width="400" height="500" alt="圖片" />
</div>
</div>
</body>
</html>
運(yùn)行結(jié)果



{
"code": 0,
"detect_result:": [
{
"bbox": [
51.0,
265.0,
543.0,
437.0
],
"class": "b'person 0.78'"
},
{
"bbox": [
43.0,
433.0,
543.0,
609.0
],
"class": "b'person 0.77'"
},
{
"bbox": [
44.0,
133.0,
543.0,
309.0
],
"class": "b'person 0.76'"
},
{
"bbox": [
46.0,
526.0,
543.0,
665.0
],
"class": "b'person 0.74'"
},
{
"bbox": [
107.0,
51.0,
525.0,
181.0
],
"class": "b'person 0.62'"
}
],
"errmsg": "OK",
"fileName": "/api/download/1645974252.jpg",
"token": "MTY0NTk3NDI1Mi5qcGc="
}

以上就是Python+Flask編寫一個(gè)簡單的行人檢測API的詳細(xì)內(nèi)容,更多關(guān)于Python Flask行人檢測的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Window10上Tensorflow的安裝(CPU和GPU版本)
這篇文章主要介紹了Window10上Tensorflow的安裝(CPU和GPU版本),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
Python使用海龜繪圖實(shí)現(xiàn)貪吃蛇游戲
這篇文章主要為大家詳細(xì)介紹了Python使用海龜繪圖實(shí)現(xiàn)貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
用Python自動(dòng)清理系統(tǒng)垃圾的實(shí)現(xiàn)
這篇文章主要介紹了用Python自動(dòng)清理系統(tǒng)垃圾的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
OpenCV+Python--RGB轉(zhuǎn)HSI的實(shí)現(xiàn)
今天小編就為大家分享一篇OpenCV+Python--RGB轉(zhuǎn)HSI的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Django 查詢數(shù)據(jù)庫返回JSON的實(shí)現(xiàn)
和前端交互全部使用JSON,如何將數(shù)據(jù)庫查詢結(jié)果轉(zhuǎn)換成JSON格式,本文就來介紹一下,感興趣的小伙伴們可以參考一下2021-08-08

