读取海康威视C5W-4G摄像头的视频数据
首先将电脑与摄像头连接在同一路由器下,路由器配置固定IP地址,然后通过以下代码获取视频
import os
import cv2
img_save_dir = “Video”
if not os.path.exists(img_save_dir):
os.makedirs(img_save_dir)
source = “rtsp://admin:
(
p
a
s
s
w
o
r
d
)
@
(password)@
(password)@(ip addr)/Streaming/Channels/1”
cam = cv2.VideoCapture(source)
img_counter = 0
while(cam.isOpened()):
ret, frame = cam.read()
cv2.imshow(‘Real Time Video onitoring’, frame)
if not ret:
break
# press ESC to escape (ESC ASCII value: 27)
if cv2.waitKey(1) & 0xFF == 27:
break
# press Space to capture image (Space ASCII value: 32)
elif cv2.waitKey(1) & 0xFF == 32:
print(“Saving image …”)
img_file = img_save_dir + “/opencv_frame_{}.jpg”.format(img_counter)
cv2.imwrite(img_file, frame)
print(“WebCam Image {}: {} written!”.format(img_counter, img_file))
img_counter += 1
else:
pass
cam.release()
cv2.destroyAllWindows()