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.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 def __init__(self, resolution=(320, 240), framerate=32, rotate=0): # initialize the camera and stream self.camera = PiCamera() self.camera.rotation = rotate self.camera.resolution = resolution self.camera.framerate = framerate 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 def start(self): # start the thread to read frames from the video stream t = Thread(target=self.update, args=()) t.daemon = True t.start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: 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
class PiVideoStream: def __init__(self, resolution=(640, 480), framerate=48): # initialize the camera and stream self.camera = PiCamera() #self.camera.video_stabilization = True self.camera.resolution = resolution self.camera.framerate = framerate 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 def start(self): # start the thread to read frames from the video stream t = Thread(target=self.update, args=()) t.daemon = True t.start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: 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 # Settings for consistent images capture # Must be called some times after the camera is started def consistent(self): self.camera.shutter_speed = self.camera.exposure_speed self.camera.exposure_mode = 'off' g = self.camera.awb_gains self.camera.awb_mode = 'off' self.camera.awb_gains = g
class PiVideoStream: font = cv2.FONT_HERSHEY_SIMPLEX def __init__(self, resolution=(208, 160), framerate=32): self.camera = PiCamera() self.camera.resolution = resolution self.camera.framerate = framerate self.rawCapture = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True) self.frame = None self.stopped = False self.raw = 0 def start(self): Thread(target=self.update, args=()).start() return self def update(self): for f in self.stream: self.frame = f.array self.rawCapture.truncate(0) if (self.stopped): self.stream.close() self.rawCapture.close() self.camera.close() return def read(self): return self.frame def stop(self): self.stopped = True def PID(self, linha, pidvalues, target, clmin, clmax): f_bgr = self.frame f_bw = cv2.cvtColor(f_bgr, cv2.COLOR_BGR2GRAY) ret, mask = cv2.threshold(f_bw, 50, 255, cv2.THRESH_BINARY_INV) curva = 0 for i in pidvalues: tmp = centroLinha(mask[i, linha[0]:linha[1]:linha[2]], clmin, clmax, target, linha[0], linha[2]) cv2.circle(f_bgr, (tmp, i), 10, (0, 255, 0), 2) if (tmp != 0): curva = curva + tmp - 104 self.raw = curva cv2.putText(f_bgr, str(curva), (90, 20), self.font, 0.8, (0, 255, 0), 2, cv2.CV_AA) return f_bgr def PIDraw(self): return self.raw
class PiVideoStream: def __init__(self, resolution=(CAMERA_WIDTH, CAMERA_HEIGHT), framerate=CAMERA_FRAMERATE, rotation=0, hflip=False, vflip=False): # initialize the camera and stream self.camera = PiCamera() self.camera.resolution = resolution self.camera.rotation = rotation self.camera.framerate = framerate self.camera.hflip = hflip self.camera.vflip = vflip 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 def start(self): # start the thread to read frames from the video stream t = Thread(target=self.update, args=()) t.daemon = True t.start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: 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
class PiVideoStream: def __init__(self, resolution=(320, 240), format="bgr", framerate=30): # initialize the camera and stream self.camera = PiCamera() time.sleep(.2) #let camera init self.camera.resolution = resolution self.camera.framerate = framerate self.camera.framerate = 30 self.camera.contrast = 30 #40 self.camera.saturation = 50 #70 self.camera.brightness = 30 self.camera.awb_mode = 'off' self.camera.awb_gains = (1.5, 1.21) self.camera.shutter_speed = 200 #self.camera.exposure_mode = 'auto' self.rawCapture = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCapture, format=format, use_video_port=True) time.sleep(.2) #let camera init # initialize the frame and the variable used to indicate # if the thread should be stopped self.frame = None self.stopped = False def start(self): # start the thread to read frames from the video stream Thread(target=self.update, args=()).start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: 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): # flag thread to stop self.stopped = True
class PiVideoStream: def __init__(self, resolution=(320, 240), framerate=32, **kwargs): # initialize the camera self.camera = PiCamera() # set camera parameters self.camera.resolution = resolution self.camera.framerate = framerate # set optional camera parameters (refer to PiCamera docs) for (arg, value) in kwargs.items(): setattr(self.camera, arg, value) # initialize the stream 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 def start(self): # start the thread to read frames from the video stream t = Thread(target=self.update, args=()) t.daemon = True t.start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: 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
class PiCamera(BaseCamera): def __init__(self, image_w=160, image_h=120, image_d=3, framerate=20): from picamera.array import PiRGBArray from picamera import PiCamera resolution = (image_w, image_h) # initialize the camera and stream self.camera = PiCamera() # PiCamera gets resolution (height, width) self.camera.resolution = resolution self.camera.framerate = framerate self.rawCapture = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCapture, format="rgb", use_video_port=True) # initialize the frame and the variable used to indicate # if the thread should be stopped self.frame = None self.on = True self.image_d = image_d print('PiCamera loaded.. .warming camera') time.sleep(2) def run(self): f = next(self.stream) frame = f.array self.rawCapture.truncate(0) if self.image_d == 1: frame = rgb2gray(frame) return frame def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.truncate(0) if self.image_d == 1: self.frame = rgb2gray(self.frame) # if the thread indicator variable is set, stop the thread if not self.on: break def shutdown(self): # indicate that the thread should be stopped self.on = False print('stoping PiCamera') time.sleep(.5) self.stream.close() self.rawCapture.close() self.camera.close()
class PiVideoStream: def __init__(self, resolution=(320, 240), framerate=32, format="bgr", **kwargs): # initialize the camera self.camera = PiCamera(resolution=resolution, framerate=framerate, **kwargs) # initialize the stream self.rawCapture = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCapture, format=format, use_video_port=True) # initialize the frame and the variable used to indicate # if the thread should be stopped self.frame = None self.should_stop = False self.is_stopped = True def start(self): # start the thread to read frames from the video stream t = Thread(target=self.update, args=()) t.daemon = True t.start() self.is_stopped = False return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.should_stop: print("PiVideoStream: closing everything") self.stream.close() self.rawCapture.close() self.camera.close() self.should_stop = False self.is_stopped = True return def read(self): # return the frame most recently read return self.frame def stop(self): # indicate that the thread should be stopped self.should_stop = True # Block in this thread until stopped while not self.is_stopped: pass
class PiVideoStream: def __init__(self, resolution=(320, 240), format = "bgr", framerate=30): # initialize the camera and stream self.camera = PiCamera() time.sleep(.2) #let camera init self.camera.resolution = resolution self.camera.framerate = framerate self.camera.framerate = 30 self.camera.sharpness = -100 self.camera.contrast = 50 self.camera.saturation = 100 self.camera.brightness = 40 self.camera.awb_mode = 'off' self.camera.awb_gains = (1.29, 1.44) self.camera.shutter_speed = 800 self.camera.exposure_mode = 'night' self.rawCapture = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCapture, format=format, use_video_port=True) time.sleep(.2) #let camera init # initialize the frame and the variable used to indicate # if the thread should be stopped self.frame = None self.stopped = False def start(self): # start the thread to read frames from the video stream Thread(target=self.update, args=()).start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: 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): # flag thread to stop self.stopped = True
class PiVideoStream: def __init__(self, resolution=(320,240), framerate=60): # initialize the camera and stream self.camera = PiCamera() self.camera.resolution = resolution #self.camera.hflip = True self.camera.framerate = framerate self.rawCapture = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True) self.camera.hflip=False self.camera.vflip=False # initialize the frame and the variable used to indicate # if the thread should be stopped self.frame = None self.stopped = False def start(self): # start the thread to read frames from the video stream Thread(target=self.update, args=(), daemon=False).start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array #hsv = cv2.cvtColor(self.frame,cv2.COLOR_BGR2HSV) self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: self.stream.close() self.rawCapture.close() self.camera.close() return def hflip(self): self.camera.hflip= !self.camera.hflip def vflip(self): self.camera.vflip= !self.camera.vflip 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 self.join()
class ListVideoStream: """ Thread to capture and store images in a list until stopped """ def __init__(self, resolution=(CAMERA_WIDTH, CAMERA_HEIGHT), framerate=CAMERA_FRAMERATE, rotation=0, hflip=CAMERA_HFLIP, vflip=CAMERA_VFLIP): """ initialize the camera and stream """ self.camera = PiCamera() self.camera.resolution = resolution self.camera.rotation = rotation self.camera.framerate = framerate self.camera.hflip = hflip self.camera.vflip = vflip self.camera.video_stabilization = True # todo: test HFOV 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_list = [] self.stopped = False def start(self): """ start the thread to read frames from the video stream """ t = Thread(target=self.update, args=()) t.daemon = True t.start() return self def update(self): """ keep looping infinitely until the thread is stopped """ for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame_list.append(f.array) self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: self.stream.close() self.rawCapture.close() self.camera.close() return def get_frames(self): """ return the frame most recently read """ return self.frame_list def stop(self): """ indicate that the thread should be stopped """ self.stopped = True
class PiVideoStream: #320*240 original def __init__(self, resolution=(320, 220), framerate=90): # initialize the camera and stream self.camera = PiCamera() self.camera.resolution = resolution self.camera.framerate = framerate #brightness 70 y contrst 50 #self.camera.ISO = 800 self.camera.awb_mode = 'off' rg, bg = (1.7, 1.5) self.camera.awb_gains = (rg, bg) self.camera.sharpness = 100 self.camera.brightness = 70 self.camera.contrast = 40 self.camera.shutter_speed = 500 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 def start(self): # start the thread to read frames from the video stream Thread(target=self.update, args=()).start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: 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
class PiVideoStream: def __init__(self, resolution=(2592, 240), framerate=8): # initialize the camera and stream self.queue = Queue() self.frame_num = 0 self.camera = PiCamera(sensor_mode=4) self.camera.resolution = resolution self.camera.framerate = framerate self.camera.shutter_speed = 100 self.camera.iso = 50 self.camera.image_effect = 'none' self.camera.awb_gains = (0.9, 0.0) self.camera.awb_mode = 'off' time.sleep(5.0) self.camera.exposure_mode = 'off' self.rawCapture = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True) self.frame = None self.stopped = False def start(self): # start reading frames from the video stream self.update() return self def update(self): # keep looping infinitely until the video stream is stopped print "Starting Update" for f in self.stream: # grab the frame from the stream and put in queue self.frame = f.array self.queue.put(self.frame) self.frame_num += 1 # clear the stream buffer self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the video stream # and free up camera resources if self.stopped: 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 video stream should be stopped self.stopped = True
class PiVideoStream: def __init__(self, resolution=(320, 240), framerate=32, awb_gains=0, exposure_mode='auto'): # initialize the camera and stream self.camera = PiCamera() self.camera.resolution = resolution self.camera.framerate = framerate if (awb_gains != 0): self.camera.awb_mode = 'off' self.camera.awb_gains = awb_gains self.camera.exposure_mode = exposure_mode 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 def start(self): # start the thread to read frames from the video stream t = Thread(target=self.update, args=()) t.daemon = True t.start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: 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
class PiVideoStream: def __init__(self, resolution=(320, 240), framerate=32, rotation=0, b=50, c=50): # initialize the camera and stream self.camera = PiCamera() self.camera.rotation = rotation self.camera.brightness = b self.camera.contrast = c # self.camera.image_effect='sketch' self.camera.resolution = resolution self.camera.framerate = framerate 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 def start(self): # start the thread to read frames from the video stream Thread(target=self.update, args=()).start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: self.stream.close() self.rawCapture.close() self.camera.close() return def read(self): # return the frame most recently read return self.frame def release(self): # indicate that the thread should be stopped self.stopped = True
class streamVision(): IM_WIDTH = proto.CAM_H IM_HEIGHT = proto.CAM_V def __init__(self, resolution=(IM_WIDTH, IM_HEIGHT), framerate=20): self.camera = PiCamera() self.camera.resolution = (self.IM_WIDTH, self.IM_HEIGHT) #White balance adjustment! self.camera.awb_mode = 'off' #Options: 'off', 'sunlight', 'cloudy', 'shade', 'tungsten', 'incandescent', # 'flash', 'horizon', 'auto', 'fluorescent' self.camera.awb_gains = (1.3, 2.5) #Need to set this if awb_mode = off self.camera.vflip = False self.camera.hflip = False #self.camera.rotation = 180 # self.camera.framerate = 50 #Not needed... most likely self.rawCap = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCap, format="bgr", use_video_port=True) self.frame = None self.stopped = False def start(self): Thread(target=self.update, args=()).start() return self def take_img(self): number_of_images = 30 for idx in range(number_of_images): self.camera.capture('image%s.jpg' % idx) print('Image Captured') time.sleep(1) def update(self): for f in self.stream: self.frame = f.array self.rawCap.truncate(0) if self.stopped: self.stream.close() self.rawCap.close() self.camera.close() return def read(self): print('Frame Requested') return self.frame def stop(self): self.stopped = True
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.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 def start(self): # start the thread to read frames from the video stream Thread(target=self.update, args=()).start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame #self.frame = f.array self.frame = cv2.cvtColor(np.array(f.array), cv2.COLOR_BGR2GRAY) #cv2.imshow("see me!", self.frame) #img = cv2.imread(self.frame) #img = cv2.resize(img,(32,32)) #img = np.reshape(img,[1,32,32,3]) #classes = model.predict(img) #print classes self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: self.stream.close() self.rawCapture.close() self.camera.close() return def read(self): # return the frame most recently read return self.frame def write(self, frame, label): cv2.imwrite("images/{}/frame_{}.jpg".format(label,time.time()), frame) def stop(self): # indicate that the thread should be stopped self.stopped = True
class CameraStream: 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.hflip = True self.camera.vflip = True 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.queue = Queue(maxsize = 4) def start(self): # start the thread to read frames from the video stream t = Thread(target=self.update, args=()) t.daemon = True t.start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array #self.queue.put(f.array) self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: self.stream.close() self.rawCapture.close() self.camera.close() return def read(self): # return the frame most recently read #frame = self.queue.get() #self.queue.task_done() return self.frame #return frame def stop(self): # indicate that the thread should be stopped self.stopped = True
class PiCameraManager(object): """ Wrapper for the PiCamera """ def __init__(self): self.stopped = True self.camera = PiCamera() self.camera.resolution = (320, 240) self.camera.framerate = 24 self.camera.vflip = True self.camera.hflip = True # I need this to be right side up self.camera.rotation = 540 self.data_container = PiRGBArray(self.camera, size=(320, 240)) self.stream = self.camera.capture_continuous(self.data_container, format="bgr", use_video_port=True) #Create frame self.frame = None def Preview(self): pos_x = 100 pos_y = 100 size_x = 300 size_y = 400 self.camera.start_preview(fullscreen=False, window=(pos_x, pos_y, size_x, size_y)) def Start(self): self.stopped = False Thread(target=self.StartCamera, args=()).start() def StartCamera(self): for f in self.stream: self.frame = f.array self.data_container.truncate(0) #print("Streaming") if self.stopped: self.stream.close() self.data_container.close() self.camera.close() print("Stopped") def Stop(self): self.stopped = True print("Stop Called") def Read(self): #self.camera.capture('/home/pi/Desktop/mdl.jpg') return self.frame[0:224, 48:272, :]
class PiVideoStream(AbstractVideoStream): def __init__(self, resolution=(320, 320), framerate=60): self.name = "Picam" # initialize the camera and stream self.camera = PiCamera() self.camera.resolution = resolution self.camera.framerate = framerate 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 = True def get_resolution(self): return self.camera.resolution def start(self): # start the thread to read frames from the video stream self.stopped = False t = Thread(target=self.update, args=()) t.daemon = True print "Starting Camera..." t.start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: 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
class PiVideoStream(object): def __init__(self, resolution=(320, 240), format='rgb', framerate=24, led=True): self.camera_led = led self.camera = picamera.PiCamera() self.camera.resolution = resolution self.camera.framerate = framerate self.rawCapture = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCapture, format=format, use_video_port=True) self.frame = None self.running = False #dirty but currently the only way so it works immediately self.start() self.stop() def start(self): printD("videostream: start") # start the thread to read frames from the video stream if self.camera_led: self.camera.led = self.camera_led self.running = True Thread(target=self.update, args=()).start() sleep(0.2) #give videostream some time to start befor frames can be read def stop(self): printD("videostream: stop") # indicate that the thread should be stopped self.camera.led = False self.running = False def update(self): # keep looping infinitely until the thread is stopped for frameBuf in self.stream: # grab the frame from the stream and clear the stream in preparation for the next frame self.frame = frameBuf.array self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread if self.running == False: return def read(self): # return the frame most recently read return self.frame def quit(self): # resource camera resources try: self.running = False self.stream.close() self.rawCapture.close() self.camera.close() except: pass
class PiCameraStream(object): """ Capture frames from PiCamera using separated thread """ def __init__(self, resolution=(300, 300), preview=False, force_sleep=None): """ :param resolution: :param preview: """ self.camera = PiCamera() self.camera.resolution = resolution self.camera.vflip = True self.camera.hflip = True self.force_sleep = force_sleep self.rgb_array = PiRGBArray(self.camera, size=self.camera.resolution) self.stream = self.camera.capture_continuous(self.rgb_array, format="bgr", use_video_port=True) self.frame = None self.stopped = False self.preview = preview if self.preview: self.camera.start_preview() def start(self): Thread(target=self.continuous_capture).start() def stop(self): self.stopped = True def continuous_capture(self): for f in self.stream: self.frame = f.array self.rgb_array.truncate(0) if self.stopped: self.stream.close() self.rgb_array.close() self.camera.close() return def read(self): if self.force_sleep is not None: sleep(int(self.force_sleep)) return self.frame
class PiCamera: def __init__(self, resolution, framerate, format): from picamera.array import PiRGBArray from picamera import PiCamera resolution = (resolution[1], resolution[0]) # initialize the camera and stream self.camera = PiCamera() # PiCamera gets resolution (height, width) self.camera.resolution = resolution self.camera.framerate = framerate self.format = format self.rawCapture = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCapture, format=self.format, use_video_port=True) # initialize the frame and the variable used to indicate # if the thread should be stopped self.frame = None self.on = True logger.info('PiCamera loaded, warming camera...') time.sleep(1) def run(self): f = next(self.stream) frame = f.array self.rawCapture.truncate(0) return frame def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread if not self.on: break def shutdown(self): # indicate that the thread should be stopped self.on = False logger.info('Stopping PiCamera') time.sleep(.5) self.stream.close() self.rawCapture.close() self.camera.close()
def trackingMain(pipein): global cntrx global x_cmd global center_width global extra global xgood global y_cmd global fire_cmd global ygood global missile_count global cntry global center_height global obj_min_w global rawCapture count_center = 0 dir_conn, turret_conn = Pipe() p = Process(target=turret_mot_ctrlwp.command_sel, args=(turret_conn, )) p.start() print("turret motor control online!") x_aim_thread = x_Thread(.06) x_aim_thread.start() y_aim_thread = y_Thread(.06) y_aim_thread.start() send_cmd_thread = send_Thread(dir_conn, pipein, .6) #might be able to speed this up. send_cmd_thread.start() #print(up_lim_pin) camera = PiCamera() #camera.hflip = True ## probably need to remove these camera.vflip = True ## camera.resolution = (cam_width, cam_height) camera.framerate = 32 rawCapture = PiRGBArray(camera, size=(cam_width, cam_height)) print("setting up picamera. 2 second wait") time.sleep(2) while (1): tracking(camera) rawCapture.close() camera.close() camera.release() cv2.destroyAllWindows()
class VideoStream(object): def __init__(self): self.camera = PiCamera() self.camera.resolution = (1920, 1080) self.preview_window = (0, 0, 640, 480) self.camera.framerate = 15 self.stream = None self.rgb_array = PiRGBArray(self.camera, size=self.camera.resolution) # initialize the frame and the variable used to indicate # if the thread should be stopped self.frame = None self.preview_stopped = False self.capture_stopped = False def capture_hi_res(self): self.stream = self.camera.capture_continuous(self.rgb_array, use_video_port=True, splitter_port=2, format='bgr') Thread(target=self.capture_raw, args=()).start() def capture_raw(self): for f in self.stream: self.frame = f.array self.rgb_array.truncate(0) if self.capture_stopped: self.stream.close() self.rgb_array.close() self.camera.close() def stop_capture(self): self.capture_stopped = True def preview_stream(self): # start the thread to read frames from the video stream Thread(target=self.preview, args=()).start() def preview(self): self.camera.start_preview(fullscreen=False, window=self.preview_window) if self.preview_stopped: self.camera.stop_preview() def read(self): return self.frame def stop_preview(self): self.preview_stopped = True
class PiVideoStream(object): def __init__(self, resolution=(320, 240), format='rgb', framerate=24, led=True): self.camera_led = led self.camera = picamera.PiCamera() self.camera.resolution = resolution self.camera.framerate = framerate self.rawCapture = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCapture, format=format, use_video_port=True) self.frame = None self.running = False self.start() self.stop() def start(self): printD("videostream: start") #Thread um die Frames der Kamera zu lesen if self.camera_led: self.camera.led = self.camera_led self.running = True Thread(target=self.update, args=()).start() sleep(0.2) #Bisschen Zeit zum starten geben def stop(self): printD("videostream: stop") self.camera.led = False self.running = False def update(self): # Lasse laufen bis Thread gestoppt wird for frameBuf in self.stream: # Bekomme Frame vom Stream und leere den stream für nächsten frame self.frame = frameBuf.array self.rawCapture.truncate(0) # Stoppe Thread if self.running == False: return def read(self): # Gebe Frame zurück return self.frame def quit(self): # Schieße Kamera try: self.running = False self.stream.close() self.rawCapture.close() self.camera.close() except: pass
class PiStreamAndButton: def __init__(self, resolution=(320, 240), framerate=20, channel=37): # initialize the camera and stream self.camera = PiCamera() self.camera.resolution = resolution self.camera.framerate = framerate 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 # initialte the Button self.button = modules.PiButton.Button(channel) def start(self): # start the thread to read frames from the video stream Thread(target=self.update, args=()).start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: self.stream.close() self.rawCapture.close() self.camera.close() return def read(self): # return the frame most recently read return self.frame def getButtonCount(self): return self.button.count def stop(self): # indicate that the thread should be stopped self.stopped = True
def take_raspicampic(i): try: camera.close() print("camera closed") except: print("camera was not open") try: from picamera import PiCamera from picamera.array import PiRGBArray camera = PiCamera() except: print("camera was not closed last time or is still in use") #camera.close() #rawCapture.close() print("Raspberry camera") # import the necessary packages #camera = PiCamera() print("Raspberry Camera loaded") # following camera settings are not needed #x_res = 640 #y_res = 480 x_res = 320 y_res = 240 #x_res = y_res = 64 camera.resolution = (x_res, y_res) camera.framerate = 32 camera.exposure_mode = 'sports' # if the iso is set, pictures will look more similar camera.iso = 100 camera.shutter_speed = 1 #camera.vflip = True # alternative rawCapture = PiRGBArray(camera) rawCapture = PiRGBArray(camera, size=(x_res, y_res)) # allow the camera to warmup time.sleep(0.1) camera.capture(rawCapture, format="bgr") image = rawCapture.array camera.close() rawCapture.close() #is this even possible? # display the image on screen and wait for a keypress #cv2.imshow("Image", image) timestr = time.strftime("%Y%m%d-%H%M%S") filename = f"position_{i}_{timestr}.jpg" cv2.imwrite(filename, image) # if the camera is not closed, it cannot be opened again... # a future version might not open / close for every picture return filename
class PiVideoStream: # Orange ball HSV values LOWER = (60, 50, 150) UPPER = (110, 110, 210) SENSOR_MODE = 4 RESOLUTION = (800, 608) def __init__(self): # initialize the camera and stream self.camera = PiCamera(sensor_mode=PiVideoStream.SENSOR_MODE) self.camera.resolution = PiVideoStream.RESOLUTION self.rawCapture = PiRGBArray(self.camera) 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 def start(self): # start the thread to read frames from the video stream Thread(target=self.update, args=()).start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = frame.array self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: 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
def detect(camera, XMLFile, scale=1.1, neighbors=3, mSize=(0, 0)): # Takes a picture and returns cones cascade objects cone_cascade = cv2.CascadeClassifier(XMLFile) rawCapture = PiRGBArray(camera) camera.capture(rawCapture, format="bgr") image = rawCapture.array gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cones = cone_cascade.detectMultiScale(gray, scaleFactor=scale, minNeighbors=neighbors, minSize=mSize) #cones = cone_cascade.detectMultiScale(gray, scaleFactor = 1.3, minNeighbors = 6, minSize = (60, 60)) rawCapture.close() return cones
class PiVideoStream(object): def __init__(self, resolution=(320, 240), frame_rate=32): # Init camera and Stream self.camera = PiCamera() self.camera.resolution = resolution self.camera.framerate = frame_rate self.rawCapture = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True) self.frame = None self.stopped = False def start(self): """ Starts the update thread that reads the camera data :return: """ Thread(target=self.update, args=()).start() return self def update(self): """ Repeatedly stores the most recently captured frame :return: """ for f in self.stream: # Store the current frame self.frame = f.array # Clear the array ready for the next frame self.rawCapture.truncate(0) if self.stopped: self.stream.close() self.rawCapture.close() self.camera.close() return def read(self): """ Gets the most recently read frame :return: Most recently captured RGB array from camera """ return self.frame def stop(self): """ Exits out of the update function/thread :return: """ self.stopped = True
class PiVideoStream: def __init__(self, resolution=(640, 480), framerate=25): # initialize the camera and stream self.camera = PiCamera() self.camera.resolution = resolution self.camera.framerate = framerate self.rawCapture = PiRGBArray(self.camera, size=resolution) time.sleep(0.1) 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.framenum = 0 self.stopped = False self.frame_timestamp = time.time() def start(self): # start the thread to read frames from the video stream Thread(target=self.update, args=()).start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in preparation for the next frame frame_timestamp = time.time() self.frame = f.array self.framenum += 1 self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread and resource camera resources if self.stopped: self.stream.close() self.rawCapture.close() self.camera.close() return self def Frame_Number(self): return self.framenum def read(self): # return the frame most recently read return self.frame, self.framenum, self.frame_timestamp def stop(self): # indicate that the thread should be stopped self.stopped = True
class PiVideoStream: def __init__(self): # initialize the camera and stream self.camera = PiCamera(sensor_mode=4) self.camera.resolution = (x, y) #self.camera.shutter_speed = 16200 self.camera.shutter_speed = 29000 #self.camera.framerate = 40 self.rawCapture = PiRGBArray(self.camera) 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 def adjustExp(self, speed): self.camera.shutter_speed = speed def start(self): # start the thread to read frames from the video stream Thread(target=self.update, args=()).start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: 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
class PiVideoStream: def __init__(self, resolution=(480, 320), framerate=5): # initialize the camera and stream self.camera = PiCamera() self.camera.resolution = resolution self.camera.vflip = True self.camera.hflip = True self.camera.framerate = framerate #self.camera.video_stabilization = True 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 def start(self): # start the thread to read frames from the video stream Thread(target=self.update, args=()).start() return self def update(self): # keep looping until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.seek(0) self.rawCapture.truncate(0) #time.sleep(1/16) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: 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
class CameraStream: def __init__(self, resolution=(1024, 768), framerate=32): #init code self.pi = False if os.uname()[1] == "raspberrypi": from picamera.array import PiRGBArray from picamera import PiCamera self.pi = True self.camera = PiCamera() self.camera.resolution = resolution self.camera.framerate = framerate self.rawCapture = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True) else: self.camera = cv2.VideoCapture(0) self.frame = None self.stopped = False def start(self): Thread(target=self.update, args=()).start() return self def update(self): if self.pi == True: for f in self.stream: self.frame = f.array self.rawCapture.truncate(0) if self.stopped: self.stream.close() self.rawCapture.close() self.camera.close() return else: while self.stopped == False: (grabbed, self.frame) = self.camera.read() self.camera.release() return def read(self): return self.frame def stop(self): self.stopped = True
class Scry: '''Utilize multi-threading for capturing and processing frames.''' def __init__(self, resolution=(640, 480), framerate=15, rotation=270): # Init camera module and stream. self.camera = PiCamera() self.camera.resolution = resolution self.camera.framerate = framerate self.camera.rotation = rotation self.rawCapture = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True) # Init frame and a var to indicate the thread stopping point. self.frame = None self.stopped = False def start(self): '''Start a thread to intercept incoming frames.''' Thread(target=self.update, args=()).start() return self def update(self): '''Continuously loop until the thread is stopped.''' for f in self.stream: # Grab frame and clear stream in preparation for the next frame. self.frame = f.array self.rawCapture.truncate(0) # If the thread indicator is set, stop thread and camera resources. if self.stopped: self.stream.close() self.rawCapture.close() self.camera.close() return def read(self): '''Return the last frame that was analyzed.''' return self.frame def stop(self): '''Indicate the stopping point for a thread.''' self.stopped = True
class Camera(Thread): last_image = None def __init__(self, width=480, height=360): global instance super(Camera, self).__init__() instance = self self.exit_flag = False self.width = width self.height = height self.camera = PiCamera() # self.camera.resolution = (1024, 768) # self.camera.framerate = 30 # self.camera.iso = 400 # self.camera.exposure_mode = 'auto' # self.camera.awb_mode = 'tungsten' # self.camera.meter_mode = 'average' # self.camera.exposure_compensation = 3 time.sleep(1) self.stream = PiRGBArray(self.camera, size=(self.width, self.height)) @staticmethod def get_instance(): global instance return instance def run(self): logging.info('Starting %s thread' % self.__class__) while not self.exit_flag: self.capture() logging.info('Closing %s thread' % self.__class__) def capture(self): self.stream.seek(0) self.camera.capture(self.stream, format='bgr', use_video_port=True, resize=(self.width, self.height)) self.last_image = self.stream.array def cleanup(self): logging.info('Cleaning up') self.exit_flag = True time.sleep(1) self.camera.close() self.stream.close()
class CameraThread: def start(self): self.p = Process(target=self.update, args=(q,)) self.p.start() return self def update(self, queue): self.camera = PiCamera() self.image = None self.camera.resolution = (w, h) self.camera.framerate = 60 self.camera.brightness = 70 self.camera.contrast = 100 self.rawCapture = PiRGBArray(self.camera, size=(w, h)) time.sleep(0.1) mtx = np.matrix([[313.1251541, 0., 157.36763381], [0., 311.84837219, 130.36209271], [0., 0., 1.]]) dist = np.matrix([[-0.42159111, 0.44966352, -0.00877638, 0.00070653, -0.43508731]]) newcameramtx, roi = cv2.getOptimalNewCameraMatrix(mtx, dist, (w, h), 0, (w, h)) self.mapx, self.mapy = cv2.initUndistortRectifyMap(mtx, dist, None, newcameramtx, (w, h), 5) self.stream = self.camera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True) self.stopped = False for f in self.stream: self.image = cv2.remap(f.array, self.mapx, self.mapy, cv2.INTER_LINEAR) self.rawCapture.truncate(0) if not q.full(): queue.put(self.image, False) if self.stopped: self.stream.close() self.rawCapture.close() self.camera.close() return def stop(self): self.stopped = True
class PiVideoStream: def __init__(self, resolution=(400,200), framerate=60): #Start up camera self.camera = PiCamera() self.camera.resolution = resolution self.camera.framerate = framerate self.camera.rotation = -90 self.rawCap = PiRGBArray(self.camera, size=resolution) self.stream = self.camera.capture_continuous(self.rawCap, format="bgr", use_video_port=True) self.frame = None self.stopped = False self.fps = FPS().start() def start(self): Thread(target=self.update, args=()).start() return self def update(self): # Continually grab frames from camera for f in self.stream: self.frame = f.array self.rawCap.truncate(0) self.fps.update() if self.stopped: self.stream.close() self.rawCap.close() self.camera.close() return def read(self): return self.frame def stop(self): self.fps.stop() self.stopped = True def getFPS(self): return self.fps.fps()
class ColorSepPVStream: def __init__(self, resolution=(320, 240), framerate=20): # initialize the camera and stream self.camera = PiCamera() self.camera.resolution = resolution self.camera.framerate = framerate 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.xOffset = 0 def start(self): # start the thread to read frames from the video stream Thread(target=self.update, args=()).start() return self def update(self): # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame # lower = np.array([17,15,100], dtype = "uint8") # upper = np.array([50,56,200], dtype = "uint8") # greenLower = (40, 100, 6) # greenUpper = (64, 255, 255) greenLower1 = (160, 120, 6) greenUpper1 = (180, 255, 255) greenLower2 = (160, 120, 6) greenUpper2 = (180, 255, 255) # pts = deque(maxlen=args["buffer"]) image = f.array # rot_image = np.rot90(image,2) image = np.rot90(image, 2) # blurred = cv2.GaussianBlur(rot_image, (11, 11), 0) hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) mask1 = cv2.inRange(hsv, greenLower1, greenUpper1) mask2 = cv2.inRange(hsv, greenLower2, greenUpper2) mask = cv2.bitwise_or(mask1, mask2) mask = cv2.erode(mask, None, iterations=2) mask = cv2.dilate(mask, None, iterations=2) output = cv2.bitwise_and(image, image, mask=mask) # find contours in the mask and initialize the current # (x, y) center of the ball cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] center = None # only proceed if at least one contour was found if len(cnts) > 0: # find the largest contour in the mask, then use # it to compute the minimum enclosing circle and # centroid c = max(cnts, key=cv2.contourArea) ((x, y), radius) = cv2.minEnclosingCircle(c) M = cv2.moments(c) center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) # need to calculate only the X offset from image center self.xOffset = center[0] - 160 # 320 pixel wide image # print(self.xOffset) # only proceed if the radius meets a minimum size if radius > 10: # draw the circle and centroid on the frame, # then update the list of tracked points cv2.circle(output, (int(x), int(y)), int(radius), (0, 255, 255), 2) cv2.circle(output, center, 5, (0, 0, 255), -1) # update the points queue # pts.appendleft(center) # print(center) self.frame = output self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: self.stream.close() self.rawCapture.close() self.camera.close() return def read(self): # return the frame most recently read return self.frame def readxOffset(self): # return the frame most recently read return self.xOffset def stop(self): # indicate that the thread should be stopped self.stopped = True
rawCapture.truncate(0) fps.update() # check to see if the desired number of frames have been reached if i == args["num_frames"]: break # stop the timer and display FPS information fps.stop() print("[INFO] elasped time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) # do a bit of cleanup cv2.destroyAllWindows() stream.close() rawCapture.close() camera.close() # created a *threaded *video stream, allow the camera sensor to warmup, # and start the FPS counter print("[INFO] sampling THREADED frames from `picamera` module...") vs = PiVideoStream().start() time.sleep(2.0) fps = FPS().start() # loop over some frames...this time using the threaded stream while fps._numFrames < args["num_frames"]: # grab the frame from the threaded video stream and resize it # to have a maximum width of 400 pixels frame = vs.read() frame = imutils.resize(frame, width=400)
class RcCamera(BaseCamera): """A class for reading from the raspberry pi's picamera""" def __init__( self, width, height, window_name="PiCamera", enable_draw=True, pipeline=None, update_fn=None, fn_params=None, **pi_camera_args ): """ :param width: set a width for the capture :param height: set a height for the capture :param window_name: set a opencv window name :param enable_draw: whether the opencv window should be shown (boosts frames per second) :param pipeline: a class with a method named update. This class should parse the frame a return any useful data :param update_fn: the camera runs on a separate thread. Put any extra code to run in this function :param fn_params: parameters to pass to update_fn :param pi_camera_args: any extra parameters that should be passed to the picamera """ super(RcCamera, self).__init__(width, height, window_name, enable_draw, pipeline, update_fn, fn_params) # initialize the picamera self.camera = PiCamera(**pi_camera_args) self.camera.resolution = self.width, self.height self.raw_capture = PiRGBArray(self.camera, size=(self.width, self.height)) time.sleep(0.1) self.picam_capture = self.camera.capture_continuous(self.raw_capture, format="bgr", use_video_port=True) def update(self): """Keep reading from the camera until self.stopped is True""" for f in self.picam_capture: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.raw_capture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: self.picam_capture.close() self.raw_capture.close() self.camera.close() return if self.pipeline is not None: self.analyzed_frame, self.pipeline_results = self.pipeline.update(self, self.frame) if self.is_recording: self.record_frame() if self.update_fn is not None: if not self.update_fn(self.fn_params): self.stop() self.frame_num += 1 self.slider_num += 1
class TimeLaps(object): def __init__(self, resolution=(2592, 1944), fps=1, delay=360, folder=".", avg_awb=200, avg_speed=25, config_file="parameters.json"): self.resolution = resolution self.fps = fps self.delay = delay self.folder = os.path.abspath(folder) self.avg_awb = avg_awb # time laps over which average gains self.avg_speed = avg_speed # time laps over which average speed self.avg_speed_nb_img = 3.0 self.config_file = os.path.abspath(config_file) self.red_gains = [] self.blue_gains = [] self.speeds = [] self.load_config() self.camera = PiCamera(resolution=self.resolution, framerate=self.fps) self.raw = PiRGBArray(self.camera) self.camera.awb_mode = "auto" self.camera.iso = 0 self.camera.shutter_speed = 0 self.last_gain = 1 self.last_speed = 0 self.last_wb = (1,1) self.next_speed = 1 self.next_wb = (1,1) self.position = Position(0,0) self.start_time = self.last_img = time.time() self.next_img = self.last_img + self.delay self.trajectory = Trajectory(json_file=config_file) self.position = self.trajectory.goto(self.delay) self.load_config() def __del__(self): self.raw.close() self.camera.close() self.raw = None self.camera = None self.trajectory = None def load_config(self): if os.path.isfile(self.config_file): with open(self.config_file) as jsonfile: dico = json.load(jsonfile) self.red_gains = dico.get("red_gains", self.red_gains) self.blue_gains = dico.get("blue_gains", self.blue_gains) self.speeds = dico.get("speeds", self.speeds) self.delay = dico.get("delay", self.delay) self.avg_speed_nb_img = dico.get("avg_speed_nb_img", self.avg_speed_nb_img) self.avg_speed = dico.get("avg_speed", self.avg_speed) self.avg_awb = dico.get("avg_awb", self.avg_awb) def save_config(self): dico = {"red_gains": self.red_gains, "blue_gains": self.blue_gains, "speeds": self.speeds, "trajectory": self.trajectory.config, "avg_awb": self.avg_awb, "avg_speed": self.avg_speed, "self.avg_speed_nb_img": self.avg_speed_nb_img, "delay": self.delay } with open(self.config_file,"w") as jsonfile: jsonfile.write(json.dumps(dico, indent=2)) def save(self, ary, name, exp_speed=125, iso=100): "save an array" Image.fromarray(ary).save(name) exif = pyexiv2.ImageMetadata(name) exif.read() exif["Exif.Photo.ExposureTime"] = Fraction(1, int(round(exp_speed))) exif["Exif.Photo.ISOSpeedRatings"] = iso exif.comment = "pan=%s, tilt=%s"%self.position #exif["Exif.MakerNote.Tilt"] = self.position.tilt exif.write(preserve_timestamps=True) def capture(self): """Take a picture with the camera, if there is enough light """ if not self.is_valid(): print("Skipped, low light Speed %.3f/iso%.0f"%(self.last_speed, self.last_gain)) self.last_img = time.time() self.next_img = self.last_img + self.delay return ns = self.next_speed iso = 100 while (ns < 4) and (iso < 800): iso *= 2 ns *= 2 if ns < 1.0: ns = 1.0 if iso > 800: iso = 800 self.camera.awb_mode = 'off' self.camera.awb_gains = self.next_wb self.camera.iso = iso self.camera.shutter_speed = int(round(1e6/ns)) self.last_img = time.time() self.next_img = self.last_img + self.delay fname = datetime.datetime.fromtimestamp(self.last_img).strftime("%Y-%m-%d-%Hh%Mm%Ss.jpg") print("%s s/%.3f iso %i, expected speed=%.3f R=%.3f B=%.3f"%(fname, ns, iso, self.next_speed, self.next_wb[0], self.next_wb[1])) self.camera.capture(self.raw, "rgb") self.save(self.raw.array, fname, ns, iso) self.raw.truncate(0) self.camera.awb_mode = "auto" self.camera.iso = 0 self.camera.shutter_speed = 0 self.position = self.trajectory.goto(self.next_img - self.start_time) def measure(self): self.camera.capture(self.raw, 'rgb') self.raw.truncate(0) r,b = self.camera.awb_gains self.last_speed = 1.0e6 / self.camera.exposure_speed self.last_gain = 1.0 * self.camera.analog_gain * self.camera.digital_gain if not self.is_valid(): return r = 1.0 * r b = 1.0 * b self.last_wb = (r, b) ls = self.last_speed / self.last_gain self.red_gains.append(r) self.blue_gains.append(b) self.speeds.append(math.log(ls, 2.0)) if len(self.red_gains) > self.avg_awb: self.red_gains = self.red_gains[-self.avg_awb:] self.blue_gains = self.blue_gains[-self.avg_awb:] if len(self.speeds) > self.avg_speed: self.speeds = self.speeds[-self.avg_speed:] if len(self.speeds) < len(SG): self.speeds += [self.speeds[-1]] * (len(SG) - len(self.speeds)) rg = sum(self.red_gains)/len(self.red_gains) bg = sum(self.blue_gains)/len(self.blue_gains) mgspeed = sum(i*j for i,j in zip(SG, self.speeds[-len(SG):])) ns = 2.0 ** (mgspeed) #ns = 2.0 ** (sum(self.speeds)/len(self.speeds)) print("len %s/%s, \t S %.3f/iso%.0f -> %.3f \t R %.3f-> %.3f \t B %.3f -> %.3f"%(len(self.red_gains),len(self.speeds), self.last_speed, 100.*self.last_gain, ns, r, rg, b, bg)) self.next_speed = ns self.next_wb = rg,bg self.save_config() def is_valid(self): return self.last_speed/self.last_gain >=0.4
class Camera(object): stopped = False image = None camera = None raw_capture = None stream = None lock = Lock() debug_image = None def start(self, resolution=(640, 480), framerate=16): """ Starts camera input in a new thread. :param resolution Resolution :param framerate Framerate """ self.stopped = False # Initialize camera self.camera = PiCamera() self.camera.resolution = resolution self.camera.framerate = framerate self.raw_capture = PiRGBArray(self.camera, size=resolution) self.raw_capture.truncate(0) self.stream = self.camera.capture_continuous(self.raw_capture, format="bgr", use_video_port=True) # Start thread thread = Thread(target=self.update, args=()) thread.daemon = True thread.start() def stop(self): """ Stops camera input. """ self.stopped = True def read(self): """ Returns the most recent image read from the camera input. """ with self.lock: return self.image def update(self): """ Grabs next image from camera. """ for f in self.stream: # Sleep a while time.sleep(0.01) # Grab image rotated_image = f.array self.raw_capture.truncate(0) # Rotate image 180 degrees (height, width) = rotated_image.shape[:2] center = (width / 2, height / 2) matrix = cv2.getRotationMatrix2D(center, 180, 1.0) rotated_image = cv2.warpAffine(rotated_image, matrix, (width, height)) # Grab image with self.lock: if self.debug_image is None: self.image = rotated_image else: self.image = self.debug_image # Stop if self.stopped: self.stream.close() self.raw_capture.close() self.camera.close() return def set_debug_image(self, image): """ Overrides the camera input with the given image. Set to None to revert to camera input. :param image: Debug image """ with self.lock: if len(image.shape) == 3 and image.shape[2] == 4: image = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR) # Remove alpha channel self.debug_image = image self.image = image
class PiVideoStream: def __init__(self, resolution=(640, 480), framerate=32): # initialize the camera and stream self.camera = PiCamera() self.camera.resolution = resolution self.camera.framerate = framerate self.rawCapture = PiRGBArray(self.camera, size=resolution) #time.sleep(0.1) self.stream = self.camera.capture_continuous(self.rawCapture, format="bgr", use_video_port=True) print("[INFO] Initialised Camera") # initialize the frame and the variable used to indicate # if the thread should be stopped self.frameCaptured = False self.frame = None self.stopped = False def start(self): # start the thread to read frames from the video stream print("[Start Camera Thread]") t = Thread(target=self.update, args=()) t.daemon = True t.start() return self def update(self): # keep looping infinitely until the thread is stopped print("[INFO] Waiting for Feed") fps = FPS().start() for f in self.stream: #print("[INFO] Reading Feed") self.frameCaptured = True # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array fps.update() self.rawCapture.truncate(0) # if the thread indicator variable is set, stop the thread # and resource camera resources if self.stopped: self.stream.close() self.rawCapture.close() self.camera.close() fps.stop() #print("Thread FPS: {: .2f}".format(fps.fps())) return def isOpened(self): return self.frameCaptured def release(self): self.stop() def get(self, prop): res = self.camera.resolution if prop == cv2.CAP_PROP_FRAME_WIDTH: return res[0] else: return res[1] def read(self): # return the frame most recently read return (True,self.frame) def stop(self): # indicate that the thread should be stopped self.stopped = True
class PiVideoStream: def __init__(self, resolution=(320, 240), framerate=20): """ Create a videostream from a PiCamera :param resolution: camera resolution, default to (320x240) :type resolution: tuple of ints :param framerate: camera framerate, default to 20 (really 16) :type framerate: int """ # initialize the camera and stream self.camera = PiCamera() print " Got camera " + str(self.camera) self.camera.resolution = resolution self.camera.framerate = framerate 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 def start(self): """ Start the thread corresponding to a camera object """ #start the thread to read frames from the video stream Thread(target=self.update, args=()).start() return self def update(self): """ Continually look for frames from the camera; when stopped, shut down """ # keep looping infinitely until the thread is stopped for f in self.stream: # grab the frame from the stream and clear the stream in # preparation for the next frame self.frame = f.array self.rawCapture.truncate(0) if self.stopped: self.stream.close() self.rawCapture.close() self.camera.close() return def read(self): """ Return the most recent frame """ # return the frame most recently read return self.frame def stop(self): """ Set an environment variable to shutdown camera """ # indicate that the thead should be stopped self.stopped = True