修改hls service适配改动

This commit is contained in:
zqc
2026-02-27 11:28:29 +08:00
parent 7d4cca44d8
commit a72b8116a5

View File

@@ -7,34 +7,25 @@ import os
import time import time
import threading import threading
import queue import queue
import yaml
import glob import glob
import argparse
import sys
from dataclasses import dataclass from dataclasses import dataclass
# from biz.checkpoint.checkpoint_biz import FrameProcessorWorker from biz.checkpoint.checkpoint_biz import FrameProcessorWorker
from biz.prison.prison_biz import FrameProcessorWorker # from biz.prison.prison_biz import FrameProcessorWorker
from common.camera_config import CameraConfig, parse_cameras_from_json, parse_cameras_from_yaml
from utils.web_socket_sender import WebSocketSender from utils.web_socket_sender import WebSocketSender
from utils.logger import get_logger from utils.logger import get_logger
logger = get_logger(__name__) logger = get_logger(__name__)
WS_HOST = "0.0.0.0"
WS_PORT = 8765
# HLS配置 # HLS配置
HLS_SEGMENT_DIR = "D:\\ProjectDoc\\Police\\data\\hls" # TS分片存储目录 HLS_SEGMENT_DIR = "D:\\ProjectDoc\\Police\\data\\hls" # TS分片存储目录
HLS_SEGMENT_PATTERN = "segment_%09d.ts" # TS文件命名模式 HLS_SEGMENT_PATTERN = "segment_%09d.ts" # TS文件命名模式
# ========================= 数据结构 =========================
@dataclass
class CameraConfig:
id: int
name: str
index: str
# ========================= 合并的TS读取和帧处理线程 ========================= # ========================= 合并的TS读取和帧处理线程 =========================
class HLSFrameProcessor(threading.Thread): class HLSFrameProcessor(threading.Thread):
def __init__(self, camera_cfg: CameraConfig, raw_queue: queue.Queue, stop_event: threading.Event): def __init__(self, camera_cfg: CameraConfig, raw_queue: queue.Queue, stop_event: threading.Event):
@@ -290,19 +281,19 @@ class HLSFrameProcessor(threading.Thread):
# ========================= 服务主类 ========================= # ========================= 服务主类 =========================
class HLSKadianService: class HLSKadianService:
def __init__(self, config_path: str = "config.yaml"): def __init__(self, cameras: list[CameraConfig], ws_host: str = "0.0.0.0", ws_port: int = 8765, algorithm: str = ""):
with open(config_path, "r", encoding="utf-8") as f: self.cameras = cameras
cfg = yaml.safe_load(f) self.ws_host = ws_host
self.cameras = [CameraConfig(id=c["id"], name=c.get("name", f"cam_{c['id']}"), index=c["index"]) self.ws_port = ws_port
for c in cfg.get("cameras", [])] self.algorithm = algorithm
self.stop_event = threading.Event() self.stop_event = threading.Event()
self.raw_queue = queue.Queue(maxsize=3) # 原始队列,容量较小 self.raw_queue = queue.Queue(maxsize=3) # 原始队列,容量较小
self.ws_queue = queue.Queue(maxsize=1000) # WebSocket队列 self.ws_queue = queue.Queue(maxsize=1000) # WebSocket队列
self.frame_processor_workers = [] self.frame_processor_workers = []
self.biz_processor = FrameProcessorWorker(self.raw_queue, self.ws_queue, self.stop_event) self.biz_processor = FrameProcessorWorker(self.raw_queue, self.ws_queue, self.stop_event, self.cameras)
self.ws_sender = WebSocketSender(self.ws_queue, self.stop_event, WS_HOST, WS_PORT) self.ws_sender = WebSocketSender(self.ws_queue, self.stop_event, self.ws_host, self.ws_port)
def start(self): def start(self):
# 确保TS分片目录存在 # 确保TS分片目录存在
@@ -334,10 +325,30 @@ class HLSKadianService:
if __name__ == "__main__": if __name__ == "__main__":
service = HLSKadianService("config.yaml") parser = argparse.ArgumentParser(description="HLS Service for 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")
parser.add_argument("--ws-host", type=str, default="0.0.0.0", help="WebSocket host")
parser.add_argument("--ws-port", type=int, default=8765, help="WebSocket port")
parser.add_argument("--algorithm", type=str, default="", help="Algorithm type")
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 = HLSKadianService(cameras, ws_host=args.ws_host, ws_port=args.ws_port, algorithm=args.algorithm)
service.start() service.start()
try: try:
while True: while True:
time.sleep(1) time.sleep(1)
except KeyboardInterrupt: except KeyboardInterrupt:
service.stop() service.stop()