Opencv 录制多路 camera
felicx 化神

前言

最近在写个小需求,利用opencv录制多路usb摄像头数据,并将拍摄视频拼接显示在同一个窗口。

思路

挺简单的,就是通过cv2.VideoCapture获取/dev/video*的信息,获取每个摄像头的frame后,通过numpyhstackvstack将两帧拼接起来。
代码如下:

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
43
44
45
46
47
import cv2
import numpy as np

# 0,2,4代表/dev/video后缀
videoFront = cv2.VideoCapture(0)
videoLeft = cv2.VideoCapture(2)
videoRight = cv2.VideoCapture(4)

width = (int(videoFront.get(cv2.CAP_PROP_FRAME_WIDTH)))
height = (int(videoFront.get(cv2.CAP_PROP_FRAME_HEIGHT)))

# 设置保存视频的宽高、帧率、格式
sz = (width*2, height*2)
fps = 30
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
vout = cv2.VideoWriter()
vout.open('./output.mp4', fourcc, fps, sz, True)
write_ok = False

# while (videoLeft.isOpened() and videoRight.isOpened()):
while (True):
retFront, frameFront = videoFront.read()
retLeft, frameLeft = videoLeft.read()
retRight, frameRight = videoRight.read()
if(retFront and retLeft and retRight):
frameFront = cv2.resize(frameFront, (width, height), interpolation=cv2.INTER_CUBIC)
frameUp = np.hstack((frameFront, frameFront))

frameLeft = cv2.resize(frameLeft, (width, height), interpolation=cv2.INTER_CUBIC)
frameRight = cv2.resize(frameRight, (width, height), interpolation=cv2.INTER_CUBIC)
frameDown = np.hstack((frameLeft, frameRight))

frame = np.vstack((frameUp, frameDown))

if write_ok:
print("saving video...")
vout.write(frame)
cv2.imshow('frame', frame)

key = cv2.waitKey(1) & 0xFF
if key == ord("w"):
write_ok = write_ok is not True
if key == ord("q"):
break

videoLeft.release()
videoRight.release()
 评论
评论插件加载失败
正在加载评论插件
由 Hexo 驱动 & 主题 Keep
访客数 访问量