-
Notifications
You must be signed in to change notification settings - Fork 0
/
coordinator.py
60 lines (48 loc) · 1.9 KB
/
coordinator.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
class Coordinator():
"""This module comprises the camera and detector, to check if a set is complete."""
def __init__(self, camera, detector, usecase):
self.camera = camera
self.detector = detector
self.usecase = usecase
self.isRunning = False
def configureUsecase(self, config):
self.usecase.configure(config)
def configureDetector(self, config):
self.detector.configure(config['model'])
def start(self, frameCallback, usecaseCallback):
"""Video streaming generator function."""
# Start service only once.
if self.isRunning:
return
print("Starting Detection Service")
self.camera.open()
self.isRunning = True
while self.isRunning:
try:
frame = self.camera.read()
if (self.detector.name == 'TF2Detector'):
jpg, objects = self.detector.detect(frame)
else:
jpg = self.camera.encodeJPG(frame)
objects = self.detector.detect(frame)
self.usecase.run(jpg, objects, usecaseCallback)
frameCallback({ 'frame': jpg, 'objects': [obj._asdict() for obj in objects] })
except IOError:
self.isRunning = False
return
# Close the camera when service is stopped.
self.camera.close()
def stop(self):
if (self.isRunning == True):
print("Stopping Detection Service")
self.isRunning = False
def status(self):
return {
'isRunning': self.isRunning,
'frameSize': self.camera.resolution,
'inputSize': self.detector.inputSize,
'labels': self.detector.labels,
'usecase': self.usecase.status(),
'detector': self.detector.name,
'model': self.detector.modelDir
}