使用python连接到Hikvision Camera并打开CV

时间:2019-03-03 16:08:23

标签: python opencv image-processing ip-camera hikvision

我想将Hikvision ip摄像机与python连接,并使用以下代码打开cv:

import numpy as np
import cv2

cap = cv2.VideoCapture()
cap.open("rtsp://yourusername:yourpassword@172.16.30.248:555/Streaming/channels/2/")

while(True):
     # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',ret)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

当我运行代码时,出现此错误:

    Traceback (most recent call last):
  File "C:\Users\Amin\Desktop\ip camera in py\csm.py", line 15, in <module>
    cv2.imshow('frame',ret)
cv2.error: OpenCV(4.0.0) c:\projects\opencv-python\opencv\modules\imgproc\src\color.hpp:261: error: (-2:Unspecified error) in function '__cdecl cv::CvtHelper<struct cv::Set<1,-1,-1>,struct cv::Set<3,4,-1>,struct cv::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'
> Unsupported depth of input image:
>     'VDepth::contains(depth)'
> where
>     'depth' is 6 (CV_64F)

我测试了我的相机VLCPlayer,它运行良好!

我认为问题在于opencv4!

我该如何解决? tnx很多

1 个答案:

答案 0 :(得分:1)

错误是您在此处的OpenCV的BOOLEAN函数中传递了Mat值而不是imshow()值:

cv2.imshow('frame',ret)

所以您应该通过框架:

cv2.imshow('frame',frame)
cap.read()中的

Python返回两个值,一个是Boolean,它表示是否成功读取了该帧,第二个是帧本身。因此,应检查ret是否为true,然后显示框架。