OpenCV can open the camera via the head videocapture () method
Camera variable = cv2. Videocapture (n) n is an integer, the built-in camera is 0, if there are other cameras are 1,2,3,4, ...
Cap = Cv2. Videocapture (0)
Whether the camera is open can be judged by the isopened () method
Camera variables. isopened ()
Returns True if Open returns false anyway
Cap.isopened ()
Boolean variable, image variable = camera variable. Read () reads the image through the read () method
Boolean variable is true, indicates read succeeded, False indicates read failed
Ret,img = Cap.read ()
Release the camera
Camera variables. Release ()
Cap.release ()
The key variable = Cv2.waitkey (n) waitkey (n) method can obtain the user input, and can get the ASCLL code value of the key.
code example:
Import cv2# Basic Drawing # import Numpy#cv2.namedwindow ("image") #创建窗口 # grab webcam video Image cap = Cv2. Videocapture (0) #创建内置摄像头变量while (cap.isopened ()): #isOpened () detects if the camera is open ret,img = Cap.read () #把摄像头获取的图像信息保存之img变量 if ret = = True: #如果摄像头读取图像成功 cv2.imshow (' Image ', img) k = Cv2.waitkey (100) if k = = Ord (' a ') or k = = Ord (' A '): cv2.imwrite (' test.jpg ', img) breakcap.release () # Close camera cv2.waitkey (0) Cv2.destroyallwindow ()
Python's OpenCV (v)---Grab camera video images