Я пытаюсь написать код на Python, чтобы камера захватывала изображение каждые x минут в течение период всего y минут с использованием пакета cv2. К сожалению, при этом я получаю сообщение об ошибке.
Пожалуйста, найдите мой код и результат ниже:
Код: Выделить всё
import cv2
import os
import time
from datetime import datetime, timedelta
output_directory =
camera = cv2.VideoCapture(0)
if not camera.isOpened():
print("Error: Could not access the camera.")
else:
print("Camera found and initialized. Starting photo capture...")
end_time = datetime.now() + timedelta(hours=24)
capture_interval = 10 * 60 # Capture every 10 minutes (600 seconds)
try:
while datetime.now() < end_time:
ret, frame = camera.read()
print(f"Capture status: {ret}") # Print whether the capture was successful
if not ret:
print("Error: Failed to capture image.")
continue # Skip this iteration and try again
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
file_path = os.path.join(output_directory, f"photo_{timestamp}.jpg")
cv2.imwrite(file_path, frame)
print(f"Photo captured and saved at {file_path}")
time.sleep(capture_interval)
except KeyboardInterrupt:
print("Photo capture interrupted by user.")
finally:
camera.release()
print("Camera released. Photo capture ended.")
Я очень незнаком с доступом к внешним устройствам и управлением ими с помощью кода Python. Мы будем очень признательны за ввод кода, а также альтернативные способы сделать это, помимо Python.
Заранее извиняюсь за любое недоразумение с моей стороны — я новичок в программировании и устройствах.
Подробнее здесь: https://stackoverflow.com/questions/793 ... ics-camera
Мобильная версия