为何%Processor Time (Process _Total)值能超出100%?

本文探讨了%ProcessorTime指标在多核或多处理器环境下为何能超出100%的情况,介绍了不同计数器的具体含义,并分析了多处理器系统上资源管理器报告的处理器利用率超过100%的原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

为何%Processor Time (Process _Total)值能超出100%?  


% Processor Time (Processor _Total)指处理器用来执行非闲置线程时间的百分比。计算方法是,度量处理器用来执行空闲线程的时间,然后用 100% 减去该值。(每个处理器有一个空闲线程,该线程在没有其他线程可以运行时消耗周期)。此计数器是处理器活动的主要指示器,显示在采样间隔期间所观察的繁忙时间平均百分比。应注意,对处理器是否空闲的计算是在系统时钟的内部采样间隔期间(10ms)执行的。考虑到现在的处理器速度非常快,因此,在处理器可能会用大量时间为系统时钟采样间隔之间的线程提供服务时,% Processor Time 会低估处理器利用率。当恰好进行采样后即向计时器发出信号时,更可能对应用程序做出不准确地度量,基于工作负荷的计时器应用程序是一个这样的示例。

 

%Processor Time (Process _Total) 是所有进程线程使用处理器执行指令所花的时间百分比。指令是计算机执行的基础单位。线程是执行指令的对象,进程是程序运行时创建的对象。此计数包括处理某些硬件间隔和陷阱条件所执行的代码。

 

System.Diagnostics.PerformanceCounter and % Processor Time on multi-core or multi-cpu

1.     “\Process(…)\% Processor Time” can go up to N*100 (where N is the number of CPUs) because it adds up the CPU usage of the requested process across all the CPUs.

2.     “\Process(_Total)\% Processor Time” should always be around N*100 (where N is the number CPUs) because it adds up the CPU usage of each process, including the idle process.

3.     “\Processor(…)\% Processor Time” can go up to 100 because it’s the CPU usage of the requested CPU.

4.     “\Processor(_Total)\% Processor Time” can go up to 100 because it’s the average CPU usage across all CPUs.

 

Examining Processor Time Data:

On multiprocessor systems, the Processor\% Processor Time value reported by System Monitor will never exceed 100 percent for any particular processor or thread. On the other hand, the value of the % Processor Time reported for the Process object can report values over 100 percent; if such values occur, this could indicate that threads of the process are cumulatively using more than 100 percent of a processor. To get more detailed information, use the Thread object counters to analyze the processor time each thread within a process is using. Investigating other data described in this section, such as DPC activity or context switching, might help you to interpret high processor-time values.

多处理器系统中,由系统监视报告的处理器Processor Time%值对任何个别处理器或线程不会超出100%。另一方面,进程对象Processor Time%报告值能超出100%,如果超出就意味着该进程的这些线程在累加地超出100%使用一个处理器。要得到更详尽的信息,使用线程对象计数器去分析该处理器在一个进程里使用每个线程的时间。调查在这部分描述的其它数据,如延迟过程调用(DPC)活动或上下文切换,可能会帮助你去解释高Processor Time%值。

 

Resource Manager may report % Processor Utilization to be more than 100% for some reports on Multi-Processor servers

Processor usage of each process on the server is sampled at regular intervals using a standard operating system call - note that the operating system call used returns the sum of processor time used by all threads within the process. Resource Manager expresses the processor time used during the sampled interval as a percentage by dividing this time by the elapsed time since the previous sample.

On a multi-processor system it is quite possible for the percentage reported to exceed 100% in the case of a multi-threaded process. For example, on a dual-processor system, a multi-threaded process in which two threads are performing lengthy processor intensive calculations would cause a value of about 200% to be recorded.

This process-specific processor utilization is useful for gauging the state (and possibly health) of the process; if a process which normally uses little processor time is seen to be consuming a figure approaching 100%, a likely explanation is that it has encountered a bug which has caused it to enter an uncontrolled looping state.

@echo off setlocal enabledelayedexpansion :: 配置参数 set LOG_FILE=chrome_usage.log set INTERVAL=5 :: 间隔时间() set MAX_RUN_TIME=0 :: 最大运行时间(),0表示无限制 set CHROME_PROCESS=chrome.exe :: 检查日志文件是否存在,不存在则创建表头 if not exist "%LOG_FILE%" ( echo 时间,CPU占用率(%),内存占用率(Kb) > "%LOG_FILE%" ) echo 开始监控Chrome浏览器,每隔%INTERVAL%秒记录一次数据... echo 日志文件: %CD%\%LOG_FILE% echo 按Ctrl+C停止监控... :: 记录开始时间 set START_TIME=%time% set ELAPSED_TIME=0 :MAIN_LOOP :: 检查是否达到最大运行时间 if %MAX_RUN_TIME% GTR 0 ( set /a ELAPSED_TIME=!ELAPSED_TIME!+%INTERVAL% if !ELAPSED_TIME! GEQ %MAX_RUN_TIME% goto END_LOOP ) :: 获取当前时间 for /f "tokens=1-4 delims=:.," %%a in ("%time%") do ( set CUR_HOUR=%%a set CUR_MIN=%%b set CUR_SEC=%%c set CUR_MSEC=%%d ) set CUR_TIME=!CUR_HOUR!:!CUR_MIN!:!CUR_SEC! :: 获取Chrome进程的CPU和内存占用 set TOTAL_CPU=0 set TOTAL_MEM=0 set PROC_COUNT=0 for /f "tokens=1-5 delims= " %%p in ('wmic process where name^="%CHROME_PROCESS%" get ProcessID,PercentProcessorTime,WorkingSetSize ^| findstr /v "ProcessID"') do ( set /a TOTAL_CPU=!TOTAL_CPU!+%%q set /a TOTAL_MEM=!TOTAL_MEM!+%%r set /a PROC_COUNT=!PROC_COUNT!+1 ) :: 计算平均 if %PROC_COUNT% GTR 0 ( set /a AVG_CPU=!TOTAL_CPU! / %PROC_COUNT% set /a AVG_MEM=!TOTAL_MEM! / %PROC_COUNT% :: 写入日志 echo !CUR_TIME!,!AVG_CPU!%,!AVG_MEM! >> "%LOG_FILE%" :: 输出到控制台 echo [!CUR_TIME!] CPU: !AVG_CPU!%, 内存: !AVG_MEM! KB ) else ( echo [!CUR_TIME!] 未找到Chrome进程 echo [!CUR_TIME!],0,0 >> "%LOG_FILE%" ) :: 等待指定间隔 timeout /t %INTERVAL% /nobreak >nul goto MAIN_LOOP :END_LOOP echo 监控已停止,总运行时间: !ELAPSED_TIME! 秒 goto END :ERROR_HANDLING echo 发生错误: %errorlevel% goto END :END endlocal
06-18
import numpy as np import cv2 from ultralytics import YOLO import torch import time import queue from threading import Thread import psutil import multiprocessing import multiprocessing from plate_recognition.plate_rec import init_model, get_plate_result import GPUtil import pynvml import argparse import statistics from collections import defaultdict import os import threading import platform import subprocess import signal from concurrent.futures import ThreadPoolExecutor # 全局配置 CLASSES = ['danger', 'car_danger', 'headstock', 'light', 'number', '1number', 'double_number'] DETECTOR_MODEL_PATH = './weights/best.engine' # DETECTOR_MODEL_PATH = './weights/best_fp32.engine' TEXT_MODEL_PATH = './weights/plate_rec.pth' def show_frame(frame_data, stream_id, latency_queue): frame, capture_time = frame_data window_name = f"Stream {stream_id}" if frame is not None and frame.size > 0: cv2.imshow(window_name, frame) if cv2.waitKey(1) & 0xFF == ord('q'): return latency = time.time() - capture_time latency_queue.put((stream_id, latency)) def display_process(display_queue, stream_id, latency_queue): window_name = f"Stream {stream_id}" cv2.namedWindow(window_name, cv2.WINDOW_NORMAL) cv2.resizeWindow(window_name, 800, 600) try: while True: frame_data = display_queue.get() if frame_data is None: break frame, capture_time = frame_data if frame is not None and frame.size > 0: cv2.imshow(window_name, frame) latency = time.time() - capture_time latency_queue.put((stream_id, latency)) if cv2.waitKey(1) & 0xFF == ord('q'): break finally: cv2.destroyWindow(window_name) print(f"显示进程{stream_id}退出") class EnhancedResourceMonitor: def __init__(self, gpu_id, process_mgr, interval=0.5): self.gpu_id = gpu_id self.interval = interval self.running = False self.lock = threading.Lock() self.data = defaultdict(list) self.process_mgr = process_mgr # 进程管理器引用 # GPU硬件信息 self.gpu_arch = "Ada Lovelace" self.sm_count = 56 self.peak_tflops = 35.6 self.cores_per_sm = 128 def start(self): pynvml.nvmlInit() self.handle = pynvml.nvmlDeviceGetHandleByIndex(self.gpu_id) self.running = True self.thread = Thread(target=self._monitor, daemon=True) self.thread.start() def _monitor(self): while self.running: try: # 监控所有工作进程 worker_stats = [] for p in self.process_mgr.processes: try: proc = psutil.Process(p.pid) with proc.oneshot(): worker_stats.append({ 'cpu': proc.cpu_percent(), 'mem': proc.memory_info().rss / (1024 ** 2), 'threads': proc.num_threads() }) except (psutil.NoSuchProcess, psutil.AccessDenied): continue # GPU监控 util = pynvml.nvmlDeviceGetUtilizationRates(self.handle) mem_info = pynvml.nvmlDeviceGetMemoryInfo(self.handle) clock_mhz = pynvml.nvmlDeviceGetClockInfo(self.handle, pynvml.NVML_CLOCK_SM) # 计算实际算力 current_tflops = (self.sm_count * (clock_mhz / 1000) * self.cores_per_sm * 2) / 1000 util_percent = (current_tflops / self.peak_tflops) * 100 # 记录数据 with self.lock: if worker_stats: self.data['worker_cpu'].append(sum(s['cpu'] for s in worker_stats)) self.data['worker_mem'].append(sum(s['mem'] for s in worker_stats)) self.data['worker_threads'].append(sum(s['threads'] for s in worker_stats)) self.data['gpu_util'].append(util.gpu) self.data['gpu_mem'].append(mem_info.used / (1024 ** 2)) self.data['gpu_tflops'].append(current_tflops) except Exception as e: print(f"监控错误: {str(e)}") time.sleep(self.interval) def stop(self): self.running = False if hasattr(self, 'thread'): self.thread.join() pynvml.nvmlShutdown() return self._generate_report() def _generate_report(self): report = "\n[程序资源报告]\n" # 进程统计 if self.data.get('worker_threads'): report += f"- 工作进程数: {len(self.process_mgr.processes)}\n" report += f"- 总线程数: {max(self.data['worker_threads'])}\n" report += f"- 峰CPU使用: {max(self.data['worker_cpu']):.1f}%\n" report += f"- 峰内存占用: {max(self.data['worker_mem']):.1f}MB\n" # GPU统计 if self.data.get('gpu_tflops'): avg_tflops = statistics.mean(self.data['gpu_tflops']) report += "\n[GPU资源]\n" report += f"- 平均利用率: {statistics.mean(self.data['gpu_util']):.1f}%\n" report += f"- 峰显存: {max(self.data['gpu_mem']):.1f}MB\n" report += f"- 平均算力: {avg_tflops:.1f} TFLOPS\n" report += f"- 算力利用率: {avg_tflops/self.peak_tflops*100:.1f}%\n" return report class ResourceMonitor: def __init__(self, gpu_id, interval=0.5): self.gpu_id = gpu_id self.interval = interval self.running = False self.data = defaultdict(list) self.gpu_arch = "Ada Lovelace" self.sm_count = 56 # RTX 4070 SUPER有56个SM self.peak_tflops = 35.6 # 理论算力35.6 TFLOPS self.cores_per_sm = 128 # Ada架构每个SM有128个CUDA核心 self.lock = threading.Lock() # 添加锁 self.main_pid = os.getpid() # 记录主进程PID def start(self): pynvml.nvmlInit() self.handle = pynvml.nvmlDeviceGetHandleByIndex(self.gpu_id) self.running = True self.thread = Thread(target=self._monitor, daemon=True) self.thread.start() def _monitor(self): while self.running: try: # 改进的进程监控 main_process = psutil.Process(self.main_pid) with main_process.oneshot(): # 原子化读取 process_cpu = main_process.cpu_percent(interval=0.1) # 更短间隔 process_mem = main_process.memory_info().rss / (1024 ** 2) process_threads = main_process.num_threads() # 确保不会记录到0 if process_cpu == 0 and len(self.data['process_cpu']) > 0: process_cpu = self.data['process_cpu'][-1] * 0.9 # 使用上次的90% # 记录数据 with self.lock: self.data['process_cpu'].append(process_cpu) self.data['process_memory'].append(process_mem) self.data['process_threads'].append(process_threads) # 系统进程统计 process_count = len(list(psutil.process_iter())) cpu_percent = psutil.cpu_percent() mem = psutil.virtual_memory() # 获取当前Python进程的资源使用 current_process = psutil.Process() process_cpu = current_process.cpu_percent() process_mem = current_process.memory_info().rss / (1024 ** 2) # MB process_threads = current_process.num_threads() # GPU监控 util = pynvml.nvmlDeviceGetUtilizationRates(self.handle) mem_info = pynvml.nvmlDeviceGetMemoryInfo(self.handle) graphics_clock = pynvml.nvmlDeviceGetClockInfo(self.handle, pynvml.NVML_CLOCK_GRAPHICS) sm_clock = pynvml.nvmlDeviceGetClockInfo(self.handle, pynvml.NVML_CLOCK_SM) power_usage = pynvml.nvmlDeviceGetPowerUsage(self.handle) / 1000 # 瓦特 total_mem = sum(p.memory_info().rss for p in psutil.process_iter(['pid', 'name']) if 'python' in p.info['name'].lower()) / (1024**2) # MB # 获取当前GPU时钟频率 clock_mhz = pynvml.nvmlDeviceGetClockInfo( self.handle, pynvml.NVML_CLOCK_SM ) # 收集数据 self.data['system_processes'].append(process_count) self.data['system_cpu'].append(cpu_percent) self.data['system_memory'].append(mem.used / (1024**3)) # GB self.data['process_cpu'].append(process_cpu) self.data['process_memory'].append(process_mem) self.data['process_threads'].append(process_threads) self.data['gpu_util'].append(util.gpu) self.data['gpu_mem'].append(mem_info.used / (1024**2)) # MB self.data['gpu_power'].append(power_usage) self.data['gpu_clock_graphics'].append(graphics_clock) self.data['gpu_clock_sm'].append(sm_clock) # 实时算力计算 (TFLOPS = SM数 * 时钟频率(GHz) * 每SM核心数 * 2 / 1000) current_tflops = (self.sm_count * (clock_mhz / 1000) * self.cores_per_sm * 2) / 1000 util_percent = (current_tflops / self.peak_tflops) * 100 self.data['gpu_tflops'].append(current_tflops) self.data['gpu_sm_clock'].append(clock_mhz) self.data['gpu_util_actual'].append(util_percent) except Exception as e: print(f"算力监控错误: {e}") except (psutil.NoSuchProcess, pynvml.NVMLError) as e: print(f"监控错误(忽略): {str(e)}") except Exception as e: print(f"意外的监控错误: {str(e)}") time.sleep(self.interval) def stop(self): self.running = False self.thread.join() pynvml.nvmlShutdown() return self._generate_report() def _generate_report(self): if not any(len(v) > 0 for v in self.data.values()): return "无监控数据" report = "\n[资源使用报告]\n" report += "\n[算力分析 - RTX 4070 SUPER]\n" report += f"- GPU架构: {self.gpu_arch}\n" report += f"- 流式多处理器(SM): {self.sm_count}\n" report += f"- CUDA核心: {self.sm_count * self.cores_per_sm}\n" report += f"- 理论峰算力: {self.peak_tflops} TFLOPS\n" if self.data.get('gpu_tflops'): avg_tflops = statistics.mean(self.data['gpu_tflops']) max_tflops = max(self.data['gpu_tflops']) avg_clock = statistics.mean(self.data['gpu_sm_clock']) report += "\n[实际运行数据]\n" report += f"- 平均SM时钟: {avg_clock} MHz\n" report += f"- 平均算力: {avg_tflops:.1f} TFLOPS\n" report += f"- 峰算力: {max_tflops:.1f} TFLOPS\n" report += f"- 算力利用率: {avg_tflops/self.peak_tflops*100:.1f}%\n" # 瓶颈分析 avg_util = statistics.mean(self.data['gpu_util']) if avg_util > 90 and util_percent < 70: report += "\n[警告] 高GPU利用率但低算力利用率,可能存在内存带宽瓶颈\n" elif avg_tflops < 0.7 * self.peak_tflops: report += "\n[提示] 算力未充分利用,建议检查:\n" report += " • 批次大小(batch size)是否过小\n" report += " • 模型是否存在大量分支操作\n" # 系统级统计 report += "[系统资源]\n" system_metrics = { 'system_processes': ('系统进程数', '{:.0f}'), 'system_cpu': ('系统CPU使用率(%)', '{:.1f}'), 'system_memory': ('系统内存使用(GB)', '{:.2f}') } for key, (name, fmt) in system_metrics.items(): values = self.data.get(key, []) if values: report += ( f"{name}:\n" f" 平均: {fmt.format(statistics.mean(values))}\n" f" 最大: {fmt.format(max(values))}\n" f" 最小: {fmt.format(min(values))}\n" f" 采样数: {len(values)}\n\n" ) # 进程级统计 report += "[主进程资源]\n" process_metrics = { 'process_cpu': ('进程CPU使用率(%)', '{:.1f}'), 'process_memory': ('进程内存使用(MB)', '{:.1f}'), 'process_threads': ('程内部的线程数量', '{:.0f}') } for key, (name, fmt) in process_metrics.items(): values = self.data.get(key, []) if values: report += ( f"{name}:\n" f" 平均: {fmt.format(statistics.mean(values))}\n" f" 最大: {fmt.format(max(values))}\n" f" 最小: {fmt.format(min(values))}\n" f" 采样数: {len(values)}\n\n" ) # GPU统计 report += "[GPU资源]\n" gpu_metrics = { 'gpu_util': ('GPU利用率(%)', '{:.1f}'), 'gpu_mem': ('显存使用(MB)', '{:.1f}'), 'gpu_power': ('GPU功耗(W)', '{:.1f}'), 'gpu_clock_graphics': ('图形时钟(MHz)', '{:.0f}'), 'gpu_clock_sm': ('SM时钟(MHz)', '{:.0f}') } for key, (name, fmt) in gpu_metrics.items(): values = self.data.get(key, []) if values: report += ( f"{name}:\n" f" 平均: {fmt.format(statistics.mean(values))}\n" f" 最大: {fmt.format(max(values))}\n" f" 最小: {fmt.format(min(values))}\n" f" 采样数: {len(values)}\n\n" ) return report class VideoProcessor: def __init__(self, device): # 添加CUDA初始化 torch.cuda.empty_cache() # 加载模型前设置优化选项 torch.backends.cudnn.benchmark = True torch.backends.cuda.matmul.allow_tf32 = True torch.backends.cudnn.allow_tf32 = True # 加载模型 self.model = YOLO(DETECTOR_MODEL_PATH, task='detect') self.plate_rec_model = init_model(device, TEXT_MODEL_PATH) self.rtsp_url = "rtsp://admin:guoxinzhike901@192.168.1.108:554/cam/realmonitor?channel=1&subtype=0" self.max_retries = 3 # 预热GPU # with torch.no_grad(): # dummy_input = torch.randn(1, 3, 640, 640).to(device) # _ = self.model(dummy_input) self.device = device self.frame_count = 0 self.plate_text_cache = {} def _reconnect(self): cap = cv2.VideoCapture(self.rtsp_url, cv2.CAP_FFMPEG) cap.set(cv2.CAP_PROP_RTSP_TRANSPORT, cv2.CAP_RTSP_TRANSPORT_TCP) return cap # 在VideoProcessor类中添加中文显示支持 def process_frame(self, frame): # 增强版中文显示函数(带错误处理和字体回退) def put_chinese_text(img, text, position, font_scale, color, thickness): """支持中文显示的增强函数""" try: from PIL import Image, ImageDraw, ImageFont img_pil = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) draw = ImageDraw.Draw(img_pil) # 尝试加载字体(优先使用自定义字体,失败则回退系统字体) try: font_path = os.path.join("fonts", "platech.ttf") font = ImageFont.truetype(font_path, int(font_scale * 30)) except: font = ImageFont.load_default() print("警告:使用默认字体,中文显示可能不正常") draw.text(position, text, font=font, fill=color) return cv2.cvtColor(np.array(img_pil), cv2.COLOR_RGB2BGR) except Exception as e: print(f"文本渲染失败,使用OpenCV默认显示: {str(e)}") cv2.putText(img, text, position, cv2.FONT_HERSHEY_SIMPLEX, font_scale, color, thickness) return img self.frame_count += 1 # 1. 输入帧验证 if frame is None or frame.size == 0: print("错误:接收到空帧") return frame # 2. 模型推理(添加详细日志) try: results = self.model.track( frame, persist=True, imgsz=640, verbose=False # 关闭YOLO内置输出 ) # 调试输出 print(f"帧 {self.frame_count}: 检测到 {len(results)} 个结果") except Exception as e: print(f"模型推理错误: {str(e)}") return frame # 3. 结果解析与渲染 class_colors = { 'danger': (0, 0, 255), 'car_danger': (0, 165, 255), 'headstock': (255, 0, 0), 'light': (255, 255, 0), 'number': (0, 255, 0), '1number': (0, 255, 255), 'double_number': (128, 0, 128) } for result in results: # 验证检测结果有效性 if not hasattr(result, 'boxes') or result.boxes is None: print("警告:结果中未包含有效检测框") continue for box in result.boxes: try: # 解析检测框数据 cls_id = int(box.cls[0].item()) class_name = CLASSES[cls_id] x1, y1, x2, y2 = map(int, box.xyxy[0].tolist()) conf = box.conf[0].item() track_id = int(box.id[0].item()) if box.id is not None else None # 车牌特殊处理 if class_name == 'number' and (track_id not in self.plate_text_cache or self.frame_count % 5 == 0): plate_img = frame[y1:y2, x1:x2] if plate_img.size > 0: plate_text = get_plate_result(plate_img, self.device, self.plate_rec_model) or "识别失败" self.plate_text_cache[track_id] = plate_text try: if track_id not in self.plate_text_cache or self.frame_count % 5 == 0: plate_img = frame[y1:y2, x1:x2] if plate_img.size > 0: plate_text = get_plate_result(plate_img, self.device, self.plate_rec_model) or "识别失败" self.plate_text_cache[track_id] = plate_text else: plate_text = "无效区域" display_text = f"{self.plate_text_cache.get(track_id, '加载中...')} ID:{track_id} {conf:.2f}" except Exception as e: print(f"车牌处理异常: {str(e)}") display_text = f"车牌识别错误 ID:{track_id}" else: display_text = f"{class_name} {conf:.2f}" + (f" ID:{track_id}" if track_id else "") # 渲染检测框和文本 color = class_colors.get(class_name, (255, 255, 255)) cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2) # 文本位置修正(确保不超出画面) y_text = max(y1 - 10, 10) frame = put_chinese_text(frame, display_text, (x1, y_text), 0.7, color, 2) except Exception as e: print(f"单检测框处理错误: {str(e)}") continue return frame def display_thread(display_queue, stream_id, latency_queue): window_name = f"Stream {stream_id}" cv2.namedWindow(window_name, cv2.WINDOW_NORMAL) cv2.resizeWindow(window_name, 800, 600) try: while True: frame_data = display_queue.get() if frame_data is None: break frame, capture_time = frame_data if frame is not None and frame.size > 0: cv2.imshow(window_name, frame) latency = time.time() - capture_time latency_queue.put((stream_id, latency)) if cv2.waitKey(1) & 0xFF == ord('q'): break finally: cv2.destroyWindow(window_name) print(f"显示线程{stream_id}退出") class StreamSimulator: def __init__(self, source_url, num_streams, shared_frame_queue): self.source_url = source_url self.num_streams = num_streams self.shared_frame_queue = shared_frame_queue self.display_queues = [multiprocessing.Queue(maxsize=2000) for _ in range(num_streams)] # 使用 multiprocessing.Queue self.stop_flag = multiprocessing.Event() self.capture_process = None def start(self): self.capture_process = multiprocessing.Process(target=self._capture_and_distribute) self.capture_process.start() def stop(self): self.stop_flag.set() if self.capture_process: self.capture_process.join(timeout=5) if self.capture_process.is_alive(): self.capture_process.terminate() print("强制终止捕获进程") def _capture_and_distribute(self): rtsp_url = self.source_url cap = cv2.VideoCapture(rtsp_url, cv2.CAP_FFMPEG) cap.set(cv2.CAP_PROP_BUFFERSIZE, 1) cap.set(cv2.CAP_PROP_FPS, 15) skip_frames = 2 # 每 2 帧处理 1 帧 frame_count = 0 try: while not self.stop_flag.is_set(): ret, frame = cap.read() if not ret: print("帧读取失败,重连中...") cap.release() time.sleep(2) cap = cv2.VideoCapture(rtsp_url, cv2.CAP_FFMPEG) continue frame_count += 1 if frame_count % skip_frames == 0: for i in range(self.num_streams): try: if not self.shared_frame_queue.full(): self.shared_frame_queue.put((frame.copy(), i, time.time()), block=False) else: print(f"共享帧队列已满,丢弃旧帧") self.shared_frame_queue.get_nowait() self.shared_frame_queue.put((frame.copy(), i, time.time()), block=False) except Exception as e: print(f"帧队列操作警告: {type(e).__name__}") finally: cap.release() self.shared_frame_queue.put(None) for q in self.display_queues: q.put(None) def dispatch_process(result_queue, display_queues): frame_buffer = [] while True: data = result_queue.get() if data is None: break # 检查是否为 'stats' 二元组 if isinstance(data, tuple) and len(data) == 2 and data[0] == 'stats': continue # 跳过中间统计数据 # 检查是否为帧数据(三元组) elif isinstance(data, tuple) and len(data) == 3: processed_frame, stream_id, capture_time = data frame_buffer.append((processed_frame, stream_id, capture_time)) frame_buffer.sort(key=lambda x: x[2]) # 按时间戳排序 for frame, sid, _ in frame_buffer: if not display_queues[sid].full(): display_queues[sid].put((frame, capture_time), block=False) else: print(f"显示队列 {sid} 已满,丢帧") frame_buffer = [] # 清空缓冲区 # 检查是否为字典(最终统计数据) elif isinstance(data, dict): continue # 跳过最终统计数据 else: print(f"警告:未知数据类型: {type(data)}") def display_process(display_queue, stream_id, latency_queue): window_name = f"Stream {stream_id}" cv2.namedWindow(window_name, cv2.WINDOW_NORMAL) cv2.resizeWindow(window_name, 800, 600) frame_count = 0 try: while True: frame_data = display_queue.get() if frame_data is None: break frame, capture_time = frame_data if frame is not None and frame.size > 0: cv2.imshow(window_name, frame) frame_count += 1 if frame_count % 10 == 0: latency = time.time() - capture_time latency_queue.put((stream_id, latency)) if cv2.getWindowProperty(window_name, cv2.WND_PROP_VISIBLE) < 1: break if cv2.waitKey(1) & 0xFF == ord('q'): break finally: cv2.destroyWindow(window_name) print(f"显示进程{stream_id}退出") def worker_process(input_queue, gpu_id, result_queue, stats_queue, monitor_interval=5): import numpy as np # 显式导入 numpy print(f"In worker process {os.getpid()}, np is {np}, type(np.empty((1,))) = {type(np.empty((1,)))}") from collections import defaultdict torch.set_num_threads(1) cv2.setNumThreads(1) torch.cuda.set_device(gpu_id) device = torch.device(f"cuda:{gpu_id}" if torch.cuda.is_available() else "cpu") processor = VideoProcessor(device) start_time = time.time() stats = { 'frame_count': 0, 'avg_fps': 0, 'max_gpu_mem': 0, 'process_time': 0, 'stream_id': None } frame_counts_per_stream = defaultdict(int) try: while True: frame_data = input_queue.get() if frame_data is None: break frame, stream_id, capture_time = frame_data stats['stream_id'] = stream_id # 单帧处理 start_process = time.time() results = processor.model.track(frame, imgsz=640, verbose=False) # 处理单帧 stats['process_time'] += time.time() - start_process processed_frame = processor.process_frame(frame) # 移除 cProfile if processed_frame is not None and processed_frame.size > 0: stats['frame_count'] += 1 frame_counts_per_stream[stream_id] += 1 result_queue.put((processed_frame, stream_id, capture_time)) # 定期更新统计信息 if stats['frame_count'] % monitor_interval == 0: duration = time.time() - start_time stats['avg_fps'] = stats['frame_count'] / duration if torch.cuda.is_available(): mem = torch.cuda.max_memory_allocated() / (1024 ** 2) stats['max_gpu_mem'] = max(stats['max_gpu_mem'], mem) stats['worker_pid'] = os.getpid() stats['frame_counts_per_stream'] = dict(frame_counts_per_stream) stats_queue.put(('stats', stats.copy())) except Exception as e: print(f"工作进程错误: {e}") finally: stats['worker_pid'] = os.getpid() stats['frame_counts_per_stream'] = dict(frame_counts_per_stream) stats_queue.put(stats) def get_gpu_info(): """获取GPU信息""" pynvml.nvmlInit() gpu_count = pynvml.nvmlDeviceGetCount() gpus = [] for i in range(gpu_count): handle = pynvml.nvmlDeviceGetHandleByIndex(i) name = pynvml.nvmlDeviceGetName(handle) mem = pynvml.nvmlDeviceGetMemoryInfo(handle) gpus.append({ 'id': i, 'name': name.decode('utf-8') if isinstance(name, bytes) else name, 'total_mem': mem.total / (1024 ** 2) }) pynvml.nvmlShutdown() return gpus import os import argparse def monitor_resources(gpu_id, interval=5): """资源监控线程""" pynvml.nvmlInit() handle = pynvml.nvmlDeviceGetHandleByIndex(gpu_id) while True: # GPU监控 util = pynvml.nvmlDeviceGetUtilizationRates(handle) mem_info = pynvml.nvmlDeviceGetMemoryInfo(handle) # CPU监控 cpu_percent = psutil.cpu_percent() mem = psutil.virtual_memory() print(f"\n[资源监控] GPU: {util.gpu}% 显存: {mem_info.used/1024**2:.1f}/{mem_info.total/1024**2:.1f}MB | " f"CPU: {cpu_percent}% 内存: {mem.used/1024**3:.1f}/{mem.total/1024**3:.1f}GB") time.sleep(interval) class DynamicProcessManager: def __init__(self, num_workers): self.num_workers = num_workers self.processes = [] self.result_queues = [] def start_workers(self, input_queue, gpu_id, result_queue, stats_queue): for i in range(self.num_workers): p = multiprocessing.Process( target=worker_process, args=(input_queue, gpu_id, result_queue, stats_queue) ) self.processes.append(p) p.start() def stop_workers(self): for p in self.processes: if p.is_alive(): p.terminate() try: p.join(timeout=1) except: pass if p.is_alive(): if platform.system() == "Windows": subprocess.run(['taskkill', '/F', '/PID', str(p.pid)], check=False) else: os.kill(p.pid, signal.SIGKILL) print(f"强制终止进程 {p.pid}") self.processes = [] def get_gpu_info(): pynvml.nvmlInit() gpu_count = pynvml.nvmlDeviceGetCount() gpus = [] for i in range(gpu_count): handle = pynvml.nvmlDeviceGetHandleByIndex(i) name = pynvml.nvmlDeviceGetName(handle) mem = pynvml.nvmlDeviceGetMemoryInfo(handle) gpus.append({ 'id': i, 'name': name.decode('utf-8') if isinstance(name, bytes) else name, 'total_mem': mem.total / (1024 ** 2) }) pynvml.nvmlShutdown() return gpus class ProgramMonitor: def __init__(self, gpu_id, process_manager, result_queue, stats_queue, args): self.gpu_id = gpu_id self.result_queue = result_queue self.stats_queue = stats_queue self.process_manager = process_manager self.args = args self.running = False self.stop_flag = threading.Event() self.data = { 'process': defaultdict(list), 'workers': defaultdict(list), 'gpu': defaultdict(list), 'fps_per_stream': defaultdict(list), 'total_fps': [], 'worker_stats': [], 'cpu_per_core': [], 'mem_bandwidth': [] } self.lock = threading.Lock() self.gpu_info = { 'arch': "Ada Lovelace", 'sm_count': 56, 'cores_per_sm': 128, 'peak_tflops': 35.6 } self.total_frame_counts = defaultdict(int) self.last_frame_counts = defaultdict(lambda: defaultdict(int)) self.start_time = None self.stop_time = None self.last_mem_time = time.time() self.last_mem_bytes = psutil.virtual_memory().used def start(self): pynvml.nvmlInit() self.handle = pynvml.nvmlDeviceGetHandleByIndex(self.gpu_id) self.running = True self.start_time = time.time() self.thread = Thread(target=self._monitor, daemon=True) self.thread.start() def _monitor(self): last_cpu_times = {} while not self.stop_flag.is_set(): try: # Process stats from stats_queue try: data = self.stats_queue.get_nowait() if isinstance(data, tuple) and data[0] == 'stats': stats = data[1] worker_pid = stats['worker_pid'] frame_counts_per_stream = stats['frame_counts_per_stream'] with self.lock: for stream_id, count in frame_counts_per_stream.items(): delta = count - self.last_frame_counts[worker_pid][stream_id] self.total_frame_counts[stream_id] += delta self.last_frame_counts[worker_pid][stream_id] = count except queue.Empty: pass # Main process monitoring main_process = psutil.Process(os.getpid()) with main_process.oneshot(): current_cpu_time = main_process.cpu_times() pid = main_process.pid if pid in last_cpu_times: cpu_usage = self._calculate_cpu_usage(last_cpu_times[pid], current_cpu_time) self.data['process']['cpu'].append(cpu_usage) last_cpu_times[pid] = current_cpu_time self.data['process']['mem'].append(main_process.memory_info().rss / (1024 ** 2)) self.data['process']['threads'].append(main_process.num_threads()) # Worker processes monitoring for p in self.process_manager.processes: try: proc = psutil.Process(p.pid) with proc.oneshot(): current_cpu_time = proc.cpu_times() pid = p.pid if pid in last_cpu_times: cpu_usage = self._calculate_cpu_usage(last_cpu_times[pid], current_cpu_time) self.data['workers']['cpu'].append(cpu_usage) last_cpu_times[pid] = current_cpu_time self.data['workers']['mem'].append(proc.memory_info().rss / (1024 ** 2)) self.data['workers']['threads'].append(proc.num_threads()) except (psutil.NoSuchProcess, psutil.AccessDenied): continue # Memory bandwidth monitoring current_time = time.time() current_mem_bytes = psutil.virtual_memory().used time_delta = current_time - self.last_mem_time if time_delta > 0: mem_bandwidth = (current_mem_bytes - self.last_mem_bytes) / time_delta / (1024 ** 2) with self.lock: self.data['mem_bandwidth'].append(mem_bandwidth) self.last_mem_time = current_time self.last_mem_bytes = current_mem_bytes # CPU per core monitoring cpu_per_core = psutil.cpu_percent(percpu=True) with self.lock: self.data['cpu_per_core'].append(cpu_per_core) self._monitor_gpu() except Exception as e: print(f"监控错误: {str(e)}") time.sleep(0.5) def _monitor_gpu(self): try: util = pynvml.nvmlDeviceGetUtilizationRates(self.handle) mem_info = pynvml.nvmlDeviceGetMemoryInfo(self.handle) clock_mhz = pynvml.nvmlDeviceGetClockInfo(self.handle, pynvml.NVML_CLOCK_SM) current_tflops = (self.gpu_info['sm_count'] * (clock_mhz / 1000) * self.gpu_info['cores_per_sm'] * 2) / 1000 with self.lock: self.data['gpu']['util'].append(util.gpu) self.data['gpu']['mem'].append(mem_info.used / (1024 ** 2)) self.data['gpu']['tflops'].append(current_tflops) except pynvml.NVMLError as e: print(f"GPU监控错误: {str(e)}") def stop(self): self.stop_time = time.time() self.running = False self.stop_flag.set() if self.thread.is_alive(): self.thread.join(timeout=2) report = self.generate_report() pynvml.nvmlShutdown() return report def generate_report(self): report = "\n=== 程序资源使用报告 ===\n" # System information (unchanged) report += "\n[系统信息]\n" report += f"- CPU核心数: {psutil.cpu_count(logical=False)}物理/{psutil.cpu_count()}逻辑\n" report += f"- 系统内存: {psutil.virtual_memory().total / (1024**3):.1f}GB\n" report += f"- 系统CPU使用率: {psutil.cpu_percent(interval=1):.1f}%\n" report += f"- 系统内存使用: {psutil.virtual_memory().used / (1024**3):.1f}GB / {psutil.virtual_memory().total / (1024**3):.1f}GB\n" gpu_name_raw = pynvml.nvmlDeviceGetName(self.handle) gpu_name = gpu_name_raw.decode('utf-8') if isinstance(gpu_name_raw, bytes) else gpu_name_raw total_gpu_mem = pynvml.nvmlDeviceGetMemoryInfo(self.handle).total / (1024 ** 2) report += f"- GPU型号: {gpu_name}\n" report += f"- GPU总显存: {total_gpu_mem:.1f}MB\n" # Main process stats (unchanged) if self.data['process']['cpu']: report += "\n[主进程资源]\n" report += f"- 平均CPU使用率: {statistics.mean(self.data['process']['cpu']):.1f}%\n" report += f"- 峰CPU使用率: {max(self.data['process']['cpu']):.1f}%\n" report += f"- 平均内存占用: {statistics.mean(self.data['process']['mem']):.1f}MB\n" report += f"- 峰内存占用: {max(self.data['process']['mem']):.1f}MB\n" report += f"- 线程数: {max(self.data['process']['threads'])}\n" # Worker processes stats (unchanged except for FPS section) if self.data['workers']['cpu']: num_workers = min(self.args.streams * 4, psutil.cpu_count(logical=True) * 2) num_samples = len(self.data['workers']['cpu']) // num_workers if num_samples > 0: worker_cpu_per_sample = [self.data['workers']['cpu'][i*num_workers:(i+1)*num_workers] for i in range(num_samples)] worker_mem_per_sample = [self.data['workers']['mem'][i*num_workers:(i+1)*num_workers] for i in range(num_samples)] worker_threads_per_sample = [self.data['workers']['threads'][i*num_workers:(i+1)*num_workers] for i in range(num_samples)] avg_worker_cpu = statistics.mean([statistics.mean(sample) for sample in worker_cpu_per_sample]) total_worker_cpu = statistics.mean([sum(sample) for sample in worker_cpu_per_sample]) avg_worker_mem = statistics.mean([statistics.mean(sample) for sample in worker_mem_per_sample]) total_worker_mem = statistics.mean([sum(sample) for sample in worker_mem_per_sample]) max_total_worker_threads = max([sum(sample) for sample in worker_threads_per_sample]) report += f"\n[工作进程资源 ({num_workers}个)]\n" report += f"- 平均CPU使用率(每个进程): {avg_worker_cpu:.1f}%\n" report += f"- 总CPU使用率: {total_worker_cpu:.1f}%\n" report += f"- 平均内存占用(每个进程): {avg_worker_mem:.1f}MB\n" report += f"- 总内存占用: {total_worker_mem:.1f}MB\n" report += f"- 总线程数(峰): {max_total_worker_threads}\n" # Video stream performance with accurate FPS if self.total_frame_counts: elapsed_time = self.stop_time - self.start_time report += "\n[视频流性能]\n" for stream_id in range(self.args.streams): if stream_id in self.total_frame_counts: avg_fps = self.total_frame_counts[stream_id] / elapsed_time report += f"- 视频流 {stream_id}: 平均 FPS {avg_fps:.1f}\n" total_frames = sum(self.total_frame_counts.values()) total_fps = total_frames / elapsed_time report += f"- 总吞吐量: {total_fps:.1f} FPS\n" # CPU per core (unchanged) if self.data.get('cpu_per_core'): avg_cpu_per_core = [statistics.mean([sample[i] for sample in self.data['cpu_per_core']]) for i in range(len(self.data['cpu_per_core'][0]))] overall_avg_cpu = statistics.mean(avg_cpu_per_core) report += "\n[CPU 硬件线程利用率]\n" for i, avg in enumerate(avg_cpu_per_core): report += f"- 逻辑处理器 {i}: {avg:.1f}%\n" report += f"- 16 个硬件线程平均利用率: {overall_avg_cpu:.1f}%\n" # Total process stats (unchanged) if self.data['process']['cpu'] and self.data['workers']['cpu']: num_display_processes = self.args.streams total_cpu = statistics.mean(self.data['process']['cpu']) + total_worker_cpu total_mem = statistics.mean(self.data['process']['mem']) + total_worker_mem total_threads = max(self.data['process']['threads']) + max_total_worker_threads total_processes = 1 + num_workers + num_display_processes + 1 report += "\n[所有进程总计]\n" report += f"- 总CPU使用率: {total_cpu:.1f}%\n" report += f"- 总内存占用: {total_mem:.1f}MB\n" report += f"- 总线程数: {total_threads}\n" report += f"- 总进程数: {total_processes}(1个主进程 + {num_workers}个工作进程 + {num_display_processes}个显示进程 + 1个分发进程)\n" # GPU stats (unchanged) if self.data['gpu']['tflops']: avg_tflops = statistics.mean(self.data['gpu']['tflops']) util_percent = min((avg_tflops / self.gpu_info['peak_tflops']) * 100, 100.0) report += "\n[GPU资源]\n" report += f"- 平均利用率: {statistics.mean(self.data['gpu']['util']):.1f}%\n" report += f"- 峰显存: {max(self.data['gpu']['mem']):.1f}MB\n" report += f"- 平均算力: {avg_tflops:.1f}/{self.gpu_info['peak_tflops']} TFLOPS\n" report += f"- 算力利用率: {util_percent:.1f}%\n" # Memory bandwidth (unchanged) if self.data.get('mem_bandwidth'): avg_mem_bandwidth = statistics.mean(self.data['mem_bandwidth']) max_mem_bandwidth = max(self.data['mem_bandwidth']) report += "\n[存储器带宽]\n" report += f"- 平均内存带宽: {avg_mem_bandwidth:.1f} MB/s\n" report += f"- 峰内存带宽: {max_mem_bandwidth:.1f} MB/s\n" return report def _calculate_cpu_usage(self, prev_times, curr_times): """ 计算基于前后的 CPU 时间的使用率百分比。 参数: prev_times: 上一次的 CPU 时间(psutil.cpu_times 对象) curr_times: 当前的 CPU 时间(psutil.cpu_times 对象) 返回: CPU 使用率(百分比) """ delta_user = curr_times.user - prev_times.user delta_system = curr_times.system - prev_times.system delta_total = (curr_times.user + curr_times.system) - (prev_times.user + prev_times.system) if delta_total > 0: cpu_usage = ((delta_user + delta_system) / delta_total) * 100 else: cpu_usage = 0.0 return cpu_usage # _monitor_gpu and _calculate_cpu_usage remain unchanged def main(): parser = argparse.ArgumentParser() parser.add_argument('--streams', type=int, default=1) parser.add_argument('--source', type=str, default="") parser.add_argument('--gpu_id', type=int, default=0) args = parser.parse_args() camera_config = { 'username': 'admin', 'password': 'guoxinzhike901' } source_url = args.source if args.source else \ f"rtsp://{camera_config['username']}:{camera_config['password']}@192.168.1.108/" gpus = get_gpu_info() print("\n[硬件配置]") print(f"- CPU核心: {psutil.cpu_count(logical=False)}物理/{psutil.cpu_count()}逻辑") print(f"- 内存: {psutil.virtual_memory().total / (1024**3):.1f}GB") print(f"- 使用GPU {args.gpu_id}: {gpus[args.gpu_id]['name']}") print(f" 显存: {gpus[args.gpu_id]['total_mem']:.1f}MB") os.environ['OMP_NUM_THREADS'] = '1' os.environ['MKL_NUM_THREADS'] = '1' print(f"\n[测试配置]") print(f"- 模拟视频流数: {args.streams}") print(f"- 视频源: {source_url}") # 创建共享队列 frame_queue_size = max(2000, 200 * args.streams) shared_frame_queue = multiprocessing.Queue(maxsize=frame_queue_size) display_queue_size = max(50, 20 * args.streams) shared_result_queue = multiprocessing.Queue(maxsize=2000) stats_queue = multiprocessing.Queue() # New queue for stats # 固定工作进程数为 16 num_workers = 1 #min(args.streams * 8, psutil.cpu_count(logical=True) * 2) process_mgr = DynamicProcessManager(num_workers) simulator = StreamSimulator(source_url, args.streams, shared_frame_queue) monitor = ProgramMonitor(args.gpu_id, process_mgr, shared_result_queue, stats_queue, args) monitor.args = args # 传递 args latency_queue = multiprocessing.Queue() # 启动工作进程 process_mgr.start_workers(shared_frame_queue, args.gpu_id, shared_result_queue, stats_queue) # 启动分发进程 dispatch_p = multiprocessing.Process( target=dispatch_process, args=(shared_result_queue, simulator.display_queues), daemon=True ) dispatch_p.start() simulator.start() monitor.start() # 启动显示进程 display_threads = [] for i in range(args.streams): t = Thread(target=display_thread, args=(simulator.display_queues[i], i+1, latency_queue)) display_threads.append(t) t.start() time.sleep(0.5) print("\n[测试开始] 程序将运行30秒...") start_time = time.time() end_time = start_time + 60*5 try: while time.time() < end_time: time.sleep(1) remaining = int(end_time - time.time()) if remaining % 10 == 0 or remaining <= 5: print(f"剩余时间: {remaining}秒") finally: runtime = time.time() - start_time print(f"\n[测试完成] 实际运行时间: {runtime:.1f}秒") print("停止模拟器...") simulator.stop() print("生成报告并停止监控...") report = monitor.stop() print("停止工作进程...") process_mgr.stop_workers() # 停止显示线程 for q in simulator.display_queues: q.put(None) for t in display_threads: t.join() # 停止分发进程 shared_result_queue.put(None) dispatch_p.join(timeout=5) if dispatch_p.is_alive(): dispatch_p.terminate() # 收集延迟测量 latencies = [] while not latency_queue.empty(): try: stream_id, latency = latency_queue.get_nowait() latencies.append(latency) except queue.Empty: break if latencies: min_latency = min(latencies) max_latency = max(latencies) avg_latency = sum(latencies) / len(latencies) report += f"\n[延迟统计]\n" report += f"- 测量次数: {len(latencies)}\n" report += f"- 最低延迟: {min_latency:.3f}秒\n" report += f"- 最高延迟: {max_latency:.3f}秒\n" report += f"- 平均延迟: {avg_latency:.3f}秒\n" else: report += "\n[延迟统计]\n- 无延迟数据\n" if torch.cuda.is_available(): torch.cuda.empty_cache() print(report) if __name__ == '__main__': multiprocessing.set_start_method('spawn') # multiprocessing.set_start_method('fork') # Linux 默认方法 main() # 测试4路视频流 # python det_ocr_shipinliu_pre.py --streams 1 --gpu_id 0 """ === 程序资源使用报告 === === 程序资源使用报告 === [系统信息] - CPU核心数: 10物理/16逻辑 - 系统内存: 63.8GB - 系统CPU使用率: 14.1% - 系统内存使用: 26.3GB / 63.8GB - GPU型号: NVIDIA GeForce RTX 4070 SUPER - GPU总显存: 12282.0MB [主进程资源] - 平均CPU使用率: 16.3% - 峰CPU使用率: 28.1% - 平均内存占用: 385.1MB - 峰内存占用: 385.7MB - 线程数: 9 [工作进程资源 (16个)] - 平均CPU使用率(每个进程): 22.1% - 总CPU使用率: 354.2% - 平均内存占用(每个进程): 801.5MB - 总内存占用: 12823.3MB - 总线程数(峰): 304 [所有进程总计] - 总CPU使用率: 370.5% - 总内存占用: 13208.4MB - 总线程数: 313 - 总进程数: 19(1个主进程 + 16个工作进程 + 1个显示进程 + 1个分发进程) [GPU资源] - 平均利用率: 31.3% - 峰显存: 8226.7MB - 平均算力: 22.7/35.6 TFLOPS - 算力利用率: 63.8% [延迟统计] - 测量次数: 67 - 最低延迟: 0.024秒 - 最高延迟: 2.499秒 - 平均延迟: 0.287秒 """ # python det_ocr_shipinliu_pre.py --streams 2 --gpu_id 0 """ === 程序资源使用报告 === [系统信息] - CPU核心数: 10物理/16逻辑 - 系统内存: 63.8GB - 系统CPU使用率: 9.8% - 系统内存使用: 26.3GB / 63.8GB - GPU型号: NVIDIA GeForce RTX 4070 SUPER - GPU总显存: 12282.0MB [主进程资源] - 平均CPU使用率: 15.3% - 峰CPU使用率: 40.6% - 平均内存占用: 386.4MB - 峰内存占用: 387.1MB - 线程数: 9 [工作进程资源 (16个)] - 平均CPU使用率(每个进程): 20.8% - 总CPU使用率: 333.1% - 平均内存占用(每个进程): 960.3MB - 总内存占用: 15364.2MB - 总线程数(峰): 328 [所有进程总计] - 总CPU使用率: 348.4% - 总内存占用: 15750.6MB - 总线程数: 337 - 总进程数: 20(1个主进程 + 16个工作进程 + 2个显示进程 + 1个分发进程) [GPU资源] - 平均利用率: 50.5% - 峰显存: 8328.6MB - 平均算力: 12.6/35.6 TFLOPS - 算力利用率: 35.4% [延迟统计] - 测量次数: 327 - 最低延迟: 0.027秒 - 最高延迟: 0.757秒 - 平均延迟: 0.080秒 """ # python det_ocr_shipinliu_pre.py --streams 3 --gpu_id 0 """ [系统信息] - CPU核心数: 10物理/16逻辑 - 系统内存: 63.8GB - 系统CPU使用率: 9.5% - 系统内存使用: 26.2GB / 63.8GB - GPU型号: NVIDIA GeForce RTX 4070 SUPER - GPU总显存: 12282.0MB [主进程资源] - 平均CPU使用率: 26.2% - 峰CPU使用率: 53.1% - 平均内存占用: 386.1MB - 峰内存占用: 386.6MB - 线程数: 9 [工作进程资源 (16个)] - 平均CPU使用率(每个进程): 43.9% - 总CPU使用率: 702.5% - 平均内存占用(每个进程): 1018.8MB - 总内存占用: 16301.3MB - 总线程数(峰): 322 [所有进程总计] - 总CPU使用率: 728.7% - 总内存占用: 16687.5MB - 总线程数: 331 - 总进程数: 21(1个主进程 + 16个工作进程 + 3个显示进程 + 1个分发进程) [GPU资源] - 平均利用率: 52.2% - 峰显存: 7861.9MB - 平均算力: 18.9/35.6 TFLOPS - 算力利用率: 53.1% [延迟统计] - 测量次数: 327 - 最低延迟: 0.030秒 - 最高延迟: 3.756秒 - 平均延迟: 1.077秒 """ # python det_ocr_shipinliu_pre.py --streams 4 --gpu_id 0 cpu100 """ === 程序资源使用报告 === [系统信息] - CPU核心数: 10物理/16逻辑 - 系统内存: 63.8GB - 系统CPU使用率: 58.6% - 系统内存使用: 36.3GB / 63.8GB - GPU型号: NVIDIA GeForce RTX 4070 SUPER - GPU总显存: 12282.0MB [主进程资源] - 平均CPU使用率: 28.0% - 峰CPU使用率: 53.1% - 平均内存占用: 386.4MB - 峰内存占用: 386.8MB - 线程数: 9 [工作进程资源 (16个)] - 平均CPU使用率(每个进程): 48.0% - 总CPU使用率: 768.7% - 平均内存占用(每个进程): 1585.2MB - 总内存占用: 25363.6MB - 总线程数(峰): 320 [所有进程总计] - 总CPU使用率: 796.7% - 总内存占用: 25750.1MB - 总线程数: 329 - 总进程数: 22(1个主进程 + 16个工作进程 + 4个显示进程 + 1个分发进程) [GPU资源] - 平均利用率: 52.9% - 峰显存: 7991.3MB - 平均算力: 20.2/35.6 TFLOPS - 算力利用率: 56.8% [延迟统计] - 测量次数: 327 - 最低延迟: 1.480秒 - 最高延迟: 14.222秒 - 平均延迟: 8.113秒 """ # python det_ocr_shipinliu_pre.py --streams 5 --gpu_id 0 """ """ # python det_ocr_shipinliu_pre.py --streams 16 --gpu_id 0 """ """ # python det_ocr_shipinliu_pre.py --streams 20 --gpu_id 0 """ """ (yolov8_bt) (base) zhang@zhang:~/danger/yolov7_crnn_ocr_detection$ python det_ocr_shipinliu_pre.py --streams 1 --gpu_id 0 [硬件配置] - CPU核心: 10物理/16逻辑 - 内存: 62.6GB - 使用GPU 0: NVIDIA GeForce RTX 4070 SUPER 显存: 12282.0MB [测试配置] - 模拟视频流数: 1 - 视频源: rtsp://admin:guoxinzhike901@192.168.1.108/ [测试开始] 程序将运行30秒... In worker process 35804, np is <module 'numpy' from '/home/zhang/miniconda3/envs/yolov8_bt/lib/python3.9/site-packages/numpy/__init__.py'>, type(np.empty((1,))) = <class 'numpy.ndarray'> Loading weights/best.engine for TensorRT inference... [06/07/2025-18:54:11] [TRT] [I] Loaded engine size: 39 MiB [06/07/2025-18:54:11] [TRT] [W] Using an engine plan file across different models of devices is not recommended and is likely to affect performance or even cause errors. [06/07/2025-18:54:13] [TRT] [I] [MemUsageChange] TensorRT-managed allocation in engine deserialization: CPU +0, GPU +33, now: CPU 0, GPU 33 (MiB) [06/07/2025-18:54:13] [TRT] [I] [MemUsageChange] TensorRT-managed allocation in IExecutionContext creation: CPU +0, GPU +39, now: CPU 0, GPU 72 (MiB) 剩余时间: 290秒 剩余时间: 280秒 剩余时间: 270秒 (yolov8_bt) (base) zhang@zhang:~/danger/yolov7_crnn_ocr_detection$ python -c "import numpy as np; print(np.__version__)" 1.23.0 代码运行后报错
06-08
import os import cv2 import numpy as np import psutil import time import argparse import json from datetime import datetime import logging import signal import sys import traceback import threading import GPUtil import subprocess import gc import shutil import queue import concurrent.futures import tracemalloc import platform import requests import zipfile class VideoProcessor: def __init__(self, config): self.config = config self.canceled = False self.start_time = time.time() self.frame_counter = 0 self.progress = 0 self.status = "就绪" self.fps = 0.0 self.mem_usage = 0.0 self.cpu_percent = 0.0 self.system_mem_percent = 0.0 self.system_mem_used = 0.0 self.system_mem_available = 0.0 self.gpu_load = 0.0 self.gpu_memory_used = 0.0 self.gpu_memory_total = 0.0 self.logger = logging.getLogger("VideoProcessor") self.resources = [] # 跟踪需要释放的资源 self.monitor_active = False self.monitor_thread = None # 多线程队列 self.frame_queue = queue.Queue(maxsize=self.config.get('queue_size', 30)) self.processed_queue = queue.Queue(maxsize=self.config.get('queue_size', 30)) # CUDA流管理 self.cuda_streams = [] self.cuda_ctx = None # 检测移动环境 self.is_mobile = self.detect_mobile_environment() if self.is_mobile: self.logger.info("检测到移动环境,启用移动端优化配置") # 内存跟踪 if self.config.get('enable_memory_monitor', False): tracemalloc.start() self.logger.info("内存跟踪已启用") # 注册信号处理 signal.signal(signal.SIGINT, self.signal_handler) signal.signal(signal.SIGTERM, self.signal_handler) def detect_mobile_environment(self): """检测是否在移动环境中运行""" try: system = platform.system().lower() uname = os.uname() # Android检测 if 'linux' in system and 'android' in uname.version.lower(): self.logger.info("检测到Android环境") return True # iOS检测 if system == 'darwin' and 'ios' in uname.machine.lower(): self.logger.info("检测到iOS环境") return True return False except Exception as e: self.logger.warning(f"移动环境检测失败: {str(e)}") return False def signal_handler(self, signum, frame): """处理中断信号""" self.logger.warning(f"接收到中断信号: {signum}, 正在优雅地停止...") self.cancel() sys.exit(1) def start_resource_monitor(self, interval=1): """启动资源监控线程""" self.monitor_active = True self.monitor_thread = threading.Thread( target=self.monitor_resources, args=(interval,), daemon=True ) self.monitor_thread.start() self.logger.info("资源监控线程已启动") def stop_resource_monitor(self): """停止资源监控线程""" if self.monitor_thread and self.monitor_thread.is_alive(): self.monitor_active = False self.monitor_thread.join(timeout=2.0) self.logger.info("资源监控线程已停止") def monitor_resources(self, interval=1): """资源监控线程函数""" self.logger.info("资源监控开始") print("\n资源监控 | 时间戳 | CPU使用率 | 内存使用 | GPU使用率 | GPU显存") print("-" * 70) while self.monitor_active: try: # CPU监控 cpu_percent = psutil.cpu_percent(interval=None) # 内存监控 mem = psutil.virtual_memory() mem_usage = f"{mem.used / (1024**3):.1f}GB/{mem.total / (1024**3):.1f}GB" # GPU监控 gpu_info = "" try: gpus = GPUtil.getGPUs() if gpus: gpu = gpus[0] gpu_info = f"{gpu.load*100:.1f}% | {gpu.memoryUsed:.1f}MB/{gpu.memoryTotal:.0f}MB" # 更新GPU状态 self.gpu_load = gpu.load * 100 self.gpu_memory_used = gpu.memoryUsed self.gpu_memory_total = gpu.memoryTotal else: gpu_info = "No GPU" except Exception as e: gpu_info = f"Error: {str(e)}" timestamp = time.strftime('%H:%M:%S') print(f"{timestamp} | {cpu_percent:6.1f}% | {mem_usage:^15} | {gpu_info}") self.logger.info(f"资源监控 | {timestamp} | CPU: {cpu_percent}% | 内存: {mem_usage} | GPU: {gpu_info}") # 内存泄漏检测 if self.config.get('enable_memory_monitor', False): snapshot = tracemalloc.take_snapshot() top_stats = snapshot.statistics('lineno') self.logger.info("内存分配Top 10:") for stat in top_stats[:10]: self.logger.info(str(stat)) time.sleep(interval) except Exception as e: self.logger.error(f"资源监控出错: {str(e)}") time.sleep(5) # 出错后等待5秒再重试 def init_cuda(self): """初始化CUDA环境""" if not self.config.get('use_gpu_processing', False) or self.is_mobile: return try: device_id = self.config.get('gpu_device_index', 0) if cv2.cuda.getCudaEnabledDeviceCount() > device_id: # 设置CUDA设备 cv2.cuda.setDevice(device_id) device = cv2.cuda.DeviceInfo(device_id) self.logger.info(f"使用GPU设备: {device.name()}") # 创建CUDA流 num_streams = self.config.get('cuda_streams', 4) self.cuda_streams = [cv2.cuda_Stream() for _ in range(num_streams)] self.logger.info(f"已创建 {num_streams} 个CUDA流") # 创建CUDA上下文 self.cuda_ctx = cv2.cuda.Device(device_id).createContext() self.logger.info("CUDA上下文已创建") else: self.logger.warning("请求的GPU设备不可用,将使用CPU处理") self.config['use_gpu_processing'] = False except Exception as e: self.logger.error(f"CUDA初始化失败: {str(e)}") self.config['use_gpu_processing'] = False def open_video_with_acceleration(self, file_path): """使用硬件加速打开视频""" # 移动端使用专用API if self.is_mobile: self.logger.info("移动端: 使用Android专用API") try: # Android专用API cap = cv2.VideoCapture(file_path, cv2.CAP_ANDROID) if cap.isOpened(): self.logger.info("Android专用API打开成功") self.resources.append(cap) return cap else: self.logger.warning("Android专用API打开失败,尝试默认方式") except: self.logger.warning("Android专用API不可用,使用默认方式") # 桌面端或移动端备选方案 if self.config.get('hardware_acceleration', 'disable') == 'disable': cap = cv2.VideoCapture(file_path) self.resources.append(cap) return cap cap = cv2.VideoCapture() self.resources.append(cap) acceleration = { 'auto': cv2.VIDEO_ACCELERATION_ANY, 'any': cv2.VIDEO_ACCELERATION_ANY, 'nvidia': cv2.VIDEO_ACCELERATION_NVIDIA, 'intel': cv2.VIDEO_ACCELERATION_INTEL, 'vaapi': cv2.VIDEO_ACCELERATION_VAAPI }.get(self.config.get('hardware_acceleration', 'auto'), cv2.VIDEO_ACCELERATION_ANY) params = [ cv2.CAP_PROP_HW_ACCELERATION, acceleration, cv2.CAP_PROP_HW_DEVICE, self.config.get('gpu_device_index', 0) ] # 降低延迟的优化参数 if self.config.get('reduce_latency', True): params.extend([ cv2.CAP_PROP_BUFFERSIZE, self.config.get('buffer_size', 3), cv2.CAP_PROP_FPS, self.config.get('target_fps', 30) ]) # MJPEG压缩 if self.config.get('use_mjpeg', True): params.extend([ cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M','J','P','G') ]) # 设置解码线程数 decoding_threads = self.config.get('decoding_threads', 0) if decoding_threads > 0: params.extend([cv2.CAP_PROP_FFMPEG_THREADS, decoding_threads]) try: cap.open(file_path, apiPreference=cv2.CAP_FFMPEG, params=params) # Intel专用加速 if self.config.get('hardware_acceleration', '') == 'intel' and cap.isOpened(): cap.set(cv2.CAP_PROP_INTEL_VIDEO_SRC_HW_ACCEL, 1) except Exception as e: self.logger.error(f"硬件加速打开失败: {str(e)}, 使用默认方式") cap = cv2.VideoCapture(file_path) return cap def update_system_stats(self): """更新系统资源统计""" self.cpu_percent = psutil.cpu_percent(interval=0.1) mem = psutil.virtual_memory() self.system_mem_percent = mem.percent self.system_mem_used = mem.used / (1024 ** 3) # GB self.system_mem_available = mem.available / (1024 ** 3) # GB def print_progress(self): """美观的进度显示""" elapsed = time.time() - self.start_time eta = (100 - self.progress) * elapsed / max(1, self.progress) if self.progress > 0 else 0 # 进度条 bar_length = 30 filled_length = int(bar_length * self.progress / 100) bar = '█' * filled_length + '-' * (bar_length - filled_length) # 队列状态 queue_status = f"Q: {self.frame_queue.qsize()}/{self.processed_queue.qsize()}" progress_str = ( f"进度: |{bar}| {self.progress}% " f"| 速度: {self.fps:.1f}fps " f"| 用时: {elapsed:.1f}s " f"| 剩余: {eta:.1f}s " f"| CPU: {self.cpu_percent:.0f}% " f"| 内存: {self.mem_usage:.1f}MB " f"| GPU: {self.gpu_load:.1f}% " f"| {queue_status}" ) print(f"\r{progress_str}", end="") self.logger.info(progress_str) def capture_thread(self, cap, total_frames): """视频捕获线程 (生产者)""" frame_idx = 0 while cap.isOpened() and not self.canceled and frame_idx < total_frames: ret, frame = cap.read() if not ret: break # 放入队列,非阻塞方式防止死锁 try: self.frame_queue.put((frame_idx, frame), timeout=1.0) frame_idx += 1 except queue.Full: if self.canceled: break time.sleep(0.01) # 发送结束信号 self.frame_queue.put((None, None)) self.logger.info(f"捕获线程完成,共捕获 {frame_idx} 帧") def processing_thread(self, output_resolution): """视频处理线程 (消费者)""" output_width, output_height = output_resolution while not self.canceled: try: # 获取帧,带超时防止死锁 frame_idx, frame = self.frame_queue.get(timeout=2.0) # 结束信号 if frame_idx is None: self.processed_queue.put((None, None)) self.frame_queue.task_done() break # 处理帧 try: # 移动端使用轻量级算法 if self.is_mobile: # 移动端优化:使用Canny边缘检测替代复杂特征检测 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 100, 200) # 将边缘检测结果与原帧合并 frame[:, :, 0] = np.minimum(frame[:, :, 0] + edges, 255) frame[:, :, 1] = np.minimum(frame[:, :, 1] + edges, 255) frame[:, :, 2] = np.minimum(frame[:, :, 2] + edges, 255) # 移动端使用快速插方法 processed_frame = cv2.resize(frame, output_resolution, interpolation=cv2.INTER_LINEAR) else: # 桌面端使用完整算法 if self.config.get('use_gpu_processing', False) and self.cuda_streams: # 选择CUDA流 (轮询) stream_idx = frame_idx % len(self.cuda_streams) stream = self.cuda_streams[stream_idx] # 上传到GPU gpu_frame = cv2.cuda_GpuMat() gpu_frame.upload(frame, stream=stream) # GPU处理 if output_resolution: gpu_frame = cv2.cuda.resize(gpu_frame, output_resolution, stream=stream) # 下载回CPU processed_frame = gpu_frame.download(stream=stream) else: # CPU处理 if output_resolution: processed_frame = cv2.resize(frame, output_resolution) else: processed_frame = frame # 放入已处理队列 self.processed_queue.put((frame_idx, processed_frame), timeout=1.0) except cv2.error as e: if 'CUDA' in str(e): self.logger.error(f"GPU处理失败: {str(e)},切换到CPU模式") self.config['use_gpu_processing'] = False # 使用CPU重试 processed_frame = cv2.resize(frame, output_resolution) if output_resolution else frame self.processed_queue.put((frame_idx, processed_frame), timeout=1.0) else: self.logger.error(f"处理帧 {frame_idx} 失败: {str(e)}") except Exception as e: self.logger.error(f"处理帧 {frame_idx} 时出错: {str(e)}") self.frame_queue.task_done() except queue.Empty: if self.canceled: break except Exception as e: self.logger.error(f"处理线程出错: {str(e)}") self.logger.info("处理线程退出") def writer_thread(self, out, total_frames): """写入线程""" frame_idx = 0 last_log_time = time.time() while not self.canceled and frame_idx < total_frames: try: # 获取已处理帧 idx, processed_frame = self.processed_queue.get(timeout=2.0) # 结束信号 if idx is None: break # 写入输出 if processed_frame is not None: out.write(processed_frame) # 更新计数 self.frame_counter += 1 frame_idx += 1 # 计算帧率 elapsed = time.time() - self.start_time self.fps = self.frame_counter / elapsed if elapsed > 0 else 0 # 更新内存使用 process = psutil.Process(os.getpid()) self.mem_usage = process.memory_info().rss / (1024 ** 2) # MB # 更新系统状态 self.update_system_stats() # 更新进度 self.progress = int(frame_idx * 100 / total_frames) # 定期打印进度 current_time = time.time() if current_time - last_log_time > 1.0 or frame_idx % 50 == 0: self.print_progress() last_log_time = current_time # 内存管理 if frame_idx % 100 == 0: gc.collect() # 检查内存使用情况 if self.system_mem_percent > 90: self.logger.warning(f"系统内存使用超过90%! (当前: {self.system_mem_percent}%)") print(f"\n警告: 系统内存使用过高 ({self.system_mem_percent}%)") self.processed_queue.task_done() except queue.Empty: if self.canceled: break except Exception as e: self.logger.error(f"写入线程出错: {str(e)}") self.logger.info(f"写入线程完成,共写入 {frame_idx} 帧") def run(self): try: self.status = "处理中..." self.logger.info("视频处理开始") self.logger.info(f"主视频: {self.config['main_video']}") self.logger.info(f"副视频: {self.config['sub_video']}") self.logger.info(f"输出文件: {self.config['output_path']}") self.start_time = time.time() # 初始化CUDA self.init_cuda() # 启动资源监控 self.start_resource_monitor() # 打开主视频 self.logger.info("正在打开主视频...") main_cap = self.open_video_with_acceleration(self.config['main_video']) if not main_cap.isOpened(): self.status = "无法打开主视频文件!" self.logger.error(f"无法打开主视频文件: {self.config['main_video']}") return False # 获取主视频信息 main_fps = main_cap.get(cv2.CAP_PROP_FPS) main_width = int(main_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) main_height = int(main_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) main_total_frames = int(main_cap.get(cv2.CAP_PROP_FRAME_COUNT)) self.logger.info(f"主视频信息: {main_width}x{main_height}@{main_fps:.1f}fps, 总帧数: {main_total_frames}") # 打开副视频 self.logger.info("正在打开副视频...") sub_cap = self.open_video_with_acceleration(self.config['sub_video']) if not sub_cap.isOpened(): self.status = "无法打开副视频文件!" self.logger.error(f"无法打开副视频文件: {self.config['sub_video']}") return False # 获取副视频信息 sub_fps = sub_cap.get(cv2.CAP_PROP_FPS) sub_width = int(sub_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) sub_height = int(sub_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) sub_total_frames = int(sub_cap.get(cv2.CAP_PROP_FRAME_COUNT)) self.logger.info(f"副视频信息: {sub_width}x{sub_height}@{sub_fps:.1f}fps, 总帧数: {sub_total_frames}") # 创建输出目录 output_dir = os.path.dirname(self.config['output_path']) if output_dir and not os.path.exists(output_dir): try: os.makedirs(output_dir) self.logger.info(f"已创建输出目录: {output_dir}") except Exception as e: self.status = f"无法创建输出目录: {output_dir}" self.logger.error(f"创建输出目录失败: {str(e)}") return False # 创建输出视频 output_width, output_height = self.config['output_resolution'] fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(self.config['output_path'], fourcc, main_fps, (output_width, output_height)) self.resources.append(out) if not out.isOpened(): self.status = "无法创建输出视频文件!请检查分辨率设置。" self.logger.error(f"无法创建输出视频: {self.config['output_path']}, 分辨率: {output_width}x{output_height}") return False # 计算主视频分段参数 if self.config['main_segment_type'] == '秒': segment_length_main = int(float(self.config['segment_a']) * main_fps) else: segment_length_main = int(self.config['segment_a']) b1 = int(self.config['b1']) b2 = int(self.config['b2']) replace_frame_count = b2 - b1 + 1 # 计算副视频分段参数 if self.config['sub_segment_type'] == '秒': segment_length_sub = int(float(self.config['segment_c']) * sub_fps) else: segment_length_sub = int(self.config['segment_c']) d = int(self.config['d']) # 计算主视频段数 segments_main = (main_total_frames + segment_length_main - 1) // segment_length_main # 计算副视频段数 segments_sub = (sub_total_frames + segment_length_sub - 1) // segment_length_sub # 检查段数是否匹配 if segments_main > segments_sub: if self.config['sub_option'] == '循环使用': self.logger.warning(f"副视频段数不足({segments_sub}),将循环使用以满足主视频段数({segments_main})") else: self.status = "副视频段数不足,无法完成替换!" self.logger.error(f"副视频段数不足: {segments_sub} < {segments_main}") return False # 初始化性能监控 process = psutil.Process(os.getpid()) self.logger.info("="*50) self.logger.info("开始视频处理") self.logger.info(f"主视频: {self.config['main_video']} ({main_total_frames}帧, {main_fps:.1f}fps)") self.logger.info(f"副视频: {self.config['sub_video']} ({sub_total_frames}帧, {sub_fps:.1f}fps)") self.logger.info(f"输出文件: {self.config['output_path']}") self.logger.info(f"分辨率: {output_width}x{output_height}") self.logger.info(f"主视频分段数: {segments_main}, 每段{segment_length_main}帧") self.logger.info(f"替换帧范围: {b1}-{b2} (每段替换{replace_frame_count}帧)") self.logger.info(f"副视频分段数: {segments_sub}, 每段{segment_length_sub}帧") self.logger.info(f"副视频起始帧: {d}") self.logger.info(f"使用GPU处理: {self.config.get('use_gpu_processing', False)}") self.logger.info(f"CUDA流数量: {len(self.cuda_streams)}") self.logger.info(f"移动环境: {self.is_mobile}") self.logger.info("="*50) print("\n" + "="*50) print("开始视频处理") print(f"主视频: {self.config['main_video']} ({main_total_frames}帧, {main_fps:.1f}fps)") print(f"副视频: {self.config['sub_video']} ({sub_total_frames}帧, {sub_fps:.1f}fps)") print(f"输出文件: {self.config['output_path']}") print(f"分辨率: {output_width}x{output_height}") print(f"主视频分段数: {segments_main}, 每段{segment_length_main}帧") print(f"替换帧范围: {b1}-{b2} (每段替换{replace_frame_count}帧)") print(f"副视频分段数: {segments_sub}, 每段{segment_length_sub}帧") print(f"副视频起始帧: {d}") print(f"使用GPU处理: {self.config.get('use_gpu_processing', False)}") print(f"CUDA流数量: {len(self.cuda_streams)}") print(f"移动环境: {self.is_mobile}") print("="*50 + "\n") # 启动多线程处理 with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor: # 启动捕获线程 capture_future = executor.submit( self.capture_thread, main_cap, main_total_frames ) # 启动处理线程 processing_future = executor.submit( self.processing_thread, (output_width, output_height) ) # 启动写入线程 writer_future = executor.submit( self.writer_thread, out, main_total_frames ) # 等待所有线程完成 concurrent.futures.wait( [capture_future, processing_future, writer_future], return_when=concurrent.futures.ALL_COMPLETED ) if not self.canceled: self.status = "处理完成" self.progress = 100 self.print_progress() print(f"\n\n处理完成!输出文件: {self.config['output_path']}") self.logger.info(f"处理完成! 总帧数: {self.frame_counter}, 耗时: {time.time() - self.start_time:.1f}秒") self.logger.info(f"输出文件: {self.config['output_path']}") return True return False except Exception as e: self.status = f"处理过程中发生错误: {str(e)}" error_trace = traceback.format_exc() self.logger.error(f"处理过程中发生错误: {str(e)}") self.logger.error(f"错误详情:\n{error_trace}") print(f"\n\n错误: {str(e)}") return False finally: self.stop_resource_monitor() self.release_resources() if self.config.get('enable_memory_monitor', False): tracemalloc.stop() def release_resources(self): """释放所有资源""" self.logger.info("正在释放资源...") for resource in self.resources: try: if hasattr(resource, 'release'): resource.release() elif hasattr(resource, 'close'): resource.close() except Exception as e: self.logger.warning(f"释放资源时出错: {str(e)}") # 释放CUDA资源 if self.cuda_ctx: try: self.cuda_ctx.destroy() self.logger.info("CUDA上下文已释放") except Exception as e: self.logger.warning(f"释放CUDA上下文时出错: {str(e)}") self.resources = [] self.logger.info("资源已释放") def cancel(self): """取消处理""" self.canceled = True self.status = "正在取消..." self.logger.warning("用户请求取消处理") print("\n正在取消处理...") # 清空队列 while not self.frame_queue.empty(): try: self.frame_queue.get_nowait() self.frame_queue.task_done() except queue.Empty: break while not self.processed_queue.empty(): try: self.processed_queue.get_nowait() self.processed_queue.task_done() except queue.Empty: break self.stop_resource_monitor() self.release_resources() def get_video_info(file_path): """获取视频文件信息""" cap = None try: cap = cv2.VideoCapture(file_path) if cap.isOpened(): width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) fps = cap.get(cv2.CAP_PROP_FPS) frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) duration = frame_count / fps if fps > 0 else 0 return { "width": width, "height": height, "fps": fps, "frame_count": frame_count, "duration": duration } return None except Exception as e: print(f"获取视频信息时出错: {str(e)}") return None finally: if cap and cap.isOpened(): cap.release() def validate_config(config): """验证配置参数""" # 检查文件存在 if not os.path.exists(config['main_video']): print(f"错误: 主视频文件不存在 - {config['main_video']}") return False if not os.path.exists(config['sub_video']): print(f"错误: 副视频文件不存在 - {config['sub_video']}") return False # 检查输出目录 output_dir = os.path.dirname(config['output_path']) if output_dir and not os.path.exists(output_dir): try: os.makedirs(output_dir) print(f"已创建输出目录: {output_dir}") except: print(f"错误: 无法创建输出目录 - {output_dir}") return False # 检查参数有效性 try: # 主视频参数 segment_a = float(config['segment_a']) if segment_a <= 0: print("错误: 分段长度必须大于0!") return False b1 = int(config['b1']) b2 = int(config['b2']) if b1 < 0 or b2 < 0: print("错误: 帧索引不能为负数!") return False if b1 > b2: print("错误: 替换开始帧(b1)必须小于或等于替换结束帧(b2)!") return False # 副视频参数 segment_c = float(config['segment_c']) if segment_c <= 0: print("错误: 分段长度必须大于0!") return False d = int(config['d']) if d < 0: print("错误: 帧索引不能为负数!") return False # 分辨率 width = int(config['output_resolution'][0]) height = int(config['output_resolution'][1]) if width <= 0 or height <= 0: print("错误: 分辨率必须大于0!") return False return True except ValueError: print("错误: 请输入有效的数字参数!") return False def save_config(config, file_path): """保存配置到文件""" try: with open(file_path, 'w') as f: json.dump(config, f, indent=2) print(f"配置已保存到: {file_path}") except Exception as e: print(f"保存配置时出错: {str(e)}") def load_config(file_path): """从文件加载配置""" try: with open(file_path, 'r') as f: config = json.load(f) # 确保配置中包含所有必要字段 required_keys = [ 'main_video', 'sub_video', 'output_path', 'main_segment_type', 'segment_a', 'b1', 'b2', 'sub_segment_type', 'segment_c', 'd', 'sub_option', 'output_resolution' ] for key in required_keys: if key not in config: print(f"警告: 配置文件中缺少 '{key}' 参数") return config except FileNotFoundError: print(f"错误: 配置文件不存在 - {file_path}") except Exception as e: print(f"加载配置时出错: {str(e)}") return None def create_default_config(): """创建默认配置""" return { "main_video": "main_video.mp4", "sub_video": "sub_video.mp4", "output_path": "output/output_video.mp4", "main_segment_type": "秒", # 默认按秒分段 "segment_a": "1", # 默认1秒 "b1": "1", # 默认替换开始帧 "b2": "1", # 默认替换结束帧 "sub_segment_type": "帧", # 默认按帧分段 "segment_c": "1", # 默认1帧 "d": "1", # 默认起始帧 "sub_option": "循环使用", "output_resolution": [1280, 720], "hardware_acceleration": "auto", "gpu_device_index": 0, "reduce_latency": True, "decoding_threads": 4, "use_gpu_processing": True, "cuda_streams": 4, "queue_size": 30, "buffer_size": 3, "target_fps": 30, "use_mjpeg": True, "enable_memory_monitor": False, "mobile_optimized": True # 新增移动端优化标志 } def detect_hardware_acceleration(): """更全面的硬件加速支持检测""" print("\n=== 硬件加速支持检测 ===") print(f"OpenCV版本: {cv2.__version__}") # 检测CUDA支持 if cv2.cuda.getCudaEnabledDeviceCount() > 0: print("CUDA支持: 可用") for i in range(cv2.cuda.getCudaEnabledDeviceCount()): try: device = cv2.cuda.getDevice(i) print(f" 设备 {i}: {device.name()}, 计算能力: {device.majorVersion()}.{device.minorVersion()}") except: print(f" 设备 {i}: 信息获取失败") else: print("CUDA支持: 不可用") # 检测OpenCL支持 print(f"OpenCL支持: {'可用' if cv2.ocl.haveOpenCL() else '不可用'}") # 获取FFMPEG信息 try: result = subprocess.run(['ffmpeg', '-version'], capture_output=True, text=True) ffmpeg_version = result.stdout.split('\n')[0] print(f"FFMPEG版本: {ffmpeg_version}") except: print("FFMPEG版本: 未找到") # 检测可用加速类型 acceleration_types = { 'NVIDIA': cv2.VIDEO_ACCELERATION_NVIDIA, 'Intel': cv2.VIDEO_ACCELERATION_INTEL, 'VAAPI': cv2.VIDEO_ACCELERATION_VAAPI, 'ANY': cv2.VIDEO_ACCELERATION_ANY } print("\n支持的硬件加速类型:") available_accelerations = [] for name, accel_type in acceleration_types.items(): cap = cv2.VideoCapture() try: params = [cv2.CAP_PROP_HW_ACCELERATION, accel_type] test_result = cap.open("", apiPreference=cv2.CAP_FFMPEG, params=params) status = "可用" if test_result else "不可用" print(f"- {name}: {status}") if test_result: available_accelerations.append(name.lower()) except: print(f"- {name}: 检测失败") finally: if cap.isOpened(): cap.release() # 如果没有可用的硬件加速,提供备选方案 if not available_accelerations: print("\n警告: 未检测到任何硬件加速支持!") print("建议:") print("1. 使用软件解码 (设置 hardware_acceleration: 'disable')") print("2. 安装以下备选库:") print(" - NVIDIA GPU 用户: 安装 CUDA Toolkit 和 cuDNN") print(" - Intel GPU 用户: 安装 Intel Media SDK") print(" - AMD/其他 GPU 用户: 安装 VAAPI") print("3. 重新编译OpenCV以支持硬件加速") print("4. 使用支持硬件加速的FFmpeg版本") else: print("\n检测到以下可用的硬件加速类型:") print(", ".join(available_accelerations)) print("在配置文件中设置 'hardware_acceleration' 参数使用") def preview_frame(config, frame_index, is_main=True): """预览指定视频的指定帧""" video_path = config['main_video'] if is_main else config['sub_video'] cap = cv2.VideoCapture(video_path) if not cap.isOpened(): print(f"无法打开视频文件: {video_path}") return total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) if frame_index >= total_frames: print(f"帧索引超出范围 (最大: {total_frames-1})") cap.release() return cap.set(cv2.CAP_PROP_POS_FRAMES, frame_index) ret, frame = cap.read() if ret: # 创建预览窗口 window_name = f"预览: {'主视频' if is_main else '副视频'} - 帧 {frame_index}" cv2.namedWindow(window_name, cv2.WINDOW_NORMAL) # 调整窗口大小 height, width = frame.shape[:2] max_height = 800 if height > max_height: scale = max_height / height frame = cv2.resize(frame, (int(width * scale), max_height)) cv2.imshow(window_name, frame) cv2.waitKey(0) cv2.destroyAllWindows() else: print(f"无法读取帧 {frame_index}") cap.release() def batch_process(config_file, output_dir): """批量处理多个配置""" try: with open(config_file) as f: batch_configs = json.load(f) except Exception as e: print(f"加载批量配置文件失败: {str(e)}") return total_tasks = len(batch_configs) print(f"\n开始批量处理 {total_tasks} 个任务") for i, config in enumerate(batch_configs): print(f"\n处理任务 {i+1}/{total_tasks}") print(f"主视频: {config['main_video']}") print(f"副视频: {config['sub_video']}") # 添加时间戳到输出文件名 timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") base_name = os.path.basename(config['output_path']) config['output_path'] = os.path.join( output_dir, f"{timestamp}_{base_name}" ) # 验证配置 if not validate_config(config): print(f"任务 {i+1} 配置验证失败,跳过") continue # 创建处理器 processor = VideoProcessor(config) success = processor.run() if success: print(f"任务 {i+1} 完成: {config['output_path']}") else: print(f"任务 {i+1} 失败") # 任务间延迟,让系统冷却 if i < total_tasks - 1: print("\n等待5秒,准备下一个任务...") time.sleep(5) def setup_logging(): """配置日志系统""" log_dir = "logs" if not os.path.exists(log_dir): os.makedirs(log_dir) timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") log_file = os.path.join(log_dir, f"video_processor_{timestamp}.log") logging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', handlers=[ logging.FileHandler(log_file), logging.StreamHandler() ] ) logger = logging.getLogger() logger.info(f"日志系统初始化完成, 日志文件: {log_file}") return logger, log_file def install_termux_dependencies(): """安装Termux所需的依赖""" print("正在安装Termux依赖...") commands = [ "pkg update && pkg upgrade -y", "pkg install python libjpeg-turbo libvulkan vulkan-loader-android ffmpeg -y", "pkg install vulkan-tools vulkan-validation-layers -y", "pkg install ocl-icd opencl-headers -y" ] for cmd in commands: print(f"执行: {cmd}") result = subprocess.run(cmd, shell=True) if result.returncode != 0: print(f"命令执行失败: {cmd}") return False print("Termux依赖安装完成") return True def verify_gpu_support(): """验证GPU支持情况""" print("\n验证GPU支持:") # 验证MediaCodec支持 print("\n1. MediaCodec支持:") result = subprocess.run(["ffmpeg", "-hwaccels"], capture_output=True, text=True) if "mediacodec" in result.stdout: print(" ✓ 支持MediaCodec硬件加速") else: print(" ✗ 不支持MediaCodec硬件加速") # 验证Vulkan支持 print("\n2. Vulkan支持:") try: result = subprocess.run(["vulkaninfo"], capture_output=True, text=True) if "deviceName" in result.stdout: print(" ✓ 支持Vulkan API") else: print(" ✗ 不支持Vulkan API") except FileNotFoundError: print(" ✗ vulkaninfo未安装,无法验证Vulkan支持") # 验证OpenCL支持 print("\n3. OpenCL支持:") try: result = subprocess.run(["clinfo"], capture_output=True, text=True) if "Platform Name" in result.stdout: print(" ✓ 支持OpenCL") else: print(" ✗ 不支持OpenCL") except FileNotFoundError: print(" ✗ clinfo未安装,无法验证OpenCL支持") print("\n验证完成") def setup_termux_gpu_acceleration(): """设置Termux GPU加速环境""" print("="*50) print("Termux GPU加速视频处理设置") print("="*50) # 安装基础依赖 if not install_termux_dependencies(): print("依赖安装失败,无法继续设置") return # 验证GPU支持 verify_gpu_support() # 下载并编译CLBlast print("\n编译安装CLBlast...") commands = [ "pkg install git cmake make -y", "git clone https://github.com/CNugteren/CLBlast", "cd CLBlast && mkdir build && cd build", "cmake .. -DCMAKE_INSTALL_PREFIX=$PREFIX", "make install" ] for cmd in commands: print(f"执行: {cmd}") result = subprocess.run(cmd, shell=True) if result.returncode != 0: print(f"命令执行失败: {cmd}") return print("\nGPU加速环境设置完成!") print("现在可以使用以下命令进行硬件加速视频处理:") print("ffmpeg -hwaccel mediacodec -i input.mp4 -c:v h264_mediacodec output.mp4") # 创建示例批处理脚本 with open("gpu_batch_process.sh", "w") as f: f.write("""#!/bin/bash # GPU加速批处理脚本 for f in *.mp4; do echo "处理: $f" ffmpeg -hwaccel mediacodec -i "$f" -c:v h264_mediacodec "gpu_$f" done echo "所有视频处理完成!" """) print("\n已创建批处理脚本: gpu_batch_process.sh") print("使用命令运行: bash gpu_batch_process.sh") def main(): # 设置日志 logger, log_file = setup_logging() # 创建参数解析器 parser = argparse.ArgumentParser(description="专业视频帧替换工具", formatter_class=argparse.RawTextHelpFormatter) parser.add_argument("--config", help="配置文件路径", default="") parser.add_argument("--save-config", help="保存默认配置到文件", action="store_true") parser.add_argument("--background", help="后台运行模式", action="store_true") parser.add_argument("--batch", help="批量处理模式,指定批量配置文件", default="") parser.add_argument("--preview-main", type=int, help="预览主视频指定帧", default=-1) parser.add_argument("--preview-sub", type=int, help="预览副视频指定帧", default=-1) parser.add_argument("--output-dir", help="批量处理输出目录", default="batch_output") parser.add_argument("--enable-gpu", help="启用GPU加速处理", action="store_true") parser.add_argument("--enable-mem-monitor", help="启用内存监控", action="store_true") parser.add_argument("--setup-termux", help="设置Termux GPU加速环境", action="store_true") args = parser.parse_args() # Termux GPU加速设置 if args.setup_termux: setup_termux_gpu_acceleration() return # 保存默认配置 if args.save_config: config_file = args.config if args.config else "video_config.json" default_config = create_default_config() save_config(default_config, config_file) print(f"默认配置已保存到: {config_file}") return # 批量处理模式 if args.batch: if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) batch_process(args.batch, args.output_dir) return # 加载配置 config = None if args.config: config = load_config(args.config) # 如果没有提供配置或加载失败,使用默认配置 if not config: print("使用默认配置") config = create_default_config() # 命令行参数覆盖配置 if args.enable_gpu: config['use_gpu_processing'] = True if args.enable_mem_monitor: config['enable_memory_monitor'] = True # 预览功能 if args.preview_main >= 0: preview_frame(config, args.preview_main, is_main=True) return if args.preview_sub >= 0: preview_frame(config, args.preview_sub, is_main=False) return # 后台模式处理 if args.background: print("后台模式运行中...") logger.info("后台模式启动") # 重定向标准输出到日志 sys.stdout = open(log_file, 'a') sys.stderr = sys.stdout # 显示硬件加速信息 detect_hardware_acceleration() # 显示配置 logger.info("\n当前配置:") logger.info(f"主视频: {config['main_video']}") logger.info(f"副视频: {config['sub_video']}") logger.info(f"输出文件: {config['output_path']}") logger.info(f"主视频分段方式: {config['main_segment_type']}, 长度: {config['segment_a']}") logger.info(f"替换帧范围: b1={config['b1']}, b2={config['b2']}") logger.info(f"副视频分段方式: {config['sub_segment_type']}, 长度: {config['segment_c']}") logger.info(f"副视频起始帧: d={config['d']}") logger.info(f"副视频不足时: {config['sub_option']}") logger.info(f"输出分辨率: {config['output_resolution'][0]}x{config['output_resolution'][1]}") logger.info(f"硬件加速: {config.get('hardware_acceleration', 'auto')}") logger.info(f"解码线程数: {config.get('decoding_threads', 0)}") logger.info(f"使用GPU处理: {config.get('use_gpu_processing', False)}") logger.info(f"CUDA流数量: {config.get('cuda_streams', 0)}") logger.info(f"队列大小: {config.get('queue_size', 30)}") logger.info(f"启用内存监控: {config.get('enable_memory_monitor', False)}") logger.info(f"移动端优化: {config.get('mobile_optimized', True)}") print("\n当前配置:") print(f"主视频: {config['main_video']}") print(f"副视频: {config['sub_video']}") print(f"输出文件: {config['output_path']}") print(f"主视频分段方式: {config['main_segment_type']}, 长度: {config['segment_a']}") print(f"替换帧范围: b1={config['b1']}, b2={config['b2']}") print(f"副视频分段方式: {config['sub_segment_type']}, 长度: {config['segment_c']}") print(f"副视频起始帧: d={config['d']}") print(f"副视频不足时: {config['sub_option']}") print(f"输出分辨率: {config['output_resolution'][0]}x{config['output_resolution'][1]}") print(f"硬件加速: {config.get('hardware_acceleration', 'auto')}") print(f"解码线程数: {config.get('decoding_threads', 0)}") print(f"使用GPU处理: {config.get('use_gpu_processing', False)}") print(f"CUDA流数量: {config.get('cuda_streams', 0)}") print(f"队列大小: {config.get('queue_size', 30)}") print(f"启用内存监控: {config.get('enable_memory_monitor', False)}") print(f"移动端优化: {config.get('mobile_optimized', True)}\n") # 验证配置 if not validate_config(config): logger.error("配置验证失败") return # 显示视频信息 main_info = get_video_info(config['main_video']) if main_info: logger.info("主视频信息:") logger.info(f" 尺寸: {main_info['width']}x{main_info['height']}") logger.info(f" 帧率: {main_info['fps']:.1f} fps") logger.info(f" 总帧数: {main_info['frame_count']}") logger.info(f" 时长: {main_info['duration']:.1f}秒") print("主视频信息:") print(f" 尺寸: {main_info['width']}x{main_info['height']}") print(f" 帧率: {main_info['fps']:.1f} fps") print(f" 总帧数: {main_info['frame_count']}") print(f" 时长: {main_info['duration']:.1f}秒") sub_info = get_video_info(config['sub_video']) if sub_info: logger.info("\n副视频信息:") logger.info(f" 尺寸: {sub_info['width']}x{sub_info['height']}") logger.info(f" 帧率: {sub_info['fps']:.1f} fps") logger.info(f" 总帧数: {sub_info['frame_count']}") logger.info(f" 时长: {sub_info['duration']:.1f}秒") print("\n副视频信息:") print(f" 尺寸: {sub_info['width']}x{sub_info['height']}") print(f" 帧率: {sub_info['fps']:.1f} fps") print(f" 总帧数: {sub_info['frame_count']}") print(f" 时长: {sub_info['duration']:.1f}秒") # 确认开始处理 if not args.background: print("\n按 Enter 开始处理,或输入 'c' 取消...") user_input = input().strip().lower() if user_input == 'c': logger.info("用户取消处理") print("处理已取消") return # 创建并运行处理器 logger.info("开始视频处理") processor = VideoProcessor(config) processor.run() # 保存配置 timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") config_file = f"video_config_{timestamp}.json" save_config(config, config_file) logger.info(f"配置已保存: {config_file}") if __name__ == "__main__": main() 分析此代码所需依赖
06-30
下次请把要修改的文件的位置,名称告诉我 然后我把文件发给你,你改好了(要完整的),再还给我,这是E:\AI_System\agent里的environment_interface.py:“import os import psutil import platform import json import sqlite3 import sys import subprocess import requests import logging from datetime import datetime from pathlib import Path logger = logging.getLogger('EnvironmentInterface') class EnvironmentInterface: def __init__(self, base_dir: str): # 配置日志 self.logger = logging.getLogger('EnvironmentInterface') self.logger.setLevel(logging.INFO) console_handler = logging.StreamHandler() console_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')) self.logger.addHandler(console_handler) self.logger.propagate = False # 工作区路径设置(使用绝对路径) self.workspace_root = self._resolve_workspace_path(base_dir) # 解析路径并确保目录存在 self.models_dir = self._resolve_and_create_dir(os.path.join(self.workspace_root, "01_模型存储")) self.cache_dir = self._resolve_and_create_dir(os.path.join(self.workspace_root, "01_模型存储", "下载缓存")) self.system_dir = self._resolve_and_create_dir(os.path.join(self.workspace_root, "02_核心代码")) self.temp_dir = self._resolve_and_create_dir(os.path.join(self.workspace_root, "04_环境工具", "临时补丁")) self.python_dir = self._resolve_and_create_dir(os.path.join(self.workspace_root, "04_环境工具", "Python环境")) # 环境配置 os.environ['PATH'] = f"{self.python_dir};{self.python_dir}\\Scripts;{os.environ['PATH']}" os.environ['HF_HOME'] = self.cache_dir # 安全策略 self.authorized_actions = { "file_access": True, "web_search": True, "command_exec": True, "software_install": False, "hardware_control": False } self.action_log = [] # 初始化数据库(使用绝对路径) self.environment_db = os.path.join(self.system_dir, 'environment.db') self._init_db() self.logger.info("✅ 环境接口初始化完成") def _resolve_workspace_path(self, base_dir: str) -> str: """解析工作区路径为绝对路径""" try: # 使用Path对象确保跨平台兼容性 base_path = Path(base_dir).resolve() workspace_path = base_path / "AI_Workspace" if not workspace_path.exists(): workspace_path.mkdir(parents=True, exist_ok=True) self.logger.info(f"创建工作区目录: {workspace_path}") return str(workspace_path) except Exception as e: self.logger.error(f"工作区路径解析失败: {str(e)}") # 回退到默认路径 fallback_path = os.path.join(os.path.expanduser("~"), "AI_Workspace") os.makedirs(fallback_path, exist_ok=True) return fallback_path def _resolve_and_create_dir(self, path): """解析路径并确保目录存在""" try: # 转换为绝对路径 abs_path = os.path.abspath(path) # 创建目录(如果不存在) if not os.path.exists(abs_path): os.makedirs(abs_path, exist_ok=True) self.logger.info(f"创建目录: {abs_path}") return abs_path except Exception as e: self.logger.error(f"目录解析失败: {path} - {str(e)}") # 创建临时目录作为回退 temp_path = os.path.join(self.workspace_root, "temp") os.makedirs(temp_path, exist_ok=True) return temp_path def _init_db(self): """初始化环境数据库""" try: conn = sqlite3.connect(self.environment_db) c = conn.cursor() # 系统信息表 c.execute('''CREATE TABLE IF NOT EXISTS system_info ( id INTEGER PRIMARY KEY, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP, os TEXT, cpu TEXT, memory REAL, disk_usage REAL )''') # 文件探索历史表 c.execute('''CREATE TABLE IF NOT EXISTS file_exploration ( id INTEGER PRIMARY KEY, path TEXT UNIQUE, last_visited DATETIME, visit_count INTEGER DEFAULT 0 )''') # 资源管理表 c.execute('''CREATE TABLE IF NOT EXISTS resources ( id INTEGER PRIMARY KEY, name TEXT, type TEXT CHECK(type IN ('skin', 'furniture', 'tool')), path TEXT, is_active BOOLEAN DEFAULT 0 )''') conn.commit() conn.close() self.logger.info(f"✅ 数据库初始化完成: {self.environment_db}") except Exception as e: self.logger.error(f"❌ 数据库初始化失败: {str(e)}") # 系统监控功能 def get_system_info(self): """获取并记录系统信息""" try: # 获取内存使用情况 mem = psutil.virtual_memory() mem_used = round(mem.used / (1024 ** 3), 1) mem_total = round(mem.total / (1024 ** 3), 1) # 获取磁盘使用情况 disk_usage = psutil.disk_usage('/').percent info = { "os": f"{platform.system()} {platform.release()}", "cpu": f"{platform.processor()} ({psutil.cpu_count(logical=False)} cores)", "memory": f"{mem_used}GB/{mem_total}GB", "disk_usage": f"{disk_usage}%" } # 保存到数据库 conn = sqlite3.connect(self.environment_db) c = conn.cursor() c.execute('''INSERT INTO system_info (os, cpu, memory, disk_usage) VALUES (?, ?, ?, ?)''', (info['os'], info['cpu'], mem_used, disk_usage)) conn.commit() conn.close() self.log_action("system_monitor", "采集系统信息") return info except Exception as e: self.logger.error(f"❌ 获取系统信息失败: {str(e)}") return { "os": f"Error: {str(e)}", "cpu": "0%", "memory": "0GB/0GB", "disk_usage": "0%" } # 文件探索功能 def explore_directory(self, path=None): """探索目录内容""" try: target_path = path or self.workspace_root target_path = os.path.abspath(target_path) # 安全路径检查 if not target_path.startswith(os.path.abspath(self.workspace_root)): return {"error": "访问路径超出工作区范围"} if not os.path.exists(target_path): return {"error": "路径不存在"} # 记录探索历史 self._record_exploration(target_path) contents = [] for item in os.listdir(target_path): full_path = os.path.join(target_path, item) try: is_dir = os.path.isdir(full_path) size = os.path.getsize(full_path) if not is_dir else 0 modified_time = os.path.getmtime(full_path) contents.append({ "name": item, "type": "directory" if is_dir else "file", "path": full_path, "size": f"{size / 1024:.1f}KB" if size < 1024 ** 2 else f"{size / (1024 ** 2):.1f}MB", "modified": datetime.fromtimestamp(modified_time).strftime("%Y-%m-%d %H:%M") }) except PermissionError: contents.append({ "name": item, "type": "unknown", "error": "权限不足" }) except Exception as e: contents.append({ "name": item, "type": "unknown", "error": str(e) }) self.log_action("file_explore", f"探索路径: {target_path}") return { "current_path": target_path, "contents": sorted(contents, key=lambda x: (x['type'] == 'directory', x['name']), reverse=True) } except Exception as e: self.logger.error(f"❌ 探索目录失败: {str(e)}") return {"error": str(e)} def _record_exploration(self, path): """记录探索历史到数据库""" try: conn = sqlite3.connect(self.environment_db) c = conn.cursor() c.execute("SELECT id FROM file_exploration WHERE path = ?", (path,)) if c.fetchone(): c.execute('''UPDATE file_exploration SET last_visited = CURRENT_TIMESTAMP, visit_count = visit_count + 1 WHERE path = ?''', (path,)) else: c.execute('''INSERT INTO file_exploration (path, last_visited, visit_count) VALUES (?, CURRENT_TIMESTAMP, 1)''', (path,)) conn.commit() except Exception as e: self.logger.error(f"❌ 记录探索历史失败: {str(e)}") finally: conn.close() # 资源管理功能 def get_resource(self, resource_type): """获取特定类型资源""" try: conn = sqlite3.connect(self.environment_db) c = conn.cursor() c.execute('''SELECT name, path, is_active FROM resources WHERE type = ?''', (resource_type,)) resources = [ {"name": item[0], "path": item[1], "is_active": bool(item[2])} for item in c.fetchall() ] return resources except Exception as e: self.logger.error(f"❌ 获取资源失败: {str(e)}") return [] finally: conn.close() def activate_resource(self, resource_name): """激活特定资源""" try: conn = sqlite3.connect(self.environment_db) c = conn.cursor() c.execute('''UPDATE resources SET is_active = 0 WHERE type = (SELECT type FROM resources WHERE name = ?)''', (resource_name,)) c.execute('''UPDATE resources SET is_active = 1 WHERE name = ?''', (resource_name,)) conn.commit() self.log_action("resource_activate", f"激活资源: {resource_name}") return True except Exception as e: self.logger.error(f"❌ 激活资源失败: {str(e)}") return False finally: conn.close() # 工作区管理功能 def get_workspace_info(self): """获取工作区信息""" return { "workspace": self.workspace_root, "models": self.models_dir, "system": self.system_dir, "cache": self.cache_dir, "temp": self.temp_dir, "python": self.python_dir } # 辅助功能 def _is_authorized(self, action): """检查操作授权状态""" return self.authorized_actions.get(action, False) def log_action(self, action, details): """记录环境操作日志""" log_entry = { "timestamp": datetime.now().isoformat(), "action": action, "details": details } self.action_log.append(log_entry) # 保存到文件 log_file = os.path.join(self.workspace_root, "environment_actions.log") try: with open(log_file, 'a', encoding='utf-8') as f: f.write(json.dumps(log_entry, ensure_ascii=False) + "\n") except Exception as e: self.logger.error(f"❌ 记录日志失败: {str(e)}") # 同时记录到logger self.logger.info(f"{action}: {details}") return True # 使用示例 if __name__ == "__main__": # 配置日志 logging.basicConfig(level=logging.INFO) # 创建环境接口实例 env = EnvironmentInterface(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # 获取工作区信息 print("工作区信息:", json.dumps(env.get_workspace_info(), indent=2, ensure_ascii=False)) # 获取系统信息 print("系统信息:", json.dumps(env.get_system_info(), indent=2, ensure_ascii=False)) # 探索目录 print("工作区内容:", json.dumps(env.explore_directory(), indent=2, ensure_ascii=False)) ”E:\AI_System\agent里的autonomous_agent.py:“import os import time import logging from dotenv import load_dotenv # 使用绝对导入 from core.config import system_config from core.environment import DefaultEnvironment, AIHome, ActionExecutor from .environment_interface import EnvironmentInterface # 导入修复后的环境接口 class AutonomousAgent: def __init__(self): self.logger = self._setup_logger() self.logger.info("🔁 初始化自主智能体核心模块...") try: # 加载环境变量 load_dotenv() # 初始化环境感知系统 self._initialize_environment_systems() # 初始化各子系统 self._initialize_subsystems() self.logger.info("✅ 自主智能体初始化完成") except Exception as e: self.logger.error(f"❌ 智能体初始化失败: {str(e)}") raise def _setup_logger(self): """配置日志记录器""" logger = logging.getLogger('AutonomousAgent') logger.setLevel(system_config.LOG_LEVEL) # 创建控制台处理器 console_handler = logging.StreamHandler() console_handler.setLevel(system_config.LOG_LEVEL) # 创建格式化器 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') console_handler.setFormatter(formatter) # 添加处理器 logger.addHandler(console_handler) logger.propagate = False return logger def _initialize_environment_systems(self): """初始化环境感知系统 - 修复后""" # 获取项目根目录 base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # 修复:使用具体实现类 DefaultEnvironment self.environment = DefaultEnvironment(base_dir) self.logger.info("✅ 环境接口初始化完成") # 初始化智能家居环境 self.home = AIHome(base_dir) self.logger.info("✅ 智能家居环境初始化完成") # 修复:ActionExecutor 不需要参数 self.executor = ActionExecutor() self.logger.info("✅ 动作执行器初始化完成") # 新增:初始化环境接口(用于数据库和资源管理) self.env_interface = EnvironmentInterface(base_dir) self.logger.info("✅ 环境接口(数据库)初始化完成") # 记录环境状态 env_status = self.environment.get_system_info() self.logger.info(f"📊 系统状态: OS={env_status['os']}, CPU={env_status['cpu']}, " f"内存={env_status['memory']}GB, 磁盘={env_status['disk_usage']}%") def _initialize_subsystems(self): """初始化所有子系统""" # 1. 初始化模型管理器 from .model_manager import ModelManager self.model_manager = ModelManager( model_dir=system_config.MODEL_DIR, device=system_config.DEVICE, default_model=system_config.DEFAULT_MODEL ) self.logger.info("✅ 模型管理器初始化完成") # 2. 初始化健康系统 from .health_system import HealthSystem self.health_system = HealthSystem(self) self.logger.info("✅ 健康系统初始化完成") # 3. 初始化记忆系统 from .memory_system import MemorySystem self.memory_system = MemorySystem(self) self.logger.info("✅ 记忆系统初始化完成") # 4. 初始化情感系统 from .affective_system import AffectiveSystem self.affective_system = AffectiveSystem(agent=self) self.logger.info("✅ 情感系统初始化完成") # 5. 初始化认知架构 from .cognitive_architecture import CognitiveArchitecture self.cognitive_architecture = CognitiveArchitecture(agent=self) self.logger.info("✅ 认知架构初始化完成") # 6. 初始化通信系统 from .communication_system import CommunicationSystem self.communication_system = CommunicationSystem( cognitive_system=self.cognitive_architecture, agent=self # 传递智能体实例 ) self.logger.info("✅ 通信系统初始化完成") def process_input(self, user_input, user_id="default"): """处理用户输入(通过通信系统)""" return self.communication_system.process_input(user_input, user_id) def run_periodic_tasks(self): """运行周期性任务""" # 更新健康状态 self.health_system.update() # 情感系统更新 self.affective_system.grow() # 记忆系统维护 self.memory_system.consolidate_memories() # 修复:环境状态监控 - 使用时间戳 current_time = time.time() if not hasattr(self, "_last_env_check"): self._last_env_check = current_time # 每5分钟检查一次 if current_time - self._last_env_check > 300: self._last_env_check = current_time self._monitor_environment() def _monitor_environment(self): """监控环境状态 - 修复后""" try: # 获取当前系统状态 env_status = self.environment.get_system_info() # 安全解析磁盘使用率 disk_str = env_status.get('disk_usage', '0%').strip() if disk_str.endswith('%'): try: disk_usage = float(disk_str[:-1]) except ValueError: disk_usage = 0.0 else: disk_usage = 0.0 if disk_usage > 90: self.logger.warning(f"⚠️ 磁盘空间不足!当前使用率: {disk_usage}%") # 安全解析内存使用 memory_str = env_status.get('memory', '0GB/0GB') try: # 支持多种格式: "3.2GB/16GB" 或 "3.2/16.0 GB" parts = memory_str.replace('GB', '').replace(' ', '').split('/') if len(parts) >= 2: memory_used = float(parts[0]) memory_total = float(parts[1]) memory_ratio = memory_used / memory_total if memory_ratio > 0.85: self.logger.warning( f"⚠️ 内存使用过高!已用: {memory_used:.1f}GB, " f"总共: {memory_total:.1f}GB, " f"使用率: {memory_ratio * 100:.1f}%" ) except (ValueError, TypeError, ZeroDivisionError) as e: self.logger.error(f"内存数据解析失败: {memory_str} - {str(e)}") # 记录到健康系统 self.health_system.record_environment_status(env_status) except Exception as e: self.logger.error(f"环境监控失败: {str(e)}", exc_info=True) def get_status(self): """获取智能体状态报告""" # 获取基础状态 status = { "health": self.health_system.get_status(), "affective": self.affective_system.get_state(), "memory_stats": self.memory_system.get_stats(), "model": self.model_manager.get_current_model_info() } # 添加环境信息 try: env_status = self.environment.get_system_info() status["environment"] = { "os": env_status.get("os", "未知"), "cpu": env_status.get("cpu", "0%"), "memory": env_status.get("memory", "0GB/0GB"), "disk": env_status.get("disk_usage", "0%") } except Exception as e: status["environment"] = {"error": str(e)} return status # 环境交互方法 def explore_environment(self, path=None): """探索环境目录""" try: return self.environment.explore_directory(path) except Exception as e: self.logger.error(f"环境探索失败: {str(e)}") return {"error": str(e)} def execute_action(self, action, parameters): """通过执行器执行动作""" try: result = self.executor.execute(action, parameters) self.logger.info(f"执行动作: {action} {parameters} → {result.get('status', '未知')}") return result except Exception as e: self.logger.error(f"动作执行失败: {action} {parameters} - {str(e)}") return {"status": "error", "message": str(e)} ”没找到你说的agent_controller.py,只看见agent_core.py:“from agent.cognitive_architecture import CognitiveArchitecture from .model_manager import ModelManager from .memory_system import MemorySystem from .knowledge_manager import KnowledgeManager from .decision_system import DecisionSystem from .affective_system import AffectiveSystem from .health_system import HealthSystem from .environment_interface import EnvironmentInterface from .communication_system import CommunicationSystem class AgentCore: def __init__(self, config): self.config = config self.model_manager = ModelManager(config) self.memory = MemorySystem(config) self.knowledge = KnowledgeManager(config, self.memory) self.cognition = CognitiveArchitecture(config, self.model_manager) self.affect = AffectiveSystem(config) self.decision = DecisionSystem(config, self.cognition, self.affect) self.environment = EnvironmentInterface(config) self.communication = CommunicationSystem(config) self.health = HealthSystem(config) self.initialized = False def initialize(self): """初始化所有核心组件""" if self.initialized: return True try: # 加载默认模型 self.model_manager.load_model(self.config.DEFAULT_MODEL) # 初始化认知架构 self.cognition.initialize() # 加载环境接口 self.environment.load_environment() # 启动健康监测 self.health.start_monitoring() self.initialized = True return True except Exception as e: print(f"❌ 智能体初始化失败: {str(e)}") return False def process_input(self, input_data): """处理输入数据""" if not self.initialized: return {"error": "Agent not initialized"} # 认知处理 processed = self.cognition.process_input(input_data) # 情感评估 affective_response = self.affect.evaluate(processed) # 决策生成 decision = self.decision.make_decision(processed, affective_response) # 执行行动 result = self.environment.execute_action(decision) # 更新记忆 self.memory.store_experience({ "input": input_data, "processed": processed, "decision": decision, "result": result }) return result”
最新发布
08-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值