-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaptureImageFromCamera.py
93 lines (73 loc) · 2.01 KB
/
captureImageFromCamera.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import sys
import cv2
import time
import sys
# sinistra L1.png ...
# destra R1.png ...
def returnCameraIndexes():
# checks the first 10 indexes.
arr = []
for index in range(10):
cap = cv2.VideoCapture(index)
if cap.read()[0]:
arr.append(index)
cap.release()
return arr
def capture(cap, size=24):
(grabbed, frame) = cap.read()
img = cv2.resize(frame, (size, size))
#img = cv2.equalizeHist(img)
return img
def takePicture(cap, letter='', num=0, path='../data/images/', name=None):
resized = capture(cap)
if name is None:
path += letter + str(num) + '.png'
else:
path += name + '.png'
cv2.imwrite(path, resized)
def infinity(start=0):
i = start
while True:
yield i
i += 1
def get_start_index(path):
import os
files = os.listdir(path)
if 'data.json' in files:
files.remove('data.json')
if len(files) == 0:
return 0
else:
return max([int(x.split('.')[0][1:]) for x in files]) + 1
def main():
camera_indexes = returnCameraIndexes()
print(f'{camera_indexes=}')
path = '../data/images/'
loading = ['|', '/', '-', '\\']
caps = [cv2.VideoCapture(i) for i in camera_indexes]
if len(caps) == 0:
print('No camera found')
exit(1)
elif len(caps) == 1:
print('Only one camera found')
labels = ['L']
elif len(caps) == 2:
labels = ['L', 'R']
else:
print('More than two cameras found')
exit(1)
start_index = get_start_index(path)
print(f'{start_index=}')
for i in infinity(start_index):
try:
for cap, label in zip(caps, labels):
takePicture(cap, label, i, path)
time.sleep(0.5)
print(f'\rCapturing index {i=}, {loading[i % len(loading)]}', end='')
except KeyboardInterrupt:
print("KeyboardInterrupt")
break
for cap in caps:
cap.release()
if __name__ == "__main__":
main()