1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| 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) 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带宽问题,这里加上这句(未测试)
1
| capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG"))
|
另一个demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| import cv2 import threading
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()
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()
|
又一个例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
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()
|
例子加一
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| 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() success1 = capture_1.grab()
if success0 and success1: frame0 = capture_0.retrieve() frame1 = capture_1.retrieve() else: print(success0, success1)
|
再来一个吧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| 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():
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", cv2.resize(c, (1920, 1080))) if cv2.waitKey(1) == ord('q'): break cap_0.release() cap_1.release()
|