import cv2, os, time from datetime import datetime def capture_images_from_webcam(output_folder, interval): while True: try: if not os.path.exists(output_folder): os.makedirs(output_folder) cap = cv2.VideoCapture(0) if not cap.isOpened(): time.sleep(interval) continue cap.set(cv2.CAP_PROP_FRAME_WIDTH, 3840) cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 2160) width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) img_counter = 0 while True: ret, frame = cap.read() if not ret: raise Exception(“Capture failed.”) timestamp = datetime.now().strftime(“%Y-%m-%d %H:%M:%S.%f”)[:-3] font = cv2.FONT_HERSHEY_SIMPLEX scale, colour, thick = 2, (0, 255, 255), 3 sz, _ = cv2.getTextSize(timestamp, font, scale, thick) pos = (width - sz[0] - 10, height - 10) cv2.putText(frame, timestamp, pos, font, scale, colour, thick, cv2.LINE_AA) img_path = os.path.join(output_folder, f”image_{img_counter:04d}.png”) cv2.imwrite(img_path, frame) img_counter += 1 cv2.imshow(‘Webcam’, frame) t0 = time.time() while time.time() - t0 < interval: if cv2.waitKey(1) & 0xFF == ord(‘q’): cap.release() cv2.destroyAllWindows() return except Exception: time.sleep(interval) # Parameters output_folder = r” interval = 60 capture_images_from_webcam(output_folder, interval) |