camera_playground.py 763 B

123456789101112131415161718192021222324252627
  1. from picamera2 import Picamera2
  2. import cv2
  3. def main():
  4. picam2 = Picamera2()
  5. picam2.configure(picam2.create_preview_configuration(main={"format": "BGR888", "size": (640, 480)}))
  6. picam2.start()
  7. x, y, w, h = [290, 140, 240, 170]
  8. print("Type 's' to save a still image. Type 'q' to quit.")
  9. while True:
  10. cmd = input("Command [s/q]: ").strip().lower()
  11. if cmd == 's':
  12. frame = picam2.capture_array()
  13. # Draw a green rectangle on the image
  14. cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
  15. cv2.imwrite("still_image.jpg", frame)
  16. print("Saved still_image.jpg")
  17. elif cmd == 'q':
  18. break
  19. picam2.stop()
  20. if __name__ == "__main__":
  21. main()