欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

树莓派python3+opencv调用usb摄像头显示并保存

程序员文章站 2022-05-29 09:50:50
树莓派python3+opencv调用usb摄像头显示并保存直接上代码运行指令运行结果直接上代码camera.pyimport cv2import numpy as npname = 0cap = cv2.VideoCapture(0) cap.set(3,640)cap.set(4,480) ret, frame = cap.read()rows, cols, channels = frame.shapeprint(cols, rows, channels)while(1):...

树莓派python3+opencv调用usb摄像头显示并保存

直接上代码

camera.py

import cv2
import numpy as np
name = 0
cap = cv2.VideoCapture(0)
 
cap.set(3,640)
cap.set(4,480)
 
ret, frame = cap.read()
rows, cols, channels = frame.shape
print(cols, rows, channels)
while(1):
        ret,frame = cap.read()
        dst = frame
        cv2.imshow('usb camera', dst)
 
        k = cv2.waitKey(50)
        if (k == ord('q')):
            break
        elif(k == ord('s')):
                #name = input('name:')
                name += 1
                filename = '/home/pi/camera' + str(name) + '.jpg'
                cv2.imwrite(filename, dst)
                print(filename)
                #break 
cap.release()
cv2.destroyAllWindows()

这个地方注意一下保存图片的地址,地址写不对的话是不能保存的

运行指令

python3 camera.py

运行结果

唉,日常忘记拍照或者截图,不过亲测有效的!遇到问题的话可以评论交流!

本文地址:https://blog.csdn.net/guo_xyx/article/details/107371987