- PiCameraPipeline: refactored interfaces

git-svn-id: http://moon:8086/svn/software/trunk/projects/opencv@332 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
2016-10-22 08:22:07 +00:00
parent a572e2889e
commit a59fa57d9a
3 changed files with 75 additions and 57 deletions
+57 -44
View File
@@ -69,50 +69,13 @@ class RgbProcess(Fifo):
self.fps = None
self.frames = 0
# called on construction
def onConstruct(self):
pass
# called on first frame
def onFirstFrame(self):
self.start()
def onLastFrame(self):
self.stop()
# may be overriden to process in caller context
def onData(self, data):
self.write(data)
# thread main loop
def run(self):
self.started()
while not self.cancel:
self.process()
self.frames += 1
self.stopped()
def started(self):
print(self.name + ": started()")
self.fps = FPS().start()
if self.next is not None:
self.next.onFirstFrame()
def stopped(self):
self.fps.stop()
print(self.name + ": stopped()")
print(self.name + ": Processed " + str(self.frames) + " frames")
print(self.name + ": FPS = " + str(self.frames/self.fps.elapsed()))
# abstractmethod
def process(self):
pass
# Interface
# should be called to start thread
def start(self):
if not self.thread.is_alive():
self.thread.start()
# Interface
# should be called to exit thread
def stop(self):
if self.thread.is_alive():
@@ -121,6 +84,56 @@ class RgbProcess(Fifo):
self.cancel = True
self.thread.join()
# overideable method
# called on construction
def onConstruct(self):
pass
# overideable method
# called on first frame
def onFirstFrame(self):
pass
# overideable method
# called on last frame
def onLastFrame(self):
pass
# overideable method
def onProcess(self):
pass
# protected
# may be overriden to process in caller context
def onData(self, data):
self.write(data)
# private
# thread main loop
def run(self):
self.started()
while not self.cancel:
self.onProcess()
self.frames += 1
self.stopped()
# private
def started(self):
print(self.name + ": started()")
self.onFirstFrame()
self.fps = FPS().start()
if self.next is not None:
self.next.start()
# private
def stopped(self):
self.fps.stop()
self.onLastFrame()
print(self.name + ": stopped()")
print(self.name + ": Processed " + str(self.frames) + " frames")
print(self.name + ": FPS = " + str(self.frames/self.fps.elapsed()))
class RgbProcessorAdapter(object):
def __init__(self, resolution=(320, 240, 3)):
self.resolution = resolution
@@ -143,7 +156,7 @@ class RgbProcessorAdapter(object):
if self.size == 0:
for proc in self.processors:
proc.onFirstFrame()
proc.start()
self.fps = FPS().start()
@@ -169,7 +182,7 @@ class RgbProcessorAdapter(object):
print("Recorded " + str(frames) + " frames (" + str(self.size) + " bytes)")
print("FPS = " + str(frames/self.fps.elapsed()))
for proc in self.processors:
proc.onLastFrame()
proc.stop()
if ("__main__" == __name__):
import cv2
@@ -179,7 +192,7 @@ if ("__main__" == __name__):
def __init__(self, name, resolution=(320, 240, 3), numEntries=2, next=None):
super(MyRgbProcess1, self).__init__(name, resolution, numEntries, next)
def process(self):
def onProcess(self):
data = self.read(1.0)
if data is not None:
gray = cv2.cvtColor(data,cv2.COLOR_BGR2GRAY)
@@ -195,7 +208,7 @@ if ("__main__" == __name__):
def __init__(self, name, resolution=(320, 240, 3), numEntries=2, next=None):
super(MyRgbProcess2, self).__init__(name, resolution, numEntries, next)
def process(self):
def onProcess(self):
data = self.read(1.0)
if data is not None:
gray = cv2.cvtColor(data,cv2.COLOR_BGR2GRAY)