feat: add device parameter support for GPU/CPU switching in inference API

- InfraredDetector.detect() 和 detect_and_draw() 新增 device 参数
- TempFeatureExtractor.detect_and_extract() 新增 device 参数
- FaultDiagnoser.diagnose() 和 batch_diagnose() 新增 device 参数
- batch_extract_features() 新增 device 参数
- 命令行 --device 选项支持指定推理设备
- detect_and_draw() 返回值改为 (image, n_detections) 避免重复检测
This commit is contained in:
SkyLi
2026-03-27 09:10:57 +08:00
parent 9a5f6fe473
commit febd9eea8d
3 changed files with 32 additions and 17 deletions
+10 -5
View File
@@ -29,12 +29,14 @@ class FaultDiagnoser:
self.extractor = TempFeatureExtractor(model_path)
self.temp_threshold = temp_threshold or {}
def diagnose(self, image: Union[str, np.ndarray]) -> Dict:
def diagnose(self, image: Union[str, np.ndarray],
device: Optional[str] = None) -> Dict:
"""
对图像进行故障诊断
Args:
image: 图像路径或数组
device: 运行设备,例如 '0' 表示GPU 0,'cpu' 表示CPU
Returns:
诊断结果
@@ -47,7 +49,7 @@ class FaultDiagnoser:
}
if self.detector and self.detector.model:
detections = self.detector.detect(image)
detections = self.detector.detect(image, device=device)
result['detections'] = detections
import cv2
@@ -148,13 +150,15 @@ class FaultDiagnoser:
}
def batch_diagnose(self, image_dir: Union[str, Path],
output_path: Union[str, Path]) -> None:
output_path: Union[str, Path],
device: Optional[str] = None) -> None:
"""
批量诊断
Args:
image_dir: 图像目录
output_path: 输出JSON路径
device: 运行设备,例如 '0' 表示GPU 0,'cpu' 表示CPU
"""
image_dir = Path(image_dir)
results = []
@@ -164,7 +168,7 @@ class FaultDiagnoser:
continue
try:
result = self.diagnose(str(img_path))
result = self.diagnose(str(img_path), device=device)
result['image'] = img_path.name
results.append(result)
except Exception as e:
@@ -192,6 +196,7 @@ if __name__ == '__main__':
parser.add_argument('--image-dir', type=str, required=True, help='Image directory')
parser.add_argument('--output', type=str, required=True, help='Output JSON path')
parser.add_argument('--threshold', type=str, help='Threshold config JSON')
parser.add_argument('--device', type=str, default=None, help='Device (e.g., "0" for GPU, "cpu" for CPU)')
args = parser.parse_args()
@@ -200,4 +205,4 @@ if __name__ == '__main__':
threshold = load_threshold_config(args.threshold)
diagnoser = FaultDiagnoser(args.model, threshold)
diagnoser.batch_diagnose(args.image_dir, args.output)
diagnoser.batch_diagnose(args.image_dir, args.output, device=args.device)