- 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
+16 -11
View File
@@ -32,7 +32,7 @@ ap.add_argument("-n", "--seconds", type=int, default=10,
help="# of seconds to run")
ap.add_argument("-r", "--framerate", type=int, default=30,
help="Framerate in frame/s")
ap.add_argument("-e", "--with-ev3", type=bool, default=False,
ap.add_argument("-e", "--with-ev3", type=str, default='off',
help="Framerate in frame/s")
ap.add_argument("-f", "--filename", type=str, default='piCamera',
help="Choose video source")
@@ -41,7 +41,7 @@ args = vars(ap.parse_args())
videoFile = args["filename"]
framerate = args["framerate"]
seconds = args["seconds"]
with_ev3 = args["with_ev3"]
with_ev3 = args["with_ev3"] == 'on'
class MyRgbProcess(RgbProcess):
def __init__(self, name, resolution=(320, 240, 3), numEntries=2, next=None, brick=None):
@@ -49,14 +49,13 @@ class MyRgbProcess(RgbProcess):
self.brick = brick
self.isVisible = False
def sendBrick(self, errorX, errorY):
if self.brick is not None:
data = struct.pack("f", errorX)
ev3.system_command.write_mailbox(self.brick, 'ErrorX', data)
data = struct.pack("f", errorY)
ev3.system_command.write_mailbox(self.brick, 'ErrorY', data)
def onFirstFrame(self):
self.sendBrick(0, 0)
def process(self):
def onLastFrame(self):
self.sendBrick(0, 0)
def onProcess(self):
data = self.read(1.0)
if data is not None:
gray = cv2.cvtColor(data,cv2.COLOR_BGR2GRAY)
@@ -108,13 +107,19 @@ class MyRgbProcess(RgbProcess):
if self.next is not None:
self.next.write(cv2.cvtColor(gray_blurred,cv2.COLOR_GRAY2BGR))
# Connect to brick
def sendBrick(self, errorX, errorY):
if self.brick is not None:
data = struct.pack("f", errorX)
ev3.system_command.write_mailbox(self.brick, 'ErrorX', data)
data = struct.pack("f", errorY)
ev3.system_command.write_mailbox(self.brick, 'ErrorY', data)
if with_ev3:
# Connect to brick
with ev3.EV3() as brick:
videoSource = VideoSource(videoFile, (width,height,3), framerate)
proc = MyRgbProcess('MyRgbProcess', (240, 320, 3), 4, brick)
proc = MyRgbProcess('MyRgbProcess', (240, 320, 3), 4, brick=brick)
videoSource.processorAdd(proc)
videoSource.start()
+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)
+2 -2
View File
@@ -13,7 +13,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)
@@ -29,7 +29,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)