Compare commits
5
Commits
9d2402d278
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c099c90aad | ||
|
|
fd0e468db9 | ||
|
|
b343ef2ddd | ||
|
|
53e704e0bf | ||
|
|
4b9374fcdc |
@@ -133,7 +133,6 @@ dmypy.json
|
|||||||
data/
|
data/
|
||||||
datasets/
|
datasets/
|
||||||
*.csv
|
*.csv
|
||||||
*.json
|
|
||||||
*.xml
|
*.xml
|
||||||
*.txt
|
*.txt
|
||||||
# Image files (uncomment if you have large image datasets)
|
# Image files (uncomment if you have large image datasets)
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Circuit Recognition Web Service
|
||||||
|
After=network.target
|
||||||
|
Wants=network.target
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=simple
|
||||||
|
User=root
|
||||||
|
Group=root
|
||||||
|
WorkingDirectory=/home/feie9454/elements_wires_congition
|
||||||
|
Environment=PATH=/home/feie9454/elements_wires_congition/venv/bin
|
||||||
|
ExecStart=/home/feie9454/elements_wires_congition/venv/bin/python main.py
|
||||||
|
Restart=always
|
||||||
|
RestartSec=10
|
||||||
|
StandardOutput=journal
|
||||||
|
StandardError=journal
|
||||||
|
SyslogIdentifier=circuit-recognition
|
||||||
|
|
||||||
|
# 安全设置
|
||||||
|
NoNewPrivileges=true
|
||||||
|
PrivateTmp=true
|
||||||
|
ProtectSystem=strict
|
||||||
|
ReadWritePaths=/home/feie9454/elements_wires_congition
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=multi-user.target
|
||||||
@@ -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
|
from flask_cors import CORS
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import cv2
|
import cv2
|
||||||
from pre_func.img_prec import img_recognition
|
import os
|
||||||
app = Flask(__name__)
|
from src.pre_func.img_prec import img_recognition
|
||||||
|
|
||||||
|
# 设置静态文件目录
|
||||||
|
app = Flask(__name__, static_folder='src/web', static_url_path='/static')
|
||||||
CORS(app)
|
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'])
|
@app.route('/process_image', methods=['POST'])
|
||||||
def process_image():
|
def process_image():
|
||||||
if 'image' not in request.files:
|
if 'image' not in request.files:
|
||||||
@@ -24,4 +66,15 @@ def process_image():
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
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=12399)
|
||||||
|
|||||||
Executable
+63
@@ -0,0 +1,63 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Circuit Recognition Service Management Script
|
||||||
|
# 使用方法: ./manage_service.sh [install|start|stop|restart|status|logs|uninstall]
|
||||||
|
|
||||||
|
SERVICE_NAME="circuit-recognition"
|
||||||
|
SERVICE_FILE="/home/feie9454/elements_wires_congition/circuit-recognition.service"
|
||||||
|
SYSTEM_SERVICE_PATH="/etc/systemd/system/circuit-recognition.service"
|
||||||
|
|
||||||
|
case "$1" in
|
||||||
|
install)
|
||||||
|
echo "安装服务..."
|
||||||
|
sudo cp "$SERVICE_FILE" "$SYSTEM_SERVICE_PATH"
|
||||||
|
sudo systemctl daemon-reload
|
||||||
|
sudo systemctl enable "$SERVICE_NAME"
|
||||||
|
echo "✅ 服务已安装并设置为开机自启"
|
||||||
|
echo "使用 './manage_service.sh start' 启动服务"
|
||||||
|
;;
|
||||||
|
start)
|
||||||
|
echo "启动服务..."
|
||||||
|
sudo systemctl start "$SERVICE_NAME"
|
||||||
|
echo "✅ 服务已启动"
|
||||||
|
;;
|
||||||
|
stop)
|
||||||
|
echo "停止服务..."
|
||||||
|
sudo systemctl stop "$SERVICE_NAME"
|
||||||
|
echo "✅ 服务已停止"
|
||||||
|
;;
|
||||||
|
restart)
|
||||||
|
echo "重启服务..."
|
||||||
|
sudo systemctl restart "$SERVICE_NAME"
|
||||||
|
echo "✅ 服务已重启"
|
||||||
|
;;
|
||||||
|
status)
|
||||||
|
echo "服务状态:"
|
||||||
|
sudo systemctl status "$SERVICE_NAME"
|
||||||
|
;;
|
||||||
|
logs)
|
||||||
|
echo "查看服务日志:"
|
||||||
|
sudo journalctl -u "$SERVICE_NAME" -f
|
||||||
|
;;
|
||||||
|
uninstall)
|
||||||
|
echo "卸载服务..."
|
||||||
|
sudo systemctl stop "$SERVICE_NAME"
|
||||||
|
sudo systemctl disable "$SERVICE_NAME"
|
||||||
|
sudo rm -f "$SYSTEM_SERVICE_PATH"
|
||||||
|
sudo systemctl daemon-reload
|
||||||
|
echo "✅ 服务已卸载"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "使用方法: $0 {install|start|stop|restart|status|logs|uninstall}"
|
||||||
|
echo ""
|
||||||
|
echo "命令说明:"
|
||||||
|
echo " install - 安装服务并设置开机自启"
|
||||||
|
echo " start - 启动服务"
|
||||||
|
echo " stop - 停止服务"
|
||||||
|
echo " restart - 重启服务"
|
||||||
|
echo " status - 查看服务状态"
|
||||||
|
echo " logs - 实时查看服务日志"
|
||||||
|
echo " uninstall - 卸载服务"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
@@ -8,6 +8,7 @@ dependencies = [
|
|||||||
"flask>=3.1.1",
|
"flask>=3.1.1",
|
||||||
"flask-cors>=6.0.1",
|
"flask-cors>=6.0.1",
|
||||||
"numpy>=2.3.0",
|
"numpy>=2.3.0",
|
||||||
|
"opencv-python>=4.11.0.86",
|
||||||
"scikit-image>=0.25.2",
|
"scikit-image>=0.25.2",
|
||||||
"ultralytics>=8.3.156",
|
"ultralytics>=8.3.156",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
# 这个文件使 src 目录成为一个 Python 包
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
# 这个文件使 pre_func 目录成为一个 Python 包
|
||||||
@@ -1,9 +1,13 @@
|
|||||||
import cv2
|
import cv2
|
||||||
|
import os
|
||||||
from ultralytics import YOLO
|
from ultralytics import YOLO
|
||||||
|
|
||||||
|
|
||||||
def elements_recognition(img):
|
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
|
original = img
|
||||||
img = cv2.resize(original, (1000, int(original.shape[0] * 1000 / original.shape[1])))
|
img = cv2.resize(original, (1000, int(original.shape[0] * 1000 / original.shape[1])))
|
||||||
results = model(img)[0]
|
results = model(img)[0]
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import datetime
|
import datetime
|
||||||
from pre_func import wires_recog
|
from . import wires_recog
|
||||||
from pre_func import ele_recog
|
from . import ele_recog
|
||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import base64
|
import base64
|
||||||
@@ -20,8 +20,8 @@ def generate_json(wires, components):
|
|||||||
"单刀双掷开关": "switch",
|
"单刀双掷开关": "switch",
|
||||||
"单刀单掷开关": "switch",
|
"单刀单掷开关": "switch",
|
||||||
"灯泡": "lightBulb",
|
"灯泡": "lightBulb",
|
||||||
"电源": "BATTERY",
|
"电源": "battery",
|
||||||
"电池电源": "BATTERY",
|
"电池电源": "battery",
|
||||||
"电阻": "RESISTOR",
|
"电阻": "RESISTOR",
|
||||||
"黑盒电流表": "ammeter",
|
"黑盒电流表": "ammeter",
|
||||||
"螺线管": "solenoid"
|
"螺线管": "solenoid"
|
||||||
@@ -89,7 +89,8 @@ def generate_json(wires, components):
|
|||||||
elem["resistance"] = 10
|
elem["resistance"] = 10
|
||||||
elif label == "battery":
|
elif label == "battery":
|
||||||
elem["voltage"] = 9
|
elem["voltage"] = 9
|
||||||
elem["batterType"] = "BATTERRY"
|
elem["type"] = "battery"
|
||||||
|
elem["batteryType"] = "BATTERY"
|
||||||
elem["internalResistance"] = 0.01
|
elem["internalResistance"] = 0.01
|
||||||
elif label == "resistor":
|
elif label == "resistor":
|
||||||
elem["resistorType"] = "RESISTOR"
|
elem["resistorType"] = "RESISTOR"
|
||||||
@@ -187,8 +188,8 @@ def visualize_wires_and_components(image, results, components):
|
|||||||
"单刀双掷开关": "switch",
|
"单刀双掷开关": "switch",
|
||||||
"单刀单掷开关": "switch",
|
"单刀单掷开关": "switch",
|
||||||
"灯泡":"lightBulb",
|
"灯泡":"lightBulb",
|
||||||
"电源": "BATTERY",
|
"电源": "battery",
|
||||||
"电池电源": "BATTERY",
|
"电池电源": "battery",
|
||||||
"电阻": "RESISTOR",
|
"电阻": "RESISTOR",
|
||||||
"黑盒电流表": "ammeter",
|
"黑盒电流表": "ammeter",
|
||||||
"螺线管": "solenoid"
|
"螺线管": "solenoid"
|
||||||
@@ -69,6 +69,7 @@ dependencies = [
|
|||||||
{ name = "flask" },
|
{ name = "flask" },
|
||||||
{ name = "flask-cors" },
|
{ name = "flask-cors" },
|
||||||
{ name = "numpy" },
|
{ name = "numpy" },
|
||||||
|
{ name = "opencv-python" },
|
||||||
{ name = "scikit-image" },
|
{ name = "scikit-image" },
|
||||||
{ name = "ultralytics" },
|
{ name = "ultralytics" },
|
||||||
]
|
]
|
||||||
@@ -78,6 +79,7 @@ requires-dist = [
|
|||||||
{ name = "flask", specifier = ">=3.1.1" },
|
{ name = "flask", specifier = ">=3.1.1" },
|
||||||
{ name = "flask-cors", specifier = ">=6.0.1" },
|
{ name = "flask-cors", specifier = ">=6.0.1" },
|
||||||
{ name = "numpy", specifier = ">=2.3.0" },
|
{ name = "numpy", specifier = ">=2.3.0" },
|
||||||
|
{ name = "opencv-python", specifier = ">=4.11.0.86" },
|
||||||
{ name = "scikit-image", specifier = ">=0.25.2" },
|
{ name = "scikit-image", specifier = ">=0.25.2" },
|
||||||
{ name = "ultralytics", specifier = ">=8.3.156" },
|
{ name = "ultralytics", specifier = ">=8.3.156" },
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user