-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzone.py
More file actions
179 lines (148 loc) · 5.23 KB
/
zone.py
File metadata and controls
179 lines (148 loc) · 5.23 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import cv2, subprocess
from numpy import *
from utils import *
import projector
class Zone:
used_vdi = []
def __init__(self, idx, videodevices, arena):
self.arena = arena
self.id = idx # Zone ID
self.vdi = idx # Video ID
self.videodevices = videodevices # list of video devices
self.gridsize = (25, 18) # Grid size to apply to the zone.
self.v4l2ucp = -1 # Flag for v4l2ucp sub process
self.cap = -1 # Capture device object (OpenCV)
self.resolutions = [(1920,1080)] # new camera crashes if resolution is changed
self.ri = 0 # Selected resolution Index
self.srcwidth = 0
self.srcheight = 0
self.image = None
self.width = 0
self.height = 0
self.depth = 0
self.cameraMatrix = None
self.distCoefs = None
self.rvecs = None
self.tvecs = None
self.warped = False
self.warpwidth = 0
self.warpheight = 0
# Initialize the project for the zone.
# Set the projector image height to a little less than the native vertical resolution.
# Set the projector image width to the native horizontal resolution.
self.projector = projector.Projector(600, 800)
cv2.namedWindow("ZoneProjector"+str(idx), cv2.WND_PROP_FULLSCREEN)
self.initVideoDevice()
self.calibrated = False
return
def recalibrate(self):
self.calibrated = False
self.cameraMatrix = None
self.distCoefs = None
self.rvecs = None
self.tvecs = None
self.projector.outputCalibrationImage()
return
def getImage(self):
#skip if capture is disabled
if self.cap == -1:
return False
#get the next frame from the zones capture device
success, self.image = self.cap.read()
if not success:
print("Error reading from camera: "+str(self.vdi));
global ui
ui.exit = True
return False
self.height,self.width,self.depth = self.image.shape
self.warped = False
return True
def nextAvailableDevice(self):
self.vdi += 1
if self.vdi >= len(self.videodevices):
self.vdi = 0
if self.vdi != -1:
try:
self.used_vdi.index(self.vdi)
except ValueError:
return
self.nextAvailableDevice()
return
def updateVideoDevice(self):
self.close()
self.nextAvailableDevice()
if self.vdi > -1:
self.initVideoDevice()
else:
self.close()
return
def openV4l2ucp(self):
self.v4l2ucp = subprocess.Popen(['v4l2ucp',self.videodevices[self.vdi]])
return
def closeV4l2ucp(self):
if self.v4l2ucp != -1:
self.v4l2ucp.kill()
self.v4l2ucp = -1
return
def initVideoDevice(self):
if self.vdi != -1:
self.cap = cv2.VideoCapture(self.vdi)
self.srcwidth = self.resolutions[self.ri][0]
self.srcheight = self.resolutions[self.ri][1]
self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, self.srcwidth)
self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, self.srcheight)
self.used_vdi.append(self.vdi)
return
def close(self):
self.closeV4l2ucp()
self.closeCap()
cv2.destroyWindow("ZoneProjector"+str(self.id))
try:
self.used_vdi.remove(self.vdi)
except ValueError:
pass
return
def closeCap(self):
if self.cap != -1:
self.cap.release()
self.cap = -1
return
def updateResolution(self):
self.ri += 1
if self.ri >= len(self.resolutions):
self.ri = 0
x = self.resolutions[self.ri][0]
y = self.resolutions[self.ri][1]
self.close()
self.initVideoDevice()
for corner in self.corners:
corner.location = (-1, -1)
corner.symbolcenter = (-1,-1)
return
def warpImage(self):
# Undistort the frame
#img = cv2.cvtColor(self.image, cv2.COLOR_BGR2GRAY)
img = self.image
h, w = img.shape[:2]
newcameramtx, roi = cv2.getOptimalNewCameraMatrix(self.cameraMatrix, self.distCoefs, (w, h), 1, (w, h))
dst = cv2.undistort(img, self.cameraMatrix, self.distCoefs, None, newcameramtx)
self.image = dst
print "roi:", roi
# crop the image
x, y, w, h = roi
if w > 0 and h > 0:
dst = dst[y:y+h, x:x+w]
self.image = dst
self.height = h
self.width = w
#self.image = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)
# if self.image is not None:
# self.height, self.width, self.depth = self.image.shape
# Warp the image to be the optimal size
# warpedimage = zeros((self.warpheight, self.warpwidth, 3), uint8)
# dsize = (self.warpwidth, self.warpheight)
# cv2.warpPerspective(self.image, self.M, dsize, dst=warpedimage, borderMode=cv2.BORDER_TRANSPARENT)
# self.image = warpedimage
# self.height,self.width,self.depth = self.image.shape
# self.warped = True
return