How does Opencv open and use Hikvision industrial cameras

Article reprinted: Use the opencv interface cv2.VideoCapture() interface to call the Hikvision robot industrial camera in the python language

1. Opencv interface

Use opencv's official interface VideoCapture() interface to directly call Hikvision industrial cameras, and
then briefly introduce the VideoCapture() interface
: cv2.VideoCapture()
Function: Get video files in the local directory or use the camera of this computer to get real-time Data;
this article needs to use this interface to call Hikvision industrial cameras;
parameters:
write "0" for the parameter, and call the local computer camera;
write "1" for the parameter, call the external camera

Call Hikvision Robot Industrial Camera

prerequisite

Hikvision industrial cameras are called by cv2.VideoCapture(). You need to register the directshow plug-in provided by Hikvision in advance to ensure that you can find and open Hikvision cameras using directshow. Haikang SDK download
: Introduction to Hikvision Robot Industrial Camera SDK
. :
insert image description here
Run with administrator privileges: InstallDSSvc_x64.bat script
insert image description here

Call Haikang industrial camera debugging

Make sure that the industrial camera can get the stream normally on its MVS client.
Any industrial camera will do, as long as the MVS can get the stream normally.
insert image description here

Opencv code call

1. Turn on the camera

import cv2
 
cap = cv2.VideoCapture(1)  # 调用目录下的视频
# cap=cv2.VideoCapture(0)  #调用摄像头‘0’一般是打开电脑自带摄像头,‘1’是打开外部摄像头(只有一个摄像头的情况)
 
if False == cap.isOpened():
    print(0)
else:
    print(1)

In the above code, if the call is successful, it returns 1, and if the call fails, it returns 0;

2. Set the size and frame rate of the acquired image

cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)  # 设置图像宽度
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1024)  # 设置图像高度
cap.set(cv2.CAP_PROP_FPS , 15)   # 设置帧率

Because the image size obtained by directshow is 640X480, we need to set the image size by ourselves.
3. Image acquisition and display

# 显示图像
while True:
    ret, frame = cap.read()
    # print(ret)  #
    ########图像不处理的情况
    frame_1 = cv2.resize(frame , (640 , 512))
    cv2.imshow("frame", frame_1)
 
    input = cv2.waitKey(1)
    if input == ord('q'):
        break
 
cap.release()  # 释放摄像头
cv2.destroyAllWindows()  # 销毁窗口

4. Effect display
insert image description here
5. Complete code:

import cv2
 
cap = cv2.VideoCapture(1)  # 调用目录下的视频
# cap=cv2.VideoCapture(0)  #调用摄像头‘0’一般是打开电脑自带摄像头,‘1’是打开外部摄像头(只有一个摄像头的情况)
 
if False == cap.isOpened():
    print(0)
else:
    print(1)
 
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)  # 设置图像宽度
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1024)  # 设置图像高度
cap.set(cv2.CAP_PROP_FPS , 15)   # 设置帧率
# 显示图像
while True:
    ret, frame = cap.read()
    # print(ret)  #
    ########图像不处理的情况
    frame_1 = cv2.resize(frame , (640 , 512))
    cv2.imshow("frame", frame_1)
 
    input = cv2.waitKey(1)
    if input == ord('q'):
        break
 
cap.release()  # 释放摄像头
cv2.destroyAllWindows()  # 销毁窗口

Summarize

As the whole process above, you can use the cv2.VideoCapture() interface to call the Haikang industrial camera, and display the image data through the opencv interface! ! !

In my opinion, this method has limitations.
1. The use of multiple cameras, this method has no way to control and distinguish between cameras, and it may not be possible to use multiple cameras.
2. Most of the parameters of the camera cannot be set through the interface.
3. The special features of the camera Functions, such as events, watermarks and other functions, cannot be used normally
4. When troubleshooting abnormal problems, third-party interfaces cannot troubleshoot problems

Guess you like

Origin blog.csdn.net/qq_23107577/article/details/117773842