Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import time
- # Initialize the video capture
- cap = cv2.VideoCapture(0)
- # Loop until the user presses the 'q' key
- while True:
- # Capture the frame from the video capture
- ret, frame = cap.read()
- # Check if the frame was successfully captured
- if not ret:
- continue
- # Display the frame in a window
- cv2.imshow('Video Feed', frame)
- # Check if the user pressed the 's' key
- key = cv2.waitKey(1)
- if key == ord('s'):
- # Get the current time
- current_time = time.time()
- # Format the time as a string
- time_string = time.strftime('%Y-%m-%d-%H-%M-%S', time.gmtime(current_time))
- # Generate the image file name using the time string
- image_file_name = 'picture-{}.jpg'.format(time_string)
- # Save the frame to the image file
- cv2.imwrite(image_file_name, frame)
- print('Picture saved: {}'.format(image_file_name))
- # Check if the user pressed the 'd' key
- elif key == ord('d'):
- break
- # Release the video capture and destroy the windows
- cap.release()
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement