You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
5.2 KiB

#include <iostream>
#include <string>
#include "json/json.h"
#include "ImgCheckBase.h"
#include "ImgCheckConfig.h"
#include <opencv2/opencv.hpp>
#include "CheckUtil.hpp"
#include "SaveImageFile.h"
struct SystemConfigParam
{
std::string str_CheckConfigJson; // 检测基础参数json文件
std::string str_AIModelJson; // AI 模型 json文件
std::string str_ProcessNodeJson; // 过程节点 json文件
std::string str_detImgPath; // 测试图片路径;
std::string str_ImagesPath; // 批量测试
std::string str_SaveImagesPath; // 批量保存测试
SystemConfigParam()
{
str_AIModelJson = "";
str_ProcessNodeJson = "";
str_detImgPath = "";
str_ImagesPath = "";
str_SaveImagesPath = "";
}
bool valid()
{
if (str_AIModelJson.size() &&
str_ProcessNodeJson.size())
{
return true;
}
return false;
}
};
bool ReadSystemConfig(const std::string &strPath, std::shared_ptr<SystemConfigParam> &pConfig)
{
printf("Reading system config %s\n", strPath.c_str());
Json::CharReaderBuilder builder;
builder["collectComments"] = true;
Json::Value root;
std::string err;
std::ifstream ifs(strPath);
if (!ifs.is_open())
{
printf("error:file is open\n");
return false;
}
if (!Json::parseFromStream(builder, ifs, &root, &err))
{
printf("error:parseFromStream\n");
return false;
}
pConfig = std::make_shared<SystemConfigParam>();
// path
pConfig->str_detImgPath = root["detImgPath"].asString();
pConfig->str_ImagesPath = root["ImagesPath"].asString();
pConfig->str_SaveImagesPath = root["SaveImagesPath"].asString();
pConfig->str_CheckConfigJson = root["CheckConfigJson"].asString();
pConfig->str_AIModelJson = root["AIModelJson"].asString();
pConfig->str_ProcessNodeJson = root["ProcessNodeJson"].asString();
printf("str_AIModelJson %s ProcessNodeJson %s\n", pConfig->str_AIModelJson.c_str(), pConfig->str_ProcessNodeJson.c_str());
return pConfig->valid();
}
int main(int argc, char *argv[])
{
bool bsave = false;
if (argc > 1 && string(argv[1]) != "-h")
{
if (string(argv[1]) == "-s")
{
bsave = true;
}
}
printf("test ImgMeasure Count >>>>>> start bsave %d\n",bsave);
std::shared_ptr<SystemConfigParam> psystem_param;
DETECT_TYPE detect_type = DETECT_TYPE_QX; //检测类型
// ReadSystemConfig("../data/Top1_Det/System_Config.json", psystem_param);
// CAMERA_POSITION cam_position = CAMERA_TOP_1;//相机位置
ReadSystemConfig("../data/Top2_Det/System_Config.json", psystem_param);
CAMERA_POSITION cam_position = CAMERA_TOP_2;//相机位置
// ReadSystemConfig("../data/Side1_Det/System_Config.json", psystem_param);
// CAMERA_POSITION cam_position = CAMERA_SIDE_1;//相机位置
// ReadSystemConfig("../data/Side2_Det/System_Config.json", psystem_param);
// CAMERA_POSITION cam_position = CAMERA_SIDE_2;//相机位置
// ReadSystemConfig("../data/Down_Det/System_Config.json", psystem_param);
// CAMERA_POSITION cam_position = CAMERA_DOWN_1;//相机位置
//测旋转
// ReadSystemConfig("../data/Down_Det/System_Config.json", psystem_param);
// CAMERA_POSITION cam_position = CAMERA_DOWN_1;//相机位置
// detect_type = DETECT_TYPE_ANGLE;
std::shared_ptr<ImgCheckBase> pImgCount = ImgCheckBase::GetInstance();
printf("%s %s \n", pImgCount->GetVersion().c_str(), pImgCount->GetErrorInfo().c_str());
cv::Mat img = cv::imread(psystem_param->str_detImgPath);
printf("img path %s \n",psystem_param->str_detImgPath.c_str());
if (img.empty())
{
printf(" det img is empty >> exit \n");
return 1;
/* code */
}
printf("%d %d \n", img.cols, img.rows);
Cam_Param cam_param;
cam_param.AIModel_param_path = psystem_param->str_AIModelJson;
cam_param.check_param_path = psystem_param->str_CheckConfigJson;
cam_param.cam_position = cam_position;
int re = pImgCount->RunStart((void *)&cam_param);
if (re != 0)
{
printf("ImgMeasure Init Fail >>>>>>> \n");
return 1;
}
std::shared_ptr<shareImage> tem = std::make_shared<shareImage>();
tem->img = img;
tem->bdebugSaveImg = bsave;
tem->cam_position = cam_position;
tem->detect_type = detect_type;
std::shared_ptr<CheckResult> result;
re = pImgCount->CheckImg(tem, result);
if (re != 0)
{
}
if (!result->resultImg.empty())
{
cv::imwrite("resultImg.png", result->resultImg);
}
std::string strImgPath = psystem_param->str_ImagesPath;
if (strImgPath != "")
{
std::cout << strImgPath << std::endl;
std::vector<cv::String> img_paths;
bool bgo = true;
try
{
cv::glob(strImgPath, img_paths, true);
}
catch (const std::exception &e)
{
bgo = false;
std::cout << "Invalid or non-existent directory: " << strImgPath << std::endl;
}
if (bgo)
{
SaveImageFile sv;
sv.SetSavePath(psystem_param->str_SaveImagesPath);
for (int i = 0; i < img_paths.size(); i++)
{
std::cout << img_paths[i] << std::endl;
std::string str = img_paths[i];
std::string strName = CheckUtil::splitFilePath(str);
cv::Mat img = cv::imread(str);
//
std::shared_ptr<shareImage> tem = std::make_shared<shareImage>();
tem->img = img;
std::shared_ptr<CheckResult> result;
re = pImgCount->CheckImg(tem, result);
printf("%d %d %s det time %f \n", img.cols, img.rows, strName.c_str(), result->UseTimeMS);
sv.saveImg(img, result->resultImg, strName, result->nresult);
}
}
}
printf("test ImgMeasure Count >>>>>> End \n");
return 0;
}