Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uuid
|
| 3 |
+
from flask import Flask, request, jsonify, send_file
|
| 4 |
+
from flask_cors import CORS
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
+
|
| 7 |
+
app = Flask(__name__)
|
| 8 |
+
CORS(app) # 允许所有域名进行访问
|
| 9 |
+
|
| 10 |
+
# 模型字典,可以根据需要更新
|
| 11 |
+
models = {
|
| 12 |
+
'追踪': 'models/detect/yolov8n.pt',
|
| 13 |
+
'检测': 'models/detect/弹珠模型.pt',
|
| 14 |
+
'分类': 'models/classify/yolov8n-cls.pt',
|
| 15 |
+
'姿势': 'models/pose/yolov8n-pose.pt',
|
| 16 |
+
'分割': 'models/segment/yolov8n-seg.pt'
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
model_instances = {}
|
| 20 |
+
|
| 21 |
+
def load_model(model_path):
|
| 22 |
+
try:
|
| 23 |
+
return YOLO(model_path) # 直接调用训练好的模型
|
| 24 |
+
except Exception as e:
|
| 25 |
+
print(f"Failed to load model from {model_path}: {e}")
|
| 26 |
+
return None
|
| 27 |
+
|
| 28 |
+
@app.route('/')
|
| 29 |
+
def home():
|
| 30 |
+
return "Welcome to the YOLOv8 Flask App!"
|
| 31 |
+
|
| 32 |
+
@app.route('/request', methods=['POST'])
|
| 33 |
+
def handle_request():
|
| 34 |
+
try:
|
| 35 |
+
selected_model = request.form.get('model')
|
| 36 |
+
if selected_model not in models:
|
| 37 |
+
return jsonify({'error': 'Invalid model selected.'}), 400
|
| 38 |
+
|
| 39 |
+
model_path = models[selected_model]
|
| 40 |
+
if selected_model not in model_instances:
|
| 41 |
+
model_instances[selected_model] = load_model(model_path)
|
| 42 |
+
model = model_instances[selected_model]
|
| 43 |
+
|
| 44 |
+
if model is None:
|
| 45 |
+
return jsonify({'error': 'Model is not loaded due to connection error.'}), 500
|
| 46 |
+
|
| 47 |
+
img = request.files.get('img')
|
| 48 |
+
if img is None:
|
| 49 |
+
return jsonify({'error': 'No image provided.'}), 400
|
| 50 |
+
|
| 51 |
+
img_name = str(uuid.uuid4()) + '.jpg'
|
| 52 |
+
img_path = os.path.join('./img', img_name)
|
| 53 |
+
os.makedirs(os.path.dirname(img_path), exist_ok=True)
|
| 54 |
+
img.save(img_path)
|
| 55 |
+
|
| 56 |
+
print(f"Image saved at {img_path}")
|
| 57 |
+
|
| 58 |
+
save_dir = './runs/detect'
|
| 59 |
+
os.makedirs(save_dir, exist_ok=True)
|
| 60 |
+
results = model.predict(img_path, save=True, project=save_dir, name=img_name.split('.')[0], device='cpu') # Device should be 'cpu' or 'cuda'
|
| 61 |
+
|
| 62 |
+
# 获取预测结果路径
|
| 63 |
+
predicted_img_path = os.path.join(save_dir, img_name.split('.')[0], img_name)
|
| 64 |
+
|
| 65 |
+
if not os.path.exists(predicted_img_path):
|
| 66 |
+
print(f"Prediction result not found at {predicted_img_path}")
|
| 67 |
+
return jsonify({'error': 'Prediction result not found.'}), 500
|
| 68 |
+
|
| 69 |
+
print(f"Prediction successful. Result saved at {predicted_img_path}")
|
| 70 |
+
return jsonify({'message': 'Prediction successful!', 'img_path': f'/get/{img_name}'})
|
| 71 |
+
|
| 72 |
+
except Exception as e:
|
| 73 |
+
print(f"Error during request processing: {e}")
|
| 74 |
+
return jsonify({'error': f'An error occurred during processing: {e}'}), 500
|
| 75 |
+
|
| 76 |
+
@app.route('/get/<filename>', methods=['GET'])
|
| 77 |
+
def get_file(filename):
|
| 78 |
+
try:
|
| 79 |
+
save_dir = './runs/detect'
|
| 80 |
+
predicted_img_path = os.path.join(save_dir, filename.split('.')[0], filename)
|
| 81 |
+
print(f"Trying to send file from path: {predicted_img_path}")
|
| 82 |
+
|
| 83 |
+
if not os.path.exists(predicted_img_path):
|
| 84 |
+
return jsonify({'error': 'Prediction result not found.'}), 404
|
| 85 |
+
|
| 86 |
+
return send_file(predicted_img_path, mimetype='image/jpeg')
|
| 87 |
+
|
| 88 |
+
except Exception as e:
|
| 89 |
+
print(f"Error during file retrieval: {e}")
|
| 90 |
+
return jsonify({'error': f'Failed to retrieve the result image. Error: {e}'}), 500
|
| 91 |
+
|
| 92 |
+
if __name__ == '__main__':
|
| 93 |
+
app.run(host='0.0.0.0', port=5000, debug=True) # 允许从任何网络接口访问
|