python呼叫海康人臉相機登入等介面(開發環境Linux)
阿新 • • 發佈:2019-01-25
python呼叫海康SDK存在幾個問題,一是海康官方沒有python技術支援,二是海康SDK涉及到的引數傳遞特
別多,因此不建議直接用python呼叫海康SDK,最好是直接修改海康的C++demo,然後編譯成可執行程式或者
動態庫供python呼叫,下面的登入介面是python直接呼叫海康的SDK庫進行登入的
海康的庫主要放在lib下,在使用海康的庫函式的時候,因為不知道該函式是在哪個庫裡面,所以我直接採用了
遍歷庫的辦法去執行程式
import os import ctypes #獲取所有的庫檔案到一個列表 path = "/home/caobin/chike/chike/CH_HCNetSDK_V5.2.7.4_build20170606_Linux64/lib/"so_list = [] def add_so(path,so_list): files = os.listdir(path) for file in files: if not os.path.isdir(path+file): if file.endswith(".so"): so_list.append(path+file) else: add_so(path+file+"/",so_list) add_so(path,so_list) for lib in so_list: print(lib) lUserID = 0 m_lAlarmHandle = 0 def callCpp(func_name,*args): for so_lib in so_list: # print(so_lib) try: lib = ctypes.cdll.LoadLibrary(so_lib) try: value = eval("lib.%s"%func_name)(*args) print("呼叫的庫:"+so_lib) print("執行成功,返回值:"+str(value)) return value except: continue except: print("庫檔案載入失敗:"+so_lib) continue print("沒有找到介面!") return False #定義結構體 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)] #使用者註冊裝置 def NET_DVR_Login_V30(sDVRIP = "192.168.1.63",wDVRPort = 8000,sUserName = "admin",sPassword = "guoji123"): init_res = callCpp("NET_DVR_Init")#SDK初始化 if init_res: print("SDK初始化成功") 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") DeviceInfo = LPNET_DVR_DEVICEINFO_V30() DeviceInfoRef = ctypes.byref(DeviceInfo) lUserID = callCpp("NET_DVR_Login_V30",sDVRIP,wDVRPort,sUserName,sPassword,DeviceInfoRef) print("登入結果:"+str(lUserID)) if lUserID == -1: error_info = callCpp("NET_DVR_GetLastError") print("登入錯誤資訊:" + str(error_info)) return error_info else: return lUserID NET_DVR_Login_V30()