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()

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值