Jetson Nano 学习 opencv 1 (USB摄像头)

B站视频学习

【AI on the Jetson Nano】国外大神超超超简单明了的人工智能入门课程(英文字幕)_哔哩哔哩_bilibili

1.读取摄像头信息并对窗口做一些基本操作

import cv2               # 导入 opencv

# 你可以在终端输入如下命令查看使用的USB是哪一个 : ls /dev/video* 
# for usb camera /dev/video0, the device_id will be 0
dispH=640
dispW=480
# Create the Camera instance
cam = cv2.VideoCapture('/dev/video0')        # 选择摄像头,直接用0可能错误
cam.set(cv2.CAP_PROP_FRAME_WIDTH,dispW)      # 设置高宽
cam.set(cv2.CAP_PROP_FRAME_HEIGHT,dispH)

print('USB Camera is now ready')
while True:
    try:
        # 读入画面并显示
        ret,frame = cam.read()
        cv2.imshow('nanoCam', frame)
        cv2.moveWindow('nanoCam',0,0)          # 移动窗口到(0,0)
        # 改变窗口大小并显示
        frameSmall=cv2.resize(frame,(320,240)) # 修改大小,定义新窗口大小
        cv2.imshow('fSmall',frameSmall)        # 显示修改后的窗口
        cv2.moveWindow('fSmall',700,0)

        # 转化为灰度图,在另一个位置显示
        gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)   
        cv2.imshow('grayVideo',gray)
        cv2.moveWindow('grayVideo',0,500)
        # 改变窗口大小并显示
        graySmall=cv2.resize(gray,(320,240))  # 修改大小
        cv2.imshow('gSmall',graySmall)
        cv2.moveWindow('gSmall',700,300)
        
        # 按q结束
        if cv2.waitKey(1) == ord('q'):
            break
    except KeyboardInterrupt:
        break

cam.release()
cv2.destroyAllWindows()

2.Reading  and  Writing  Video Files

import cv2

# you can see connected USB cameras by running : ls /dev/video* on the terminal
# for usb camera /dev/video2, the device_id will be 2
dispH=480
dispW=640
# Create the Camera instance
cam = cv2.VideoCapture('/dev/video0')   #直接用0可能错误
cam.set(cv2.CAP_PROP_FRAME_WIDTH,dispW)
cam.set(cv2.CAP_PROP_FRAME_HEIGHT,dispH)

# 设置视频存放位置,视频名称,视频格式,video_fps,宽高大小
outVid=cv2.VideoWriter('videos/myCam.avi',cv2.VideoWriter_fourcc(*'XVID'),20.0,(dispW,dispH))

print('USB Camera is now ready')
while True:
    try:
        # read the camera image
        ret,frame = cam.read()
        # display the frame
        cv2.imshow('nanoCam', frame)
        cv2.moveWindow('nanoCam',0,0)
        outVid.write(frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    except KeyboardInterrupt:
        break

cam.release()
outVid.release()
cv2.destroyAllWindows()

你可能感兴趣的:(opencv,opencv,学习,计算机视觉)