96 lines
3.4 KiB
Plaintext
96 lines
3.4 KiB
Plaintext
'''
|
||
Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
|
||
Date: 2023-03-29 20:22:34
|
||
LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
|
||
LastEditTime: 2023-04-05 15:46:07
|
||
FilePath: /luyuetong-data/yolox-pytorch/flask_on.py
|
||
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://githaub.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
|
||
'''
|
||
from flask import Flask, make_response, request, jsonify,send_file
|
||
from flask_cors import CORS
|
||
from werkzeug.utils import secure_filename
|
||
from PIL import Image
|
||
from yolo import YOLO
|
||
from predict import PREDICT
|
||
import os
|
||
import quanju
|
||
import requests
|
||
import urllib.request
|
||
from io import BytesIO
|
||
import base64
|
||
import io
|
||
|
||
|
||
app = Flask(__name__)
|
||
CORS(app)
|
||
|
||
@app.route('/uploads', methods=['GET','POST'])
|
||
def uploads():
|
||
global filename
|
||
# 检查是否收到了POST请求
|
||
if request.method == 'POST':
|
||
|
||
|
||
# 检查是否收到了文件
|
||
if 'file' not in request.files:
|
||
return jsonify({'error': 'No file uploaded.'}), 400
|
||
|
||
file = request.files['file']
|
||
filename = file.read() #此时转换出来的是二进制内容
|
||
file_path = filename.decode("utf-8")
|
||
|
||
print(file_path)
|
||
print(f"file:{file}")
|
||
|
||
# 将blob转换为base64编码的字符串
|
||
image_data = base64.b64encode(file.read()).decode('utf-8')
|
||
|
||
# 将base64编码的字符串解码为图片数据
|
||
img = Image.open(io.BytesIO(base64.b64decode(image_data)))
|
||
Image.open(img)
|
||
# 将bytes类型传递给预测函数进行处理
|
||
result = PREDICT(img)
|
||
|
||
|
||
#
|
||
# print(file)
|
||
# filename = secure_filename(file.filename)
|
||
# file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
||
# result = PREDICT()
|
||
|
||
#检查是否选择了文件
|
||
if file.filename == '':
|
||
return jsonify({'error': 'No file selected.'}), 400
|
||
|
||
|
||
# # 检查文件类型是否合法
|
||
# # if file and allowed_file(file.filename):
|
||
# filename = secure_filename(file.filename)
|
||
#保存文件到本地
|
||
# file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
||
|
||
#自定义一个全局变量承接名字(自定义库.变量)
|
||
# quanju.mingzi = filename
|
||
return "ok"
|
||
|
||
# 将文件路径传递给模型进行预测
|
||
# result = PREDICT(os.path.join(app.config['UPLOAD_FOLDER'], filename))
|
||
|
||
# return send_file(f'./results/result_{quanju.mingzi}', mimetype='image/jpeg')
|
||
# return send_file('results/result_{}'.format(quanju.mingzi), mimetype='image/jpeg')
|
||
|
||
# return jsonify({'error': 'Invalid file type.'}), 400
|
||
|
||
|
||
|
||
def allowed_file(filename):
|
||
# 检查文件类型是否合法,这里仅接受jpeg、jpg、png类型的图片文件
|
||
return '.' in filename and filename.rsplit('.', 1)[1].lower() in {'jpeg', 'jpg', 'png'}
|
||
|
||
|
||
if __name__ == '__main__':
|
||
# app.config['UPLOAD_FOLDER'] = './uploads'
|
||
app.config['UPLOAD_FOLDER'] = 'yolox-pytorch/uploads'
|
||
# app.debug = False
|
||
app.run(host='10.21.19.104', port=8000)
|