From b4308d6cdc19bf618f2406bb55eadc601d44c73f Mon Sep 17 00:00:00 2001 From: SkyLi Date: Fri, 27 Mar 2026 12:21:22 +0800 Subject: [PATCH] fix: update analyze_temp_features.py for new API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除对InfraredDetector的依赖,改用TempFeatureExtractor.detect_and_extract() - 添加--device参数支持GPU/CPU切换 - 简化特征提取逻辑,直接使用detect_and_extract()返回的特征 - 添加空结果警告提示 --- scripts/analyze_temp_features.py | 70 ++++++++++++++++---------------- 1 file changed, 34 insertions(+), 36 deletions(-) diff --git a/scripts/analyze_temp_features.py b/scripts/analyze_temp_features.py index ca52d7f..4089bc2 100644 --- a/scripts/analyze_temp_features.py +++ b/scripts/analyze_temp_features.py @@ -16,7 +16,6 @@ import matplotlib.pyplot as plt from tqdm import tqdm from collections import defaultdict -from src.detect import InfraredDetector from src.extract_temp import TempFeatureExtractor, load_infrared_image @@ -24,7 +23,8 @@ def analyze_validation_set( image_dir: str, model_path: str, conf: float = 0.25, - output_dir: str = None + output_dir: str = None, + device: str = None ): """ 分析验证集,提取ROI温度特征并绘制直方图 @@ -34,6 +34,7 @@ def analyze_validation_set( model_path: YOLO模型路径 conf: 检测置信度阈值 output_dir: 输出目录 + device: 运行设备,例如 '0' 表示GPU 0,'cpu' 表示CPU """ image_dir = Path(image_dir) if output_dir: @@ -43,8 +44,7 @@ def analyze_validation_set( output_dir = Path("output/temp_analysis") output_dir.mkdir(parents=True, exist_ok=True) - # 初始化检测器和温度特征提取器 - detector = InfraredDetector(model_path, conf=conf) + # 初始化温度特征提取器(包含检测功能) extractor = TempFeatureExtractor(model_path) # 获取所有图像 @@ -64,45 +64,28 @@ def analyze_validation_set( # 加载灰度图像(用于温度提取) gray_img = load_infrared_image(str(img_path)) - # 进行检测(复用已加载的图像) - detections = detector.detect(gray_img, conf=conf) + # 使用detect_and_extract一次性完成检测和特征提取 + detections = extractor.detect_and_extract(gray_img, conf=conf, device=device) for det in detections: - bbox = det['bbox'] - x1, y1, x2, y2 = map(int, bbox) - - # 确保边界在图像范围内 - x1, y1 = max(0, x1), max(0, y1) - x2, y2 = min(gray_img.shape[1], x2), min(gray_img.shape[0], y2) - - if x2 - x1 <= 0 or y2 - y1 <= 0: - continue - - roi = gray_img[y1:y2, x1:x2] - if roi.size == 0: - continue - - # 提取温度特征 - features = extractor.extract_from_roi(roi) - # 收集全局特征 - all_features['temp_max'].append(features['temp_max']) - all_features['temp_min'].append(features['temp_min']) - all_features['temp_mean'].append(features['temp_mean']) - all_features['temp_std'].append(features['temp_std']) - all_features['temp_median'].append(features['temp_median']) - all_features['gradient_mean'].append(features['gradient_mean']) - all_features['gradient_std'].append(features['gradient_std']) + all_features['temp_max'].append(det['temp_max']) + all_features['temp_min'].append(det['temp_min']) + all_features['temp_mean'].append(det['temp_mean']) + all_features['temp_std'].append(det['temp_std']) + all_features['temp_median'].append(det['temp_median']) + all_features['gradient_mean'].append(det['gradient_mean']) + all_features['gradient_std'].append(det['gradient_std']) # 计算方差(标准差的平方) - variance = features['temp_std'] ** 2 + variance = det['temp_std'] ** 2 all_features['temp_variance'].append(variance) # 按类别收集特征 class_name = det['class_name'] - class_features[class_name]['temp_max'].append(features['temp_max']) - class_features[class_name]['temp_mean'].append(features['temp_mean']) - class_features[class_name]['temp_std'].append(features['temp_std']) + class_features[class_name]['temp_max'].append(det['temp_max']) + class_features[class_name]['temp_mean'].append(det['temp_mean']) + class_features[class_name]['temp_std'].append(det['temp_std']) class_features[class_name]['temp_variance'].append(variance) except Exception as e: @@ -110,7 +93,15 @@ def analyze_validation_set( continue # 打印统计信息 - print(f"\n总共提取了 {len(all_features['temp_max'])} 个ROI的温度特征") + total_rois = len(all_features['temp_max']) + print(f"\n总共提取了 {total_rois} 个ROI的温度特征") + + if total_rois == 0: + print("警告: 未提取到任何ROI特征,请检查:") + print(" 1. 模型文件是否存在: ", model_path) + print(" 2. 图像目录是否正确: ", image_dir) + print(" 3. 图像中是否包含可检测的目标") + return # 绘制全局直方图 draw_global_histograms(all_features, output_dir) @@ -306,6 +297,12 @@ if __name__ == '__main__': default='output/temp_analysis', help='输出目录' ) + parser.add_argument( + '--device', + type=str, + default=None, + help='运行设备,例如 "0" 表示GPU 0,"cpu" 表示CPU' + ) args = parser.parse_args() @@ -313,5 +310,6 @@ if __name__ == '__main__': image_dir=args.image_dir, model_path=args.model, conf=args.conf, - output_dir=args.output + output_dir=args.output, + device=args.device ) \ No newline at end of file