导入库
pip install opencv-python
代码
- 网络摄像头
'''
Opencv-python读取IP摄像头视频流
pip install opencv-python
'''
import cv2
import datetime
import time
class Camera_picture:
def __init__(self):
# 摄像头登录账号
self.user = "admin"
# 摄像头登录密码
self.pwd = "123456"
# 摄像头地址:端口
self.ip = "192.168.100.108:554"
# 截图存储位置
self.file_path = "./"
# 摄像头窗口名称
self.name = "camera"
# 连接并获取大华摄像头数据
def dahua(self):
# channel:通道号,起始为1。例如通道2,则为channel=2
# subtype:码流类型,主码流为0(即subtype=0),辅码流为1(即subtype=1)。
video_stream_path = f"rtsp://{self.user}:{self.pwd}@{self.ip}/cam/realmonitor?channel=1&subtype=0"
cap = cv2.VideoCapture(video_stream_path) # 连接摄像头
# cap.isOpened() 判断摄像头是否连接成功,如果返回True则代表连接成功可以继续操作,如果返回False则代表连接失败
print('IP摄像头是否开启: {}'.format(cap.isOpened()))
return cap
def timing_screenshot(self):
'''定时截图'''
cap = self.dahua()
if cap.isOpened(): # 连接成功
cv2.namedWindow(self.name, flags=cv2.WINDOW_FREERATIO) # 创建一个用于显示画面的窗口
last_time = datetime.datetime.now() # 记录上一次时间
while(True):
# 逐帧捕获,第一个参数返回一个布尔值(True/False),代表有没有读取到图片;第二个参数表示截取到一帧的图片
ret, frame = cap.read()
frame = cv2.resize(frame, (500, 300)) # 调整画面大小
cv2.imshow(self.name, frame) # 显示摄像头画面
# 定时获取截图
cur_time = datetime.datetime.now() # 当前时间
name = self.file_path + str(time.time()) + ".jpg" # 路径+名称+格式
if (cur_time - last_time).seconds >= 10: # 如果间隔超过10秒就截图
cv2.imwrite(name, cap.read()[1]) # 保存摄像头画面
last_time = cur_time
print("画面保存成功")
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 当一切结束后,释放VideoCapture对象
cap.release() # 释放摄像头
cv2.destroyAllWindows() # 释放所有显示图像窗口
else:
print("摄像头连接失败,请检查配置")
def manual_screenshot(self):
'''手动截图'''
cap = self.dahua()
if cap.isOpened():
cv2.namedWindow(self.name, flags=cv2.WINDOW_FREERATIO)
while(True):
ret, frame = cap.read()
frame = cv2.resize(frame, (500, 300))
cv2.imshow(self.name, frame)
event = cv2.waitKey(1) & 0xFF # 使用变量
if event == ord('s'): # 按"S"截取图片
name = self.file_path + str(time.time()) + ".jpg"
cv2.imwrite(name, cap.read()[1])
print("画面保存成功")
elif event == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
else:
print("摄像头连接失败,请检查配置")
def get_video_stream(self):
'''获取视频流'''
cap = self.dahua()
if cap.isOpened(): # 连接成功
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # 获取视频的宽度
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) # 获取视频的高度
fps = cap.get(cv2.CAP_PROP_FPS) # 获取视频的帧率
# fourcc = int(cap.get(cv2.CAP_PROP_FOURCC)) # 视频的编码
# FourCC 是用于指定视频编解码器的4字节代码
fourcc = cv2.VideoWriter_fourcc(*"MJPG")
writer = cv2.VideoWriter(f"{self.file_path}{str(time.time())}.mp4", fourcc, fps, (width, height))
while(True):
ret, frame = cap.read()
if not ret: # 返回False退出循环
break
writer.write(frame) # 视频保存
frame = cv2.resize(frame, (500, 300))
cv2.imshow(self.name, frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
else:
print("摄像头连接失败,请检查配置")
if __name__ == '__main__':
run = Camera_picture()
# run.timing_screenshot()
run.get_video_stream()
# run.manual_screenshot()
- USB摄像头
'''
Opencv-python读取USB摄像头视频流
pip install opencv-python
'''
import cv2
def usb_camera():
'''usb摄像头连接'''
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) # 连接摄像头
if cap.isOpened(): # 连接成功
while(True):
ret, frame = cap.read()
# frame = cv2.flip(frame, 1) # 摄像头是和人对立的,将图像左右调换回来正常显示
cv2.imshow("Usb Capture", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
else:
print("摄像头连接失败,请检查摄像头连接是否正常")
if __name__ == '__main__':
usb_camera()
文章来源地址https://www.toymoban.com/news/detail-527740.html
文章来源:https://www.toymoban.com/news/detail-527740.html
到了这里,关于python_获取网络(大华)摄像头画面,并存储为图片和视频的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!