(202103更新)海康威视网络摄像头sdk python版 (报错排查) 移动物体检测 多通道海康威视网洛摄像头后台检测 海康威视网络摄像头Python SDK
海康威視網絡攝像頭Python SDK+移動物體位置檢測
?
文末附 海康威視網絡攝像頭Python SDK 下載鏈接
?
本文參考博客
https://blog.csdn.net/c20081052/article/details/95082377
https://blog.csdn.net/pengpengloveqiaoqiao/article/details/89487049
?
注意:攝像頭要在鏡頭上貼紅外濾光片 過濾掉可見光
?
效果展示,已經過濾到大部分波長可見光,手在鏡頭前來回移動,紅框即檢測到移動物體的位置,并框出來(海康威視的攝像頭可見光好像不能完全被濾光片過濾掉,但是可以過濾到投影機的畫面或者屏幕的畫面,不影響使用)
?
目錄
1.將海康威視的C++? sdk 轉化為 Python sdk
2.調用海康威視Python SDK
3.調用多個海康威視攝像頭
4. 將多個攝像頭畫面拼接成一個
5.挨個攝像頭透視變換
6. 透視變換完后再在拼接后的畫面上做動態物體檢測,參考博客
1.將海康威視的C++? sdk 轉化為 Python sdk? (本文已經處理完了)
如何將海康威視的C++? sdk 轉化為 Python 可用的sdk ,請參考博主?ciky奇?的文章,鏈接如下
https://blog.csdn.net/c20081052/article/details/95082377? 如果最后一步Python調用時遇到報錯,嘗試將所有相關的文件全部復制到Python工程的路徑下
環境:
1. windows10? 64位
2. opecv
3. python3
4.海康威視網絡攝像頭64位 C++ sdk
? ??https://www.hikvision.com/cn/download_61.html
? ? 下載SDK_Win64
5.其他庫文件:
? ?5.1 opencv-python
? ?5.2 numpy
? ?5.3?multiprocessing (多進程使用)
本文已將海康威視C++ sdk轉為Python sdk處理好,可以直接下載使用,文末網址分享
?
如果HKIPCamera 一直報錯,找不到模塊? ? ,首先 python 和 opencv要安裝好,并且設置好環境變量,如果還是報錯:
?
一般是缺少VC++庫的運行時組件,兩個方案:
?
1. 從微軟官網下載VC++庫的運行時組件:??
此組件也可以解決其他項目上的很多疑難雜癥
官網鏈接:?https://visualstudio.microsoft.com/zh-hans/downloads/
?
2、下載一個DirectX修復工具
鏈接:https://pan.baidu.com/s/1thfI2SyCpQoToBxuwFnxuA?
提取碼:8aza?
?
打開后點擊? ?工具----> 選項---->擴展---->開始擴展
?
?
?
擴展完了以后,此時會默認選中同時更新C++,點擊檢測并修復,
如果缺少組件就會自動安裝,如果已經安裝了但是不是最新的,會自動更新,
等修復完了,再嘗試運行Python程序
?
?
如果還是一直存在問題,請回頭參考博文https://blog.csdn.net/c20081052/article/details/95082377一步一步重新操作
?
2.調用海康威視Python SDK
?
import cv2 import numpy as np import HKIPcameradef get_cam_frame(ip, account, password):ip = str(ip) # 攝像頭IP地址,要和本機IP在同一局域網name = str(account) # 管理員用戶名pw = str(password) # 管理員密碼HKIPcamera.init(ip, name, pw)while(True): fram = HKIPcamera.getframe() frame = np.array(fram)cv2.imshow('frame',frame)c = cv2.waitKey(10)if c == 27:break;HKIPcamera.release()if __name__ == '__main__':get_cam_frame('192.168.1.64', 'admin', '123456')?
?
3.調用多個海康威視攝像頭
? ? 調用多個攝像頭需要用到多進程,多線程不起作用
? ? 在第一步的代碼基礎上修改一下
?
import cv2 import numpy as np import HKIPcamera from multiprocessing import Process, Queuedef get_cam_frame(frameName, ip, account, password):ip = str(ip) # 攝像頭IP地址,要和本機IP在同一局域網name = str(account) # 管理員用戶名pw = str(password) # 管理員密碼HKIPcamera.init(ip, name, pw)while True:fram = HKIPcamera.getframe()frame = np.array(fram)cv2.imshow(frameName, frame)c = cv2.waitKey(10)if c == 27:break;HKIPcamera.release()if __name__ == '__main__':p0 = Process(target=get_cam_frame, args=('frame0', '192.168.1.64', 'admin', '123456',))p0.start()p1 = Process(target=get_cam_frame, args=('frame1', '192.168.1.65', 'admin', '123456',))p1.start()# 以此類推 p2 p3 ... pn?
? ??
4. 將多個攝像頭畫面拼接成一個
?
? ? ? ?再在上述的代碼基礎上修改
?
import cv2 import numpy as np import HKIPcamera from multiprocessing import Process, Queue import os import signaldef get_cam_frame(frameName, ip, account, password, n):ip = str(ip) # 攝像頭IP地址,要和本機IP在同一局域網name = str(account) # 管理員用戶名pw = str(password) # 管理員密碼HKIPcamera.init(ip, name, pw)while True:fram = HKIPcamera.getframe()frame = np.array(fram)# 由于攝像頭畫面是1920*1080,多個拼接的話,畫面會非常大,所有重置一下每個攝像頭的畫面frame = cv2.resize(frame, (768, 540), interpolation=cv2.INTER_CUBIC)n.put(frame)cv2.imshow(frameName, frame)c = cv2.waitKey(10)if c == 27:break;HKIPcamera.release()if __name__ == '__main__':q0 = Queue(3)p0 = Process(target=get_cam_frame, args=('frame0', '192.168.1.64', 'admin', '123456', q0,))p0.start()q1 = Queue(3)p1 = Process(target=get_cam_frame, args=('frame1', '192.168.1.65', 'admin', '12345', q1,))p1.start()while True:if p0.is_alive() and p1.is_alive():frame0 = q0.get()frame1 = q1.get()# 橫著拼full_frame = np.hstack([frame0, frame1])# 豎著拼# full_frame = np.vstack([frame0, frame1])cv2.imshow('full_frame', full_frame)c = cv2.waitKey(10)if c==27:os.kill(p0.pid, signal.SIGTERM)os.kill(p1.pid, signal.SIGTERM)break;?
5.挨個攝像頭透視變換
? ?如果不了解opencv-python透視變換函數的參數,請先看下此博客的透視變換函數介紹,鏈接如下:
? ?https://blog.csdn.net/wsp_1138886114/article/details/83374333
? ?
import cv2 import numpy as np import HKIPcamera from multiprocessing import Process, Queue import os import signaldef get_cam_frame(frameName, ip, account, password, n):ip = str(ip) # 攝像頭IP地址,要和本機IP在同一局域網name = str(account) # 管理員用戶名pw = str(password) # 管理員密碼HKIPcamera.init(ip, name, pw)while True:fram = HKIPcamera.getframe()frame = np.array(fram)# 由于攝像頭畫面是1920*1080,多個拼接的話,畫面會非常大,所有重置一下每個攝像頭的畫面frame = cv2.resize(frame, (768, 540), interpolation=cv2.INTER_CUBIC)n.put(frame)cv2.imshow(frameName, frame)c = cv2.waitKey(10)if c == 27:break;HKIPcamera.release()#第一個攝像頭透視變化的四個點 frame0_x0, frame0_y0, frame0_x1, frame0_y1, frame0_x2, frame0_y2, frame0_x3, frame0_y3 = 0, 0, 0, 0, 0, 0, 0, 0 #第二個攝像頭透視變化的四個點 frame1_x0, frame1_y0, frame1_x1, frame1_y1, frame1_x2, frame1_y2, frame1_x3, frame1_y3 = 0, 0, 0, 0, 0, 0, 0, 0#簡單來寫,按照從第一個攝像頭開始,鼠標每點擊一次,獲取一個點 #例如鼠標第一次點擊,就等于給坐標frame0_x0, frame0_y0 賦值, 第二次點擊就是給坐標frame0_x1, frame0_y1賦值,以此類推clickNum = 0def draw_point(event, x, y, flags, param):global frame0_x0, frame0_y0, frame0_x1, frame0_y1, frame0_x2, frame0_y2, frame0_x3, frame0_y3global frame1_x0, frame1_y0, frame1_x1, frame1_y1, frame1_x2, frame1_y2, frame1_x3, frame1_y3, clickNumif event == cv2.EVENT_LBUTTONDOWN:if clickNum == 0:frame0_x0 = xframe0_y0 = yelif clickNum == 1:frame0_x1 = xframe0_y1 = yelif clickNum == 2:frame0_x2 = xframe0_y2 = yelif clickNum == 3:frame0_x3 = xframe0_y3 = yelif clickNum == 4:frame1_x0 = xframe1_y0 = yelif clickNum == 5:frame1_x1 = xframe1_y1 = yelif clickNum == 6:frame1_x2 = xframe1_y2 = yelif clickNum == 7:frame1_x3 = xframe1_y3 = yclickNum += 1# 透視變換 def perspective_transform():global frame0, frame1, clickNumglobal frame0_x0, frame0_y0, frame0_x1, frame0_y1, frame0_x2, frame0_y2, frame0_x3, frame0_y3global frame1_x0, frame1_y0, frame1_x1, frame1_y1, frame1_x2, frame1_y2, frame1_x3, frame1_y3if clickNum==8:W_cols, H_rows = frame0.shape[:2]pts0_1 = np.float32([[frame0_x0, frame0_y0], [frame0_x1, frame0_y1],[frame0_x2, frame0_y2], [frame0_x3, frame0_y3]])pts0_2 = np.float32([[0, 0], [W_cols, 0], [0, H_rows], [W_cols, H_rows]])M0 = cv2.getPerspectiveTransform(pts0_1, pts0_2)frame0 = cv2.warpPerspective(frame0, M0, (W_cols, H_rows))# 這里的變換需要減去第一個攝像頭的畫面的寬度,768pts1_1 = np.float32([[frame1_x0-768, frame1_y0], [frame1_x1-768, frame1_y1],[frame1_x2-768, frame1_y2], [frame1_x3-768, frame1_y3]])pts1_2 = np.float32([[0, 0], [W_cols, 0], [0, H_rows], [W_cols, H_rows]])M1 = cv2.getPerspectiveTransform(pts1_1, pts1_2)frame1 = cv2.warpPerspective(frame1, M1, (W_cols, H_rows))if __name__ == '__main__':q0 = Queue(3)p0 = Process(target=get_cam_frame, args=('frame0', '192.168.1.64', 'admin', '123456', q0,))p0.start()q1 = Queue(3)p1 = Process(target=get_cam_frame, args=('frame1', '192.168.1.65', 'admin', '123456', q1,))p1.start()cv2.namedWindow('full_frame')# 鼠標點擊事件cv2.setMouseCallback('full_frame', draw_point)while True:if p0.is_alive() and p1.is_alive():frame0 = q0.get()frame1 = q1.get()# 透視變換perspective_transform()# 透視變換完再拼接# 橫著拼full_frame = np.hstack([frame0, frame1])# 豎著拼#full_frame = np.vstack([frame0, frame1])cv2.imshow('full_frame', full_frame)c = cv2.waitKey(10)if c==27:os.kill(p0.pid, signal.SIGTERM)os.kill(p1.pid, signal.SIGTERM)break;?
?
6. 透視變換完后再在拼接后的畫面上做動態物體檢測,參考博客
?https://blog.csdn.net/pengpengloveqiaoqiao/article/details/89487049
? ?
import cv2 import numpy as np import HKIPcamera from multiprocessing import Process, Queue import os import signaldef get_cam_frame(frameName, ip, account, password, n):ip = str(ip) # 攝像頭IP地址,要和本機IP在同一局域網name = str(account) # 管理員用戶名pw = str(password) # 管理員密碼HKIPcamera.init(ip, name, pw)while True:fram = HKIPcamera.getframe()frame = np.array(fram)# 由于攝像頭畫面是1920*1080,多個拼接的話,畫面會非常大,所有重置一下每個攝像頭的畫面frame = cv2.resize(frame, (768, 540), interpolation=cv2.INTER_CUBIC)n.put(frame)cv2.imshow(frameName, frame)c = cv2.waitKey(10)if c == 27:break;HKIPcamera.release()#第一個攝像頭透視變化的四個點 frame0_x0, frame0_y0, frame0_x1, frame0_y1, frame0_x2, frame0_y2, frame0_x3, frame0_y3 = 0, 0, 0, 0, 0, 0, 0, 0 #第二個攝像頭透視變化的四個點 frame1_x0, frame1_y0, frame1_x1, frame1_y1, frame1_x2, frame1_y2, frame1_x3, frame1_y3 = 0, 0, 0, 0, 0, 0, 0, 0#簡單來寫,按照從第一個攝像頭開始,鼠標每點擊一次,獲取一個點 #例如鼠標第一次點擊,就等于給坐標frame0_x0, frame0_y0 賦值, 第二次點擊就是給坐標frame0_x1, frame0_y1賦值,以此類推clickNum = 0def draw_point(event, x, y, flags, param):global frame0_x0, frame0_y0, frame0_x1, frame0_y1, frame0_x2, frame0_y2, frame0_x3, frame0_y3global frame1_x0, frame1_y0, frame1_x1, frame1_y1, frame1_x2, frame1_y2, frame1_x3, frame1_y3, clickNumif event == cv2.EVENT_LBUTTONDOWN:if clickNum == 0:frame0_x0 = xframe0_y0 = yelif clickNum == 1:frame0_x1 = xframe0_y1 = yelif clickNum == 2:frame0_x2 = xframe0_y2 = yelif clickNum == 3:frame0_x3 = xframe0_y3 = yelif clickNum == 4:frame1_x0 = xframe1_y0 = yelif clickNum == 5:frame1_x1 = xframe1_y1 = yelif clickNum == 6:frame1_x2 = xframe1_y2 = yelif clickNum == 7:frame1_x3 = xframe1_y3 = yclickNum += 1# 透視變換 def perspective_transform():global frame0, frame1, clickNumglobal frame0_x0, frame0_y0, frame0_x1, frame0_y1, frame0_x2, frame0_y2, frame0_x3, frame0_y3global frame1_x0, frame1_y0, frame1_x1, frame1_y1, frame1_x2, frame1_y2, frame1_x3, frame1_y3if clickNum==8:W_cols, H_rows = frame0.shape[:2]pts0_1 = np.float32([[frame0_x0, frame0_y0], [frame0_x1, frame0_y1],[frame0_x2, frame0_y2], [frame0_x3, frame0_y3]])pts0_2 = np.float32([[0, 0], [W_cols, 0], [0, H_rows], [W_cols, H_rows]])M0 = cv2.getPerspectiveTransform(pts0_1, pts0_2)frame0 = cv2.warpPerspective(frame0, M0, (W_cols, H_rows))# 這里的變換需要減去第一個攝像頭的畫面的寬度,768pts1_1 = np.float32([[frame1_x0-768, frame1_y0], [frame1_x1-768, frame1_y1],[frame1_x2-768, frame1_y2], [frame1_x3-768, frame1_y3]])pts1_2 = np.float32([[0, 0], [W_cols, 0], [0, H_rows], [W_cols, H_rows]])M1 = cv2.getPerspectiveTransform(pts1_1, pts1_2)frame1 = cv2.warpPerspective(frame1, M1, (W_cols, H_rows))if __name__ == '__main__':q0 = Queue(3)p0 = Process(target=get_cam_frame, args=('frame0', '192.168.1.64', 'admin', '123456', q0,))p0.start()q1 = Queue(3)p1 = Process(target=get_cam_frame, args=('frame1', '192.168.1.65', 'admin', '123456', q1,))p1.start()cv2.namedWindow('full_frame')# 鼠標點擊事件cv2.setMouseCallback('full_frame', draw_point)kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))fgbg = cv2.createBackgroundSubtractorMOG2()while True:if p0.is_alive() and p1.is_alive():frame0 = q0.get()frame1 = q1.get()# 透視變換perspective_transform()# 透視變換完再拼接# 橫著拼full_frame = np.hstack([frame0, frame1])# 豎著拼#full_frame = np.vstack([frame0, frame1])fgmask = fgbg.apply(full_frame)mask = cv2.morphologyEx(fgmask, cv2.MORPH_OPEN, kernel)contours, _ = cv2.findContours(fgmask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)for c in contours:if 100 < cv2.contourArea(c) < 40000:x, y, w, h = cv2.boundingRect(c)cv2.rectangle(full_frame, (x, y), (x + w, y + h), (0, 0, 255))cv2.imshow('full_frame', full_frame)c = cv2.waitKey(10)if c==27:os.kill(p0.pid, signal.SIGTERM)os.kill(p1.pid, signal.SIGTERM)break;?
?
最后再寫個Json或者XML(我沒寫)保存一下透視變換的點位信息完整工程分享
鏈接:https://pan.baidu.com/s/1zHwWCW5dW-XR6a98RfEuew?
提取碼:6gfc
?
?
?
?
總結
以上是生活随笔為你收集整理的(202103更新)海康威视网络摄像头sdk python版 (报错排查) 移动物体检测 多通道海康威视网洛摄像头后台检测 海康威视网络摄像头Python SDK的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一键修改计算机名-加域-加组脚本编写历程
- 下一篇: 关于数据治理的读书笔记 - 数据治理能力