OpenCV 笔记(python)

                     

贡献者: addis

  • 本文处于草稿阶段。

   以下代码可用于每秒钟拍摄两张图片保存到指定文件夹

代码 1:camera.py
import cv2
import datetime
import time
import os

def capture_images(folder_path):
  # Create the folder if it doesn't exist
  if not os.path.exists(folder_path):
      os.makedirs(folder_path)

  # Start the webcam
  cap = cv2.VideoCapture(0)

  try:
      while True:
        # Capture the first image
        ret1, img1 = cap.read()
        if ret1:
          filename1 = f"{folder_path}/" \
  			f"{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}_1.jpg"
          cv2.imwrite(filename1, img1)

        # Wait for half a second
        time.sleep(0.5)

        # Capture the second image
        ret2, img2 = cap.read()
        if ret2:
          filename2 = f"{folder_path}/" \
          f"{datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}_2.jpg"
          cv2.imwrite(filename2, img2)

        # Wait for half a second
        time.sleep(0.5)

  except KeyboardInterrupt:
      # Release the webcam when interrupted
      cap.release()

if __name__ == "__main__":
  capture_images("path/to/your/folder")

                     

© 小时科技 保留一切权利