有段时间没搞basler的相机了,最近突然有个项目需要用到工业相机,在使用时遇到了一些问题做一个记录,隐约觉得去年应该是也遇到这个问题了,没做记录,服务器上也没法用gui界面,basler提供的工具也无法使用,windows下相机是正常使用的,这周卡了两天,问了宝视纳的技术人员,也是没有解决,最后在basler的官网文档和GPT的帮助下,汇总了各类信息并做了大量尝试,总算是解决了,特此记录。
一、basler相机设置
相机的调试和设置如果有条件,最好在windows下面用basler提供的pylon Viewer和ipconfigurator配置和设置相机,避免ip不在同一个网段导致无法找到相机的问题。
(1)连接相机,使用网线可以直接连到电脑,也可以连接到交换机,在bin中打开ipconfigurator.exe
当状态显示为确定时,就可以了,IP地址和子网掩码都要设置成和宿主机或者交换机一样的网段,否则后续可以ping通网络,但是还是会找不到相机
二、opencv编译
opencv的编译上一篇文章已经详细写了,直接跳过去看就行了,linux下的opencv编译主要主要编译进去cuda和ffmpeg读取video,跳转地址:ubuntu下使用cmake编译opencv4.8.0+ffmpeg4.2.2+cuda11.1
我这里是在NX上和工程机上都测试过,本篇主要在工程机docker容器下使用,问题也主要集中在docker内使用
三、docker容器创建
docker容器的创建坑也比较多,主要的创建方式有一篇博客也详细的记录了过程,地址:linux下docker容器的使用
主要的问题如下:
3.1、docker创建前,在root权限下操作,防止后期权限不够导致出现问题
cpp
sudo su
3.2、使用--network=host将去除网络隔离
这个也是很重要的一个步骤,正常情况下,在windows和linux宿主机下都可以正常使用,而进去docker后就一直提示找不到GiE相机设备:
cpp
throw RUNTIME_EXCEPTION("No GigE cameras available.");
结合3.1的命令,提供一个创建docker容器的命令:
cpp
docker run --name pylon7.3 --network host -dit -w /cytech_ai/siping -v /cytech_ai:/cytech_ai -v /tmp/.X11-unix:/tmp/.X11-unix -e DISPLAY=unix$DISPLAY --env LANG=C.UTF-8 --gpus all --privileged baslerenv:1.0.1 /bin/bash
--name:pylon7.3:创建的容器名称
--network:host:去除网络隔离
-w:进入docker容器后自动进入的目录
-v:宿主机和docker的目录容器映射,方便文件操作
baslerenv:1.0.1:镜像名
四、docker下配置basler环境和测试相机
从官网下载对应版本的sdk,在bin中执行pylon-setup-env.sh ,我的路径是baslercode/thridparty/pylon7.3:
cpp
source baslercode/thridparty/pylon7.3/bin/pylon-setup-env.sh baslercode/thridparty/pylon7.3
进入示例文件测试docker容器内是否可以找到相机:
cpp
cd baslercode/thridparty/pylon7.3/share/pylon/Samples/C++/Grab/Grab
使用cmake make一下:
cpp
make
make后生成一个Grab可执行文件,执行该文件:
cpp
./Grab
执行后正常打印,表示相机在docker内正常连接,可以结合opencv做二次开发了。
如果执行后为如下打印提示:
表示没找到相机,排查:
(1)需要检测相机IP是否和服务器IP在统一网段,子网掩码也必须一样才行。
(2)检查下载的basler sdk版本是否成功执行了安装,环境变量是否添加成功了。
(3)重新连接相机,使用sdk内提供的工具在windows上打开相机查看,相机能否正常打开。
(4)先在宿主机下使用上述步骤,不在docker内使用,查看是否相机可以正常获取图像,如果可以,问题还是集中在docker的创建和环境变量的设置上。
(5)优化网络带宽,在windows上使用pylon Viewer菜单栏工具中选择优化网络带宽
五、docker下使用cmakelists添加basler相机
提供一个可参考的cmakelists,txt文件:
cpp
# cmake_minimum_required(VERSION <specify CMake version here>)
cmake_minimum_required(VERSION 3.10)
# 声明一个 cmake 工程
set(PROJECT_NAME baslergrabsdk)
project(${PROJECT_NAME})
set(CMAKE_CXX_STANDARD 11)
# set(CMAKE_BUILD_TYPE Debug)#调试使用
set(CMAKE_BUILD_TYPE Release)
if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
message(STATUS "!!!!! BUILD ${CMAKE_BUILD_TYPE} !!!!!")
#set(ALG_VERSIONS "NX")
#set(CMAKE_C_COMPILER "arm64")
set(ALG_VERSIONS "linuxserver")
set(CMAKE_C_COMPILER "x86")
set(CAMERA_TYPE "basler")
message(STATUS "ALG_VERSIONS info:",${ALG_VERSIONS})
if(${ALG_VERSIONS} STREQUAL "linuxserver")
#opencv
set(OpenCV_INCLUDE_DIRS "/cytech_ai/siping/baslercode/thridparty/opencv480gpu/include")
set(OpenCV_INCLUDE_SEC "/cytech_ai/siping/baslercode/thridparty/opencv480gpu/include/opencv4")
set(OpenCV_LDFLAGS "/cytech_ai/siping/baslercode/thridparty/opencv480gpu/lib")
if(${CAMERA_TYPE} STREQUAL "HK")
#海康sdk
set(HK_INCLUDE_DIRS "/data/siping/baslercode/thridparty/HCNetSDKV6.1.9.45__ArmLinux64_ZH/incCn")
set(HK_LDFLAGS "/data/siping/baslercode/thridparty/HCNetSDKV6.1.9.45__ArmLinux64_ZH/MakeAll")
set(HK_LDFLAGSCOM "/data/siping/baslercode/thridparty/HCNetSDKV6.1.9.45__ArmLinux64_ZH/MakeAll/HCNetSDKCom")
elseif(${CAMERA_TYPE} STREQUAL "basler")
set(BASLER_INCLUDE_DIRS "/cytech_ai/siping/baslercode/thridparty/pylon7.3/include")
set(BASLER_LDFLAGS "/cytech_ai/siping/baslercode/thridparty/pylon7.3/lib")
else()
message(STATUS "CAMERA_TYPE info:",${CAMERA_TYPE},"no version info ,try again!")
endif()
#glog日志库
set(GLOG_INCLUDE "/cytech_ai/siping/baslercode/thridparty/glog/include/")
set(GLOG_LIB "/cytech_ai/siping/baslercode/thridparty/glog/lib")
elseif(${ALG_VERSIONS} STREQUAL "NX")
#opencv
set(OpenCV_INCLUDE_DIRS "/cytech_ai/sipingtest/baslerpro/thridparty/opencv480gpu/include")
set(OpenCV_INCLUDE_SEC "/cytech_ai/sipingtest/baslerpro/thridparty/opencv480gpu/include/opencv4")
set(OpenCV_LDFLAGS "/cytech_ai/sipingtest/baslerpro/thridparty/opencv480gpu/lib")
if(${CAMERA_TYPE} STREQUAL "HK")
#海康sdk
set(HK_INCLUDE_DIRS "/cytech_ai/sipingtest/buildfinish/HCNetSDKV6.1.9.45__ArmLinux64_ZH/incCn")
set(HK_LDFLAGS "/cytech_ai/sipingtest/buildfinish/HCNetSDKV6.1.9.45__ArmLinux64_ZH/MakeAll")
set(HK_LDFLAGSCOM "/cytech_ai/sipingtest/buildfinish/HCNetSDKV6.1.9.45__ArmLinux64_ZH/MakeAll/HCNetSDKCom")
elseif(${CAMERA_TYPE} STREQUAL "basler")
set(BASLER_INCLUDE_DIRS "/cytech_ai/sipingtest/baslerpro/thridparty/pylon7.3/include")
set(BASLER_LDFLAGS "/cytech_ai/sipingtest/baslerpro/thridparty/pylon7.3/lib")
else()
message(STATUS "CAMERA_TYPE info:",${CAMERA_TYPE},"no version info ,try again!")
endif()
#glog日志库
set(GLOG_INCLUDE "/cytech_ai/sipingtest/buildfinish/glog/include/")
set(GLOG_LIB "/cytech_ai/sipingtest/buildfinish/glog/lib")
elseif(${ALG_VERSIONS} STREQUAL "RK3588")
#瑞芯微
set(OpenCV_DIR "/home/siping/thirdparty/opencv480/")
set(OpenCV_INCLUDE_DIRS "/home/siping/thirdparty/opencv480/include/opencv4")
set(OpenCV_LDFLAGS "/home/siping/thirdparty/opencv480/lib")
#海康sdk
set(HK_INCLUDE_DIRS "/home/siping/thirdparty/HCNetSDK_ArmLinux64_ZH/incCn")
set(HK_LDFLAGS "/home/siping/thirdparty/HCNetSDK_ArmLinux64_ZH/MakeAll")
set(HK_LDFLAGSCOM "/home/siping/thirdparty/HCNetSDK_ArmLinux64_ZH/MakeAll/HCNetSDKCom")
#瑞芯微 glog日志库
set(GLOG_INCLUDE "/home/siping/thirdparty/glog_arm64/include/")
set(GLOG_LIB "/home/siping/thirdparty/glog_arm64/lib")
else()
message(STATUS "ALG_VERSIONS info:",${ALG_VERSIONS},"no version info ,try again!")
endif()
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(${OpenCV_INCLUDE_SEC})
link_directories(${OpenCV_LDFLAGS})
message(STATUS "OpenCV library status:")
message(STATUS " path: ${OpenCV_INCLUDE_DIRS}")
message(STATUS " include path: ${OpenCV_INCLUDE_SEC}")
message(STATUS " libraries: ${OpenCV_LDFLAGS}")
if(${CAMERA_TYPE} STREQUAL "HK")
include_directories(${HK_INCLUDE_DIRS})
link_directories(${HK_LDFLAGS})
link_directories(${HK_LDFLAGSCOM})
message(STATUS "HKSDK library status:")
message(STATUS " include path: ${HK_INCLUDE_DIRS}")
message(STATUS " libraries: ${HK_LDFLAGS}")
message(STATUS " libraries: ${HK_LDFLAGSCOM}")
elseif(${CAMERA_TYPE} STREQUAL "basler")
include_directories(${BASLER_INCLUDE_DIRS})
link_directories(${BASLER_LDFLAGS})
message(STATUS "BASLER library status:")
message(STATUS " include path: ${BASLER_INCLUDE_DIRS}")
message(STATUS " libraries: ${BASLER_LDFLAGS}")
endif()
include_directories(${GLOG_INCLUDE})
link_directories(${GLOG_LIB})
message(STATUS "GLOG library status:")
message(STATUS " include path: ${GLOG_INCLUDE}")
message(STATUS " libraries: ${GLOG_LIB}")
#添加自己写的其他函数文件,注意这个名字不能和工程名冲突
aux_source_directory(include DIR_HEADER)
include_directories(${DIR_HEADER}) #添加头文件
aux_source_directory(src DIR_CPP)
#-----------------------------------------------------------------------------------
##set install path
set(CMAKE_BUILD_RPATH "${OpenCV_LDFLAGS}")
if(${ALG_VERSIONS} STREQUAL "NX")
set(CMAKE_INSTALL_PREFIX /cytech_ai/sipingtest/baslerpro/Basleralglib)
elseif(${ALG_VERSIONS} STREQUAL "linuxserver")
set(CMAKE_INSTALL_PREFIX /cytech_ai/siping/baslercode/Basleralglib)
endif()
message(STATUS "CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}")
## set runtime path
set(CMAKE_INSTALL_RPATH ".")
# 如果想生成动态库,SHARE .so
add_library(${PROJECT_NAME} SHARED ${DIR_CPP} ${DIR_HEADER})
set(${PROJECT_NAME} PROPERTIES OUTPUT_NAME ${PROJECT_NAME})
#add_executable(${PROJECT_NAME} ${DIR_CPP} main.cpp )
#链接库
target_link_libraries(${PROJECT_NAME}
${OpenCV_LDFLAGS}
-lopencv_world
-lopencv_img_hash
${GLOG_LIB}
-lglog
${BASLER_LDFLAGS}
-lFirmwareUpdate_gcc_v3_1_Basler_pylon
-lGCBase_gcc_v3_1_Basler_pylon
-lGenApi_gcc_v3_1_Basler_pylon
-lgxapi
-lpylonutility
-lpylonbase
-lXmlParser_gcc_v3_1_Basler_pylon
-lpylonc
-lpylon_TL_gige
)
install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX})
file(GLOB GLOG_LIB "${GLOG_LIB}/lib*.so*")
file(GLOB OpenCV_LDFLAGS "${OpenCV_LDFLAGS}/lib*.so*")
file(GLOB BASLER_LDFLAGS "${BASLER_LDFLAGS}/*.so*")
install(PROGRAMS
${OpenCV_LDFLAGS}
${BASLER_LDFLAGS}
${GLOG_LIB}
DESTINATION ${CMAKE_INSTALL_PREFIX})
其余的测试代码可以参考代码:机器视觉之Basler工业相机使用和配置方法(C++)