Search notes:

Python library: OpenCV

Capturing and saving frames from a webcam

Pressing space saves the current image from the webcam with an increasing counter in the filename, esc exits the loop and script.
import cv2

cam = cv2.VideoCapture(0)
# type(cam) # cv2.VideoCapture

cv2.namedWindow('frame-out')
cnt = 0

while True:

      ret, frame = cam.read()
   #  type(ret  )  #  bool
   #  type(frame)  #  numpy.ndarray
   #  frame.shape  # (height, width, nof-colors)  --  nof-colors likely to be 3

      cv2.imshow('frame-out', frame)

      if not ret:
         break

      keyPressed = cv2.waitKey(1)

      if   keyPressed % 256 == 27:
         # ESC pressed: exit loop

           break

      elif keyPressed % 256 == 32:
         # SPACE pressed: save image

          img_name = f'/tmp/cv2-capture-{cnt}.jpg'
          cv2.imwrite(img_name, frame)
          print(f'{img_name} written')

          cnt += 1

cam.release()
cv2.destroyAllWindows()

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1759561882, '216.73.216.149', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/languages/Python/libraries/OpenCV/index(74): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78