-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created top-level model in 'model' directory (__init__.py)
- Loading branch information
1 parent
25506e4
commit 441b0c7
Showing
60 changed files
with
155 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from . import lane | ||
from . import sign | ||
from . import vic | ||
from . import sensing |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .camera import Camera |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import cv2 | ||
|
||
class Camera(): | ||
def __init__(self, source): | ||
self.capture = cv2.VideoCapture(source) | ||
|
||
@property | ||
def frameShape(self): | ||
height = int(self.capture.get(cv2.CAP_PROP_FRAME_HEIGHT)) | ||
width = int(self.capture.get(cv2.CAP_PROP_FRAME_WIDTH)) | ||
channels = 3 | ||
return (height, width, channels) | ||
|
||
def __call__(self): | ||
hasFrame, frame = self.capture.read() | ||
if hasFrame: | ||
return frame |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,69 @@ | ||
import cv2 | ||
import sys | ||
import threading | ||
import time | ||
|
||
class Subject: | ||
def __init__(self, source): | ||
self.capture = cv2.VideoCapture(source) | ||
self.frameShape = ( | ||
int(self.capture.get(cv2.CAP_PROP_FRAME_HEIGHT)), | ||
int(self.capture.get(cv2.CAP_PROP_FRAME_WIDTH)), | ||
3) | ||
import time | ||
import warnings | ||
|
||
self.observers = {} | ||
class Subject: | ||
def __init__(self, strategy, delay = 0): | ||
self.strategy = strategy | ||
self.delay = delay | ||
self.output = None | ||
|
||
def addObserver(self, name, observer): | ||
self.observers[name] = observer | ||
|
||
def removeObserver(self, name): | ||
del self.observers[name] | ||
|
||
def startCapture(self): | ||
while True: | ||
hasFrame, frame = self.capture.read() | ||
if hasFrame: | ||
self.output = frame | ||
time.sleep(0.05) | ||
|
||
def startDistribution(self, observer): | ||
print(f"Distribution started for {observer}...") | ||
while True: | ||
self.observers = {} | ||
|
||
def addObservers(self, *observers): | ||
for observer in observers: | ||
self.observers[observer] = threading.Thread(target=self.observerLoop, args=(observer,)) | ||
|
||
def start(self): | ||
self.isRunning = True | ||
self.thread = threading.Thread(target=self.loop) | ||
self.thread.start() | ||
for observer in self.observers.keys(): | ||
self.observers[observer] = threading.Thread(target=self.observerLoop, args=(observer,)) | ||
self.observers[observer].start() | ||
|
||
def stop(self): | ||
self.isRunning = False | ||
for observerThread in self.observers.values(): | ||
observerThread.join() | ||
self.thread.join() | ||
|
||
def removeObserver(self, observer): | ||
if self.isRunning: | ||
warnings.warn("Cannot remove observer while running", RuntimeWarning) | ||
else: | ||
del self.observers[observer] | ||
|
||
def loop(self): | ||
while self.isRunning: | ||
self.output = self.strategy() | ||
time.sleep(self.delay) | ||
|
||
def observerLoop(self, observer): | ||
while self.isRunning: | ||
if self.output is not None: | ||
observer(self.output) | ||
|
||
def startThreadedCapture(self): | ||
threading.Thread(target=self.startCapture).start() | ||
for observer in self.observers.values(): | ||
threading.Thread(target=self.startDistribution, args=(observer,)).start() | ||
|
||
def stop(): | ||
# self.captureThread.terminate() | ||
# self.captureThread = None | ||
self.capture.release() | ||
time.sleep(self.delay) | ||
|
||
|
||
if __name__ == '__main__': | ||
subject = Subject(lambda: "test", delay = 0.05) | ||
|
||
def a(output): print(output) | ||
def b(output): print(output + " me") | ||
|
||
subject.addObservers(a, b) | ||
subject.start() | ||
time.sleep(0.1) | ||
# Does not remove observer | ||
print("trying to remove observer a") | ||
subject.removeObserver(a) | ||
time.sleep(0.1) | ||
subject.stop() | ||
# Works without warning | ||
print("trying to remove observer a") | ||
subject.removeObserver(a) | ||
subject.start() | ||
time.sleep(0.3) | ||
subject.stop() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from frame import Model, Subject | ||
from . import lane | ||
from . import sign | ||
from . import vehicle | ||
import ui_bridge as ui | ||
|
||
def generateForCamera(camera): | ||
model = {} | ||
|
||
model['sensing'] = Model() | ||
model['sensing'].addNode( | ||
name = "Camera", | ||
category = "framer", | ||
node = Subject( | ||
strategy = camera, | ||
delay = 0)) | ||
|
||
model['lane'] = lane.generate(frameShape = camera.frameShape) | ||
model['sign'] = sign.generate(frameShape = camera.frameShape) | ||
model['vehicle'] = vehicle.generate() | ||
|
||
model['sensing']("Camera", "framer").addObservers(model['lane'].head, model['sign'].head) | ||
model['sign']("NMS", "interpreted").addObservers(model['vehicle']("signs", "storage")) | ||
model['lane']("TotalError", "interpreted").addObservers(model['vehicle']("controlPackager", "control")) | ||
|
||
return model | ||
|
||
|
||
def toButtonCategories(model): | ||
categories = ui.Categories() | ||
|
||
category = ui.ButtonCategory("toggle") | ||
category.addButtons(model.category("annotator").keys()) | ||
categories.addCategory("Annotation", category) | ||
|
||
category = ui.ButtonCategory("radio") | ||
category.addButtons(model.category("framer").keys()) | ||
categories.addCategory("Frame", category) | ||
|
||
return categories |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters