1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Jetson 学习笔记(六):cv2调用CSI摄像头(jetson nx/nano) 打开海康摄像头 打开电脑摄像头

Jetson 学习笔记(六):cv2调用CSI摄像头(jetson nx/nano) 打开海康摄像头 打开电脑摄像头

时间:2020-04-22 00:22:21

相关推荐

Jetson 学习笔记(六):cv2调用CSI摄像头(jetson nx/nano) 打开海康摄像头 打开电脑摄像头

文章目录

海康摄像头电脑摄像头Jetson NX/Nano-CSI方法1方法2 Jetson Nano/Nx-USB

海康摄像头

import cv2import multiprocessing as mpnum = 0def image_put(q, name, pwd, ip, channel):#cv2.namedWindow(ip, cv2.WINDOW_NORMAL)global urlurl="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 exitif cv2.waitKey(1) & 0xFF == 27:breakcap.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') # initqueues = [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 = Trueprocess.start()for process in processes:process.join()if __name__ == '__main__':run_multi_camera()

电脑摄像头

import cv2cap = 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 exitif cv2.waitKey(1) & 0xFF == 27:breakcap.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 = 1280capture_height = 720display_width = 1280display_height = 720framerate = 60flip_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键退出breakcap.release()cv2.destroyAllWindows()else:print("打开摄像头失败")

方法2

通过jetcam来运行CSI摄像头(jetcam是用于NVIDIA jetson的易于使用的python摄像头界面)

安装过程:

/weixin_45569617/article/details/113934118

具体运行代码

from jetcam.csi_camera import CSICameraimport cv2camera0 = 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=800new_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()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。