opencv-python同时调用两个摄像头

import cv2
import numpy as np

capture = cv2.VideoCapture(0)
capture_usb = cv2.VideoCapture(2)
# 打开自带的摄像头
if capture.isOpened() and capture_usb.isOpened():
    # 以下设置显示屏的宽高
    capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
    capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
    capture_usb.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
    capture_usb.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

    # 持续读取摄像头数据
    while True:
        read_code, frame = capture.read()
        read_code2, frame2 = capture_usb.read()
        if not read_code or not read_code2:
            break
        c = np.concatenate((frame, frame2), axis=1)
        cv2.imshow("screen_title", frame)
        cv2.imshow("screen_title_usb", frame2)
        cv2.imshow("c", c)
        # 输入 q 键,保存当前画面为图片
        if cv2.waitKey(1) == ord('q'):
            # 设置图片分辨率
            frame = cv2.resize(frame, (1920, 1080))
            cv2.imwrite('pic.jpg', frame)
            capture_usb.release()
            break
    # 释放资源
    capture.release()
    cv2.destroyWindow("screen_title")

同时调用两个摄像头时会失败,推测由于usb带宽问题,这里加上这句(未测试)

capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG"))

另一个demo

import cv2
import threading

# 摄像头1的线程函数
def camera1_thread():
    cap1 = cv2.VideoCapture(0)
    while True:
        ret, frame = cap1.read()
        if ret:
            cv2.imshow('Camera 1', frame)
        if cv2.waitKey(1) == ord('q'):
            break
    cap1.release()
    cv2.destroyAllWindows()

# 摄像头2的线程函数
def camera2_thread():
    cap2 = cv2.VideoCapture(1)
    while True:
        ret, frame = cap2.read()
        if ret:
            cv2.imshow('Camera 2', frame)
        if cv2.waitKey(1) == ord('q'):
            break
    cap2.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    # 创建两个线程分别打开摄像头
    t1 = threading.Thread(target=camera1_thread)
    t2 = threading.Thread(target=camera2_thread)
    # 启动线程
    t1.start()
    t2.start()
    # 等待线程结束
    t1.join()
    t2.join()

又一个例子

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import cv2
import numpy as np
import multiprocessing
 
def openCamera(port):
    cap = cv2.VideoCapture(port)
    cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))  # 视频流格式
    cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
    cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
    width = cap.get(3)
    height = cap.get(4)
    print(width, height, cap.get(5))
 
    while True:
        ret, frame = cap.read()
 
        if not ret:
            print("get camera " + str(port) + " frame is empty")
            break
 
        title = "image" + str(port)
        cv2.imshow(title, frame)
 
        key = cv2.waitKey(10) & 0xff
        if key == ord('q'):
            break
 
    cap.release()
    cv2.destroyAllWindows()
 
 
if __name__ == '__main__':
    #多进程 树莓派上偶数对应一个摄像头
    cam1 = multiprocessing.Process(target=openCamera, args=(0,))
    cam1.start()
 
    cam2 = multiprocessing.Process(target=openCamera, args=(2,))
    cam2.start()

例子加一

import cv2
import numpy as np


capture_0 = cv2.VideoCapture(3)
capture_1 = cv2.VideoCapture(2)
# 打开自带的摄像头
if capture_0.isOpened() and capture_1.isOpened():

    success0 = capture_0.grab()
    # input("!!")
    success1 = capture_1.grab()

    if success0 and success1:
        frame0 = capture_0.retrieve()
        frame1 = capture_1.retrieve()
    else:
        print(success0, success1)

    # 持续读取摄像头数据
    # while True:
    #     print(capture, capture_usb)
    #     read_code, frame = capture.read()
    #     read_code2, frame2 = capture_usb.read()

    #     print(read_code, read_code2)
        # if not read_code or not read_code2:
        #     break
        # c = np.concatenate((frame, frame2), axis=1)
        # cv2.imshow("screen_title", frame)
        # cv2.imshow("screen_title_usb", frame2)
        # cv2.imshow("c", c)
        # 输入 q 键,保存当前画面为图片
        # if cv2.waitKey(1) == ord('q'):
        #     # 设置图片分辨率
        #     frame = cv2.resize(frame, (1920, 1080))
        #     cv2.imwrite('pic.jpg', frame)
        #     capture_usb.release()
        #     break
    # 释放资源
    # capture.release()
    # cv2.destroyWindow("screen_title")

再来一个吧

import cv2
import numpy as np


WIDTH = 1000
HEIGHT = 800
FPS = 60

cap_0 = cv2.VideoCapture("/dev/video0")
cap_1 = cv2.VideoCapture("/dev/video1")
# 打开自带的摄像头
if cap_0.isOpened() and cap_1.isOpened():
    # 以下设置显示屏的宽高
    #cap_0.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
    #cap_0.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
    #cap_0.set(5, FPS)# 帧率
    #cap_1.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
    #cap_1.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
    #cap_1.set(5, FPS)# 帧率

    # 持续读取摄像头数据
    while True:
        _0, frame_0 = cap_0.read()
        _1, frame_1 = cap_1.read()

        if not _0 or not _1:
            break
        else:
            print("cam1:", _0, ", cam2:", _1)

        c = np.concatenate((frame_0, frame_1), axis=1)
        print("c:", c.shape)
        #cv2.imshow("c", c)
        cv2.imshow("c", cv2.resize(c, (1920, 1080)))
        # 输入 q 键,保存当前画面为图片
        if cv2.waitKey(1) == ord('q'):
            break
    # 释放资源
    cap_0.release()
    cap_1.release()
posted @ 2023-01-03 15:22  bitterteaer  阅读(2169)  评论(0编辑  收藏  举报