camera_playground.py 865 B

1234567891011121314151617181920212223242526272829
  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 = [100, 0, 260, 450]
  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. frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
  14. frame = cv2.equalizeHist(frame)
  15. # Draw a green rectangle on the image
  16. cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
  17. cv2.imwrite("still_image.jpg", frame)
  18. print("Saved still_image.jpg")
  19. elif cmd == 'q':
  20. break
  21. picam2.stop()
  22. if __name__ == "__main__":
  23. main()