opencv-python open USB Or laptop front camera
Code
effect
Opencv-python Camera recording screen , Taking pictures
Opencv-python The camera records the video and saves it
Taking pictures
opencv-python open USB Or laptop front camera Codeamong video_index It's the camera number , Generally, the front camera is 0,USB A camera for 1 or 2.
import cv2def catch_video(name='my_video', video_index=0): # cv2.namedWindow(name) cap = cv2.VideoCapture(video_index) # Create a camera recognition class if not cap.isOpened(): # If no camera is detected , Report errors raise Exception('Check if the camera is on.') while cap.isOpened(): catch, frame = cap.read() # Read every picture cv2.imshow(name, frame) # stay window Show pictures on key = cv2.waitKey(10) if key & 0xFF == ord('q'): # Press q sign out break if cv2.getWindowProperty(name, cv2.WND_PROP_AUTOSIZE) < 1: # spot x sign out break # Release camera cap.release() cv2.destroyAllWindows()if __name__ == "__main__": catch_video()
effect Opencv-python Camera recording screen , Taking pictures Opencv-python The camera records the video and saves it import cv2 # Import opencv package video = cv2.VideoCapture(0) # Turn on the camera fourcc = cv2.VideoWriter_fourcc(*'XVID') # Format of video storage fps = video.get(cv2.CAP_PROP_FPS) # Frame rate # The width and height of the video size = (int(video.get(cv2.CAP_PROP_FRAME_WIDTH)), int(video.get(cv2.CAP_PROP_FRAME_HEIGHT)))out = cv2.VideoWriter('video.avi', fourcc, fps, size) # Video storage f=0while out.isOpened(): ret, img = video.read() # Start using the camera to read data , return ret by true,img Image read for if ret is False: # ret by false Then close exit() cv2.namedWindow('video', cv2.WINDOW_AUTOSIZE) # Create a file called video The window of cv2.imshow('video', img) # Capture the captured image in video Window display out.write(img) # Store the captured image print(f ,' ' , 'fps: ',fps) f+=1 # Press esc Key exit program if cv2.waitKey(1) & 0xFF == 27: video.release() # Turn off camera break
Taking pictures #coding:utf-8import cv2cap = cv2.VideoCapture(0)# Create a VideoCapture object flag = 1 # Set a flag , Used to output video information num = 1 # Increasing , Used to save the filename while(cap.isOpened()):# Cycle through every frame ret_flag, Vshow = cap.read() # Return two parameters , The first is bool Is it open normally , The second is the photo array , If only one is set, it becomes a tumple contain bool And pictures cv2.imshow("Capture_Test",Vshow) # Window display , The display name is Capture_Test k = cv2.waitKey(1) & 0xFF # Data delay per frame 1ms, The delay cannot be 0, Otherwise, the read result will be a static frame if k == ord('s'): # If a key is detected ‘s', Print string cv2.imwrite("/home/jie/ picture /VOC/image/"+ str(num) + ".jpg", Vshow) print(cap.get(3)); # Get the length and width print(cap.get(4)); print("success to save"+str(num)+".jpg") print("-------------------------") num += 1 elif k == ord('q'): # If a key is detected ‘q', sign out breakcap.release() # Release camera
The above is personal experience , I hope I can give you a reference , I also hope you can support the software development network .