fix: update analyze_temp_features.py for new API
- 移除对InfraredDetector的依赖,改用TempFeatureExtractor.detect_and_extract() - 添加--device参数支持GPU/CPU切换 - 简化特征提取逻辑,直接使用detect_and_extract()返回的特征 - 添加空结果警告提示
This commit is contained in:
@@ -16,7 +16,6 @@ import matplotlib.pyplot as plt
|
|||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from src.detect import InfraredDetector
|
|
||||||
from src.extract_temp import TempFeatureExtractor, load_infrared_image
|
from src.extract_temp import TempFeatureExtractor, load_infrared_image
|
||||||
|
|
||||||
|
|
||||||
@@ -24,7 +23,8 @@ def analyze_validation_set(
|
|||||||
image_dir: str,
|
image_dir: str,
|
||||||
model_path: str,
|
model_path: str,
|
||||||
conf: float = 0.25,
|
conf: float = 0.25,
|
||||||
output_dir: str = None
|
output_dir: str = None,
|
||||||
|
device: str = None
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
分析验证集,提取ROI温度特征并绘制直方图
|
分析验证集,提取ROI温度特征并绘制直方图
|
||||||
@@ -34,6 +34,7 @@ def analyze_validation_set(
|
|||||||
model_path: YOLO模型路径
|
model_path: YOLO模型路径
|
||||||
conf: 检测置信度阈值
|
conf: 检测置信度阈值
|
||||||
output_dir: 输出目录
|
output_dir: 输出目录
|
||||||
|
device: 运行设备,例如 '0' 表示GPU 0,'cpu' 表示CPU
|
||||||
"""
|
"""
|
||||||
image_dir = Path(image_dir)
|
image_dir = Path(image_dir)
|
||||||
if output_dir:
|
if output_dir:
|
||||||
@@ -43,8 +44,7 @@ def analyze_validation_set(
|
|||||||
output_dir = Path("output/temp_analysis")
|
output_dir = Path("output/temp_analysis")
|
||||||
output_dir.mkdir(parents=True, exist_ok=True)
|
output_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
# 初始化检测器和温度特征提取器
|
# 初始化温度特征提取器(包含检测功能)
|
||||||
detector = InfraredDetector(model_path, conf=conf)
|
|
||||||
extractor = TempFeatureExtractor(model_path)
|
extractor = TempFeatureExtractor(model_path)
|
||||||
|
|
||||||
# 获取所有图像
|
# 获取所有图像
|
||||||
@@ -64,45 +64,28 @@ def analyze_validation_set(
|
|||||||
# 加载灰度图像(用于温度提取)
|
# 加载灰度图像(用于温度提取)
|
||||||
gray_img = load_infrared_image(str(img_path))
|
gray_img = load_infrared_image(str(img_path))
|
||||||
|
|
||||||
# 进行检测(复用已加载的图像)
|
# 使用detect_and_extract一次性完成检测和特征提取
|
||||||
detections = detector.detect(gray_img, conf=conf)
|
detections = extractor.detect_and_extract(gray_img, conf=conf, device=device)
|
||||||
|
|
||||||
for det in detections:
|
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_max'].append(det['temp_max'])
|
||||||
all_features['temp_min'].append(features['temp_min'])
|
all_features['temp_min'].append(det['temp_min'])
|
||||||
all_features['temp_mean'].append(features['temp_mean'])
|
all_features['temp_mean'].append(det['temp_mean'])
|
||||||
all_features['temp_std'].append(features['temp_std'])
|
all_features['temp_std'].append(det['temp_std'])
|
||||||
all_features['temp_median'].append(features['temp_median'])
|
all_features['temp_median'].append(det['temp_median'])
|
||||||
all_features['gradient_mean'].append(features['gradient_mean'])
|
all_features['gradient_mean'].append(det['gradient_mean'])
|
||||||
all_features['gradient_std'].append(features['gradient_std'])
|
all_features['gradient_std'].append(det['gradient_std'])
|
||||||
|
|
||||||
# 计算方差(标准差的平方)
|
# 计算方差(标准差的平方)
|
||||||
variance = features['temp_std'] ** 2
|
variance = det['temp_std'] ** 2
|
||||||
all_features['temp_variance'].append(variance)
|
all_features['temp_variance'].append(variance)
|
||||||
|
|
||||||
# 按类别收集特征
|
# 按类别收集特征
|
||||||
class_name = det['class_name']
|
class_name = det['class_name']
|
||||||
class_features[class_name]['temp_max'].append(features['temp_max'])
|
class_features[class_name]['temp_max'].append(det['temp_max'])
|
||||||
class_features[class_name]['temp_mean'].append(features['temp_mean'])
|
class_features[class_name]['temp_mean'].append(det['temp_mean'])
|
||||||
class_features[class_name]['temp_std'].append(features['temp_std'])
|
class_features[class_name]['temp_std'].append(det['temp_std'])
|
||||||
class_features[class_name]['temp_variance'].append(variance)
|
class_features[class_name]['temp_variance'].append(variance)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -110,7 +93,15 @@ def analyze_validation_set(
|
|||||||
continue
|
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)
|
draw_global_histograms(all_features, output_dir)
|
||||||
@@ -306,6 +297,12 @@ if __name__ == '__main__':
|
|||||||
default='output/temp_analysis',
|
default='output/temp_analysis',
|
||||||
help='输出目录'
|
help='输出目录'
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--device',
|
||||||
|
type=str,
|
||||||
|
default=None,
|
||||||
|
help='运行设备,例如 "0" 表示GPU 0,"cpu" 表示CPU'
|
||||||
|
)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
@@ -313,5 +310,6 @@ if __name__ == '__main__':
|
|||||||
image_dir=args.image_dir,
|
image_dir=args.image_dir,
|
||||||
model_path=args.model,
|
model_path=args.model,
|
||||||
conf=args.conf,
|
conf=args.conf,
|
||||||
output_dir=args.output
|
output_dir=args.output,
|
||||||
|
device=args.device
|
||||||
)
|
)
|
||||||
Reference in New Issue
Block a user