import cv2
from picamera2 import Picamera2

ESC = 27

picam2 = Picamera2()
config = picam2.create_preview_configuration(main={'size': (640, 480), 'format': 'RGB888'})
picam2.configure(config)
picam2.start()

while True:
    frame = picam2.capture_array()
    frame = cv2.flip(frame, -1)
    cv2.imshow('Camera', frame)

    if cv2.waitKey(1) == ESC:
        break
        
picam2.stop()
cv2.destroyAllWindows()
