海康多相机同步取流保存图片

这篇具有很好参考价值的文章主要介绍了海康多相机同步取流保存图片。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

话不多说,直接上代码。代码里包含了多窗口显示图像和保存图片。文章来源地址https://www.toymoban.com/news/detail-743119.html

#"rtsp://admin:123456qq@192.168.10.192/stream1"
# rtsp://admin:Admin123@192.168.100.103:554/Streaming/Channels/101
#rtsp://admin:Admin123@192.168.100.103:554/cam/realmonitor?channel=1&subtype=0

import time
import multiprocessing as mp
import cv2
import os

"""
Source: Yonv1943 2018-06-17
https://github.com/Yonv1943/Python
https://zhuanlan.zhihu.com/p/38136322
OpenCV official demo
https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html
海康、大华IpCamera RTSP地址和格式(原创,旧版)- 2014年08月12日 23:01:18 xiejiashu
rtsp_path_hikvison = "rtsp://%s:%s@%s/h265/ch%s/main/av_stream" % (user, pwd, ip, channel)
rtsp_path_dahua = "rtsp://%s:%s@%s/cam/realmonitor?channel=%d&subtype=0" % (user, pwd, ip, channel)
https://blog.csdn.net/xiejiashu/article/details/38523437
最新(2017)海康摄像机、NVR、流媒体服务器、回放取流RTSP地址规则说明 - 2017年05月13日 10:51:46 xiejiashu
rtsp_path_hikvison = "rtsp://%s:%s@%s//Streaming/Channels/%d" % (user, pwd, ip, channel)
https://blog.csdn.net/xiejiashu/article/details/71786187
"""


def image_put(q, user, pwd, ip, channel=1):
    # cap = cv2.VideoCapture("rtsp://%s:%s@%s/stream1" % (user, pwd, ip))
    # cap = cv2.VideoCapture("rtsp://%s:%s@%sStreaming/Channels/1701?transportmode=unicast" % (user, pwd, ip))

    # if cap.isOpened():
    #     print('HIKVISION')
    # else:
    #     cap = cv2.VideoCapture("rtsp://%s:%s@%s/cam/realmonitor?channel=%d&subtype=0" % (user, pwd, ip, channel))
    #     print('DaHua')
    cap = cv2.VideoCapture("rtsp://%s:%s@%s/cam/realmonitor?channel=%d&subtype=0" % (user, pwd, ip, channel))
    if cap.isOpened():
        print('HIKVISION')
    else:
        cap = cv2.VideoCapture("rtsp://admin:123456qq@192.168.100.12/stream1")
        print('Genius')

    while True:
        q.put(cap.read()[1])
        q.get() if q.qsize() > 1 else time.sleep(0.01)



def run_opencv_camera():
    user, pwd, ip, channel = "admin", "123456qq", "33.90.7.110", 1

    cap_path = 0  # local camera (e.g. the front camera of laptop)
    # cap_path = 'video.avi'  # the path of video file
    # cap_path = "rtsp://%s:%s@%s/h264/ch%s/main/av_stream" % (user, pwd, ip, channel)  # HIKIVISION old version 2015
    # cap_path = "rtsp://%s:%s@%s//Streaming/Channels/%d" % (user, pwd, ip, channel)  # HIKIVISION new version 2017
    # cap_path = "rtsp://%s:%s@%s/cam/realmonitor?channel=%d&subtype=0" % (user, pwd, ip, channel)  # dahua

    cap = cv2.VideoCapture(cap_path)

    while cap.isOpened():
        is_opened, frame = cap.read()
        cv2.imshow('frame', frame)
        cv2.waitKey(1000)
    cap.release()

def image_get_s(queue_list, ip, img_path):
    """show in single opencv-imshow window"""

    window_name1 = "%s" % ip[0]
    window_name2 = "%s" % ip[1]
    window_name3 = "%s" % ip[2]
    # window_name4 = "%s" % ip[3]

    idx_img = 0
    cv2.namedWindow(window_name1, flags=cv2.WINDOW_FREERATIO)
    cv2.namedWindow(window_name2, flags=cv2.WINDOW_FREERATIO)
    cv2.namedWindow(window_name3, flags=cv2.WINDOW_FREERATIO)
    # cv2.namedWindow(window_name4, flags=cv2.WINDOW_FREERATIO)
    while True:
        imgs = [q.get() for q in queue_list]
        img1 = imgs[0]
        img2 = imgs[1]
        img3 = imgs[2]
        # img4 = imgs[3]

        img1_tmp = cv2.resize(img1, (int(1920/2 ), int(1080/2 )))
        img2_tmp = cv2.resize(img2, (int(1920/2 ), int(1080/2 )))
        img3_tmp = cv2.resize(img3, (int(1920 / 2), int(1080 / 2)))
        # img4_tmp = cv2.resize(img4, (int(1920 / 2), int(1080 / 2)))

        cv2.imshow(window_name1, img1_tmp)
        cv2.imshow(window_name2, img2_tmp)
        cv2.imshow(window_name3, img3_tmp)
        # cv2.imshow(window_name4, img4_tmp)
        # imgs = np.concatenate(imgs, axis=1)
        # cv2.imshow(window_name, imgs)
        key = cv2.waitKey(1)

        if key == ord('s'):
            img_path0 = os.path.join(img_path[0], str(idx_img) + '.jpg')
            cv2.imwrite(img_path0, img1)

            img_path1 = os.path.join(img_path[1], str(idx_img) + '.jpg')
            cv2.imwrite(img_path1, img2)

            img_path2 = os.path.join(img_path[2], str(idx_img) + '.jpg')
            cv2.imwrite(img_path2, img3)

            # img_path3 = os.path.join(img_path[3], str(idx_img) + '.jpg')
            # cv2.imwrite(img_path3, img3)
            # idx_img += 1

def image_get_all(queue_list, ip, img_path):
    """show in single opencv-imshow window"""

    window_name1 = "%s" % ip[0]
    window_name2 = "%s" % ip[1]
    # window_name3 = "%s" % ip[2]
    # window_name4 = "%s" % ip[3]


    idx_img = 0
    cv2.namedWindow(window_name1, flags=cv2.WINDOW_FREERATIO)
    cv2.namedWindow(window_name2, flags=cv2.WINDOW_FREERATIO)
    # cv2.namedWindow(window_name3, flags=cv2.WINDOW_FREERATIO)
    # cv2.namedWindow(window_name4, flags=cv2.WINDOW_FREERATIO)
    while True:
        imgs = [q.get() for q in queue_list]
        img1 = imgs[0]
        img2 = imgs[1]
        # img3 = imgs[2]
        # img4 = imgs[3]

        img1_tmp = cv2.resize(img1, (int(1920/2 ), int(1080/2 )))
        img2_tmp = cv2.resize(img2, (int(1920/2 ), int(1080/2 )))
        # img3_tmp = cv2.resize(img3, (int(1920/4), int(1080/ 4)))
        # img4_tmp = cv2.resize(img4, (int(1920 / 4), int(1080 / 4)))
        # print('11',len(img1_tmp))
        cv2.imshow(window_name1, img1_tmp)
        cv2.imshow(window_name2, img2_tmp)
        # cv2.imshow(window_name3, img3_tmp)
        # cv2.imshow(window_name4, img4_tmp)
        # imgs = np.concatenate(imgs, axis=1)
        # cv2.imshow(window_name, imgs)
        cv2.waitKey(1)

        cv2.imwrite(img_path[0]+str(idx_img)+'.jpg', img1)
        cv2.imwrite(img_path[1]+str(idx_img)+'.jpg', img2)
        # cv2.imwrite(img_path[2]+str(idx_img)+'.jpg', img3)
        # cv2.imwrite(img_path[3]+str(idx_img) +'.jpg',img4)
        idx_img +=1


def run_single_camera():
    # user_name, user_pwd, camera_ip = "admin", "admin123456", "172.20.114.196"
    # user_name, user_pwd, camera_ip = "admin", "Admin123", "192.168.100.103:554"
    user_name, user_pwd, camera_ip = "admin", "Admin123", "192.168.100.100:554"


    mp.set_start_method(method='spawn')  # init
    queue = mp.Queue(maxsize=2)
    processes = [mp.Process(target=image_put, args=(queue, user_name, user_pwd, camera_ip)),
                 mp.Process(target=image_get_s, args=(queue, camera_ip))]

    [process.start() for process in processes]
    [process.join() for process in processes]


def run_multi_camera():
    user_name, user_pwd = "admin", "Admin123"

    camera_ip_l = [
        # "172.20.114.196",  # ipv4
        # "[fe80::3aaf:29ff:fed3:d260]",  # ipv6
        # "192.168.100.180:554",
        # "192.168.100.182:554",
        "192.168.100.181:554",
        "192.168.100.184:554",
    ]

    mp.set_start_method(method='spawn')  # init
    queues = [mp.Queue(maxsize=len(camera_ip_l)) for _ in camera_ip_l]


    imgs_path = []
    for i in range(len(camera_ip_l)):
        imgs_path.append("E:/dataset_0902/images/{}/".format(camera_ip_l[i].split('.')[-1].split(':')[0]))
        if not os.path.exists(imgs_path[-1]):
            os.makedirs(imgs_path[-1])

    processes = [mp.Process(target=image_get_all, args=(queues, camera_ip_l, imgs_path))]

    for queue, camera_ip, img_path in zip(queues, camera_ip_l, imgs_path):
        processes.append(mp.Process(target=image_put, args=(queue, user_name, user_pwd, camera_ip)))



    for process in processes:
        process.daemon = True
        process.start()
    for process in processes:
        process.join()
    # p.join()

def image_collect(queue_list, camera_ip_l):
    import numpy as np

    """show in single opencv-imshow window"""
    window_name1 = "%s_and_so_no" % camera_ip_l[0]
    window_name2 = "%s_and_so_no" % camera_ip_l[1]
    cv2.namedWindow(window_name1, flags=cv2.WINDOW_FREERATIO)
    cv2.namedWindow(window_name2, flags=cv2.WINDOW_FREERATIO)
    while True:
        imgs = [q.get() for q in queue_list]
        img1 = imgs[0]
        img2 = imgs[1]

        # cv2.imshow(window_name1, img1)
        # cv2.imshow(window_name2, img2)

        img1_tmp = cv2.resize(img1, (int(1920 / 2), int(1080 / 2)))
        img2_tmp = cv2.resize(img2, (int(1920 / 2), int(1080 / 2)))
        cv2.imshow(window_name1, img1_tmp)
        cv2.imshow(window_name2, img2_tmp)
        # imgs = np.concatenate(imgs, axis=1)
        # cv2.imshow(window_name, imgs)
        cv2.waitKey(1)

        # if key == ord('s'):
        #     img_path = os.path.join(img_folder, str(idx_img) + '.jpg')
        #     cv2.imwrite(img_path, img)
        #     idx_img += 1
    # """show in multiple opencv-imshow windows"""
    # [cv2.namedWindow(window_name, flags=cv2.WINDOW_FREERATIO)
    #  for window_name in camera_ip_l]
    # while True:
    #     for window_name, q in zip(camera_ip_l, queue_list):
    #         cv2.imshow(window_name, q.get())
    #         cv2.waitKey(1)


def run_multi_camera_in_a_window():
    user_name, user_pwd = "admin", "Admin123"
    camera_ip_l = [
        # "172.20.114.196",  # ipv4
        # "[fe80::3aaf:29ff:fed3:d260]",  # ipv6
        "192.168.100.182:554",
        "192.168.100.183:554"
    ]

    mp.set_start_method(method='spawn')  # init
    queues = [mp.Queue(maxsize=2) for _ in camera_ip_l]

    processes = [mp.Process(target=image_collect, args=(queues, camera_ip_l))]
    for queue, camera_ip in zip(queues, camera_ip_l):
        processes.append(mp.Process(target=image_put, args=(queue, user_name, user_pwd, camera_ip)))

    for process in processes:
        process.daemon = True  # setattr(process, 'deamon', True)
        process.start()
    for process in processes:
        process.join()



def get_biaoding():
    user, pwd, ip, channel = "admin", "Admin123", "192.168.100.182:554", 1
    cap = cv2.VideoCapture("rtsp://%s:%s@%s/cam/realmonitor?channel=%d&subtype=0" % (user, pwd, ip, channel))
    if cap.isOpened():
        print('HIKVISION')
    idx_img = 20
    img_folder = "D:/20210226/camera{}".format(ip.split('.')[-1].split(':')[0])
    if not os.path.exists(img_folder):
        os.makedirs(img_folder)
    while True:
        img = cap.read()[1]
        img_tmp = cv2.resize(img, (1920 // 2, 1080 // 2))
        cv2.imshow('image', img_tmp)
        key = cv2.waitKey(1)
        if key == ord('s'):
            img_path = os.path.join(img_folder, str(idx_img) + '.jpg')
            cv2.imwrite(img_path, img)
            idx_img += 1




def get_img():
    cap1 = cv2.VideoCapture("rtsp://%s:%s@%s/cam/realmonitor?channel=%d&subtype=0" % ("admin", "Admin123", "192.168.100.181:554/", 1))
    # cap2 = cv2.VideoCapture("rtsp://%s:%s@%s/cam/realmonitor?channel=%d&subtype=0" % ("admin", "Admin123", "192.168.100.182:554/", 1))
    # cap3 = cv2.VideoCapture("rtsp://%s:%s@%s/cam/realmonitor?channel=%d&subtype=0" % ("admin", "Admin123", "192.168.100.183:554/", 1))
    # cap4 = cv2.VideoCapture("rtsp://%s:%s@%s/cam/realmonitor?channel=%d&subtype=0" % ("admin", "Admin123", "192.168.100.184:554/", 1))

    img_save1 = "E:/dataset_0902/calibrate_intri/181"
    # img_save1 = "D:/20210308/camera/181"
    # img_save2 = "D:/20210225/camera/182"
    # img_save3 = "D:/20210225/camera/183"
    # img_save4 = "D:/20210225/camera/184"
    if not os.path.exists(img_save1):
        os.makedirs(img_save1)
    # if not os.path.exists(img_save2):
    #     os.makedirs(img_save2)
    # if not os.path.exists(img_save3):
    #     os.makedirs(img_save3)
    # if not os.path.exists(img_save4):
    #     os.makedirs(img_save4)

    if cap1.isOpened():
        print('HIKVISION')

    idx_img = 0
    while True:

        time1 = cap1.get(cv2.CAP_PROP_POS_MSEC)

        print(time1)

        img1 = cap1.read()[1]
        if (img1 is None):
            print("ok")
            continue

        cv2.imwrite(img_save1 + '/' + str(idx_img) + '.jpg', img1)
        idx_img = idx_img + 1


        # key = cv2.waitKey(1)
        # if key == ord('s'):
        #     cv2.imwrite()
        #     cv2.imwrite(iimg_save1 + '/' + str(idx_img) + '.jpg', img1mg_save2 + '/' + str(idx_img) + '.jpg', img2)
        #     cv2.imwrite(img_save3 + '/' + str(idx_img) + '.jpg', img3)
        #     cv2.imwrite(img_save4 + '/' + str(idx_img) + '.jpg', img4)
        #     idx_img += 1

        img1 = cv2.resize(img1, (int(1920 / 4), int(1080 / 4)))

        cv2.imshow('1', img1)
        cv2.waitKey(1)


if __name__ == '__main__':

    # get_biaoding()
    # get_img()

    run_multi_camera()

到了这里,关于海康多相机同步取流保存图片的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用爬虫代码获得深度学习目标检测或者语义分割中的图片。

    问题描述:目标检测或者图像分割需要大量的数据,如果手动从网上找的话会比较慢,这时候,我们可以从网上爬虫下来,然后自己筛选即可。 代码如下(不要忘记安装代码依赖的库): 这里以搜索明星的图片为例,运行代码,然后根据提示输入搜索图片的名字→搜索图片

    2024年02月10日
    浏览(48)
  • JavaCv对接海康、大华摄像头SDK取流并转推到RTMP服务器

    支持H265转H264编码 本文主要介绍海康、大华SDK取流推流过程,这里就不展示对接海康、大华SDK了 这个是重点 Native.setCallbackThreadInitializer(this, new CallbackThreadInitializer(true, false, \\\"HikRealStream-\\\" + RandomUtil.randomNumbers(8))); 增加回放流速度控制 1. 引入JavaCv Maven依赖,按需引入 2. 流处理类

    2024年02月02日
    浏览(55)
  • ros来保存图像和保存记录视频的方法---gmsl相机保存视频和图片

    rosrun image_view image_view image:=/myimg_topic这个命令只是用来查看图像的,它并不会保存图像。如果你想要保存图像,你需要使用image_saver节点,并指定保存路径。例如: 下面指令就可以了,可以用 在这个命令中,/path/to/folder/是你想要保存图片的文件夹路径。image%04d.jpg是保存的图

    2024年02月04日
    浏览(35)
  • uniapp制作水印相机给图片添加水印并且保存图片至本地

    文件主要分为两大类: 1.代码包文件:代码包文件指的是在项目目录中添加的文件。 2.本地文件:通过调用接口本地产生,或通过网络下载下来,存储到本地的文件。 其中本地文件又分为三种: 1.本地临时文件:临时产生,随时会被回收的文件。运行时最多存储 4GB,结束运

    2024年02月11日
    浏览(53)
  • Python相机自动采集图像,然后模板匹配、自动截取保存图片

    通过python调用相机然后间隔一段时间进行拍摄图片,进行识别提取。 提示:以下是本篇文章正文内容,下面案例可供参考 我的电脑没有相机硬件设备,于是我通过手机在局域网的状态下进行图像传输到电脑上,在这里特别推荐软件:DroidCam Client    建议大家按照情况自行安

    2024年02月12日
    浏览(35)
  • 【opencv】解决USB相机取流卡顿问题

            最近在使用opencv接usb相机时遇到了画面很卡问题,具体体现在cv::videocapture frame 的耗时大概在200ms左右。通过查询资料发现,USB相关通常会支持YUV格式和MJPG格式输出,YUV格式未经过压缩,数据量较大,usb相机的带宽不足以支撑高分辨率,MJPG格式可支持高分辨率。

    2024年02月05日
    浏览(85)
  • Baumer工业相机堡盟工业相机如何通过BGAPI SDK直接实现Mono16位深度的图像保存(C++)

    Baumer工业相机堡盟相机是一种高性能、高质量的工业相机,可用于各种应用场景,如物体检测、计数和识别、运动分析和图像处理。 Baumer的万兆网相机拥有出色的图像处理性能,可以实时传输高分辨率图像。此外,该相机还具有快速数据传输、低功耗、易于集成以及高度可扩

    2024年02月10日
    浏览(45)
  • 【python脚本】python实现:目标检测裁剪图片样本,根据类标签文件进行裁剪保存

    我在进行目标检测时候,比如红绿灯检测,目标区域很小,样本杂乱。 想要筛选错误样本的话,很困难。可以把目标区域裁剪出来。人大脑处理对于这样的异己比较敏感。样本量较少的话可以自己筛一筛。样本量较大的话,可以训练一个分类模型帮你筛一下。 它就可以实现

    2024年02月15日
    浏览(41)
  • 【D435i深度相机YOLO V5结合实现目标检测】

    参考:Ubutntu下使用realsense d435i(三):使用yolo v5测量目标物中心点三维坐标 欢迎大家阅读2345VOR的博客【D435i深度相机YOLO V5结合实现目标检测】🥳🥳🥳 2345VOR鹏鹏主页: 已获得CSDN《嵌入式领域优质创作者》称号👻👻👻,座右铭:脚踏实地,仰望星空🛹🛹🛹 本文章属于

    2024年02月08日
    浏览(68)
  • realsense D455深度相机+YOLO V5结合实现目标检测(一)

    realsense D455深度相机+YOLO V5结合实现目标检测(二)第二篇链接 可以实现将D435,D455深度相机和yolo v5结合到一起,在识别物体的同时,还能测到物体相对与相机的距离。 说明一下为什么需要做这个事情?1.首先为什么需要用到realsense D455深度相机? 因为他是普通的相机还加了一个

    2024年02月06日
    浏览(50)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包