Python之调用MAG14热成像仪C++接口SDK

机器人上使用某型号的热成像仪,厂家提供了C++接口的SDK,而我们的主程序是python,因此参考《Python调用C++程序的几种方法》,利用boost的方式实现调用(该博文不具有通用性,目的是记录过程)

在TX2上完成热成像仪的采集工作,因此首先给厂家提供TX2的交叉编译工具《NVIDIA TX2交叉编译工具》,如图所示,第一个表示交叉编译工具,第二个表示厂家返回的SDK

解压后, 在sample-console文件夹中,新建以下两个文件

main.cpp

/*
* 版权声明: 暂无
* 文件名称 : main.cpp
* 创建者 : ZONGXP
* 创建日期: 2018/07/20
* 文件描述: 本文件主要存放热成像仪的图像保存函数saveImage()
* 历史记录: 无
*/

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;

#include "../base/MagDevice.h"
#include "../base/MagService.h"
#include <unistd.h>
#include <iostream>
#include <errno.h>
#include <time.h>
#include <typeinfo>
#include <Python.h>

using namespace std;
CMagService service;
CMagDevice device;

void NewFrame(UINT intChannelIndex, int intCameraTemperature, DWORD dwFFCCounterdown,
              DWORD dwCamState, DWORD dwStreamType, ULONG dwUser)
{
    //cout << "[MSG] A new frame is coming" << endl;

    CMagDevice* pDev = (CMagDevice*)dwUser;

    if (dwStreamType == STREAM_TEMPERATURE)
    {
        const UCHAR* pData = NULL;
        const UCHAR* pClrBar = NULL;
        const BITMAPINFO* pInfo = NULL;
        if (!pDev->GetOutputBMPdata(&pData, &pInfo))
        {
            cout << "[ERR] 获取图像失败." << endl;
            return;
        }

        if (!pDev->GetOutputColorBardata(&pClrBar, &pInfo))
        {
            cout << "获取颜色条数据失败." << endl;
            return;
        }
    }
    else if (dwStreamType == STREAM_HYBRID || dwStreamType == STREAM_VIDEO)
    {
        //const UCHAR* pYV12 = pDev->GetOutputVideoYV12();
    }
}

int initInfrared(string host)
{

    //CMagService service;
    if ( !service.IsInitialized() )
    {
        cout << "[ERR] 相机服务初始化失败." << endl;
        return 0;
    }
    service.EnableAutoReConnect(TRUE);//开启断线重连

    //CMagDevice device;
    if (!device.IsInitialized())
    {
        cout << "[ERR] 相机设备初始化失败." << endl;
        return 0;
    }
    const char* ip = host.c_str();
    if (!device.LinkCamera(ip))
    //if (!device.LinkCamera("192.168.171.67"))
    //if (!device.LinkCamera(argv[1]))
    {
        cout << errno << endl;
        cout << "[ERR] 连接相机失败."  << endl;
        return 0;
    }

    const struct_CamInfo* pCamInfo = device.GetCamInfo();
    if (!pCamInfo)
    {
        cout << "[ERR] 获取相机参数失败." << endl;
        return 0;
    }

    OutputPara paraOut;
    paraOut.dwFPAWidth = pCamInfo->intFPAWidth;
    paraOut.dwFPAHeight = pCamInfo->intFPAHeight;
    paraOut.dwBMPWidth = pCamInfo->intVideoWidth;
    paraOut.dwBMPHeight = pCamInfo->intVideoHeight;
    paraOut.dwColorBarWidth = 16;
    paraOut.dwColorBarHeight = pCamInfo->intVideoHeight;

    device.SetColorPalette(IronBow);//设置图像颜色样式
    if(!device.StartProcessImage(&paraOut, NewFrame, STREAM_TEMPERATURE, (ULONG)&device))
    {
        cout << "[ERR] 传输数据失败." << endl;
        return 0;
    }
    return 1;
}
int savePicture(string name)
{
    const char* charname = name.c_str();
    device.SaveBMP(1,charname);//退出程序之前保存一幅图像
    //device.SaveBMP(1,NULL);

    //输出温度信息
    int array[50]= {};
    //UINT channel = 1;
    UINT x0 = 0;
    UINT y0 = 0;
    UINT x1 = 159;
    UINT y1 = 119;
    if(device.GetRectTemperatureInfo(x0,y0,x1,y1,array))
    {
        //cout << "the lowest tem is :" << array[0] << endl;
        //cout << "the highest tem is :" << array[1] << endl;
        //cout << "the average tem is :" << array[2] << endl;
    }
    else
    {
        cout << "obtain temp failed " << endl;
    }
    return array[1];

}

BOOST_PYTHON_MODULE(infrared)
{
    def("initInfrared", initInfrared);
    def("savePicture", savePicture);
}

Makefile

######################################################################
# Build options
CPP = g++
INCFLAGS = -I/usr/include/python3.5 -I/usr/local/include
CPPFLAGS = -O2 -Wall -fPIC 
LINKFLAGS = -L/usr/local/lib -L../base/linux -lthermogroupsdk -Wl,-rpath,../base/linux -lboost_python3

######################################################################
# Target
DIR = ./
OBJS = $(DIR)/main.cpp.o $(DIR)/MagDevice.cpp.o $(DIR)/MagService.cpp.o
TARGET = $(DIR)/infrared.so

######################################################################
# Build
all : $(DIR) $(BASE) $(TARGET)

$(DIR):
	mkdir $(DIR)
	
$(TARGET) : $(OBJS)
	$(CPP)  -shared -o $@ $^  $(LINKFLAGS) 
	
$(DIR)/main.cpp.o : main.cpp ../base/MagDevice.h ../base/MagService.h
	$(CPP) $(CPPFLAGS) -c -o $@ $< -I/usr/include/python3.5 -I/usr/local/include
	
$(DIR)/MagDevice.cpp.o : ../base/MagDevice.cpp ../base/MagDevice.h ../base/linux/ThermoGroupSDK.h
	$(CPP) $(CPPFLAGS) -c -o $@ $< -I/usr/include/python3.5 -I/usr/local/include
	
$(DIR)/MagService.cpp.o : ../base/MagService.cpp ../base/MagService.h ../base/linux/ThermoGroupSDK.h
	$(CPP) $(CPPFLAGS) -c -o $@ $< -I/usr/include/python3.5 -I/usr/local/include
	
.PHONEY:clean

clean:
	rm -rf infrared.so
	rm -rf *.o

修改boost对应的python版本,按如下方式建立软链接即可

# 搜索 libpython3.5m.so
sudo find / -name "libpython3.5m.so"
#find: ‘/run/user/1001/gvfs’: Permission denied
#/usr/lib/aarch64-linux-gnu/libpython3.5m.so
#/usr/lib/python3.5/config-3.5m-aarch64-linux-gnu/libpython3.5m.so
# cd 到上面搜索到的目录
cd /usr/lib/aarch64-linux-gnu
sudo ln -s libboost_python-py35.so libboost_python3.so

执行make后,会在文件夹中生成infrared.so文件,运行python即可import infrared,调用里边的两个函数。

上述makefile是生成python3版本的可执行文件,如果要在python2环境中使用,首先将全部的python3.5替换为python2.7,然后-lboost_python3替换为-lboost_python,再make后即可通过python2调用

相关推荐
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页