python调用海康sdk操作热成像设备获取对应点温度
Python调用海康sdk操作热成像设备获取对应点温度。官方提供sdk的c++版的开发文档自己写对应的Python接口和类。下面的代码示例了通过SDK获取热成像画面上某一点的具体温度。
hCNetSDK = CDLL(‘./libhcnetsdk.so’)是海康sdk的目录,可以是相对路径也可以是绝对路径。
./sdklog是海康的SDK日志写入的文件夹,如果无法正常得出结果可以看看下面的日志。
-
实时测温 设备配置好测温规则之后会自动检测配置的点、线或框的温度,实时测温即获取这些规则检测到的温度信息。热成像相关功能配置只支持在热成像通道上实现。热成像重载云台和热成像中载云台设备,有两个通道,第一个通道为可见光通道,第二个通道为热成像通道;热成像单目筒机只有一个通道,为热成像通道。
-
全屏测温(代码目前实现方式) 全屏测温,可以获取设备输出的探测器的每个像素点的温度信息,可通过抓热图或者全屏测温实时上传方式获取。热成像相关功能配置只支持在热成像通道上实现。热成像重载云台和热成像中载云台设备,有两个通道,第一个通道为可见光通道,第二个通道为热成像通道;热成像单目筒机只有一个通道,为热成像通道。
- 代码结构如下
1.dll动态库python函数封装
# coding=utf-8
from ctypes import c_int32, c_char_p, c_void_p, c_float, c_size_t, c_ubyte, c_long, cdll, POINTER, CDLL, c_bool, c_long, c_short
from hk_class import *
import sys
# 回调函数类型定义
hCNetSDK = None;
if 'linux' == sys.platform:
fun_ctype = CFUNCTYPE
hCNetSDK = cdll.LoadLibrary('./lib/libhcnetsdk.so')
else:
hCNetSDK = CDLL('./lib/HCNetSDK.dll')
fun_ctype = WINFUNCTYPE
SERIALNO_LEN = 48 # 序列号长度
NAME_LEN = 32 # 用户名长度
# //boolean NET_DVR_Init();
NET_DVR_Init = hCNetSDK.NET_DVR_Init
NET_DVR_Init.restype = c_bool
NET_DVR_Init.argtypes = ()
# boolean NET_DVR_Cleanup();
NET_DVR_Cleanup = hCNetSDK.NET_DVR_Cleanup
NET_DVR_Cleanup.restype = c_bool
NET_DVR_Cleanup.argtypes = ()
# NativeLong NET_DVR_Login_V30(String sDVRIP, short wDVRPort, String sUserName, String sPassword, NET_DVR_DEVICEINFO_V30 lpDeviceInfo);
NET_DVR_Login_V30 = hCNetSDK.NET_DVR_Login_V30
NET_DVR_Login_V30.restype = c_long
NET_DVR_Login_V30.argtypes = (c_char_p, c_short, c_char_p, c_char_p, POINTER(NET_DVR_DEVICEINFO_V30))
# boolean NET_DVR_Logout_V30(NativeLong lUserID);
NET_DVR_Logout_V30 = hCNetSDK.NET_DVR_Logout_V30
NET_DVR_Logout_V30.restype = c_bool
NET_DVR_Logout_V30.argtypes = (c_long,)
# boolean NET_DVR_SetSTDConfig(NativeLong lUserID, int dwCommand, NET_DVR_STD_CONFIG lpInConfigParam);
NET_DVR_SetSTDConfig = hCNetSDK.NET_DVR_SetSTDConfig
NET_DVR_SetSTDConfig.restype = c_bool
NET_DVR_SetSTDConfig.argtypes = (c_long, c_int32, NET_DVR_STD_CONFIG)
# boolean NET_DVR_GetSTDConfig(NativeLong lUserID, int dwCommand, NET_DVR_STD_CONFIG lpOutConfigParam);
NET_DVR_GetSTDConfig = hCNetSDK.NET_DVR_GetSTDConfig
NET_DVR_GetSTDConfig.restype = c_bool
NET_DVR_GetSTDConfig.argtypes = (c_long, c_int32, NET_DVR_STD_CONFIG)
# boolean NET_DVR_CaptureJPEGPicture_WithAppendData(NativeLong lUserID, int lChannel, NET_DVR_JPEGPICTURE_WITH_APPENDDATA lpJpegWithAppend);
NET_DVR_CaptureJPEGPicture_WithAppendData = hCNetSDK.NET_DVR_CaptureJPEGPicture_WithAppendData
NET_DVR_CaptureJPEGPicture_WithAppendData.restype = c_bool
NET_DVR_CaptureJPEGPicture_WithAppendData.argtypes = (c_long, c_int32, POINTER(NET_DVR_JPEGPICTURE_WITH_APPENDDATA))
# int NET_DVR_GetLastError();
NET_DVR_GetLastError = hCNetSDK.NET_DVR_GetLastError
NET_DVR_GetLastError.restype = c_int32
NET_DVR_GetLastError.argtypes = ()
# 启用日志文件写入接口
# boolean NET_DVR_SetLogToFile(int bLogEnable, String strLogDir, boolean bAutoDel);
NET_DVR_SetLogToFile = hCNetSDK.NET_DVR_SetLogToFile
NET_DVR_SetLogToFile.restype = c_bool
NET_DVR_SetLogToFile.argtypes = (c_int32, c_char_p, c_bool)
# 单帧数据捕获并保存成JPEG存放在指定的内存空间中。
# BOOL NET_DVR_CaptureJPEGPicture_NEW(
# LONG lUserID,
# LONG lChannel,
# LPNET_DVR_JPEGPARA lpJpegPara,
# char *sJpegPicBuffer,
# DWORD dwPicSize,
# LPDWORD lpSizeReturned
# );
NET_DVR_CaptureJPEGPicture_new = hCNetSDK.NET_DVR_CaptureJPEGPicture_NEW
NET_DVR_CaptureJPEGPicture_new.restype = c_bool
NET_DVR_CaptureJPEGPicture_new.argtypes = (c_long, c_long, POINTER(NET_DVR_JPEGPARA), c_char_p, c_ulong, POINTER(c_ulong))
# BOOL NET_DVR_CaptureJPEGPicture_NEW(
# LONG lUserID,
# LONG lChannel,
# LPNET_DVR_JPEGPARA lpJpegPara,
# char *sJpegPicBuffer,
# DWORD dwPicSize,
# LPDWORD lpSizeReturned
# );
NET_DVR_CaptureJPEGPicture = hCNetSDK.NET_DVR_CaptureJPEGPicture
NET_DVR_CaptureJPEGPicture.restype = c_bool
NET_DVR_CaptureJPEGPicture.argtypes = (c_long, c_long, POINTER(NET_DVR_JPEGPARA), c_char_p)
# 码流回调函数
#(DWORD dwType, void* lpBuffer, DWORD dwBufLen, void* pUserData)
GetThermInfoCallback = fun_ctype(c_int32, c_void_p, c_int32, c_void_p)
2.结构体python类封装
# coding=utf-8
from ctypes import *
SERIALNO_LEN = 48 # 序列号长度
NAME_LEN = 32 # 用户名长度py
VCA_MAX_POLYGON_POINT_NUM = 10 #检测区域最多支持10个点的多边形
class NET_DVR_DEVICEINFO_V30(Structure):
_fields_ = [('sSerialNumber', c_ubyte * SERIALNO_LEN), ('byAlarmInPortNum', c_byte), ('byAlarmOutPortNum', c_byte), ('byDiskNum', c_byte),
('byDVRType', c_byte), ('byChanNum', c_byte), ('byStartChan', c_byte), ('byAudioChanNum', c_byte), ('byIPChanNum', c_byte), ('byRes1', c_ubyte * 24)]
class NET_VCA_POINT(Structure):
_fields_ = [('fX', c_float), ('fY', c_float)]
class NET_VCA_POLYGON(Structure):
_fields_ = [('dwPointNum', c_ulong), ('struPos',NET_VCA_POINT * 10)]
class NET_DVR_THERMOMETRY_PRESETINFO_PARAM(Structure):
_fields_ = [('byEnabled', c_byte), ('byRuleID', c_short), ('wDistance', c_short), ('fEmissivity', c_float), ('byDistanceUnit', c_byte), ('byRes', c_ubyte * 2), ('byReflectiveEnabled', c_byte),
('fReflectiveTemperature', c_float), ('szRuleName', c_ubyte * NAME_LEN), ('byRes1', c_ubyte * 63), ('byRuleCalibType', c_byte), ('struPoint', NET_VCA_POINT), ('struRegion', NET_VCA_POLYGON)]
class NET_DVR_THERMOMETRY_PRESETINFO(Structure):
_fields_ = [('dwSize', c_ulong), ('wPresetNo', c_short), ('byRes', c_ubyte * 2),
('struPresetInfo', NET_DVR_THERMOMETRY_PRESETINFO_PARAM * 40)]
class NET_DVR_THERMOMETRY_COND(Structure):
_fields_ = [('dwSize', c_ulong), ('dwChannel', c_ulong),
('wPresetNo', c_short), ('byRes', c_ubyte * 62)]
class BYTE_ARRAY(Structure):
_fields_ = [('byValue', c_byte * 2097152)]
class NET_DVR_STD_CONFIG(Structure):
_fields_ = [('lpCondBuffer', POINTER(NET_DVR_THERMOMETRY_COND)), ('dwCondSize', c_ulong), ('lpInBuffer', POINTER(NET_DVR_THERMOMETRY_PRESETINFO)), ('dwInSize', c_ulong), ('lpOutBuffer', POINTER(NET_DVR_THERMOMETRY_PRESETINFO)), ('dwOutSize', c_ulong),
('lpStatusBuffer', POINTER(BYTE_ARRAY)), ('dwStatusSize', c_ulong), ('lpXmlBuffer', c_void_p), ('dwXmlSize', c_ulong), ('byDataType', c_bool), ('byRes', c_ubyte * 23)]
class NET_VCA_RECT(Structure):
_fields_ = [('fX', c_char),('fY', c_char),('fWidth', c_char),('fHeight', c_char)]
class NET_DVR_JPEGPICTURE_WITH_APPENDDATA(Structure):
_fields_ = [('dwSize', c_int32), ('dwChannel', c_int32), ('dwJpegPicLen', c_int32), ('pJpegPicBuff', POINTER(BYTE_ARRAY)), ('dwJpegPicWidth', c_int32),
('dwJpegPicHeight', c_int32), ('dwP2PDataLen', c_int32), ('pP2PDataBuff', POINTER(BYTE_ARRAY)), ('byIsFreezedata', c_byte), ('byRes', c_byte * 255)]
# JPEG图像信息结构体。
# struct{
# WORD wPicSize;
# WORD wPicQuality;
# }NET_DVR_JPEGPARA,*LPNET_DVR_JPEGPARA;
class NET_DVR_JPEGPARA(Structure):
_fields_ = [('wPicSize', c_ulong),('wPicQuality', c_ulong)]
#点坐标参数结构体。
class NET_VCA_POINT(Structure):
_fields_ =[('fX',c_float),('fY',c_float)]
#点测温实时信息结构体。
class NET_DVR_POINT_THERM_CFG(Structure):
_fields_ = [('fTemperature', c_float),('struPoint', NET_VCA_POINT),('byRes', c_byte * 120)]
#多边形结构体。
class NET_VCA_POLYGON(Structure):
_fields_ = [('dwPointNum',c_uint32), ('struPos', NET_VCA_POINT * VCA_MAX_POLYGON_POINT_NUM)]
#框/线测温实时信息结构体。
class NET_DVR_LINEPOLYGON_THERM_CFG(Structure):
_fields_ = [('fMaxTemperature', c_float),('fMinTemperature', c_float), ('fAverageTemperature', c_float), ('fTemperatureDiff', c_float),('struRegion', NET_VCA_POLYGON), ('byRes', c_ubyte * 32)]
#区域框参数结构体。
class NET_VCA_RECT(Structure):
_fields_ = [('fX', c_float), ('fY', c_float), ('fWidth', c_float), ('fHeight', c_float)]
# 实时温度信息结构体。
class NET_DVR_THERMOMETRY_UPLOAD(Structure):
_fields_ =[('dwSize', c_uint32),('dwRelativeTime', c_uint32),('dwAbsTime', c_uint32),('szRuleName', c_char * NAME_LEN),('byRuleID', c_ubyte),('byRuleCalibType', c_ubyte),
('wPresetNo', c_uint16),('struPointThermCfg', NET_DVR_POINT_THERM_CFG),('struLinePolygonThermCfg', NET_DVR_LINEPOLYGON_THERM_CFG),('byThermometryUnit', c_ubyte),
('byDataType', c_ubyte),('byRes1', c_ubyte),('bySpecialPointThermType', c_ubyte),('fCenterPointTemperature', c_float),('fHighestPointTemperature', c_float),
('fLowestPointTemperature', c_float),('struHighestPoint', NET_VCA_POINT),('struLowestPoint', NET_VCA_POINT),('byIsFreezedata', c_ubyte),('byFaceSnapThermometryEnabled', c_ubyte),
('byRes2[2]', c_ubyte),('dwChan', c_uint32),('struFaceRect', NET_VCA_RECT),('dwTimestamp', c_uint32),('byRes[68]', c_ubyte)]
# DWORD|c_uint32 BYTE|c_ubyte WORD|c_int16
3.sdk自封装
import lib.hk_dll as hk_dll
import lib.hk_class as hk_class
from ctypes import *
import struct
# from numba import njit
# 设备信息
m_strDeviceInfo = None
SERIALNO_LEN = 48 # 序列号长度
NAME_LEN = 32 # 用户名长度py
point_bytes = (c_byte * 4)()
#用户登录信息
m_strDeviceInfo = None
#测温信息
m_strJpegWithAppenData = None
# 初始化
def init():
return hk_dll.NET_DVR_Init()
# 登录
def login(ip, port, username, password):
# 注册
m_strDeviceInfo = hk_class.NET_DVR_DEVICEINFO_V30()
m_strDeviceInfo.sSerialNumber = (c_ubyte * SERIALNO_LEN)()
m_strDeviceInfo.byRes1 = (c_ubyte * 24)()
lUserID = hk_dll.NET_DVR_Login_V30(bytes(ip), port, bytes(username), bytes(password), byref(m_strDeviceInfo))
# 打开SDK写日志的功能
hk_dll.NET_DVR_SetLogToFile(3, b'./sdklog', False)
return lUserID
# 退出登录
def logout(lUserID):
hk_dll.NET_DVR_Logout_V30(lUserID)
# 释放sdk
def cleanup():
hk_dll.NET_DVR_Cleanup()
# 获取抓拍图片最高温度
def get_temperature_all(lUserID):
ret, m_strJpegWithAppenData = get_temperature0(lUserID)
max_temperature = -50
min_temperature = 120
byValue = m_strJpegWithAppenData.pP2PDataBuff.contents.byValue
if ret:
for x in range(m_strJpegWithAppenData.dwJpegPicWidth):
for y in range(m_strJpegWithAppenData.dwJpegPicHeight):
temperature = struct.unpack('<f', struct.pack('4b', *get_bytes(byValue , (m_strJpegWithAppenData.dwJpegPicWidth * y + x) * 4, 4)))[0]
max_temperature = temperature if temperature > max_temperature else max_temperature
min_temperature = temperature if temperature < min_temperature else min_temperature
return True, max_temperature, min_temperature
return False, max_temperature, min_temperature
# 获取给定点列表最高温度
def get_temperature_max(points, sourceWidth, sourceHeight, lUserID):
ret, m_strJpegWithAppenData = get_temperature0(lUserID)
if(len(points) < 2):
return False, -2
if ret:
x1, y1 = point2point(points[0][0], points[0][1], sourceWidth, sourceHeight,
m_strJpegWithAppenData.dwJpegPicWidth, m_strJpegWithAppenData.dwJpegPicHeight)
x2, y2 = point2point(points[1][0], points[1][1], sourceWidth, sourceHeight,
m_strJpegWithAppenData.dwJpegPicWidth, m_strJpegWithAppenData.dwJpegPicHeight)
if x1 > x2 or y1 > y2:
return False, -3
byValue = m_strJpegWithAppenData.pP2PDataBuff.contents.byValue
max_temperature = -50.0
for x in range(x1, x2 + 1):
for y in range(y1, y2 + 1):
# 160 * 120
temperature = struct.unpack('<f', struct.pack('4b', *get_bytes(byValue, (m_strJpegWithAppenData.dwJpegPicWidth * y + x) * 4, 4)))[0]
max_temperature = temperature if temperature > max_temperature else max_temperature
return True, max_temperature
return False, -1
# 获取某点的温度
def get_temperature(x, y, sourceWidth, sourceHeight, lUserID):
ret, m_strJpegWithAppenData = get_temperature0(lUserID)
if ret:
x, y = point2point(x, y, sourceWidth, sourceHeight, m_strJpegWithAppenData.dwJpegPicWidth, m_strJpegWithAppenData.dwJpegPicHeight)
byValue = m_strJpegWithAppenData.pP2PDataBuff.contents.byValue
return True, struct.unpack('<f', struct.pack('4b', *get_bytes(byValue, (m_strJpegWithAppenData.dwJpegPicWidth * y + x) * 4, 4)))[0]
return False, 0.0
# 截取指定下标的和长度的返回数据
def get_bytes(src_bytes, offset, length):
global point_bytes
for i in range(length):
point_bytes[i] = src_bytes[offset + i]
return point_bytes
# 获取温度
def get_temperature0(lUserID):
bRet = False
global m_strJpegWithAppenData
if m_strJpegWithAppenData is None:
m_strJpegWithAppenData = hk_class.NET_DVR_JPEGPICTURE_WITH_APPENDDATA()
m_strJpegWithAppenData.byRes = (c_byte * 255)()
m_strJpegWithAppenData.dwChannel = 1
m_strJpegWithAppenData.pJpegPicBuff = pointer(
hk_class.BYTE_ARRAY((c_byte * 2097152)()))
m_strJpegWithAppenData.pP2PDataBuff = pointer(
hk_class.BYTE_ARRAY((c_byte * 2097152)()))
m_strJpegWithAppenData.dwSize = sizeof(m_strJpegWithAppenData)
bRet = hk_dll.NET_DVR_CaptureJPEGPicture_WithAppendData(lUserID, 2, byref(m_strJpegWithAppenData))
if bRet:
# 测温数据
print(m_strJpegWithAppenData.dwP2PDataLen)
if m_strJpegWithAppenData.dwP2PDataLen > 0:
return True, m_strJpegWithAppenData
return False, None
# 坐标转换
# @njit
def point2point(x, y, sourceWidth, sourceHeight, targetWidth, targetHeight):
x = x * targetWidth / sourceWidth
y = y * targetHeight / sourceHeight
x = x if x <= targetWidth else targetWidth
x = 0 if x < 0 else x
y = y if y <= targetHeight else targetHeight
y = 0 if y < 0 else y
return int(x), int(y)
#抓拍图片
# lUserID 登录用户id
# lChannel 渠道号
# dir 文件保存路径
def captureJPEGPicture(lUserID, lChannel, dir):
jpegpara= hk_class.NET_DVR_JPEGPARA()
jpegpara.wPicSize = 0xff
jpegpara.wPicQuality = 2
p = c_char_p()
s = byref(c_ulong())
# return hk_dll.NET_DVR_CaptureJPEGPicture(lUserID, lChannel, byref(jpegpara), p, 2048, s)
return hk_dll.NET_DVR_CaptureJPEGPicture(lUserID, lChannel, byref(jpegpara), dir)
def get_last_rror():
return hk_dll.NET_DVR_GetLastError()
4.测试调用
import hk_sdk as hk_sdk
import lib.hk_class as hkclass
import time
def test():
result = hk_sdk.init()
if not result:
print('初始化失败')
return False
print('初始化成功')
lUserID = hk_sdk.login(b"192.168.8.16", 8000, b'admin', b'a1234567')
if lUserID != -1:
print('lUserID ', lUserID)
else:
print('登录失败!')
for i in range(10):
result, temperature = hk_sdk.get_temperature_max(temperature_list, 1280, 720, userid)
if result:
print("获取的温度是", temperature, i)
else:
print("获取温度失败 ", temperature)
print("错误码是 ", hk_sdk.get_last_error())
time.sleep(1)
# 退出登录
hk_sdk.logout(lUserID)
# 释放
hk_sdk.cleanup()
if __name__ == "__main__":
test()
好了代码主要就这么多,喜欢的伙伴可以点赞留言加关注哦。如果有错误优先查看错误码,根据SDK文档给出错误码表排查原因。想要源代码的点击下面链接:
点击获取源代码