-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcaptureImages.py
52 lines (38 loc) · 1.51 KB
/
captureImages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
Python script to capture images from multiple cameras at regular intervals and save them to disk.
This script iterates through camera indices 0 to 5, attempting to open each camera.
For each opened camera, it continuously captures frames and displays them. Images are saved to the specified directory ('images' by default) every 5 seconds.
"""
import cv2
import time
from pathlib import Path
num = 0
save_interval = 5 # Save image every 5 seconds
last_save_time = time.time()
# Specify the path tpo store captured images
directory = "images" # change this to "cali_result" in case test images needs to be captured
Path(directory).mkdir(parents=True, exist_ok=True)
for i in range(6): # Try indices 0 to 5
cap = cv2.VideoCapture(i)
if not cap.isOpened():
print("Camera not found at index", i)
continue
print("Camera found at index", i)
while cap.isOpened():
success, img = cap.read()
if not success:
print("Failed to read frame")
break
cv2.imshow('Img', img)
current_time = time.time()
# Save image if save interval has elapsed
if current_time - last_save_time >= save_interval:
cv2.imwrite('images/img' + str(num) + '.png', img)
print("Image saved as 'img" + str(num) + ".png'")
num += 1
last_save_time = current_time
k = cv2.waitKey(1)
if k == 27:
break
cap.release()
cv2.destroyAllWindows()