forked from InsperDynamics/K1D-RoboCup-RMRC-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencv_test.py
98 lines (93 loc) · 3.07 KB
/
opencv_test.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
94
95
96
97
98
import cv2
import numpy as np
resolution_horizontal = 800
resolution_vertical = 450
img1 = np.zeros((resolution_vertical, resolution_horizontal, 3), np.uint8)
img2 = np.zeros((resolution_vertical, resolution_horizontal, 3), np.uint8)
def list_ports():
is_working = True
dev_port = 0
working_ports = []
available_ports = []
while is_working:
camera = cv2.VideoCapture(dev_port)
if not camera.isOpened():
is_working = False
print("Port %s is not working." %dev_port)
else:
is_reading, img = camera.read()
w = camera.get(3)
h = camera.get(4)
if is_reading:
print("Port %s is working and reads images (%s x %s)" %(dev_port,h,w))
working_ports.append(dev_port)
else:
print("Port %s for camera ( %s x %s) is present but does not reads." %(dev_port,h,w))
available_ports.append(dev_port)
dev_port +=2
return available_ports,working_ports
def auto_connect():
is_working = True
dev_port = 0
working_ports = []
widths = []
heights = []
indexA = 0
indexB = 0
while is_working:
camera = cv2.VideoCapture(dev_port)
if not camera.isOpened():
is_working = False
print("Port %s is not working." %dev_port)
else:
is_reading, img = camera.read()
w = camera.get(3)
h = camera.get(4)
if is_reading:
print("Port %s is working and reads images (%s x %s)" %(dev_port,h,w))
working_ports.append(dev_port)
widths.append(int(w))
heights.append(int(h))
dev_port +=2
for i in range(len(widths)):
if widths[i] == 1920 and heights[i] == 1080:
indexA = working_ports[i]
elif widths[i] == 2304 and heights[i] == 1536:
indexB = working_ports[i]
return indexA, indexB
#list_ports()
indexA, indexB = auto_connect()
cap1 = cv2.VideoCapture(indexA)
cap1.set(cv2.CAP_PROP_FPS, 30)
print("Opened camera A: ", cap1.isOpened())
cap2 = cv2.VideoCapture(indexB)
cap2.set(cv2.CAP_PROP_FPS, 30)
print("Opened camera B: ", cap2.isOpened())
while True:
ret, img1_local = cap1.read()
ret2, img2_local = cap2.read()
if ret:
img1 = img1_local
else:
try:
cap1.release()
cap1 = cv2.VideoCapture(indexA)
cap1.set(cv2.CAP_PROP_FPS, 30)
except:
print("Camera A disconnected")
if ret2:
img2 = img2_local
else:
try:
cap2.release()
cap2 = cv2.VideoCapture(indexB)
cap2.set(cv2.CAP_PROP_FPS, 30)
except:
print("Camera B disconnected")
resized = cv2.resize(img1, (resolution_horizontal, resolution_vertical))
resized2 = cv2.resize(img2, (resolution_horizontal, resolution_vertical))
concated = cv2.hconcat([resized, resized2])
cv2.imshow('Camera A', resized)
cv2.imshow('Camera B', resized2)
cv2.imshow('Concat', concated)
cv2.waitKey(1)