camera_playground.py 690 B

1234567891011121314151617181920212223242526272829
  1. import cv2
  2. def main():
  3. cap = cv2.VideoCapture(0)
  4. if not cap.isOpened():
  5. print("Cannot open camera")
  6. return
  7. print("Press 's' to save a still image. Press 'q' to quit.")
  8. while True:
  9. ret, frame = cap.read()
  10. if not ret:
  11. print("Can't receive frame (stream end?). Exiting ...")
  12. break
  13. cv2.imshow('Camera', frame)
  14. key = cv2.waitKey(1) & 0xFF
  15. if key == ord('s'):
  16. cv2.imwrite('still_image.jpg', frame)
  17. print("Image saved as still_image.jpg")
  18. elif key == ord('q'):
  19. break
  20. cap.release()
  21. cv2.destroyAllWindows()
  22. if __name__ == "__main__":
  23. main()