相关文章推荐
玩滑板的柚子  ·  【LuatOS-esp32】4.1 ...·  4 天前    · 
机灵的楼梯  ·  玩转CompletableFuture异步编 ...·  10 小时前    · 
完美的充值卡  ·  A Basic AWS Lambda ...·  3 月前    · 
踏实的韭菜  ·  深入解析 Cron ...·  1 年前    · 

在windous系统下Python实现海康相机登入、预览、抓图、光学变倍、相机激活、区域聚焦、区域曝光功能;linux系统下载相应的海康SDK,并将lib文件更换为相对应的库文件,同时将HCNetSDKCom文件夹拷贝出来(与lib文件夹同一级别)

python实现的功能如下:

import numpy as np
import os
import ctypes
#获取所有的库文件到一个列表
path = "lib/win64/"
def file_name(file_dir):
    pathss=[]
    for root, dirs, files in os.walk(file_dir):
        for file in files:
          pathss.append(path+file)
    return pathss
dll_list=file_name(path)
lUserID = 0
lChannel=1
def callCpp(func_name,*args):
    for HK_dll in dll_list:
            lib = ctypes.cdll.LoadLibrary(HK_dll)
                value = eval("lib.%s"%func_name)(*args)
                # print("调用的库:"+HK_dll)
                # print("执行成功,返回值:"+str(value))
                return value
            except:
                continue
        except:
            # print("库文件载入失败:"+HK_dll)
            continue
    # print("没有找到接口!")
    return False
# region 登入
#定义登入结构体
class LPNET_DVR_DEVICEINFO_V30(ctypes.Structure):
    _fields_ = [
        ("sSerialNumber", ctypes.c_byte * 48),
        ("byAlarmInPortNum", ctypes.c_byte),
        ("byAlarmOutPortNum", ctypes.c_byte),
        ("byDiskNum", ctypes.c_byte),
        ("byDVRType", ctypes.c_byte),
        ("byChanNum", ctypes.c_byte),
        ("byStartChan", ctypes.c_byte),
        ("byAudioChanNum", ctypes.c_byte),
        ("byIPChanNum", ctypes.c_byte),
        ("byZeroChanNum", ctypes.c_byte),
        ("byMainProto", ctypes.c_byte),
        ("bySubProto", ctypes.c_byte),
        ("bySupport", ctypes.c_byte),
        ("bySupport1", ctypes.c_byte),
        ("bySupport2", ctypes.c_byte),
        ("wDevType", ctypes.c_uint16),
        ("bySupport3", ctypes.c_byte),
        ("byMultiStreamProto", ctypes.c_byte),
        ("byStartDChan", ctypes.c_byte),
        ("byStartDTalkChan", ctypes.c_byte),
        ("byHighDChanNum", ctypes.c_byte),
        ("bySupport4", ctypes.c_byte),
        ("byLanguageType", ctypes.c_byte),
        ("byVoiceInChanNum", ctypes.c_byte),
        ("byStartVoiceInChanNo", ctypes.c_byte),
        ("byRes3", ctypes.c_byte * 2),
        ("byMirrorChanNum", ctypes.c_byte),
        ("wStartMirrorChanNo", ctypes.c_uint16),
        ("byRes2", ctypes.c_byte * 2)]
#用户注册设备 并登入,需要修改IP,账号、密码
def NET_DVR_Login_V30(sDVRIP = "192.168.1.65",wDVRPort = 8000,sUserName = "admin",sPassword = "guoji123"):
    init_res = callCpp("NET_DVR_Init")#SDK初始化
    if init_res:
        print("SDK初始化成功")
        error_info = callCpp("NET_DVR_GetLastError")
    else:
        error_info = callCpp("NET_DVR_GetLastError")
        print("SDK初始化错误:" + str(error_info))
        return False
    set_overtime = callCpp("NET_DVR_SetConnectTime",5000,4)#设置超时
    if set_overtime:
        print("设置超时时间成功")
    else:
        error_info = callCpp("NET_DVR_GetLastError")
        print("设置超时错误信息:" + str(error_info))
        return False
    #用户注册设备
    #c++传递进去的是byte型数据,需要转成byte型传进去,否则会乱码
    sDVRIP = bytes(sDVRIP,"ascii")
    sUserName = bytes(sUserName,"ascii")
    sPassword = bytes(sPassword,"ascii")
    print( "数据转化成功")
    DeviceInfo = LPNET_DVR_DEVICEINFO_V30()
    print(DeviceInfo)
    lUserID = callCpp("NET_DVR_Login_V30",sDVRIP,wDVRPort,sUserName,sPassword,ctypes.byref(DeviceInfo))
    print("登录成功,用户ID:"+str(lUserID))
    if lUserID == -1:
        error_info = callCpp("NET_DVR_GetLastError")
        print("登录错误信息:" + str(error_info))
        return error_info
    else:
        return lUserID
# endregion
# region 预览
#定义预览结构体
class NET_DVR_PREVIEWINFO(ctypes.Structure):
    _fields_ = [
        ("lChannel", ctypes.c_long),
        ("lLinkMode", ctypes.c_long),
        ("hPlayWnd", ctypes.c_void_p),
        ("sMultiCastIP", ctypes.c_char_p),
        ("byProtoType", ctypes.c_byte),
        ("byRes", ctypes.c_byte * 3)]
# 预览实现
def Preview():
    lpPreviewInfo=NET_DVR_PREVIEWINFO()
    # hPlayWnd需要输入创建图形窗口的handle,没有输入无法实现BMP抓图
    lpPreviewInfo.hPlayWnd=None
    lpPreviewInfo.lChannel=1
    lpPreviewInfo.dwLinkMode=0
    lpPreviewInfo.sMultiCastIP=None
    m_lRealHandle=callCpp("NET_DVR_RealPlay_V30",lUserID,ctypes.byref(lpPreviewInfo),None,None,True)
    if(m_lRealHandle<0):
        error_info = callCpp("NET_DVR_GetLastError")
        print("预览失败:" + str(error_info))
    else:
        print("预览成功")
    return m_lRealHandle
# endregion
# # region 抓图
# # BMP抓图预览的时候hPlayWnd显示窗口不能为none
# def Get_BMPPicture():
#     sBmpPicFileName = bytes("pytest.bmp", "ascii")
#     if(callCpp("NET_DVR_CapturePicture",m_lRealHandle,sBmpPicFileName)==False):
#         error_info = callCpp("NET_DVR_GetLastError")
#         print("抓图失败:" + str(error_info))
#     else:
#         print("抓图成功")
# 抓图数据结构体
class NET_DVR_JPEGPARA(ctypes.Structure):
    _fields_ = [
        ("wPicSize", ctypes.c_ushort),
        ("wPicQuality", ctypes.c_ushort)]
# jpeg抓图hPlayWnd显示窗口能为none,存在缺点采集图片速度慢
def Get_JPEGpicture():
    sJpegPicFileName = bytes("pytest.jpg", "ascii")
    lpJpegPara=NET_DVR_JPEGPARA()
    lpJpegPara.wPicSize=0
    lpJpegPara.wPicQuality=0
    if (callCpp("NET_DVR_CaptureJPEGPicture", lUserID, lChannel, ctypes.byref(lpJpegPara), sJpegPicFileName)== False):
        error_info = callCpp("NET_DVR_GetLastError")
        print("抓图失败:" + str(error_info))
    else:
        print("抓图成功")
# endregion
#定义光学变倍结构体
# 光学变倍结构体
class NET_DVR_FOCUSMODE_CFG(ctypes.Structure):
    _fields_ = [
        ("dwSize", ctypes.c_uint32),
        ("byFocusMode", ctypes.c_byte),
        ("byAutoFocusMode", ctypes.c_byte),
        ("wMinFocusDistance", ctypes.c_uint16),
        ("byZoomSpeedLevel", ctypes.c_byte),
        ("byFocusSpeedLevel", ctypes.c_byte),
        ("byOpticalZoom", ctypes.c_byte),
        ("byDigtitalZoom", ctypes.c_byte),
        ("fOpticalZoomLevel", ctypes.c_float),
        ("dwFocusPos", ctypes.c_uint32),
        ("byFocusDefinitionDisplay", ctypes.c_byte),
        ("byFocusSensitivity", ctypes.c_byte),
        ("byRes1", ctypes.c_byte*2),
        ("dwRelativeFocusPos", ctypes.c_uint32),
        ("byRes", ctypes.c_byte * 48)]
# 获取光学变倍值
def get_CamZoom():
    m_struFocusModeCfg = NET_DVR_FOCUSMODE_CFG()
    m_struFocusModeCfg.byRes
    dwReturned = ctypes.c_uint16(0)
    print(callCpp("NET_DVR_GetDVRConfig"))
    if (callCpp("NET_DVR_GetDVRConfig", lUserID, 3305, lChannel, ctypes.byref(m_struFocusModeCfg), 76,
                ctypes.byref(dwReturned)) == False):
        error_info = callCpp("NET_DVR_GetLastError")
        print("光学变倍获取失败:" + str(error_info))
        ctypes.ARRAY()
    else:
        print("光学变倍获取成功")
    return m_struFocusModeCfg.fOpticalZoomLevel
# 修改光学变倍值
def Change_CamZoom(zoomScale):
    m_struFocusModeCfg=NET_DVR_FOCUSMODE_CFG()
    dwReturned=ctypes.c_uint16(0)
    print(callCpp("NET_DVR_GetDVRConfig"))
    if (callCpp("NET_DVR_GetDVRConfig", lUserID, 3305, lChannel,ctypes.byref(m_struFocusModeCfg),76, ctypes.byref(dwReturned))==False):
        error_info = callCpp("NET_DVR_GetLastError")
        print("光学变倍获取失败:" + str(error_info))
    else:
        print("光学变倍获取成功")
        print("当前光学变倍值:"+str(m_struFocusModeCfg.fOpticalZoomLevel))
        m_struFocusModeCfg.fOpticalZoomLevel=zoomScale
        if (callCpp("NET_DVR_SetDVRConfig", lUserID, 3306, lChannel, ctypes.byref(m_struFocusModeCfg),76)==False):
            error_info = callCpp("NET_DVR_GetLastError")
            print("光学变倍修改失败:" + str(error_info))
        else:
            print("光学变倍修改成功;修改后的数据为:"+str(m_struFocusModeCfg.fOpticalZoomLevel))
class NET_DVR_XML_CONFIG_INPUT(ctypes.Structure):
    _fields_ = [
        ("dwSize", ctypes.c_uint32),
        ("lpRequestUrl", ctypes.c_void_p),
        ("dwRequestUrlLen", ctypes.c_uint32),
        ("lpInBuffer", ctypes.c_void_p),
        ("dwInBufferSize", ctypes.c_uint32),
        ("dwRecvTimeOut", ctypes.c_uint32),
        ("byForceEncrpt", ctypes.c_byte),
        ("byRes", ctypes.c_byte*31),]
# 透传接口输出参数结构体
class NET_DVR_XML_CONFIG_OUTPUT(ctypes.Structure):
    _fields_ = [
        ("dwSize", ctypes.c_uint32),
        ("lpOutBuffer", ctypes.c_void_p),
        ("dwOutBufferSize", ctypes.c_uint32),
        ("dwReturnedXMLSize", ctypes.c_uint32),
        ("lpStatusBuffer", ctypes.c_void_p),
        ("dwStatusSize", ctypes.c_uint32),
        ("byRes", ctypes.c_byte*31)]
#区域局部聚焦和局部曝光功能:矩形区域坐标左上角和右下角(startX,startY,endX,endY)
# flag=1局部聚焦功能,flag!=1局部曝光功能
def RegionalCorrection(startX,startY,endX,endY,flag=1):
    # #定义传输内容
    if(flag==1):
        choise="regionalFocus"
    else:
        choise = "regionalExposure"
    inUrl = "PUT /ISAPI/Image/channels/1/" + choise
    inPutBuffer = "<" + choise + "><StartPoint><positionX>" + str(startX) + "</positionX><positionY>" + str(startY) + "</positionY></StartPoint><EndPoint><positionX>" + str(endX) + "</positionX><positionY>" + str(endY) + "</positionY></EndPoint></" + choise + ">"
    szUrl = (ctypes.c_char * 256)()
    struInput = NET_DVR_XML_CONFIG_INPUT()
    struOuput = NET_DVR_XML_CONFIG_OUTPUT()
    struInput.dwSize=ctypes.sizeof(struInput)
    struOuput.dwSize=ctypes.sizeof(struOuput)
    dwBufferLen = 1024 * 1024
    pBuffer = (ctypes.c_char * dwBufferLen)()
#_____________________________________________put________________________________________________________
    csCommand = bytes(inUrl, "ascii")
    ctypes.memmove(szUrl, csCommand, len(csCommand))
    struInput.lpRequestUrl = ctypes.cast(szUrl,ctypes.c_void_p)
    struInput.dwRequestUrlLen = len(szUrl)
    m_csInputParam= bytes(inPutBuffer, "ascii")
    dwInBufferLen = 1024 * 1024
    pInBuffer=(ctypes.c_byte * dwInBufferLen)()
    ctypes.memmove(pInBuffer, m_csInputParam, len(m_csInputParam))
    struInput.lpInBuffer = ctypes.cast(pInBuffer,ctypes.c_void_p)
    struInput.dwInBufferSize = len(m_csInputParam)
    struOuput.lpStatusBuffer = ctypes.cast(pBuffer,ctypes.c_void_p)
    struOuput.dwStatusSize = dwBufferLen
    if (callCpp("NET_DVR_STDXMLConfig", lUserID, ctypes.byref(struInput), ctypes.byref(struOuput))):
        error_info = callCpp("NET_DVR_GetLastError")
        print("上传成功:" + str(error_info))
    else:
        error_info = callCpp("NET_DVR_GetLastError")
        print("上传失败:错误号为"+ str(error_info))
#海康相机激活参数结构体
class NET_DVR_ACTIVATECFG(ctypes.Structure):
    _fields_ = [
        ("dwSize", ctypes.c_uint32),
        ("sPassword",ctypes.c_byte*16),
        ("byRes",  ctypes.c_byte*108)]
#海康相机激活
def OnActivateDevice():
    init_res = callCpp("NET_DVR_Init")#SDK初始化
    if init_res:
        print("SDK初始化成功")
        error_info = callCpp("NET_DVR_GetLastError")
    else:
        error_info = callCpp("NET_DVR_GetLastError")
        print("SDK初始化错误:" + str(error_info))
        return False
    set_overtime = callCpp("NET_DVR_SetConnectTime",5000,4)#设置超时
    if set_overtime:
        print("设置超时时间成功")
    else:
        error_info = callCpp("NET_DVR_GetLastError")
        print("设置超时错误信息:" + str(error_info))
        return False
    szLan=(ctypes.c_char * 256)()
    pwd=bytes('guoji123', "ascii")#相机激活所需密码
    DevAddr=bytes('192.168.1.64', "ascii") #相机初始默认IP地址
    struActivateCfg=NET_DVR_ACTIVATECFG()
    struActivateCfg.dwSize=ctypes.sizeof(struActivateCfg)
    ctypes.memmove(struActivateCfg.sPassword, pwd, len(pwd))
    if(callCpp("NET_DVR_ActivateDevice",DevAddr,8000,ctypes.byref(struActivateCfg))):
        error_info = callCpp("NET_DVR_GetLastError")
        print("激活成功:" + str(error_info))
    else:
        error_info = callCpp("NET_DVR_GetLastError")
        print("激活失败:" + str(error_info))
# OnActivateDevice()#海康相机激活
# 相机登入print(ctypes.sizeof(NET_DVR_FOCUSMODE_CFG))
lUserID=NET_DVR_Login_V30()
m_lRealHandle=Preview()
# Get_JPEGpicture()
# Change_CamZoom(1)
#区域局部聚焦和局部曝光功能:矩形区域坐标左上角和右下角(startX,startY,endX,endY)
# flag=1局部聚焦功能,flag!=1局部曝光功能
RegionalCorrection(20,20,50,50,flag=1)

最新Demo地址: https://download.csdn.net/download/weixin_40851278/10686000

在windous系统下Python实现海康相机登入、预览、抓图、光学变倍、相机激活、区域聚焦、区域曝光功能;linux系统下载相应的海康SDK,并将lib文件更换为相对应的库文件,同时将HCNetSDKCom文件夹拷贝出来(与lib文件夹同一级别)    python实现的功能如下:import numpy as npimport osimport ctypes#获取所有的库文... 各个手机平台对JSR规范支持大全 做手机开发的人,最需要了解的是什么呢,没错,就是了解你手中的这款手机能支持些什么,能支持sms吗,能支持mms吗,能支持多媒体吗,支持蓝牙吗等..... 为了方便手机开发爱好者,特别整理下,各个手机厂商已经手机型号对JSR规范的支持整理,也希望大家可...
NVR设备上传人脸 片到人脸库定义 SDK 接口:测试 录批量获取人脸库信息创建人脸库上传人脸 片到人脸库完整代码 开放平台—— 文档链接—— 开发包和文档下载链接 硬件: 超脑NVR(全称Network Video Recorder,即网络视频录像机)、人脸摄像机。 环境:JDK_1.8 Windows 64位 jna.jar版本为3.0.9 <dependency> <groupId>net.java.dev.jna</groupId>
1//单元格编辑结束,焦点离开或者按回车键时 比如:你要判断当前单元格输 内容是否合法。privatevoidfpSpread1_EditModeOff(objectsender, System.EventArgs e) 2//单元格得到焦点时触发privatevoidfpSpread1_EnterCell(objectsender, FarPoint....
查看 相机 SDK ,查看通用配置接口MV_CC_Set/Get(数据类型)Value,每一个参数设置对应一个数据类型,可以在XML节点参数类型列表查看。1、 曝光 时间设置 可以看到 曝光 时间是一个Float数据类型 ,GetNode:key参数是ExposureTime,查看 相机 的官方软件MVS已获得当前 相机 曝光 时间范围。连接上 相机 后,点击常用属性,在 曝光 时间设置停留鼠标,就可以显示出范围。 因此, 曝光 时间a的范围是27-25000000us,代码为: 2、白平衡设置
Python 语言具有广泛的应用范围,其中涉及到 SDK 实时 opencv也是很常见的一种应用方式。 Python 调用 SDK 实时 opencv有以下步骤: 1. 安装 SDK 首先需要在官网下载并安装 SDK ,并在安装后把 SDK 自带的头文件和库文件放到自己的项目目录下。 2. 安装OpenCV 需要在本地安装好OpenCV,并确保可以在 python 调用 OpenCV。 3. 编写 Python 程序 在 python 中编写程序,首先需要导 SDK 的相关库,如 SDK 的头文件和库文件。以 Python 2为例,代码如下: import os import sys import datetime import time import cv2 from HCNet SDK import * 4. 初始化 SDK Python 程序中通过NET_DVR_Init()函数初始化 SDK ,这个函数需要在使用 SDK 的任何其他函数之前 调用 。 5. 录设备 录设备可以通过NET_DVR_Login_V30()函数 实现 ,其中需要传 设备IP地址、端口号、用户名和密码等参数。 6. 获取实时 通过NET_DVR_RealPlay_V30()函数获取实时 像。该函数需要传 设备 录ID、 窗口句柄等参数,可以使用OpenCV在窗口中显示 像。 7. 停止 和退出 录 在程序结束时需要通过NET_DVR_StopRealPlay()和NET_DVR_Logout()函数停止 和退出 录。 以上就是 Python 调用 SDK 实时 opencv的基本流程。在实际应用中,可以根据需要进行调整和完善。