# import the necessary packages import time from picamera import PiCamera from piCameraPipeline import PiCameraPipeline from piCameraPipeline.PiCameraPipeline import RgbProcess from piCameraPipeline.PiCameraPipeline import RgbProcessorAdapter if ("__main__" == __name__): import cv2 # Unit test with to threaded processors class MyRgbProcess1(RgbProcess): def __init__(self, name, res=(320, 240, 3), numEntries=2, next=None): super(MyRgbProcess1, self).__init__(name, res, numEntries, next) def onProcess(self): data = self.read(1.0) if data is not None: gray = cv2.cvtColor(data,cv2.COLOR_BGR2GRAY) gray_blurred = cv2.medianBlur(gray,5) cv2.imshow('displayThread1', gray_blurred) cv2.waitKey(1) if self.next is not None: self.next.write(cv2.cvtColor(gray_blurred,cv2.COLOR_GRAY2BGR)) class MyRgbProcess2(RgbProcess): def __init__(self, name, res=(320, 240, 3), numEntries=2, next=None): super(MyRgbProcess2, self).__init__(name, res, numEntries, next) def onProcess(self): data = self.read(1.0) if data is not None: gray = cv2.cvtColor(data,cv2.COLOR_BGR2GRAY) # Canny edge detection canny = cv2.Canny(gray, 100, 50) cv2.imshow('displayThread2', canny) cv2.waitKey(1) # initialize the camera and stream camera = PiCamera() camera.resolution = (320, 240) camera.framerate = 60 frameDim = (camera.resolution[0], camera.resolution[1], 3) myProcessor2 = MyRgbProcess2('Process 2', (320, 240, 3), 4) myProcessor1 = MyRgbProcess1('Process 1', (320, 240, 3), 4, myProcessor2) myRgbProcessorAdapter = RgbProcessorAdapter(frameDim) myRgbProcessorAdapter.processorAdd(myProcessor1) camera.start_recording(myRgbProcessorAdapter, format="bgr") # allow the camera to warmup and start the FPS counter print("[INFO] sampling frames from `picamera` module...") time.sleep(10.0) camera.stop_recording() myProcessor1.stop() # do a bit of cleanup cv2.destroyAllWindows() camera.close()