81 lines
2.3 KiB
Python
81 lines
2.3 KiB
Python
from flask import Flask, request, jsonify, send_from_directory, send_file
|
||
from flask_cors import CORS
|
||
import numpy as np
|
||
import cv2
|
||
import os
|
||
from src.pre_func.img_prec import img_recognition
|
||
|
||
# 设置静态文件目录
|
||
app = Flask(__name__, static_folder='src/web', static_url_path='/static')
|
||
CORS(app)
|
||
|
||
|
||
@app.route('/')
|
||
def index():
|
||
"""主页路由,提供index.html"""
|
||
return send_from_directory(app.static_folder, 'index.html')
|
||
|
||
|
||
@app.route('/web')
|
||
def web_index():
|
||
"""Web应用入口"""
|
||
return send_from_directory(app.static_folder, 'index.html')
|
||
|
||
|
||
@app.route('/web/<path:filename>')
|
||
def web_static(filename):
|
||
"""提供web目录下的静态文件"""
|
||
return send_from_directory(app.static_folder, filename)
|
||
|
||
|
||
@app.route('/static/<path:filename>')
|
||
def static_files(filename):
|
||
"""提供静态文件服务"""
|
||
return send_from_directory(app.static_folder, filename)
|
||
|
||
|
||
@app.route('/api/status')
|
||
def api_status():
|
||
"""API状态检查"""
|
||
return jsonify({
|
||
'status': 'running',
|
||
'message': 'Elements Wires Recognition API is running',
|
||
'endpoints': {
|
||
'POST /process_image': 'Image recognition endpoint',
|
||
'GET /': 'Main web interface',
|
||
'GET /web': 'Web application',
|
||
'GET /api/status': 'API status check'
|
||
}
|
||
})
|
||
|
||
|
||
@app.route('/process_image', methods=['POST'])
|
||
def process_image():
|
||
if 'image' not in request.files:
|
||
return jsonify({'error': 'No image part in the request'}), 400
|
||
|
||
file = request.files['image']
|
||
if file.filename == '':
|
||
return jsonify({'error': 'No selected image'}), 400
|
||
|
||
file_bytes = np.frombuffer(file.read(), np.uint8)
|
||
img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
|
||
|
||
result = img_recognition(img)
|
||
return result
|
||
|
||
|
||
if __name__ == '__main__':
|
||
print("🚀 Starting Elements Wires Recognition Web Server...")
|
||
print("📁 Static files served from: src/web/")
|
||
print("🌐 Web interface available at:")
|
||
print(" - http://localhost:8000/")
|
||
print(" - http://0.0.0.0:8000/")
|
||
print("🔧 API endpoints:")
|
||
print(" - POST /process_image (for image recognition)")
|
||
print(" - GET /web/<filename> (for static files)")
|
||
print(" - GET /static/<filename> (for static files)")
|
||
print("=" * 50)
|
||
|
||
app.run(host='0.0.0.0', debug=True, port=25273)
|