python调用IP摄像头

一、手机端下载软件

至于怎么下载??

直接去浏览器搜索,并找到对应的下面的这个即可,也可以用我提供的这个链接去下载

IP Camera摄像头app下载-IP Camera无线摄像头app下载 v28.7.3手机客户端 - 多多软件站

 二、勾选RTSP服务器,然后记下这里的局域网地址,后续会用到

三、在代码中调用即可

值得注意的是,把那个source变成自己的摄像头的地址即可,把下面完整的代码拷贝运行就可以啦,快去试试吧

import cv2

# 替换本地摄像头为网络摄像头地址
source = 'http://admin:admin@10.33.10.220:8081'
cap = cv2.VideoCapture(source)

if not cap.isOpened():
    print("无法打开网络摄像头")
    exit()

frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
if fps == 0:
    fps = 30


# *mp4v 就是解包操作, 等同于 'm' 'p' '4' 'v'
fourcc = cv2.VideoWriter_fourcc(*'XVID')
# avi格式的视频,下面保存视频的时候后缀名要是avi
# fourcc = cv2.VideoWriter_fourcc(*'XVID')
# 检查视频捕捉是否成功打开

print(frame_width, frame_height, fps, fourcc)
# 参数二: 用哪种编码
# 参数三: 帧率
# 参数四: 分辨率, 表示摄像头拍视频, 这个大小搞错了也不行,很重要,要搞清楚自己摄像头的分辨率
vw = cv2.VideoWriter('output.avi', fourcc, fps, (frame_width, frame_height))
# vw = cv2.VideoWriter('output.avi', fourcc, 30, (640, 480))

frame_count = 0
max_frames = int(fps * 100000)  # 10秒视频的帧数,假设帧率是30

while cap.isOpened() and frame_count < max_frames:
    ret, frame = cap.read()

    if not ret:
        break

    # 写每一帧数据
    # vw.write(frame)

    # 增加帧计数
    frame_count += 1

    # 注释掉显示部分的代码
    cv2.imshow('frame', frame)

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

print('done')

vw.release()
cap.release()
cv2.destroyAllWindows()

四、也可以用多个摄像头

这个streams.txt 每个视频源单独成行

本地摄像头填0

USB摄像头填1,2,3…

IP摄像头要根据摄像头类型,按下面格式填写

rtsp://admin(账号):admin(密码)@ip:端口/(主码流,辅码流之类的
http

 

代码如下:

import cv2
import numpy as np

# 读取文件内容
with open('streams.txt', 'r') as file:
    sources = file.read().splitlines()

# 初始化捕捉对象列表和 VideoWriter 对象
caps = []
vws = []
frame_widths = []
frame_heights = []
fps = []
max_frames = []

for i, source in enumerate(sources):
    print(source, type(source))
    if source.isdigit():
        cap = cv2.VideoCapture(int(source))
    else:
        cap = cv2.VideoCapture(source)

    if not cap.isOpened():
        print(f"无法打开摄像头: {source}")
        continue

    # 添加摄像头到列表
    caps.append(cap)

    # 获取摄像头参数
    frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    frame_widths.append(frame_width)
    frame_heights.append(frame_height)

    current_fps = cap.get(cv2.CAP_PROP_FPS)
    if current_fps == 0:
        current_fps = 30
    fps.append(current_fps)

    # 创建 VideoWriter 对象
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    output_filename = f'output_{i}.avi'
    vw = cv2.VideoWriter(output_filename, fourcc, current_fps, (frame_width, frame_height))
    vws.append(vw)

    # 计算最大帧数(10000秒)
    max_frames.append(int(current_fps * 10000))

# 主循环
frame_counts = [0] * len(caps)
exit_flag = False
while not exit_flag:
    for i, cap in enumerate(caps):
        if not cap.isOpened():
            continue

        ret, frame = cap.read()
        if not ret:
            continue

        # 写每一帧数据到对应的 VideoWriter
        vws[i].write(frame)

        # 增加帧计数
        frame_counts[i] += 1

        # 注释掉显示部分的代码
        cv2.imshow(f'frame_{i}', frame)

        # 检查是否按下 'q' 键以退出
        if cv2.waitKey(1) == ord('q'):
            exit_flag = True
            break

    # 检查所有摄像头是否已达到最大帧数
    if all(frame_count >= max_frames[i] for i, frame_count in enumerate(frame_counts)):
        exit_flag = True

print('done')

# 释放所有捕捉对象和 VideoWriter
for cap in caps:
    cap.release()

for vw in vws:
    vw.release()

cv2.destroyAllWindows()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值