python uses the opencv method to drive the camera

  • 2020-12-09 00:55:19
  • OfStack

As shown below:


#coding:utf-8
 
import cv2
import sys
from PIL import Image
 
 
def CatchUsbVideo(window_name, camera_idx):
 cv2.namedWindow(window_name)
 
 #  Capture camera 
 cap = cv2.VideoCapture(camera_idx)
 
 while cap.isOpened():
  ok, frame = cap.read() #  read 1 The frame data 
  if not ok:
   break
  #  According to the image 
  cv2.imshow(window_name, frame)
  c = cv2.waitKey(10)
  if c & 0xFF == ord('q'):
   break
 
   #  Release the camera and destroy all Windows 
 cap.release()
 cv2.destroyAllWindows()
 
 
if __name__ == '__main__':
 CatchUsbVideo("FaceRect", 0)

Related articles: