網(wǎng)站首頁 編程語言 正文
前提條件
1.了解Python語言,并會安裝第三方庫
2.了解Python Web Flask框架
3.了解PyTorch深度學習框架
實驗環(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
項目結(jié)構
相關說明:
- static:用于存儲靜態(tài)文件,比如css、js和圖片等
- templates:存放模板文件
- upload:用于保存上傳文件
- flask_app.py: 應用程序主文件
- predict.py:預測文件
主要代碼
完整代碼,暫時沒空整理,如整理完,后續(xù)會發(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/", 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)
圖片識別--Person 圖片識別--Person
![]()
運行結(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=" }
原文鏈接:https://blog.csdn.net/FriendshipTang/article/details/123172225
相關推薦
- 2022-08-20 Python超詳細講解內(nèi)存管理機制_python
- 2023-12-16 VMware vSphere vCenter 6.5 重置管理員 administrator@vsp
- 2022-06-08 Spring Cloud Nacos 配置變更感知
- 2023-07-07 Spring 管理事務的方式有幾種? Spring 事務中的隔離級別有哪幾種?
- 2022-11-09 發(fā)布?Android?library?到?Maven?解析_Android
- 2022-04-05 如何定義多個context:property-placeholder配置
- 2024-01-05 使用idea構建父子類springboot項目教程
- 2022-06-26 Go語言開源庫實現(xiàn)Onvif協(xié)議客戶端設備搜索_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支