Advertisement
Monitory

Take pictures

Dec 9th, 2022
1,177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.11 KB | None | 0 0
  1. import cv2
  2. import time
  3.  
  4. # Initialize the video capture
  5. cap = cv2.VideoCapture(0)
  6.  
  7. # Loop until the user presses the 'q' key
  8. while True:
  9.     # Capture the frame from the video capture
  10.     ret, frame = cap.read()
  11.  
  12.     # Check if the frame was successfully captured
  13.     if not ret:
  14.         continue
  15.  
  16.     # Display the frame in a window
  17.     cv2.imshow('Video Feed', frame)
  18.  
  19.     # Check if the user pressed the 's' key
  20.     key = cv2.waitKey(1)
  21.     if key == ord('s'):
  22.         # Get the current time
  23.         current_time = time.time()
  24.  
  25.         # Format the time as a string
  26.         time_string = time.strftime('%Y-%m-%d-%H-%M-%S', time.gmtime(current_time))
  27.  
  28.         # Generate the image file name using the time string
  29.         image_file_name = 'picture-{}.jpg'.format(time_string)
  30.  
  31.         # Save the frame to the image file
  32.         cv2.imwrite(image_file_name, frame)
  33.         print('Picture saved: {}'.format(image_file_name))
  34.  
  35.     # Check if the user pressed the 'd' key
  36.     elif key == ord('d'):
  37.         break
  38.  
  39. # Release the video capture and destroy the windows
  40. cap.release()
  41. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement