海康摄像头
import cv2
import multiprocessing as mp
num = 0
def image_put(q, name, pwd, ip, channel):
#cv2.namedWindow(ip, cv2.WINDOW_NORMAL)
global url
url="rtsp://%s:%s@%s:%s//Streaming/Channels/1" \
% (name, pwd, ip, channel)
cap = cv2.VideoCapture(url)
# 获取视频帧率
fps = cap.get(cv2.CAP_PROP_FPS)
print('fps: ', fps)
if cap.isOpened():
print('HIKVISION1')
print('camera ' + ip + " connected.")
while cap.isOpened():
ret, frame = cap.read()
# 抓取图片不成功再重新抓取
if not ret:
cap = cv2.VideoCapture("rtsp://%s:%s@%s:%s//Streaming/Channels/1" \
% (name, pwd, ip, channel))
print('HIKVISION2')
ret, frame = cap.read()
frame = cv2.resize(frame, (800,600))
cv2.imshow(ip,frame)
# Press esc on keyboard to exit
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
# 解决进程问题
def run_multi_camera():
user_name, user_pwd = "admin", "a12345678"
camera_ip_l = ["10.16.14.151",]
ports = ['556']
mp.set_start_method(method='spawn') # init
queues = [mp.Queue(maxsize=2) for _ in camera_ip_l]
processes = []
for queue, camera_ip,port in zip(queues, camera_ip_l,ports):
processes.append(mp.Process(target=image_put, args=(queue, user_name, user_pwd, camera_ip,port)))
for process in processes:
process.daemon = True
process.start()
for process in processes:
process.join()
if __name__ == '__main__':
run_multi_camera()
电脑摄像头
import cv2
cap = cv2.VideoCapture(0)
while cap.isOpened():
cv2.namedWindow('Pc-camera', cv2.WINDOW_NORMAL)
ret, frame = cap.read()
frame = cv2.resize(frame, (800,600))
cv2.imshow('Pc-camera',frame)
# Press esc on keyboard to exit
if cv2.waitKey(1) & 0xFF == 27:
break
cap.release()
cv2.destroyWindow()
Jetson NX/Nano-CSI
方法1
使用Gstreamer读取CSI摄像头主要分为3个步骤:创建Gstreamer管道;将管道绑定opencv的视频流;逐帧提取和显示。下面首先给出基于Python的详细代码:
import cv2
# 设置gstreamer管道参数
def gstreamer_pipeline(
capture_width=1280, #摄像头预捕获的图像宽度
capture_height=720, #摄像头预捕获的图像高度
display_width=1280, #窗口显示的图像宽度
display_height=720, #窗口显示的图像高度
framerate=60, #捕获帧率
flip_method=0, #是否旋转图像
):
return (
"nvarguscamerasrc ! "
"video/x-raw(memory:NVMM), "
"width=(int)%d, height=(int)%d, "
"format=(string)NV12, framerate=(fraction)%d/1 ! "
"nvvidconv flip-method=%d ! "
"video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
"videoconvert ! "
"video/x-raw, format=(string)BGR ! appsink"
% (
capture_width,
capture_height,
framerate,
flip_method,
display_width,
display_height,
)
)
if __name__ == "__main__":
capture_width = 1280
capture_height = 720
display_width = 1280
display_height = 720
framerate = 60
flip_method = 0
# 创建管道
print(gstreamer_pipeline(capture_width,capture_height,display_width,display_height,framerate,flip_method))
#管道与视频流绑定
cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
if cap.isOpened():
window_handle = cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE)
# 逐帧显示
while cv2.getWindowProperty("CSI Camera", 0) >= 0:
ret_val, img = cap.read()
cv2.imshow("CSI Camera", img)
keyCode = cv2.waitKey(30) & 0xFF
if keyCode == 27:# ESC键退出
break
cap.release()
cv2.destroyAllWindows()
else:
print("打开摄像头失败")
方法2
通过jetcam来运行CSI摄像头(jetcam是用于NVIDIA jetson的易于使用的python摄像头界面)
安装过程:
https://blog.csdn.net/weixin_45569617/article/details/113934118
具体运行代码
from jetcam.csi_camera import CSICamera
import cv2
camera0 = CSICamera(capture_device=0, width=224, height=224)
#camera1 = CSICamera(capture_device=1, width=224, height=224)
image0 = camera0.read()
print(image0.shape)
#image1 = camera1.read()
#print(image1.shape)
print(camera0.value.shape)
#print(camera1.value.shape)
while 1:
image0 = camera0.read()
#image1 = camera1.read()
cv2.imshow("CSI Camera0", image0)
#cv2.imshow("CSI Camera1", image1)
kk = cv2.waitKey(1)
if kk == ord('q'): # 按下 q 键,退出
break
Jetson Nano/Nx-USB
先判断是否存在USB摄像头
1.如何是开机插上的摄像头就应该先关机重启之后才会显示
2.开机,输入ls /dev/video* 看看是不是有摄像头设备
3.输入nvgstcapture 一般来说就可以将摄像头直接打开,如果需要关闭直接关闭终端就好了
4.要是不显示就更新一下,sudo apt-get update sudo apt-get upgrade
下面是执行的代码
CSI摄像头的标识为0,因此这个USB摄像头的标识为1,这个可以在实际使用时通过测试来得到。另外,上述代码中对图像的尺寸做了限制,如果宽度超过800,则等比的缩放图像再显示。
import cv2
#创建摄像头捕获模块
cap = cv2.VideoCapture(1)
#创建窗口
window_handle = cv2.namedWindow("USB Camera", cv2.WINDOW_AUTOSIZE)
# 逐帧显示
while cv2.getWindowProperty("USB Camera", 0) >= 0:
ret_val, img = cap.read()
print(img.shape)
# 图像太大需要调整
height, width = img.shape[0:2]
if width>800:
new_width=800
new_height=int(new_width/width*height)
img = cv2.resize(img, (new_width,new_height))
cv2.imshow("USB Camera", img)
keyCode = cv2.waitKey(30) & 0xFF
if keyCode == 27:# ESC键退出
break
#释放资源
cap.release()
cv2.destroyAllWindows()