1.开发环境qt6,编译器MSCV2019,网络是halcon的对象检测,halcon用20.
2.建立qt项目
3.到halcon安装目录下复制include,lib这两个文件夹到qt项目中进行引用
4.引用到halcon静态库后,到halcon运行目录下找到静态库对应dll文件,还有引用深度学习的halcondl.dll跟halcondlxl.dll,hcanvas.dll
5.找齐这些文件后,把文件复制到qt软件运行目录下
6.到halcon运行目录下复制thirdparty文件夹到qt运行目录,不然软件运行时,halcon查找设备只能找到cpu,找不到gpu。
7.qt中加载halcon模型代码
void DeelLearning::ReadModel(QString path_model_,QString path_proparameter_)
{try{_path_model="";_path_model_proparameter="";if(_exit_model==true){HalconCpp::ClearDlModel(_dl_model_handle);HalconCpp::ClearHandle(_dl_device_handles);}_exit_model=false;/***读取模型预处理参数***/HalconCpp::HTuple hpath_proparameter_;hpath_proparameter_ = path_proparameter_.toStdString().c_str();HalconCpp::ReadDict(hpath_proparameter_,HalconCpp::HTuple(),HalconCpp::HTuple(),& this->_dl_model_proparameter);/***读取模型***/HalconCpp::HTuple hpath_model_=path_model_.toStdString().c_str();HalconCpp::ReadDlModel(hpath_model_,&this->_dl_model_handle);/***读取驱动***/HalconCpp::HTuple gen_param_name_devices_,gen_param_value_devices_;gen_param_name_devices_[0]="runtime";gen_param_name_devices_[1]="runtime";gen_param_value_devices_[0]="gpu";gen_param_value_devices_[1]="cpu";HalconCpp::QueryAvailableDlDevices(gen_param_name_devices_,gen_param_value_devices_,&_dl_device_handles);int dl_device_handles_count_=_dl_device_handles.Length();qDebug() << "dl_device_handles_count"+QString::number(dl_device_handles_count_);HalconCpp::HTuple dl_device_type_="";HalconCpp::GetDlDeviceParam(_dl_device_handles[0],"type",&dl_device_type_);/***提示下当前变量***/qDebug() << dl_device_type_.ToString().Text();/***设置模型驱动***/HalconCpp::SetDlModelParam(_dl_model_handle,"device",_dl_device_handles[0]);/***设置模型批量***/HalconCpp::SetDlModelParam(this->_dl_model_handle,"batch_size",1);_exit_model=true;_path_model=path_model_;_path_model_proparameter=path_proparameter_;}catch (HalconCpp::HException ex){qDebug()<<ex.ErrorMessage().Text();}
}
8.qt中推理代码
void DeelLearning::AnalyzeImage()
{if(_exit_image==true&&_exit_model==true){HalconCpp::HTuple pro_image_width_,pro_image_height_,pro_image_num_channels_,pro_image_range_min_,pro_image_range_max_,pro_image_normalization_type_,pro_image_domain_handling_;/***获取预处理参数***/HalconCpp::GetDictTuple(this->_dl_model_proparameter,"image_width",&pro_image_width_);qDebug() << pro_image_width_.ToString().Text();HalconCpp::GetDictTuple(this->_dl_model_proparameter,"image_height",&pro_image_height_);qDebug() << pro_image_height_.ToString().Text();HalconCpp::GetDictTuple(this->_dl_model_proparameter,"image_num_channels",&pro_image_num_channels_);qDebug() << pro_image_num_channels_.ToString().Text();HalconCpp::GetDictTuple(this->_dl_model_proparameter,"image_range_min",&pro_image_range_min_);qDebug() << pro_image_range_min_.ToString().Text();HalconCpp::GetDictTuple(this->_dl_model_proparameter,"image_range_max",&pro_image_range_max_);qDebug() << pro_image_range_max_.ToString().Text();/*****预处理图片****/HalconCpp::HTuple dl_sample_batch_;HalconCpp::CreateDict(&dl_sample_batch_);HalconCpp::ConvertImageType(this->_image,&this->_image,"real");HalconCpp::ZoomImageSize(this->_image,&this->_image,pro_image_width_,pro_image_height_,"constant");HalconCpp::HTuple rescale_range=(pro_image_range_max_-pro_image_range_min_)/255;HalconCpp::ScaleImage(this->_image,&this->_image,rescale_range,pro_image_range_min_);HalconCpp::SetDictObject(this->_image,dl_sample_batch_,"image");HalconCpp::HTuple dl_result_batch_;HalconCpp::ApplyDlModel(_dl_model_handle,dl_sample_batch_,HalconCpp::HTuple(),&dl_result_batch_);HalconCpp::GetDictTuple(dl_result_batch_,"bbox_row1",&_bbox_row1);HalconCpp::GetDictTuple(dl_result_batch_,"bbox_col1",&_bbox_col1);HalconCpp::GetDictTuple(dl_result_batch_,"bbox_row2",&_bbox_row2);HalconCpp::GetDictTuple(dl_result_batch_,"bbox_col2",&_bbox_col2);HalconCpp::GetDictTuple(dl_result_batch_,"bbox_class_id",&_bbox_class_id);HalconCpp::GetDictTuple(dl_result_batch_,"bbox_class_name",&_bbox_class_name);HalconCpp::GetDictTuple(dl_result_batch_,"bbox_confidence",&_bbox_class_confidence);}
}