下面是在halcon上的推理示例
训练使用 MVTec Deep Learning Tool 22.10 (user) 工具,最后导出推理模型.
*读取图像
* Image Acquisition 02: Code generated by Image Acquisition 02
list_files ('E:/tu_en/halcon/混合', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
*for Index := 0 to |ImageFiles| - 1 by 1
* read_image (Image, ImageFiles[Index])
* Image Acquisition 02: Do something
*endfor
*读取训练模型
read_dl_model ('E:/tu_en/halcon/shi_li/demo1.hdl', DLModelHandle)
*设置硬件
query_available_dl_devices (['runtime', 'runtime', 'id'], ['gpu', 'cpu', 0], DLDeviceHandle)
*设置参数
set_dl_model_param (DLModelHandle, 'device', DLDeviceHandle[1])
*设置预处理参数
create_dl_preprocess_param_from_model (DLModelHandle, 'none', 'full_domain', [], [], [], DLPreprocessParam)
MaxNumInferenceImages := 10
*获取模型参数
get_dl_model_param (DLModelHandle, 'meta_data', MetaData)
*异常值 分类阈值
InferenceClassificationThreshold := number(MetaData.anomaly_classification_threshold)
*阈值下线
InferenceSegmentationThreshold := number(MetaData.anomaly_segmentation_threshold)
*分类字典
DLDatasetInfo := dict{class_names: ['ok', 'nok'], class_ids: [0, 1]}
*窗体字典
WindowDict := dict{}
*循环读取并检测
for IndexInference := 0 to |ImageFiles| - 1 by 1
* 读取图像
read_image (Image, ImageFiles[IndexInference])
gen_dl_samples_from_images (Image, DLSample)
preprocess_dl_samples (DLSample, DLPreprocessParam)
* 分类检测
apply_dl_model (DLModelHandle, DLSample, [], DLResult)
* 判断阈值
threshold_dl_anomaly_results (InferenceSegmentationThreshold, InferenceClassificationThreshold, DLResult)
*
* 显示结果
dev_display_dl_data (DLSample, DLResult, DLDatasetInfo, ['anomaly_result', 'anomaly_image'], [], WindowDict)
dev_disp_text ('Press F5 (continue)', 'window', 'bottom', 'center', 'black', [], [])
stop ()
endfor
dev_close_window_dict (WindowDict)