增加main_start.py,通过此入口启动程序
This commit is contained in:
@@ -5,10 +5,14 @@
|
||||
import cv2
|
||||
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import time
|
||||
import argparse
|
||||
import threading
|
||||
import queue
|
||||
import yaml
|
||||
import base64
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
@@ -171,11 +175,8 @@ class RTSPCaptureWorker(threading.Thread):
|
||||
|
||||
# ========================= 服务主类 =========================
|
||||
class RTSPService:
|
||||
def __init__(self, config_path: str = "config.yaml"):
|
||||
with open(config_path, "r", encoding="utf-8") as f:
|
||||
cfg = yaml.safe_load(f)
|
||||
self.cameras = [CameraConfig(id=c["id"], name=c.get("name", f"cam_{c['id']}"), index = c["index"], params=c.get("params"))
|
||||
for c in cfg.get("cameras", [])]
|
||||
def __init__(self, cameras: list[CameraConfig]):
|
||||
self.cameras = cameras
|
||||
|
||||
self.stop_event = threading.Event()
|
||||
self.raw_queue = queue.Queue(maxsize=2)
|
||||
@@ -205,8 +206,59 @@ class RTSPService:
|
||||
logger.info("[INFO] Service stopped")
|
||||
|
||||
|
||||
def parse_cameras_from_json(json_str: str) -> list[CameraConfig]:
|
||||
"""从 JSON 字符串解析摄像头配置(支持 base64 编码)"""
|
||||
try:
|
||||
# 尝试 base64 解码
|
||||
try:
|
||||
decoded = base64.b64decode(json_str).decode('utf-8')
|
||||
cameras_data = json.loads(decoded)
|
||||
except Exception:
|
||||
# 如果不是 base64,直接解析 JSON
|
||||
cameras_data = json.loads(json_str)
|
||||
|
||||
return [CameraConfig(
|
||||
id=c["id"],
|
||||
name=c.get("name", f"cam_{c['id']}"),
|
||||
index=c.get("index"),
|
||||
params=c.get("params")
|
||||
) for c in cameras_data]
|
||||
except Exception as e:
|
||||
logger.error(f"[ERROR] Failed to parse cameras JSON: {e}")
|
||||
return []
|
||||
|
||||
|
||||
def parse_cameras_from_yaml(yaml_path: str) -> list[CameraConfig]:
|
||||
"""从 YAML 文件解析摄像头配置"""
|
||||
with open(yaml_path, "r", encoding="utf-8") as f:
|
||||
cfg = yaml.safe_load(f)
|
||||
return [CameraConfig(
|
||||
id=c["id"],
|
||||
name=c.get("name", f"cam_{c['id']}"),
|
||||
index=c.get("index"),
|
||||
params=c.get("params")
|
||||
) for c in cfg.get("cameras", [])]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
service = RTSPService("config.yaml")
|
||||
parser = argparse.ArgumentParser(description="RTSP Service for Kadian Detection")
|
||||
parser.add_argument("--cameras", type=str, help="Cameras config in JSON format (or base64 encoded JSON)")
|
||||
parser.add_argument("--config", type=str, default="config.yaml", help="Path to config YAML file")
|
||||
args = parser.parse_args()
|
||||
|
||||
# 优先使用命令行传入的 cameras JSON,否则读取配置文件
|
||||
if args.cameras:
|
||||
cameras = parse_cameras_from_json(args.cameras)
|
||||
logger.info(f"[INFO] Loaded {len(cameras)} cameras from command line argument")
|
||||
else:
|
||||
cameras = parse_cameras_from_yaml(args.config)
|
||||
logger.info(f"[INFO] Loaded {len(cameras)} cameras from config file: {args.config}")
|
||||
|
||||
if not cameras:
|
||||
logger.error("[ERROR] No cameras configured, exiting...")
|
||||
sys.exit(1)
|
||||
|
||||
service = RTSPService(cameras)
|
||||
service.start()
|
||||
try:
|
||||
while True:
|
||||
|
||||
Reference in New Issue
Block a user