# import the necessary packages from imutils.video import FPS from picamera.array import PiRGBArray from picamera import PiCamera from threading import Thread import cv2 class PiVideoStream: def __init__(self, resolution=(320, 240), framerate=32): # initialize the camera and stream self.camera = PiCamera() self.camera.resolution = resolution self.camera.framerate = framerate self.camera.exposure_mode = 'auto' # self.camera.image_effect = 'colorbalance' # self.camera.image_effect_params = (0,1,1,1,0,0) self.camera.exposure_mode = 'auto' self.camera.awb_mode = 'off' self.camera.awb_gains = (1.5, 1.5) # self.camera.analog_gain = 1.75 # self.camera.digital_gain = 1.00 self.rawCapture = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True) # initialize the frame and the variable used to indicate # if the thread should be stopped self.frame = None self.stopped = False self.fps = None self.thread = None self.startup = True def printSettings(self): awb_gains = self.camera.awb_gains print ("sensor_mode : " + str(self.camera.sensor_mode)) print ("resolution : " + str(self.camera.resolution)) print ("framerate : " + str(self.camera.framerate)) print ("awb_mode : " + str(self.camera.awb_mode)) print ("awb_gains : " + str((float(awb_gains[0]), float(awb_gains[1])))) print ("analog_gain : " + str(float(self.camera.analog_gain))) print ("digital_gain : " + str(float(self.camera.digital_gain))) print ("iso : " + str(self.camera.iso)) print ("brightness : " + str(self.camera.brightness)) print ("contrast : " + str(self.camera.contrast)) print ("saturation : " + str(self.camera.saturation)) print ("exposure_mode : " + str(self.camera.exposure_mode)) print ("exposure_speed : " + str(self.camera.exposure_speed)) print ("exposure_compensation: " + str(self.camera.exposure_compensation)) print ("shutter_speed : " + str(self.camera.shutter_speed)) print ("meter_mode : " + str(self.camera.meter_mode)) print ("color_effects : " + str(self.camera.color_effects)) print ("image_effect : " + str(self.camera.image_effect)) print ("image_effect_params : " + str(self.camera.image_effect_params)) print ("sharpness : " + str(self.camera.sharpness)) def start(self): # start the thread to read frames from the video stream self.thread = Thread(target=self.update, args=()) self.thread.start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: if self.camera.exposure_mode == 'auto': if self.camera.analog_gain > 1.75 and self.camera.digital_gain == 1.0: self.camera.exposure_mode = 'off' self.printSettings() if self.startup: self.fps = FPS().start() self.startup = False # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.truncate(0) self.fps.update() # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: self.fps.stop() self.stream.close() self.rawCapture.close() self.camera.close() return def read(self): # return the frame most recently read return self.frame def stop(self): # indicate that the thread should be stopped self.stopped = True if self.thread != None: self.thread.join() def getfps(self): return self.fps