-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflask-vue.py
146 lines (117 loc) · 5.36 KB
/
flask-vue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from flask import Flask, request, jsonify, Response
from flask_cors import CORS
import os
from predictAll import predict_net,predict_chaofen,predict_color
from werkzeug.utils import secure_filename, send_from_directory
app = Flask(__name__)
# CORS(app) # 允许跨域请求
CORS(app, resources={r"/*": {"origins": "*"}})
# 设置上传文件的保存路径
UPLOAD_FOLDER = './uploads'
RESULT_FOLDER = './results'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['RESULT_FOLDER'] = RESULT_FOLDER
app.config['ALLOWED_EXTENSIONS'] = {'png', 'jpg', 'jpeg', 'gif'} # 允许上传的文件类型
# 检查文件扩展名
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
@app.route('/uploads/<model_name>/<filename>')
def uploaded_file(model_name, filename):
upload_folder = os.path.join(app.config['UPLOAD_FOLDER'], model_name)
return send_from_directory(upload_folder, filename)
@app.route('/results/<model_name>/<filename>', methods=['GET'])
def result_file(model_name, filename):
try:
result_folder = os.path.join(app.config['RESULT_FOLDER'], model_name)
result_file_path = os.path.join(result_folder, filename)
if os.path.exists(result_file_path):
# 读取图片为二进制数据
with open(result_file_path, 'rb') as f:
image_data = f.read()
# 返回图片数据流,设置正确的 MIME 类型
return Response(image_data, mimetype='image/jpg') # 根据实际图片类型设置 MIME
else:
return jsonify({'error': '文件未找到'}), 404
except Exception as e:
return jsonify({'error': '服务器内部错误', 'details': str(e)}), 500
@app.route('/fenlei_<model_name>', methods=['POST'])
def upload_file_fenlei(model_name):
print(f"Received request for model: {model_name}")
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
# 如果没有选择文件
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
# 确保上传的是图片文件
if file and allowed_file(file.filename):
# 保存图片到uploads文件夹
filename = secure_filename(file.filename)
upload_folder = os.path.join(app.config['UPLOAD_FOLDER'], model_name)
if not os.path.exists(upload_folder):
os.makedirs(upload_folder)
img_path = os.path.join(upload_folder, filename)
file.save(img_path)
# 调用predict_google_net进行预测
result_text, result_image, class_indict = predict_net(img_path, model_name)
print(result_text)
print(os.path.basename(result_image))
return jsonify({
'result_text': result_text,
'result_image': f'/results/{model_name}/{os.path.basename(result_image)}' # 返回可访问的URL
})
return jsonify({'error': 'Invalid file format'}), 400
@app.route('/chaofen_<model_name>', methods=['POST'])
def upload_file_chaofen(model_name):
print(f"Received request for model: {model_name}")
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
# 如果没有选择文件
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
# 确保上传的是图片文件
if file and allowed_file(file.filename):
# 保存图片到uploads文件夹
filename = secure_filename(file.filename)
upload_folder = os.path.join(app.config['UPLOAD_FOLDER'], model_name)
if not os.path.exists(upload_folder):
os.makedirs(upload_folder)
img_path = os.path.join(upload_folder, filename)
file.save(img_path)
# 调用predict_google_net进行预测
result_image = predict_chaofen(img_path, model_name)
print(os.path.basename(result_image))
return jsonify({
'result_text': ' ',
'result_image': f'/results/{model_name}/{os.path.basename(result_image)}' # 返回可访问的URL
})
return jsonify({'error': 'Invalid file format'}), 400
@app.route('/color_<model_name>', methods=['POST'])
def upload_file_color(model_name):
print(f"Received request for model: {model_name}")
if 'file' not in request.files:
return jsonify({'error': 'No file part'}), 400
file = request.files['file']
# 如果没有选择文件
if file.filename == '':
return jsonify({'error': 'No selected file'}), 400
# 确保上传的是图片文件
if file and allowed_file(file.filename):
# 保存图片到uploads文件夹
filename = secure_filename(file.filename)
upload_folder = os.path.join(app.config['UPLOAD_FOLDER'], model_name)
if not os.path.exists(upload_folder):
os.makedirs(upload_folder)
img_path = os.path.join(upload_folder, filename)
file.save(img_path)
# 调用predict_google_net进行预测
result_image = predict_color(img_path, model_name)
print(os.path.basename(result_image))
return jsonify({
'result_text': ' ',
'result_image': f'/results/{model_name}/{os.path.basename(result_image)}' # 返回可访问的URL
})
return jsonify({'error': 'Invalid file format'}), 400
if __name__ == '__main__':
app.run(debug=True)