CameraThread::CameraThread(int id, QSemaphore* semaphore, QMutex* resultMutex, bool* hasDefect, CameraWidget* camerawidget, std::atomic<bool>& state_run, std::atomic<bool>& state_collect, QObject* parent) : QThread(parent), id_(id), semaphore_(semaphore), resultMutex_(resultMutex), hasDefect_(hasDefect), camerawidget_(camerawidget), state_run_(state_run), state_collect_(state_collect){}
void CameraThread::run() {
pen.setColor(QColor(0, 0, 0));
font.setPixelSize(32);
DCSP_CORE *yoloDetector = new DCSP_CORE; // 设置图像的保存路径
loadModelParams(); // 加载模型参数
DCSP_INIT_PARAM params{model_path, YOLO_ORIGIN_V8, {640, 640}, currentModelParam.ConfidenceThreshold, currentModelParam.iouThreshold, true};
yoloDetector->CreateSession(params);
while (state_run_) {
cv::Mat image = camerawidget_->convertImage();
QTime afterconvertImageTime = QTime::currentTime();
qDebug() << "camera id : "<<QString(id_)<<"after convertImage : " << afterconvertImageTime; // 模型检测
bool hasdefect = detectDefect(image, yoloDetector,currentModelParam.minArea, detectionArea);
QTime afterdetectDefect = QTime::currentTime(); qDebug() << "after detectDefect : " << afterdetectDefect;
}
CameraThread::detectDefect(cv::Mat& img, DCSP_CORE *&p, float minArea, const cv::Rect& detectionArea)
{
std::vector<DCSP_RESULT> res; // 使用传入的 DCSP_CORE 对象的 RunSession 方法对图像进行检测,并将结果存储在 res 中
p->RunSession(img, res);
..
}
char *DCSP_CORE::RunSession(cv::Mat &iImg, std::vector<DCSP_RESULT> &oResult)
{
#ifdef benchmark clock_t starttime_1 = clock();
#endif // benchmark char *Ret = RET_OK;
cv::Mat processedImg; PostProcess(iImg, imgSize, processedImg);
float *blob = new float [processedImg.total() * 3];
BlobFromImage(processedImg, blob);
std::vector<int64_t> inputNodeDims = {1, 3, imgSize.at(0), imgSize.at(1)};
TensorProcess(starttime_1, iImg, blob, inputNodeDims, oResult);
..
}
char *PostProcess(cv::Mat &iImg, std::vector<int> iImgSize, cv::Mat &oImg)
{
clock_t time_1= clock();
qDebug()<<"time_1: "<<time_1;
cv::Size imageSize = iImg.size(); qDebug() << "Image size (width x height):" << imageSize.width << "x" << imageSize.height;
clock_t time_2= clock();
qDebug()<<"time_2: "<<time_2;
cv::resize(iImg, oImg, cv::Size(iImgSize.at(0), iImgSize.at(1)));
clock_t time_3= clock();
qDebug()<<"time_3: "<<time_3;
return RET_OK;
}
我有三个相同上述的线程(图像、启动时间等都基本一样),但最后的打印信息如下:
camera id : "\u0001" after convertImage : QTime("22:01:50.869")
camera id : "\u0000" after convertImage : QTime("22:01:50.869")
camera id : "\u0002" after convertImage : QTime("22:01:50.869")
time_1 : 47744
time_1 : 47744
time_1 : 47744
Image size (width x height): 1280 x 1024
Image size (width x height): 1280 x 1024
Image size (width x height): 1280 x 1024
time_2 : 47744
time_2 : 47744
time_2 : 47744
time_3 : 47772
[DCSP_ONNX(CUDA)]: 37.00ms pre-process, 23.00ms inference, 10.00ms post-process.
after detectDefect : QTime("22:01:50.942")
time_3 : 47858
time_3 : 47859
[DCSP_ONNX(CUDA)]: 131.00ms pre-process, 47.00ms inference, 15.00ms post-process.
after detectDefect : QTime("22:01:51.063")
[DCSP_ONNX(CUDA)]: 132.00ms pre-process, 50.00ms inference, 14.00ms post-process.
after detectDefect : QTime("22:01:51.066")
描述:我设置了三个上述相同的线程,获取的图像基本相同,线程启动时间、输入参数也完全相同。但pre-process时间却差别很大。经过调试,是函数char *PostProcess()时间出现了差别,可以看到上述打印信息,time_1 和time_2的时间差别不大,但是time_3(也就是在cv::resize之后),出现了差别,第一个time_3 要比后面两个time_3 快 100多ms,但是后面两个time_3 却相差差不多。 (目的:想缩短 pre-process的时间,使三个线程的时间基本和第一个 37ms一样了)