python OpenCV 调用罗技摄像头方法说明

时间:2024-03-03 22:51:47

1.首先,需要安装opencv库文件,可以通过PIP install opencv-python(scripts目录下安装);

 

2.其次,验证安装版本pip list;

 

3.验证能否打开摄像头,实例如下(亲测可用):

import cv2

cap = cv2.VideoCapture(0)

while True:

  ret,frame =cap.read()

  cv2.imshow(\'frame\',frame)

  if cv2,waitKey(1) == ord(\'1\'):

    break

cap.release()

cv2.destroyAllWindows()

 

4.验证可以录制视频并保存在指定路径下,实例如下(亲测可用):

import cv2

import matplotlib

import numpy

import os

global now_time

cap = cv2.VideoCapture(0)

fourcc = cv2.VideoWriter_fourcc(*"MJPG")

out = cv2.VideoWriter("C:\\A\\output.avi",fourcc,25.,(640,480))

while(cap.isOpened()):

  ret,frame =cap.read()

  if ret:

    out.write(frame)

    cv2.imshow(\'frame\',frame)

    if cv2,waitKey(1) == ord(\'1\'):

      break

  else:break

cap.release()

out.release()

cv2.destroyAllWindows()