【FLIR工业相机】三、图像处理:C++调用pytorch模型实时处理相机图像

【FLIR工业相机】三、图像处理:C++调用pytorch模型实时处理相机图像,第1张

系列文章目录


一、环境配置:win10+VS2017+qt5+spinnaker+opencv+python

二、相机调用:VS2017+qt5调用FLIR工业相机实时成像

三、图像处理:C++调用pytorch模型实时处理相机图像

文章目录
  • 系列文章目录
  • 1.main.cpp代码
  • 2.注意事项

1.main.cpp代码
#include "Spinnaker.h"
#include "SpinGenApi/SpinnakerGenApi.h"
#include 
#include 
#include 
#include 
#include 
#include 

using namespace Spinnaker;
using namespace Spinnaker::GenApi;
using namespace Spinnaker::GenICam;
using namespace std;
using namespace cv;

CameraPtr pCam=nullptr;
SystemPtr pSystem;
CameraList camList;

PyObject *unet_module;
PyObject *preFunc = NULL;

void InitCam()
{
	pSystem = System::GetInstance();
	camList = pSystem->GetCameras();
	unsigned int numCameras = camList.GetSize();
	cout << "Number of camers detected" << numCameras << endl;
	pCam = camList.GetByIndex(0);
	pCam->Init();
	pCam->BeginAcquisition();
}

Mat AcqImg()
{
	ImagePtr pResultImage = pCam->GetNextImage();
	const size_t width = pResultImage->GetWidth();
	const size_t height = pResultImage->GetHeight();
	ImagePtr rgbImage = pResultImage->Convert(PixelFormat_BGR8);
	unsigned int rowBytes = (double)rgbImage->GetImageSize() / (double)height;

	Mat image = cv::Mat(height, width, CV_8UC3, rgbImage->GetData(), rowBytes);
	resize(image, image, Size(height*0.25, width*0.25));

	return image;
}

void DeinitCam()
{
	pCam->EndAcquisition();
	pCam->DeInit();
	camList.Clear();
	pSystem->ReleaseInstance();
}

void PythonInit()
{
	Py_Initialize();
	if (!Py_IsInitialized())
	{
		cout << "初始化失败" << endl;
		Py_Finalize();
	}
	PyRun_SimpleString("import sys");
	PyRun_SimpleString("sys.path.append('./')");
	import_array();
	unet_module = PyImport_ImportModule("UNet_model");
	if (!unet_module)
	{
		cout << "模型导入失败" << endl;
		Py_Finalize();
	}
	else
	{
		preFunc = PyObject_GetAttrString(unet_module, "predict");
		if (!preFunc)
		{
			cout << "函数调用失败" << endl;
			Py_Finalize();
		}
	}
}

Mat ProcessImg(Mat src)
{
	PyObject *preResult = NULL;
	PyArrayObject *res_array;
	npy_intp dims[3] = { 512,512,1 };
	PyObject *p_img = PyArray_SimpleNewFromData(3, dims, NPY_UINT8, (void*)src.data);

	PyObject *args = PyTuple_New(1);
	PyTuple_SetItem(args, 0, p_img);

	preResult = PyEval_CallObject(preFunc, args);
	PyArray_OutputConverter(preResult, &res_array);
	npy_intp *shape = PyArray_SHAPE(res_array);
	Mat dst(shape[0], shape[1], CV_8UC1, PyArray_DATA(res_array));

	return dst;
}

int main()
{
	InitCam();
	PythonInit();
	
	bool flag = true;
	Mat imgOrig;
	Mat imgGray;
	Mat imgPro;
	while (1)
	{
		imgOrig = AcqImg();
		imshow("original image", imgOrig);

		
		cvtColor(imgOrig, imgGray, cv::COLOR_BGR2GRAY);
		imgPro = ProcessImg(imgGray);
		imshow("processed image", imgPro);
		
		waitKey(0);
	}

	Py_Finalize();
	DeinitCam();
	return 0;
}
2.注意事项

需要将训练好的模型*.plt* 文件和模型的结构*.py文件放在main.cpp*的同级目录下。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/578247.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-11
下一篇 2022-04-11

发表评论

登录后才能评论

评论列表(0条)

保存