1. Capture camera and real-time display
Import Cv2import numpy as Npimport pickleimport matplotlib.pyplot as Pltcap = Cv2. Videocapture (0) while True: ret,frame = Cap.read () # We operations on the frame come here Gray = Cv2.cvtcolor ( Frame, Cv2. Color_bgr2gray) # Display The resulting frame cv2.imshow (' frame ', gray) if Cv2.waitkey (1) & 0xFF = = Ord ( ' Q '): Break # When everything done, release the Capturecap.release () cv2.destroyallwindows ()
2. Capture the picture from inside the camera
Import Cv2import numpy as Npimport pickleimport matplotlib.pyplot as Pltcap = Cv2. Videocapture (0) index = 0while True: ret,frame = Cap.read () # Our operations on the frame come here Gray = Cv2. Cvtcolor (frame, cv2. Color_bgr2gray) # Display The resulting frame cv2.imshow (' frame ', gray) if Cv2.waitkey (1) & 0xFF = = Ord ( ' P '): cv2.imwrite ("kk.jpg", frame) index = index + 1 if Cv2.waitkey (1) & 0xFF = = Ord (' q '): Break # Whe n everything done, release the Capturecap.release () cv2.destroyallwindows ()
Python OpenCV captures the camera and displays content