add static web files
This commit is contained in:
parent
4b9374fcdc
commit
53e704e0bf
59
main.py
59
main.py
@ -1,12 +1,54 @@
|
||||
from flask import Flask, request, jsonify
|
||||
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__)
|
||||
|
||||
# 设置静态文件目录
|
||||
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:
|
||||
@ -24,4 +66,15 @@ def process_image():
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True, port=8000)
|
||||
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)
|
||||
|
||||
@ -1,9 +1,13 @@
|
||||
import cv2
|
||||
import os
|
||||
from ultralytics import YOLO
|
||||
|
||||
|
||||
def elements_recognition(img):
|
||||
model = YOLO('../best_model/model_2.pt')
|
||||
# 获取当前文件的目录,然后构建模型文件的绝对路径
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
model_path = os.path.join(current_dir, '../best_model/model_2.pt')
|
||||
model = YOLO(model_path)
|
||||
original = img
|
||||
img = cv2.resize(original, (1000, int(original.shape[0] * 1000 / original.shape[1])))
|
||||
results = model(img)[0]
|
||||
|
||||
@ -20,8 +20,8 @@ def generate_json(wires, components):
|
||||
"单刀双掷开关": "switch",
|
||||
"单刀单掷开关": "switch",
|
||||
"灯泡": "lightBulb",
|
||||
"电源": "BATTERY",
|
||||
"电池电源": "BATTERY",
|
||||
"电源": "battery",
|
||||
"电池电源": "battery",
|
||||
"电阻": "RESISTOR",
|
||||
"黑盒电流表": "ammeter",
|
||||
"螺线管": "solenoid"
|
||||
@ -89,7 +89,8 @@ def generate_json(wires, components):
|
||||
elem["resistance"] = 10
|
||||
elif label == "battery":
|
||||
elem["voltage"] = 9
|
||||
elem["batterType"] = "BATTERRY"
|
||||
elem["type"] = "battery"
|
||||
elem["batteryType"] = "BATTERY"
|
||||
elem["internalResistance"] = 0.01
|
||||
elif label == "resistor":
|
||||
elem["resistorType"] = "RESISTOR"
|
||||
@ -187,8 +188,8 @@ def visualize_wires_and_components(image, results, components):
|
||||
"单刀双掷开关": "switch",
|
||||
"单刀单掷开关": "switch",
|
||||
"灯泡":"lightBulb",
|
||||
"电源": "BATTERY",
|
||||
"电池电源": "BATTERY",
|
||||
"电源": "battery",
|
||||
"电池电源": "battery",
|
||||
"电阻": "RESISTOR",
|
||||
"黑盒电流表": "ammeter",
|
||||
"螺线管": "solenoid"
|
||||
|
||||
BIN
src/web/circuit-construction-kit-dc-virtual-lab-128.png
Normal file
BIN
src/web/circuit-construction-kit-dc-virtual-lab-128.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.9 KiB |
BIN
src/web/circuit-construction-kit-dc-virtual-lab-600.png
Normal file
BIN
src/web/circuit-construction-kit-dc-virtual-lab-600.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 70 KiB |
File diff suppressed because one or more lines are too long
Binary file not shown.
File diff suppressed because one or more lines are too long
1364
src/web/index.html
Normal file
1364
src/web/index.html
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,34 @@
|
||||
<?xml version="1.0"?>
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+ARIA 1.0//EN" "http://www.w3.org/WAI/ARIA/schemata/xhtml-aria-1.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
|
||||
<meta name="viewport" content="initial-scale=1,user-scalable=no,maximum-scale=1"/>
|
||||
<meta name="mobile-web-app-capable" content="yes"/>
|
||||
<meta name="phet-sim-level" content="production"/>
|
||||
|
||||
<title>‪Circuit Construction Kit: DC - Virtual Lab‬</title>
|
||||
</head>
|
||||
<!--
|
||||
Circuit Construction Kit: DC - Virtual Lab 1.5.0-dev.0
|
||||
Copyright 2002-2025, Regents of the University of Colorado
|
||||
PhET Interactive Simulations, University of Colorado Boulder
|
||||
|
||||
This file is licensed under Creative Commons Attribution 4.0
|
||||
For alternate source code licensing, see https://github.com/phetsims
|
||||
For licenses for third-party software used by this simulation, see below
|
||||
For more information, see https://phet.colorado.edu/en/licensing/html
|
||||
|
||||
The PhET name and PhET logo are registered trademarks of The Regents of the
|
||||
University of Colorado. Permission is granted to use the PhET name and PhET logo
|
||||
only for attribution purposes. Use of the PhET name and/or PhET logo for promotional,
|
||||
marketing, or advertising purposes requires a separate license agreement from the
|
||||
University of Colorado. Contact phethelp@colorado.edu regarding licensing.
|
||||
-->
|
||||
<!-- body is only made black for the loading phase so that the splash screen is black -->
|
||||
<body style="background-color:black;">
|
||||
<script type="text/javascript" src="circuit-construction-kit-dc-virtual-lab_license_adapted-from-phet.js" charset="utf-8"></script><script type="text/javascript" src="circuit-construction-kit-dc-virtual-lab_initialization_adapted-from-phet.js" charset="utf-8"></script>
|
||||
<script type="text/javascript" src="circuit-construction-kit-dc-virtual-lab_adapted-from-phet.js" charset="utf-8"></script>
|
||||
</body>
|
||||
</html>
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user