-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDirectLight.py
275 lines (221 loc) · 10.2 KB
/
DirectLight.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import numpy
import cv2
import lightpack
import time
import ConfigParser
import os
import sys
import subprocess as sp
from _winreg import *
import captureInput
import threading
PRISMATIK = 0
AMBIBOX = 1
class DirectLight(object):
def __init__(self):
self.scriptDir = os.path.dirname(os.path.realpath(__file__))
self.ResetPreferences()
def ResetPreferences(self):
config = ConfigParser.ConfigParser()
if os.path.isfile(self.scriptDir + '/DirectLight.ini'):
self.serverType = PRISMATIK
config.read(self.scriptDir + '/DirectLight.ini')
else:
self.serverType = AMBIBOX
config.read(sys.path[-1] + '/DirectLight.ini')
print 'serverType: ' + str(self.serverType)
self.host = config.get('Lightpack', 'host')
self.port = config.getint('Lightpack', 'port')
self.lpack = lightpack.lightpack(self.host, self.port)
#default capture values
self.videoX = config.getint('Capture', 'videoWidth')
self.videoY = config.getint('Capture', 'videoHeight')
self.videoFps = config.getfloat('Capture', 'videoFramerate')
self.videoDevice = config.getint('Capture', 'videoDevice')
self.videoDeviceName = config.get('Capture', 'videoDeviceName')
self.videoRenderX = config.getint('Render', 'renderWidth')
self.videoRenderY = config.getint('Render', 'renderHeight')
if self.serverType == PRISMATIK:
self.screenX = config.getint('Display', 'displayWidth')
self.screenY = config.getint('Display', 'displayHeight')
else:
self.screenX = 10000
self.screenY = 10000
self.cropTop = config.getint('Cropping', 'cropTop')
self.cropBottom = config.getint('Cropping', 'cropBottom')
self.cropLeft = config.getint('Cropping', 'cropLeft')
self.cropRight = config.getint('Cropping', 'cropRight')
tempServerType = config.get('Switches', 'serverType')
self.serverType = AMBIBOX if tempServerType.strip() == 'ambibox' else PRISMATIK
self.useFfmpeg = config.getboolean('Switches', 'useFfmpeg')
self.labColorspace = config.getboolean('Switches', 'labColorspace')
self.enableStandby = config.getboolean('Switches', 'enableStandby')
self.standbyTimeoutSeconds = config.getint('Switches', 'standbyTimeoutSeconds')
self.standby = False
def lockLpack(self):
while not self.lpack.lock():
print 'locking...'
time.sleep(1)
def unlockLpack(self):
while not self.lpack.unlock():
print 'locking...'
time.sleep(1)
def InitializeGrabbers(self):
frameLock = threading.Lock()
if self.serverType == PRISMATIK:
ffmpeg_bin = self.scriptDir + r'/ffmpeg.exe'
else:
ffmpeg_bin = sys.path[-1]+r'/ffmpeg.exe'
self.capture = captureInput.captureInput(self.videoDevice, self.videoDeviceName, self.videoX, self.videoY, self.videoFps, self.videoRenderX, self.videoRenderY, frameLock, ffmpeg_bin)
if self.useFfmpeg:
self.captureThread = threading.Thread(target=self.capture.captureFfmpeg)
else:
self.captureThread = threading.Thread(target=self.capture.captureOpenCv)
self.captureThread.daemon = True
self.captureThread.start()
self.lastFrame = numpy.uint8([[[]]])
cropWidth = self.videoRenderX-(self.cropLeft+self.cropRight)
cropHeight = self.videoRenderY-(self.cropTop+self.cropBottom)
self.xRatio = float(cropWidth)/float(self.screenX)
self.yRatio = float(cropHeight)/float(self.screenY)
self.grabbers=[]
if self.serverType == PRISMATIK:
self.grabbers = self.prismatikGrabbers()
else:
self.grabbers = self.ambiboxGrabbers()
self.signal = True
self.noSignalFrames = int(0)
self.standby = False
return True
def prismatikGrabbers(self):
retGrabbers=[]
leds = self.lpack.getLeds();
for led in leds:
if led.isspace():
continue
ledParts = led.split('-')
ledDims = ledParts[1].split(',')
ledIndex = int(ledParts[0])
left = int(float(ledDims[0])*self.xRatio)
top = int(float(ledDims[1])*self.yRatio)
newLedZone = ledZone(ledIndex+1, left, left+int(float(ledDims[2])*self.xRatio), top, top+int(float(ledDims[3])*self.yRatio))
retGrabbers.append(newLedZone)
return retGrabbers
def ambiboxGrabbers(self):
retGrabbers=[]
currentProfile = self.lpack.getProfile()
ledCount = self.lpack.getCountLeds()
reg = ConnectRegistry(None, HKEY_CURRENT_USER)
currentProfile = currentProfile.strip()
key = OpenKey(reg, r'Software\Server IR\Backlight\Profiles\%s' % currentProfile)
for i in range(0, int(ledCount)):
zBottom = QueryValueEx(key, 'Zone_Bottom_%s' % str(i))
zLeft = QueryValueEx(key, 'Zone_Left_%s' % str(i))
zRight = QueryValueEx(key, 'Zone_Right_%s' % str(i))
zTop = QueryValueEx(key, 'Zone_Top_%s' % str(i))
newLedZone = ledZone(i+1, int(float(zLeft[0])*self.xRatio), int(float(zRight[0])*self.xRatio), int(float(zTop[0])*self.yRatio), int(float(zBottom[0])*self.yRatio))
#print [newLedZone.ledIndex, newLedZone.left, newLedZone.right, newLedZone.top, newLedZone.bottom]
retGrabbers.append(newLedZone)
return retGrabbers
def ConnectToLightpack(self):
try:
self.lpack.connect()
return True
except: return False
def newSignal(self, frame):
newFrame = False
if self.lastFrame is None or len(self.lastFrame) == 1 or frame is None or len(frame) == 1:
if self.lastFrame is None and frame is None:
return False
self.lastFrame = frame
return True
newFrame = not numpy.allclose(frame, self.lastFrame)
self.lastFrame = frame
return newFrame
def startGrabbing(self):
if not self.ConnectToLightpack():
print "Unable to connect to AmbiBox!"
return
if not self.InitializeGrabbers():
print "Unable to set up grabbers"
return
if self.videoFps == 0:
self.videoFps = 30
loopThrottle = 1.0/float(self.videoFps)
standbyCheckInterval = 5
loopCounter = 0
standbyCheckThreshold = standbyCheckInterval*self.videoFps
noSignalThreshold = int(float(self.standbyTimeoutSeconds) / float(standbyCheckInterval))
cropWidth = self.videoRenderX-(self.cropLeft+self.cropRight)
cropHeight = self.videoRenderY-(self.cropTop+self.cropBottom)
while self.capture.getFrame() is None: #wait for capture to start
print 'waiting for first frame'
time.sleep(1)
continue
self.lockLpack()
while True:
frame = self.capture.getFrame()
if frame is None: #wait for capture to start
print 'waiting for first frame'
time.sleep(1)
continue
startTime = time.time()
loopCounter += 1
if loopCounter > standbyCheckThreshold:
if not self.newSignal(frame):
self.noSignalFrames += 1
else:
self.noSignalFrames = 0
loopCounter = 0
#Sleep until lights are on
currentStatus = self.lpack.getStatus()
currentStatus = currentStatus.strip()
#enter standby loop
if (self.enableStandby and self.noSignalFrames >= noSignalThreshold) or currentStatus != "on":
self.standby = True
print 'standing by'
self.lpack.unlock()
while True:
time.sleep(5)
currentStatus = self.lpack.getStatus()
currentStatus = currentStatus.strip()
if self.newSignal(self.capture.getFrame()) and currentStatus == "on":
print 'waking back up'
self.noSignalFrames = 0
self.lockLpack()
self.standby = False
break
cropFrame = frame[self.cropTop:(self.videoRenderY-self.cropBottom), self.cropLeft:(self.videoRenderX-self.cropRight)]
colorUpdate = []
for box in self.grabbers:
roi = cropFrame[box.top:box.bottom, box.left:box.right]
if self.labColorspace:
roi = cv2.cvtColor(roi, cv2.COLOR_BGR2LAB)
meanLabColor = cv2.mean(roi)
labColorInts = numpy.uint8([[[meanLabColor[0],meanLabColor[1],meanLabColor[2]]]])
meanColor = cv2.cvtColor(labColorInts, cv2.COLOR_LAB2BGR)
meanColor = meanColor[0][0]
else:
meanColor = cv2.mean(roi)
#if self.serverType == PRISMATIK:
# self.lpack.setColor(box.ledIndex, int(meanColor[2]), int(meanColor[1]), int(meanColor[0]))
#else:
colorUpdate.append((box.ledIndex, [int(meanColor[2]), int(meanColor[1]), int(meanColor[0])]))
#if self.serverType == AMBIBOX:
self.lpack.updateColors(colorUpdate)
endTime = time.time()-startTime
if endTime < loopThrottle:
time.sleep(loopThrottle-endTime)
def __del__(self):
self.capture.stopCapture()
self.lpack.unlock()
self.lpack.disconnect()
class ledZone:
def __init__(self, _ledIndex, _left, _right, _top, _bottom):
self.ledIndex = _ledIndex
self.left = _left
self.right = _right
self.top = _top
self.bottom = _bottom
directLight = DirectLight()
directLight.startGrabbing()