OpenCV+ip camera realizes remote real-time monitoring

1. Project preparation

The content used in this project is:

1.ip camera app

Mainly rely on connecting its ip to achieve the effect of remote connection, the effect is similar to the app function used by remote control drone

2. External extended display (HDMI interface)

This is not a necessity, but one more screen is for observation, and the functions of automatically identifying certain items and recording etc. still have to be realized by program code, which is essentially a signal transmission

3. Configure a computer that can run python (picture omitted)

Every programmer should have

 2. Code part

Ideas:

Import opencv, use videocapture to get the camera, and its target address is the ip address of the ip camera,

Set parameters (number of buffers, buffer size, resolution, number of frames), and then loop to capture frame by frame

in,

The ret parameter returns a Boolean value (True/False), indicating whether the content has been read;

frame means the image of a frame is intercepted cv2.waitKey(1) means: if the button is pressed, return the ASCII value of the button, otherwise return -1,

The bitwise AND operation of & 0xFF only takes the last eight bits of the return value of cv2.waitKey(1), ord('Q') means returning the ASCII value of Q, and the function of break in this block is to press Q to close (quit)

import cv2
# 创建一个窗口 名字叫做cap
cv2.namedWindow('cap', flags=cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO | cv2.WINDOW_GUI_EXPANDED)
'''
#打开USB摄像头
cap = cv2.VideoCapture(0)
'''
# 摄像头的IP地址,http://用户名:密码@IP地址:端口/
ip_camera_url = 'http://admin:[email protected]:8081/video'
# 创建一个VideoCapture
cap = cv2.VideoCapture(ip_camera_url)
print('IP摄像头是否开启: {}'.format(cap.isOpened()))
# 显示缓存数
print(cap.get(cv2.CAP_PROP_BUFFERSIZE))
# 设置缓存区的大小
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
# 调节摄像头分辨率
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)
print(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
print(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# 设置FPS
print('setfps', cap.set(cv2.CAP_PROP_FPS, 25))
print(cap.get(cv2.CAP_PROP_FPS))
while True:
    # 逐帧捕获
    ret, frame = cap.read()  
    cv2.imshow('cap', frame)
    if cv2.waitKey(1) & 0xFF == ord('Q'):
        break
# 退出后,释放VideoCapture对象
cap.release()
cv2.destroyAllWindows()

Terminal result information:

Whether the IP camera is on: True
0.0
1920.0
1080.0
setfps False
25.0

Connect the mobile phone with the computer program calling address in the form of local area network

Just like the built-in camera function of the mobile phone, the image is displayed in the window of the computer program. The content of the screenshot at this moment is conveying the image information of "CSDN is awesome, and the blogger is Ouhuang".

Program running result:

 God's perspective captured by another mobile phone:

Computer screen, mobile phone screen, external display screen,

 At this point, a simple monitoring is configured, and it can be expanded on this basis, such as target tracking, motion detection, recognition analysis, etc., then it is not a simple camera...

Guess you like

Origin blog.csdn.net/qq_53521409/article/details/127896293